From dfb5057e5cd10ca36660affdce593aff52fc9a4a Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Mon, 4 Feb 2008 16:15:05 +0000 Subject: [PATCH] Keep types defined in source files separate from each other, bug 214146. --- .../internal/index/tests/IndexBugsTests.java | 34 ++++++++++++++++++- .../index/tests/IndexUpdateTests.java | 29 ++++++++++++---- .../cdt/internal/pdom/tests/DBTest.java | 6 ++-- .../internal/core/dom/parser/ASTInternal.java | 5 ++- .../internal/core/dom/parser/c/CTypedef.java | 17 +++++++--- .../cdt/internal/core/pdom/PDOMManager.java | 2 ++ .../cdt/internal/core/pdom/WritablePDOM.java | 7 +++- .../internal/core/pdom/dom/PDOMBinding.java | 12 +++++-- .../cdt/internal/core/pdom/dom/PDOMFile.java | 4 +++ .../internal/core/pdom/dom/PDOMLinkage.java | 11 ++++-- .../core/pdom/dom/cpp/PDOMCPPLinkage.java | 2 +- .../indexer/ProjectIndexerInputAdapter.java | 27 +++++++++++---- .../text/contentassist2/CompletionTests.java | 5 +-- .../CompletionTests_PlainC.java | 5 +-- .../CPPSelectionTestsAnyIndexer.java | 10 +++--- .../selection/CSelectionTestsAnyIndexer.java | 6 ++-- 16 files changed, 141 insertions(+), 41 deletions(-) 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 24d07e3d8f4..27c1fc9c45d 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2007 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2008 Wind River Systems, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -1249,4 +1249,36 @@ public class IndexBugsTests extends BaseTestCase { fIndex.releaseReadLock(); } } + + + // typedef int unrelated; + + // class unrelated { + // public: int b; + // }; + + // #include "h1.h" + // void test() { + // unrelated a; + // a.b; + // } + public void testUnrelatedTypedef_Bug214146() throws Exception { + StringBuffer[] contents= getContentsForTest(3); + final IIndexManager indexManager = CCorePlugin.getIndexManager(); + TestSourceReader.createFile(fCProject.getProject(), "s1.cpp", contents[0].toString()); + TestSourceReader.createFile(fCProject.getProject(), "h1.h", contents[1].toString()); + TestSourceReader.createFile(fCProject.getProject(), "s2.h", contents[2].toString()); + indexManager.reindex(fCProject); + waitForIndexer(); + fIndex.acquireReadLock(); + try { + IIndexBinding[] bindings = fIndex.findBindings(new char[][] { "unrelated".toCharArray(), + "b".toCharArray() }, IndexFilter.ALL, NPM); + assertEquals(1, bindings.length); + IIndexName[] decls = fIndex.findNames(bindings[0], IIndex.FIND_ALL_OCCURENCES); + assertEquals(2, decls.length); + } finally { + fIndex.releaseReadLock(); + } + } } \ No newline at end of file diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexUpdateTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexUpdateTests.java index 2a33c402919..70902f773a3 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexUpdateTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexUpdateTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Wind River Systems, Inc. and others. + * Copyright (c) 2007, 2008 Wind River Systems, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -27,6 +27,7 @@ import org.eclipse.cdt.core.dom.ast.IParameter; import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.IVariable; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor; import org.eclipse.cdt.core.dom.ast.cpp.ICPPField; import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; @@ -518,19 +519,35 @@ public class IndexUpdateTests extends IndexTestBase { final char[] nchars = name.toCharArray(); final String refType = name + " &"; final String constRefType = "const " + refType; - IBinding[] ctors= fIndex.findBindings(new char[][]{nchars, nchars}, IndexFilter.ALL_DECLARED_OR_IMPLICIT, NPM); - assertEquals(m1 == null ? 1 : 2, ctors.length); + IIndexBinding[] ctors= fIndex.findBindings(new char[][]{nchars, nchars}, IndexFilter.ALL_DECLARED_OR_IMPLICIT, NPM); + + int count= 0; + for (int i = 0; i < ctors.length; i++) { + IIndexBinding ctor= ctors[i]; + if (((IIndexBinding) ((ICPPClassScope) ctor.getScope()).getClassType()).isFileLocal()) { + ctors[count++]= ctor; + } + } + assertEquals(m1 == null ? 1 : 2, count); final IType[] parameterTypes = ((ICPPConstructor) ctors[0]).getType().getParameterTypes(); if (parameterTypes.length!=1 || !(parameterTypes[0] instanceof ICPPReferenceType)) { - IBinding h= ctors[0]; ctors[0]= ctors[1]; ctors[1]= h; + IIndexBinding h= ctors[0]; ctors[0]= ctors[1]; ctors[1]= h; } if (m1 != null) { checkCppConstructor((ICPPConstructor) ctors[1], new String[]{"", "void"}, m1); } checkCppConstructor((ICPPConstructor) ctors[0], new String[]{"", constRefType}, m2); - IBinding assignmentOp= fIndex.findBindings(new char[][]{nchars, "operator =".toCharArray()}, IndexFilter.ALL_DECLARED_OR_IMPLICIT, NPM)[0]; - checkCppMethod((ICPPMethod) assignmentOp, new String[]{refType, constRefType}, m3); + IIndexBinding[] assignmentOps= fIndex.findBindings(new char[][]{nchars, "operator =".toCharArray()}, IndexFilter.ALL_DECLARED_OR_IMPLICIT, NPM); + count= 0; + for (int i = 0; i < assignmentOps.length; i++) { + IIndexBinding assignmentOp= assignmentOps[i]; + if (((IIndexBinding) ((ICPPClassScope) assignmentOp.getScope()).getClassType()).isFileLocal()) { + assignmentOps[count++]= assignmentOp; + } + } + assertEquals(1, count); + checkCppMethod((ICPPMethod) assignmentOps[0], new String[]{refType, constRefType}, m3); } finally { fIndex.releaseReadLock(); } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/DBTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/DBTest.java index f67c741c074..a2c0d1f7e10 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/DBTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/DBTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2007 QNX Software Systems + * Copyright (c) 2005, 2008 QNX Software Systems * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -237,8 +237,8 @@ public class DBTest extends BaseTestCase { public void testLongStringComparison() throws CoreException { Random r= new Random(314159265); - doTrials(600, ShortString.MAX_LENGTH+1, ShortString.MAX_LENGTH*2, r, true); - doTrials(600, ShortString.MAX_LENGTH+1, ShortString.MAX_LENGTH*2, r, false); + doTrials(100, ShortString.MAX_LENGTH+1, ShortString.MAX_LENGTH*2, r, true); + doTrials(100, ShortString.MAX_LENGTH+1, ShortString.MAX_LENGTH*2, r, false); } private void doTrials(int n, int min, int max, Random r, boolean caseSensitive) throws CoreException { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTInternal.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTInternal.java index 9c7e81adaef..f665bdbb348 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTInternal.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTInternal.java @@ -110,7 +110,7 @@ public class ASTInternal { } } - public static String getDeclaredInSourceFileOnly(IBinding binding) { + public static String getDeclaredInSourceFileOnly(IBinding binding, boolean requireDefinition) { IASTNode[] decls; IASTNode def; if (binding instanceof ICPPInternalBinding) { @@ -126,6 +126,9 @@ public class ASTInternal { else { return null; } + if (requireDefinition && def == null) { + return null; + } String filePath= null; if (def != null) { if ( (filePath= isPartOfSource(filePath, def)) == null) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CTypedef.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CTypedef.java index e60ed5c9418..1f0ce8db1ae 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CTypedef.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CTypedef.java @@ -1,15 +1,14 @@ /******************************************************************************* - * Copyright (c) 2005, 2006 IBM Corporation and others. + * Copyright (c) 2005, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * IBM Rational Software - Initial API and implementation - * Markus Schorn (Wind River Systems) + * IBM Rational Software - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ - package org.eclipse.cdt.internal.core.dom.parser.c; import org.eclipse.cdt.core.dom.ILinkage; @@ -28,7 +27,7 @@ import org.eclipse.core.runtime.PlatformObject; * Created on Nov 8, 2004 * @author aniefer */ -public class CTypedef extends PlatformObject implements ITypedef, ITypeContainer { +public class CTypedef extends PlatformObject implements ITypedef, ITypeContainer, ICInternalBinding { private final IASTName name; private IType type = null; @@ -105,4 +104,12 @@ public class CTypedef extends PlatformObject implements ITypedef, ITypeContainer public ILinkage getLinkage() { return Linkage.C_LINKAGE; } + + public IASTNode[] getDeclarations() { + return IASTNode.EMPTY_NODE_ARRAY; + } + + public IASTNode getDefinition() { + return name; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOMManager.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOMManager.java index 8874847f2c3..f845cc0a6ad 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOMManager.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOMManager.java @@ -68,6 +68,7 @@ import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences; import org.eclipse.cdt.internal.core.pdom.indexer.PDOMNullIndexer; import org.eclipse.cdt.internal.core.pdom.indexer.PDOMRebuildTask; import org.eclipse.cdt.internal.core.pdom.indexer.PDOMUpdateTask; +import org.eclipse.cdt.internal.core.pdom.indexer.ProjectIndexerInputAdapter; import org.eclipse.cdt.internal.core.pdom.indexer.TriggerNotificationTask; import org.eclipse.cdt.internal.core.settings.model.CProjectDescriptionManager; import org.eclipse.core.resources.IFolder; @@ -346,6 +347,7 @@ public class PDOMManager implements IWritableIndexManager, IListener { writeProjectPDOMProperties(pdom, rproject); pdom.releaseWriteLock(); } + pdom.setASTFilePathResolver(new ProjectIndexerInputAdapter(project, false)); pdom.addListener(this); fFileToProject.put(dbFile, project); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/WritablePDOM.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/WritablePDOM.java index 8877dc2f442..5c7a45e041c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/WritablePDOM.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/WritablePDOM.java @@ -50,6 +50,10 @@ public class WritablePDOM extends PDOM implements IWritableIndexFragment { public WritablePDOM(File dbPath, IIndexLocationConverter locationConverter, ChunkCache cache, Map linkageFactoryMappings) throws CoreException { super(dbPath, locationConverter, cache, linkageFactoryMappings); } + + public void setASTFilePathResolver(ASTFilePathResolver resolver) { + fPathResolver= resolver; + } public IIndexFragmentFile addFile(int linkageID, IIndexFileLocation location) throws CoreException { return super.addFile(linkageID, location); @@ -62,12 +66,13 @@ public class WritablePDOM extends PDOM implements IWritableIndexFragment { PDOMFile pdomFile = (PDOMFile) sourceFile; pdomFile.addIncludesTo(includes); pdomFile.addMacros(macros); + final ASTFilePathResolver origResolver= fPathResolver; fPathResolver= pathResolver; try { pdomFile.addNames(names); } finally { - fPathResolver= null; + fPathResolver= origResolver; } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMBinding.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMBinding.java index 9c672990622..e7cd6a56541 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMBinding.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMBinding.java @@ -309,9 +309,12 @@ public abstract class PDOMBinding extends PDOMNamedNode implements IIndexFragmen * Compares two binding fully qualified names. If b0 has * less segments than b1 then -1 is returned, if b0 has * more segments than b1 then 1 is returned. If the segment - * lengths are equal then comparison is lexographical on each + * lengths are equal then comparison is lexicographical on each * component name, beginning with the most nested name and working - * outward. The first non-zero comparison is returned as the result. + * outward. + * If one of the bindings in the hierarchy is file-local it is treated as a different + * binding. + * The first non-zero comparison is returned as the result. * @param b0 * @param b1 * @return