diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java index 6c849926c4e..85c095bda65 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java @@ -678,4 +678,46 @@ public class IndexBugsTests extends BaseTestCase { fIndex.releaseReadLock(); } } + + // typedef struct { + // float fNumber; + // int iIdx; + // } StructA_T; + + // #include "../__bugsTest__/common.h" + // StructA_T gvar1; + + // #include "../__bugsTest__/common.h" + // StructA_T gvar2; + public void testFileInMultipleFragments_bug192352() throws Exception { + StringBuffer[] contents= getContentsForTest(3); + + + ICProject p2 = CProjectHelper.createCCProject("__bugsTest_2_", "bin", IPDOMManager.ID_FAST_INDEXER); + try { + IFile f3= TestSourceReader.createFile(p2.getProject(), "src.cpp", contents[2].toString()); + IFile f1= TestSourceReader.createFile(fCProject.getProject(), "common.h", contents[0].toString()); + IFile f2= TestSourceReader.createFile(fCProject.getProject(), "src.cpp", contents[1].toString()); + waitForIndexer(); + + IIndex index= CCorePlugin.getIndexManager().getIndex(new ICProject[]{fCProject, p2}); + index.acquireReadLock(); + try { + IIndexBinding[] bindings= index.findBindings("StructA_T".toCharArray(), IndexFilter.ALL, NPM); + assertEquals(1, bindings.length); + IIndexBinding binding= bindings[0]; + IIndexName[] names= index.findReferences(binding); + assertEquals(2, names.length); + names= index.findDeclarations(binding); + assertEquals(1, names.length); + } + finally { + index.releaseReadLock(); + } + } + finally { + CProjectHelper.delete(p2); + } + } + } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/CIndex.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/CIndex.java index 6d32e68afb3..0a79701f723 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/CIndex.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/CIndex.java @@ -116,11 +116,27 @@ public class CIndex implements IIndex { } public IIndexName[] findNames(IBinding binding, int flags) throws CoreException { - ArrayList result= new ArrayList(); + LinkedList result= new LinkedList(); + int fragCount= 0; for (int i = 0; i < fPrimaryFragmentCount; i++) { IIndexFragmentBinding adaptedBinding= fFragments[i].adaptBinding(binding); if (adaptedBinding != null) { - result.addAll(Arrays.asList(fFragments[i].findNames(adaptedBinding, flags))); + final IIndexFragmentName[] names = fFragments[i].findNames(adaptedBinding, flags); + if (names.length > 0) { + result.addAll(Arrays.asList(names)); + fragCount++; + } + } + } + // bug 192352, files can reside in multipe fragments, remove duplicates + if (fragCount > 1) { + HashSet keys= new HashSet(); + for (Iterator iterator = result.iterator(); iterator.hasNext();) { + final IIndexFragmentName name = (IIndexFragmentName) iterator.next(); + final String key= name.getFile().getLocation().getURI().toString() + name.getNodeOffset(); + if (!keys.add(key)) { + iterator.remove(); + } } } return (IIndexName[]) result.toArray(new IIndexName[result.size()]);