1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Bug 440940 - For Open Declaration, filter out results in headers that

aren't included

Change-Id: I4d4ca59dbde1606105c7f3702559046fa160d686
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
This commit is contained in:
Nathan Ridge 2015-03-30 03:23:47 -04:00 committed by Sergey Prigogin
parent 4ff8bab2fb
commit bb7af88082
2 changed files with 55 additions and 2 deletions

View file

@ -27,6 +27,8 @@ import org.eclipse.cdt.core.dom.IPDOMManager;
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.IBinding;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
@ -1289,4 +1291,28 @@ public class CPPSelectionTestsIndexer extends BaseSelectionTestsIndexer {
IASTNode def = testF3(file, offset + 1);
assertTrue(def instanceof IASTName);
}
// #define WALDO 42
// #define WALDO 98
// #include "a.hpp"
// int x = WALDO;
public void testTwoMacrosWithSameName_440940() throws Exception {
StringBuilder[] buffers = getContents(3);
String aHpp = buffers[0].toString();
String bHpp = buffers[1].toString();
String cpp = buffers[2].toString();
IFile aHppFile = importFile("a.hpp", aHpp);
IFile bHppFile = importFile("b.hpp", bHpp);
IFile cppFile = importFile("test.cpp", cpp);
waitUntilFileIsIndexed(index, cppFile);
IASTNode result = testF3(cppFile, cpp.indexOf("WALDO") + 1);
assertTrue(result instanceof IASTName);
IBinding binding = ((IASTName) result).resolveBinding();
assertTrue(binding instanceof IMacroBinding);
String expansion = new String(((IMacroBinding) binding).getExpansion());
assertTrue(expansion.contains("42"));
}
}

View file

@ -70,6 +70,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IIndexMacro;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IIndexName;
@ -229,7 +230,7 @@ class OpenDeclarationsJob extends Job implements ASTRunnable {
} else {
// Leave old method as fallback for local variables, parameters and
// everything else not covered by ICElementHandle.
found = navigateOneLocation(targets);
found = navigateOneLocation(ast, targets);
}
if (!found && !navigationFallBack(ast, sourceName, kind)) {
fAction.reportSymbolLookupFailure(new String(sourceName.toCharArray()));
@ -581,7 +582,33 @@ class OpenDeclarationsJob extends Job implements ASTRunnable {
return null;
}
private boolean navigateOneLocation(IName[] names) {
private IName[] filterNamesByIndexFileSet(IASTTranslationUnit ast, IName[] names) {
IIndexFileSet indexFileSet = ast.getIndexFileSet();
if (indexFileSet == null) {
return names;
}
IName[] result = IName.EMPTY_ARRAY;
for (IName name : names) {
if (name instanceof IIndexName) {
try {
if (!indexFileSet.contains(((IIndexName) name).getFile()))
continue;
} catch (CoreException e) {}
}
result = ArrayUtil.append(result, name);
}
return result;
}
private boolean navigateOneLocation(IASTTranslationUnit ast, IName[] names) {
// If there is more than one name, try to filter out
// ones defined in a file not in the AST's index file set.
if (names.length > 1) {
IName[] filteredNames = filterNamesByIndexFileSet(ast, names);
if (filteredNames.length > 0) {
names = filteredNames;
}
}
for (IName name : names) {
if (navigateToName(name)) {
return true;