From d09429e1d596abf16f970fd20135dc8f4ea489fc Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Mon, 28 Jan 2008 14:51:02 +0000 Subject: [PATCH] Follow up for using declarations, bug 203385. --- .../tests/IndexCPPBindingResolutionTest.java | 3 +- .../internal/pdom/tests/NamespaceTests.java | 6 ++-- .../internal/core/dom/parser/ASTInternal.java | 36 +++++++++++++++++++ .../cdt/internal/core/index/CIndex.java | 28 --------------- .../internal/core/pdom/dom/PDOMBinding.java | 2 +- .../internal/core/pdom/dom/PDOMLinkage.java | 14 ++++---- .../cdt/internal/core/pdom/dom/PDOMName.java | 30 ++++++++++------ .../core/pdom/dom/cpp/PDOMCPPLinkage.java | 12 +++++++ .../pdom/dom/cpp/PDOMCPPUsingDeclaration.java | 11 +++--- 9 files changed, 84 insertions(+), 58 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionTest.java index bc2f7c166e3..5552e44a0d9 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Symbian Software Systems and others. + * Copyright (c) 2007, 2008 Symbian Software Systems 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 @@ -155,6 +155,7 @@ public abstract class IndexCPPBindingResolutionTest extends IndexBindingResoluti // class B {}; // } + // #include "header.h" // b::A aa; // b::B bb; public void testUsingTypeDirective_177917_2() { diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/NamespaceTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/NamespaceTests.java index b78d062a1fc..2e8305b9ece 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/NamespaceTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/NamespaceTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2007 IBM Corporation. + * Copyright (c) 2006, 2008 IBM Corporation. * 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 @@ -219,9 +219,7 @@ public class NamespaceTests extends PDOMTestBase { IName[] decls = pdom.findNames(variable1, IIndex.FIND_DECLARATIONS); assertEquals(0, decls.length); - //TODO: should this be 2? - //Doug: Not sure but we're at one now... IName[] refs = pdom.findNames(variable1, IIndex.FIND_REFERENCES); - assertEquals(1, refs.length); + assertEquals(2, refs.length); } } 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 1e07b666b71..9c7e81adaef 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 @@ -159,4 +159,40 @@ public class ASTInternal { } return filePath; } + + public static String getDeclaredInOneFileOnly(IBinding binding) { + IASTNode[] decls; + IASTNode def; + if (binding instanceof ICPPInternalBinding) { + ICPPInternalBinding ib= (ICPPInternalBinding) binding; + decls= ib.getDeclarations(); + def= ib.getDefinition(); + } + else if (binding instanceof ICInternalBinding) { + ICInternalBinding ib= (ICInternalBinding) binding; + decls= ib.getDeclarations(); + def= ib.getDefinition(); + } + else { + return null; + } + String filePath= null; + if (def != null) { + filePath= def.getContainingFilename(); + } + if (decls != null) { + for (int i = 0; i < decls.length; i++) { + final IASTNode node= decls[i]; + if (node != null) { + final String fn = node.getContainingFilename(); + if (filePath == null) { + filePath= fn; + } else if (!filePath.equals(fn)) { + return null; + } + } + } + } + return filePath; + } } 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 243efb24384..c5508efcc18 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 @@ -548,35 +548,7 @@ public class CIndex implements IIndex { fFragments[i].resetCacheCounters(); } } - - public String getFileLocalScopeQualifier(IIndexFileLocation loc) { - return new String(getFileLocalScopeQualifier(loc.getFullPath())); - } - - public static char[] getFileLocalScopeQualifier(String fullPath) { - char[] fname= fullPath.toCharArray(); - int fnamestart= findFileNameStart(fname); - StringBuffer buf= new StringBuffer(); - buf.append('{'); - buf.append(fname, fnamestart, fname.length-fnamestart); - buf.append(':'); - buf.append(fullPath.hashCode()); - buf.append('}'); - fname= buf.toString().toCharArray(); - return fname; - } - private static int findFileNameStart(char[] fname) { - for (int i= fname.length-2; i>=0; i--) { - switch (fname[i]) { - case '/': - case '\\': - return i+1; - } - } - return 0; - } - public IIndexFileSet createFileSet() { return new IndexFileSet(); } 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 bc8d9fe6355..9c672990622 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 @@ -86,7 +86,7 @@ public abstract class PDOMBinding extends PDOMNamedNode implements IIndexFragmen return record; } - public boolean hasDeclaration() throws CoreException { + public final boolean hasDeclaration() throws CoreException { Database db = pdom.getDB(); return db.getInt(record + FIRST_DECL_OFFSET) != 0 || db.getInt(record + FIRST_DEF_OFFSET) != 0; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMLinkage.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMLinkage.java index 379a54789da..0a7dbff881b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMLinkage.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMLinkage.java @@ -35,6 +35,7 @@ import org.eclipse.cdt.core.dom.ast.IType; 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; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPDelegate; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceAlias; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization; @@ -42,7 +43,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateScope; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration; import org.eclipse.cdt.core.index.IIndexBinding; import org.eclipse.cdt.core.index.IIndexLinkage; import org.eclipse.cdt.internal.core.Util; @@ -259,6 +259,11 @@ public abstract class PDOMLinkage extends PDOMNamedNode implements IIndexLinkage if (binding instanceof ICPPTemplateInstance) { scopeBinding = ((ICPPTemplateInstance)binding).getTemplateDefinition(); } else { + // in case this is a delegate the scope of the delegate can be different to the + // scope of the delegating party (e.g. using-declarations) + while (binding instanceof ICPPDelegate && !(binding instanceof ICPPNamespaceAlias)) { + binding= ((ICPPDelegate) binding).getBinding(); + } IScope scope = binding.getScope(); if (scope == null) { if (binding instanceof ICPPDeferredTemplateInstance) { @@ -359,12 +364,7 @@ public abstract class PDOMLinkage extends PDOMNamedNode implements IIndexLinkage } else if (binding instanceof IFunction) { IFunction f= (IFunction) binding; isFileLocal= ASTInternal.isStatic(f, false); - } else if ((binding instanceof ICPPUsingDeclaration || - binding instanceof ICPPNamespaceAlias) && binding.getScope() == null) { - // Using declarations and namespace aliases in global scope are restricted - // to the containing file. - isFileLocal= true; - } + } if (isFileLocal) { String path= ASTInternal.getDeclaredInSourceFileOnly(binding); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMName.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMName.java index d8454d36b87..1cb54f1330f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMName.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMName.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2007 QNX Software Systems and others. + * Copyright (c) 2005, 2008 QNX Software Systems 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 @@ -17,6 +17,7 @@ import java.util.ArrayList; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration; import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexName; import org.eclipse.cdt.internal.core.index.IIndexFragment; @@ -64,13 +65,7 @@ public final class PDOMName implements IIndexFragmentName, IASTFileLocation { record = db.malloc(RECORD_SIZE); // What kind of name are we - int flags = 0; - if (name.isDefinition()) - flags = IS_DEFINITION; - else if (name.isDeclaration()) - flags = IS_DECLARATION; - else - flags = IS_REFERENCE; + int flags= getRoleOfName(name); flags |= binding.getAdditionalNameFlags(flags, name); db.putByte(record + FLAGS, (byte) flags); @@ -102,6 +97,21 @@ public final class PDOMName implements IIndexFragmentName, IASTFileLocation { db.put3ByteUnsignedInt(record + NODE_OFFSET_OFFSET, fileloc.getNodeOffset()); db.putShort(record + NODE_LENGTH_OFFSET, (short) fileloc.getNodeLength()); } + + private int getRoleOfName(IASTName name) { + if (name.isDefinition()) { + return IS_DEFINITION; + } + if (name.isDeclaration()) { + return IS_DECLARATION; + } + + // special case a using-declaration is a declaration and a reference at the same time. + if (name.getBinding() instanceof ICPPUsingDeclaration) { + return IS_DEFINITION; + } + return IS_REFERENCE; + } public PDOMName(PDOM pdom, int nameRecord) { this.pdom = pdom; @@ -350,7 +360,7 @@ public final class PDOMName implements IIndexFragmentName, IASTFileLocation { } public IIndexName[] getEnclosedNames() throws CoreException { - ArrayList result= new ArrayList(); + ArrayList result= new ArrayList(); PDOMName name= getNextInFile(); while (name != null) { if (name.getEnclosingDefinitionRecord() == record) { @@ -358,6 +368,6 @@ public final class PDOMName implements IIndexFragmentName, IASTFileLocation { } name= name.getNextInFile(); } - return (IIndexName[]) result.toArray(new IIndexName[result.size()]); + return result.toArray(new PDOMName[result.size()]); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPLinkage.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPLinkage.java index 78283f68f46..1e45543f2b1 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPLinkage.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPLinkage.java @@ -58,10 +58,12 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier; import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPBasicType; import org.eclipse.cdt.internal.core.Util; +import org.eclipse.cdt.internal.core.dom.parser.ASTInternal; import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBlockScope; import org.eclipse.cdt.internal.core.index.IIndexCPPBindingConstants; import org.eclipse.cdt.internal.core.pdom.PDOM; +import org.eclipse.cdt.internal.core.pdom.WritablePDOM; import org.eclipse.cdt.internal.core.pdom.db.IBTreeComparator; import org.eclipse.cdt.internal.core.pdom.dom.IPDOMMemberOwner; import org.eclipse.cdt.internal.core.pdom.dom.PDOMASTAdapter; @@ -785,6 +787,16 @@ class PDOMCPPLinkage extends PDOMLinkage implements IIndexCPPBindingConstants { if (binding instanceof ICPPMethod) { return null; } + if (binding instanceof ICPPUsingDeclaration || binding instanceof ICPPNamespaceAlias) { + if (pdom instanceof WritablePDOM) { + final WritablePDOM wpdom= (WritablePDOM) pdom; + String path= ASTInternal.getDeclaredInOneFileOnly(binding); + if (path != null) { + return wpdom.getFileForASTPath(getLinkageID(), path); + } + } + return null; + } return super.getLocalToFile(binding); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPUsingDeclaration.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPUsingDeclaration.java index 16ecc7a9b22..dd3f05e4eed 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPUsingDeclaration.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPUsingDeclaration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Google, Inc and others. + * Copyright (c) 2008 Google, 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 @@ -33,10 +33,13 @@ import org.eclipse.core.runtime.CoreException; * @see ICPPUsingDeclaration */ class PDOMCPPUsingDeclaration extends PDOMCPPBinding implements ICPPUsingDeclaration { + @SuppressWarnings("static-access") private static final int TARGET_BINDING = PDOMCPPBinding.RECORD_SIZE + 0; // Using declarations for functions may have multiple delegates. We model such case // by creating a chain of PDOMCPPUsingDeclaration objects linked by NEXT_DELEGATE field. + @SuppressWarnings("static-access") private static final int NEXT_DELEGATE = PDOMCPPBinding.RECORD_SIZE + 4; + @SuppressWarnings({ "hiding", "static-access" }) protected static final int RECORD_SIZE = PDOMCPPBinding.RECORD_SIZE + 8; private ICPPDelegate[] delegates; @@ -116,10 +119,4 @@ class PDOMCPPUsingDeclaration extends PDOMCPPBinding implements ICPPUsingDeclara } return null; } - - public boolean hasDeclaration() throws CoreException { - // TODO(sprigogin) I'm not sure if returning unconditional true is legitimate, - // but I couldn't figure out a better way to satisfy DeclaredBindingsFilter#acceptBinding. - return true; - } }