1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 09:46:02 +02:00

Open Call Hierarchy for functions built with a macro, bug 249801.

This commit is contained in:
Markus Schorn 2008-10-07 12:49:25 +00:00
parent 21b5326fac
commit 2bfb47f111
8 changed files with 195 additions and 8 deletions

View file

@ -427,4 +427,12 @@ public class ASTNodeSelectorTest extends AST2BaseTest {
testContainedNode(x1-1, x1+2, "O");
}
// #define MACRO void m
// MACRO();
public void testEnclosingAMacro() {
int x1= fCode.indexOf("MACRO(");
int x2= x1 + "MACRO(".length();
testContainedName(x1, x2, "MACRO");
testEnclosingNode(x1, x2, "MACRO();");
}
}

View file

@ -15,6 +15,7 @@ package org.eclipse.cdt.core.dom.ast;
* for one file contained in a translation-unit, can be obtained using
* {@link IASTTranslationUnit#getNodeSelector(String)}.
*
* @noimplement This interface is not intended to be implemented by clients.
* @since 5.0
*/
public interface IASTNodeSelector {
@ -59,6 +60,33 @@ public interface IASTNodeSelector {
*/
IASTNode findFirstContainedNode(int offset, int length);
/**
* Returns the node for the exact given range, or <code>null</code> if there is no such node.
* <p>
* The method never returns a macro expansion ({@link IASTPreprocessorMacroExpansion}) or the name for
* an expansion. Rather than that the expansion itself is searched for a matching node.
* @since 5.1
*/
IASTNode findNodeInExpansion(int offset, int length);
/**
* Returns the smallest node enclosing the range, or <code>null</code> if there is no such node.
* <p>
* The method never returns a macro expansion ({@link IASTPreprocessorMacroExpansion}) or the name for
* an expansion. Rather than that the expansion itself is searched for a matching node.
* @since 5.1
*/
IASTNode findEnclosingNodeInExpansion(int offset, int length);
/**
* Returns the first node contained in the given expansion, or <code>null</code> if there is no such node.
* <p>
* The method never returns a macro expansion ({@link IASTPreprocessorMacroExpansion}) or the name for
* an expansion. Rather than that the expansion itself is searched for a matching node.
* @since 5.1
*/
IASTNode findFirstContainedNodeInExpansion(int offset, int length);
/**
* Returns a macro expansion enclosing the given range, or <code>null</code>.
*/

View file

@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
@ -45,10 +46,15 @@ public class ASTNodeSelector implements IASTNodeSelector {
return false;
}
private <T extends IASTNode> T findNode(int offsetInFile, int lengthInFile, Relation relation, Class<T> requiredClass) {
return findNode(offsetInFile, lengthInFile, relation, requiredClass, false);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTNodeSelector#getNode(int, int)
*/
private <T extends IASTNode> T findNode(int offsetInFile, int lengthInFile, Relation relation, Class<T> requiredClass) {
private <T extends IASTNode> T findNode(int offsetInFile, int lengthInFile, Relation relation,
Class<T> requiredClass, boolean searchInExpansion) {
if (!fIsValid) {
return null;
}
@ -79,10 +85,11 @@ public class ASTNodeSelector implements IASTNodeSelector {
}
}
final ASTNodeSpecification<T> nodeSpec= new ASTNodeSpecification<T>(relation, requiredClass, offsetInFile, lengthInFile);
nodeSpec.setRangeInSequence(sequenceNumber, sequenceLength);
nodeSpec.setRangeInSequence(sequenceNumber, sequenceLength, false);
nodeSpec.setSearchInExpansion(searchInExpansion);
getNode(nodeSpec);
if (altSequenceNumber != -1) {
nodeSpec.setRangeInSequence(altSequenceNumber, sequenceLength);
nodeSpec.setRangeInSequence(altSequenceNumber, sequenceLength, true);
getNode(nodeSpec);
}
return nodeSpec.getBestNode();
@ -91,6 +98,21 @@ public class ASTNodeSelector implements IASTNodeSelector {
private <T extends IASTNode> T getNode(ASTNodeSpecification<T> nodeSpec) {
fLocationResolver.findPreprocessorNode(nodeSpec);
if (!nodeSpec.requiresClass(IASTPreprocessorMacroExpansion.class)) {
// adjust sequence number for search in the expansion of macros
int seqbegin= nodeSpec.getSequenceStart();
int seqend= nodeSpec.getSequenceEnd();
IASTPreprocessorMacroExpansion expansion= nodeSpec.findLeadingMacroExpansion(this);
if (expansion != null) {
IASTFileLocation floc= expansion.getFileLocation();
seqbegin= fLocationResolver.getSequenceNumberForFileOffset(fFilePath, floc.getNodeOffset() + floc.getNodeLength()-1)+1;
}
expansion= nodeSpec.findTrailingMacroExpansion(this);
if (expansion != null) {
IASTFileLocation floc= expansion.getFileLocation();
seqend= fLocationResolver.getSequenceNumberForFileOffset(fFilePath, floc.getNodeOffset() + floc.getNodeLength())-1;
}
nodeSpec.setRangeInSequence(seqbegin, seqend-seqbegin);
FindNodeForOffsetAction nodeFinder= new FindNodeForOffsetAction(nodeSpec);
fTu.accept(nodeFinder);
}
@ -118,6 +140,27 @@ public class ASTNodeSelector implements IASTNodeSelector {
return findNode(offset, length, Relation.ENCLOSING, IASTNode.class);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTNodeSelector#getFirstContainedNode(int, int)
*/
public IASTNode findFirstContainedNodeInExpansion(int offset, int length) {
return findNode(offset, length, Relation.FIRST_CONTAINED, IASTNode.class, true);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTNodeSelector#getNode(int, int)
*/
public IASTNode findNodeInExpansion(int offset, int length) {
return findNode(offset, length, Relation.EXACT_MATCH, IASTNode.class, true);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTNodeSelector#getSurroundingNode(int, int)
*/
public IASTNode findEnclosingNodeInExpansion(int offset, int length) {
return findNode(offset, length, Relation.ENCLOSING, IASTNode.class, true);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IASTNodeSelector#getFirstContainedNode(int, int)
*/

View file

@ -12,7 +12,9 @@ package org.eclipse.cdt.internal.core.dom.parser;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTImageLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
/**
* For searching ast-nodes by offset and length, instances of this class can be used to
@ -32,6 +34,8 @@ public class ASTNodeSpecification<T extends IASTNode> {
private int fBestOffset;
private int fBestEndOffset;
private T fBestNode;
private boolean fSearchInExpansion;
private boolean fZeroToLeft= false;
public ASTNodeSpecification(Relation relation, Class<T> clazz, int fileOffset, int fileLength) {
fRelation= relation;
@ -45,6 +49,15 @@ public class ASTNodeSpecification<T extends IASTNode> {
fSeqEndNumber= offsetInSeq+lengthInSeq;
}
public void setRangeInSequence(int offsetInSeq, int lengthInSeq, boolean zeroRangeToLeft) {
setRangeInSequence(offsetInSeq, lengthInSeq);
fZeroToLeft= zeroRangeToLeft;
}
public void setSearchInExpansion(boolean searchInExpansion) {
fSearchInExpansion= searchInExpansion;
}
public Relation getRelationToSelection() {
return fRelation;
}
@ -99,7 +112,16 @@ public class ASTNodeSpecification<T extends IASTNode> {
}
public boolean isAcceptableNode(IASTNode astNode) {
return astNode != null && fClass.isAssignableFrom(astNode.getClass());
if (astNode == null || !fClass.isAssignableFrom(astNode.getClass()))
return false;
if (fSearchInExpansion) {
IASTNode check= astNode instanceof IASTName ? astNode.getParent() : astNode;
if (check instanceof IASTPreprocessorMacroExpansion) {
return false;
}
}
return true;
}
/**
@ -177,4 +199,12 @@ public class ASTNodeSpecification<T extends IASTNode> {
}
return false;
}
public IASTPreprocessorMacroExpansion findLeadingMacroExpansion(ASTNodeSelector nodeSelector) {
return nodeSelector.findEnclosingMacroExpansion(fZeroToLeft ? fFileOffset-1 : fFileOffset, 1);
}
public IASTPreprocessorMacroExpansion findTrailingMacroExpansion(ASTNodeSelector nodeSelector) {
return nodeSelector.findEnclosingMacroExpansion(fZeroToLeft ? fFileEndOffset : fFileEndOffset-1, 1);
}
}

View file

@ -137,6 +137,7 @@ class LocationCtxContainer extends LocationCtx {
addFileLocation(offset, endSequenceNumber-sequenceNumber, locations);
return true;
}
if (offset < child.fOffsetInParent)
addFileLocation(offset, child.fOffsetInParent-offset, locations);
sequenceNumber= child.fSequenceNumber;
}

View file

@ -337,4 +337,34 @@ public class CallHierarchyBugs extends CallHierarchyBaseTest {
}
// #define MACRO(name) void PREFIX_ ## name(char *a , char *b)
// #define CALL(x) call(x)
//
// void call(int);
// MACRO(Test) {
// CALL(0);
// }
public void testMacrosHidingCall_249801() throws Exception {
String content= getContentsForTest(1)[0].toString();
IFile file= createFile(getProject(), "file249801.cpp", content);
waitForIndexer(fIndex, file, CallHierarchyBaseTest.INDEXER_WAIT_TIME);
final CHViewPart ch= (CHViewPart) activateView(CUIPlugin.ID_CALL_HIERARCHY);
final IWorkbenchWindow workbenchWindow = ch.getSite().getWorkbenchWindow();
// open editor, check outline
CEditor editor= openEditor(file);
int idx = content.indexOf("MACRO(Test");
editor.selectAndReveal(idx, 0);
openCallHierarchy(editor, false);
Tree chTree= checkTreeNode(ch, 0, "PREFIX_Test(char *, char *)").getParent();
TreeItem ti= checkTreeNode(chTree, 0, 0, "call(int)");
idx = content.indexOf("CALL(0");
editor.selectAndReveal(idx+4, 0);
openCallHierarchy(editor, true);
chTree= checkTreeNode(ch, 0, "call(int)").getParent();
ti= checkTreeNode(chTree, 0, 0, "PREFIX_Test(char *, char *)");
}
}

View file

@ -39,6 +39,9 @@ public class CompletionTest_SingleName_NoPrefix extends CompletionProposalsBase
"xOtherClass",
"AStruct",
"XStruct",
"__FUNCTION__ : const char *",
"__PRETTY_FUNCTION__ : const char *",
"__func__ : const char *",
"aNamespace",
"xNamespace",
"anEnumeration",
@ -65,6 +68,7 @@ public class CompletionTest_SingleName_NoPrefix extends CompletionProposalsBase
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getCompletionPosition()
*/
@Override
protected int getCompletionPosition() {
return getBuffer().indexOf(" ") + 2;
}
@ -72,6 +76,7 @@ public class CompletionTest_SingleName_NoPrefix extends CompletionProposalsBase
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedPrefix()
*/
@Override
protected String getExpectedPrefix() {
return expectedPrefix;
}
@ -79,6 +84,7 @@ public class CompletionTest_SingleName_NoPrefix extends CompletionProposalsBase
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedResultsValues()
*/
@Override
protected String[] getExpectedResultsValues() {
return expectedResults;
}
@ -86,6 +92,7 @@ public class CompletionTest_SingleName_NoPrefix extends CompletionProposalsBase
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileName()
*/
@Override
protected String getFileName() {
return fileName;
}
@ -93,12 +100,14 @@ public class CompletionTest_SingleName_NoPrefix extends CompletionProposalsBase
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileFullPath()
*/
@Override
protected String getFileFullPath() {
return fileFullPath;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileFullPath()
*/
@Override
protected String getHeaderFileFullPath() {
return headerFileFullPath;
}
@ -106,6 +115,7 @@ public class CompletionTest_SingleName_NoPrefix extends CompletionProposalsBase
/* (non-Javadoc)
* @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileName()
*/
@Override
protected String getHeaderFileName() {
return headerFileName;
}

View file

@ -32,8 +32,17 @@ import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.IPositionConverter;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
@ -70,6 +79,7 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.core.model.ext.CElementHandleFactory;
import org.eclipse.cdt.internal.core.model.ext.ICElementHandle;
@ -386,7 +396,34 @@ public class IndexUI {
ASTProvider.getASTProvider().runOnAST(workingCopy, ASTProvider.WAIT_YES, null, new ASTRunnable() {
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
if (ast != null) {
result[0]= ast.getNodeSelector(null).findEnclosingName(offset, length);
final IASTNodeSelector nodeSelector = ast.getNodeSelector(null);
IASTName name= nodeSelector.findEnclosingName(offset, length);
if (name != null && name.getParent() instanceof IASTPreprocessorMacroExpansion) {
IASTFileLocation floc= name.getParent().getFileLocation();
IASTNode node= nodeSelector.findEnclosingNodeInExpansion(floc.getNodeOffset(), floc.getNodeLength());
if (node instanceof IASTName) {
name= (IASTName) node;
} else if (node instanceof IASTFunctionCallExpression){
IASTExpression expr= ((IASTFunctionCallExpression) node).getFunctionNameExpression();
if (expr instanceof IASTIdExpression) {
name= ((IASTIdExpression) expr).getName();
}
} else {
if (node instanceof IASTSimpleDeclaration) {
IASTNode[] dtors= ((IASTSimpleDeclaration) node).getDeclarators();
if (dtors != null && dtors.length > 0) {
node= dtors[0];
}
} else if (node instanceof IASTFunctionDefinition) {
node= ((IASTFunctionDefinition) node).getDeclarator();
}
if (node instanceof IASTDeclarator) {
IASTDeclarator dtor= CPPVisitor.findTypeRelevantDeclarator((IASTDeclarator) node);
name= dtor.getName();
}
}
}
result[0]= name;
}
return Status.OK_STATUS;
}