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

Keep types defined in source files separate from each other, bug 214146.

This commit is contained in:
Markus Schorn 2008-02-04 16:15:05 +00:00
parent fd1c9d4e28
commit dfb5057e5c
16 changed files with 141 additions and 41 deletions

View file

@ -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();
}
}
}

View file

@ -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();
}

View file

@ -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 {

View file

@ -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) {

View file

@ -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;
}
}

View file

@ -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);

View file

@ -50,6 +50,10 @@ public class WritablePDOM extends PDOM implements IWritableIndexFragment {
public WritablePDOM(File dbPath, IIndexLocationConverter locationConverter, ChunkCache cache, Map<String, IPDOMLinkageFactory> 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;
}
}

View file

@ -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<ul><li> -1 if b0 &lt; b1
@ -327,6 +330,11 @@ public abstract class PDOMBinding extends PDOMNamedNode implements IIndexFragmen
IString s0 = b0.getDBName(), s1 = b1.getDBName();
cmp = s0.compare(s1, true);
if(cmp==0) {
int l1= b0.getLocalToFileRec();
int l2= b1.getLocalToFileRec();
if (l1 != l2) {
return l1 < l2 ? -1 : 1;
}
b0 = (PDOMBinding) b0.getParentBinding();
b1 = (PDOMBinding) b1.getParentBinding();
if(b0==null || b1==null) {

View file

@ -21,6 +21,7 @@ import java.util.List;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IIndexInclude;
import org.eclipse.cdt.core.index.IIndexLocationConverter;
@ -243,6 +244,9 @@ public class PDOMFile implements IIndexFragmentFile {
}
private PDOMName createPDOMName(IASTName name, PDOMName caller) {
if (name.getBinding() instanceof IParameter) {
return null;
}
PDOMName result= null;
try {
PDOMBinding binding = ((WritablePDOM) pdom).addBinding(name);

View file

@ -24,6 +24,8 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IPointerType;
@ -31,6 +33,7 @@ import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IQualifierType;
import org.eclipse.cdt.core.dom.ast.IScope;
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.ICPPDeferredTemplateInstance;
@ -359,6 +362,7 @@ public abstract class PDOMLinkage extends PDOMNamedNode implements IIndexLinkage
return null;
}
boolean checkInSourceOnly= false;
boolean requireDefinition= false;
if (binding instanceof IVariable) {
if (!(binding instanceof IField)) {
checkInSourceOnly= ASTInternal.isStatic((IVariable) binding);
@ -366,12 +370,13 @@ public abstract class PDOMLinkage extends PDOMNamedNode implements IIndexLinkage
} else if (binding instanceof IFunction) {
IFunction f= (IFunction) binding;
checkInSourceOnly= ASTInternal.isStatic(f, false);
// } else if (binding instanceof ITypedef || binding instanceof ICompositeType || binding instanceof IEnumeration) {
// checkInSourceOnly= true;
} else if (binding instanceof ITypedef || binding instanceof ICompositeType || binding instanceof IEnumeration) {
checkInSourceOnly= true;
requireDefinition= true;
}
if (checkInSourceOnly) {
String path= ASTInternal.getDeclaredInSourceFileOnly(binding);
String path= ASTInternal.getDeclaredInSourceFileOnly(binding, requireDefinition);
if (path != null) {
return wpdom.getFileForASTPath(getLinkageID(), path);
}

View file

@ -795,7 +795,7 @@ class PDOMCPPLinkage extends PDOMLinkage implements IIndexCPPBindingConstants {
return wpdom.getFileForASTPath(getLinkageID(), path);
}
} else if (binding instanceof ICPPNamespaceAlias) {
String path= ASTInternal.getDeclaredInSourceFileOnly(binding);
String path= ASTInternal.getDeclaredInSourceFileOnly(binding, false);
if (path != null) {
return wpdom.getFileForASTPath(getLinkageID(), path);
}

View file

@ -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
@ -42,29 +42,44 @@ public class ProjectIndexerInputAdapter extends IndexerInputAdapter {
private final static boolean CASE_SENSITIVE_FILES= !new File("a").equals(new File("A")); //$NON-NLS-1$//$NON-NLS-2$
private final ICProject fCProject;
private HashMap fIflCache= new HashMap();
private HashMap<String, IIndexFileLocation> fIflCache;
public ProjectIndexerInputAdapter(ICProject cproject) {
this(cproject, true);
}
public ProjectIndexerInputAdapter(ICProject cproject, boolean useCache) {
fCProject= cproject;
fIflCache= useCache ? new HashMap<String, IIndexFileLocation>() : null;
}
public IIndexFileLocation resolveASTPath(String astPath) {
IIndexFileLocation result= (IIndexFileLocation) fIflCache.get(astPath);
if (fIflCache == null) {
return doResolveASTPath(astPath);
}
IIndexFileLocation result= fIflCache.get(astPath);
if (result == null) {
result= IndexLocationFactory.getIFLExpensive(fCProject, astPath);
result = doResolveASTPath(astPath);
fIflCache.put(astPath, result);
}
return result;
}
private IIndexFileLocation doResolveASTPath(String astPath) {
return IndexLocationFactory.getIFLExpensive(fCProject, astPath);
}
public IIndexFileLocation resolveIncludeFile(String includePath) {
IIndexFileLocation result= (IIndexFileLocation) fIflCache.get(includePath);
if (fIflCache == null) {
return doResolveASTPath(includePath);
}
IIndexFileLocation result= fIflCache.get(includePath);
if (result == null) {
File location= new File(includePath);
if (!location.exists()) {
return null;
}
result= IndexLocationFactory.getIFLExpensive(fCProject, includePath);
result = doResolveASTPath(includePath);
if (result.getFullPath() == null && !CASE_SENSITIVE_FILES) {
try {
String canonicalPath= location.getCanonicalPath();

View file

@ -819,10 +819,11 @@ public class CompletionTests extends AbstractContentAssistTest {
// void test() {
// g/*cursor*/
public void testBindingsWithoutDeclaration() throws Exception {
// gC1all, gStruct, gnsClass, gnsStruct: fix for 214146, type from a source file is not proposed.
final String[] expected= {
"gC1", "gC2", "gfC1()", "gfC2()",
"gns::", "gnsClass", "gnsFunc()", "gnsStruct", "gnsTemp",
"gClass", "gFunc()", "gStruct", "gTemp"
"gns::", "gnsFunc()", "gnsTemp",
"gFunc()", "gTemp"
};
final String[] expected2= {
"gC1", "gC2", "gfC1()", "gfC2()", "gns::"

View file

@ -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
@ -115,8 +115,9 @@ public class CompletionTests_PlainC extends AbstractContentAssistTest {
// void test() {
// g/*cursor*/
public void testBindingsWithoutDeclaration() throws Exception {
// gStruct: fix for 214146, type from a source file is not proposed.
final String[] expected= {
"gGlobalInt", "gTemp", "gFunc(void)", "gStruct"
"gGlobalInt", "gTemp", "gFunc(void)",
};
final String[] expected2= {
"gGlobalInt"

View file

@ -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
@ -200,16 +200,16 @@ public abstract class CPPSelectionTestsAnyIndexer extends BaseSelectionTestsInde
// extern int MyInt; // MyInt is in another file
// extern const int MyConst; // MyConst is in another file
// void MyFunc(int); // often used in header files
// struct MyStruct; // often used in header files
// typedef int NewInt; // a normal typedef statement
// class MyClass; // often used in header files
// struct MyStruct { int Member1; int Member2; };
// class MyClass { int MemberVar; };
// #include "basicDefinition.h"
// int MyInt;
// extern const int MyConst = 42;
// void MyFunc(int a) { cout << a << endl; }
// struct MyStruct { int Member1; int Member2; };
// class MyClass { int MemberVar; };
// class MyClass;
// struct MyStruct;
public void testBasicDefinition() throws Exception {
StringBuffer[] buffers= getContents(2);
String hcode= buffers[0].toString();

View file

@ -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
@ -84,14 +84,14 @@ public abstract class CSelectionTestsAnyIndexer extends BaseSelectionTestsIndexe
// extern int MyInt; // MyInt is in another file
// extern const int MyConst; // MyConst is in another file
// void MyFunc(int); // often used in header files
// struct MyStruct; // often used in header files
// typedef int NewInt; // a normal typedef statement
// struct MyStruct { int Member1; int Member2; };
// #include "basicDefinition.h"
// int MyInt;
// extern const int MyConst = 42;
// void MyFunc(int a) { cout << a << endl; }
// struct MyStruct { int Member1; int Member2; };
// struct MyStruct;
public void testBasicDefinition() throws Exception {
StringBuffer[] buffers= getContents(2);
String hcode= buffers[0].toString();