1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-31 21:05:37 +02:00

More consistent treatment of anonymous namespaces betwenn index and

non-index bindings.
This commit is contained in:
Sergey Prigogin 2014-08-21 22:29:30 -07:00
parent cf448ee139
commit 698bb0afe4
5 changed files with 105 additions and 41 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2013 IBM Corporation and others. * Copyright (c) 2005, 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -13,6 +13,7 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.core.dom.ast; package org.eclipse.cdt.core.dom.ast;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.List; import java.util.List;
@ -38,6 +39,7 @@ import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.parser.GCCKeywords; import org.eclipse.cdt.core.parser.GCCKeywords;
import org.eclipse.cdt.core.parser.Keywords; import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer; import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.Value; import org.eclipse.cdt.internal.core.dom.parser.Value;
import org.eclipse.cdt.internal.core.dom.parser.c.CASTTypeId; import org.eclipse.cdt.internal.core.dom.parser.c.CASTTypeId;
@ -50,7 +52,10 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownMemberClassInstan
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.TypeOfDependentExpression; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.TypeOfDependentExpression;
import org.eclipse.cdt.utils.UNCPathConverter;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/** /**
* This is a utility class to help convert AST elements to Strings corresponding to * This is a utility class to help convert AST elements to Strings corresponding to
@ -582,7 +587,7 @@ public class ASTTypeUtil {
needSpace= true; needSpace= true;
} else { } else {
if (postfix == null) { if (postfix == null) {
postfix= new ArrayList<IType>(); postfix= new ArrayList<>();
} }
postfix.add(tj); postfix.add(tj);
needParenthesis= true; needParenthesis= true;
@ -739,16 +744,27 @@ public class ASTTypeUtil {
if (owner instanceof ICPPNamespace || owner instanceof IType) { if (owner instanceof ICPPNamespace || owner instanceof IType) {
int pos= result.length(); int pos= result.length();
appendCppName(owner, normalize, qualify, result); appendCppName(owner, normalize, qualify, result);
if (binding instanceof IIndexBinding && owner instanceof ICPPNamespace && owner.getNameCharArray().length == 0) { if (owner instanceof ICPPNamespace && owner.getNameCharArray().length == 0) {
try { if (binding instanceof IIndexBinding) {
IIndexFile file = ((IIndexBinding) binding).getLocalToFile(); try {
if (file != null) { IIndexFile file = ((IIndexBinding) binding).getLocalToFile();
if (file != null) {
result.append('{');
result.append(file.getLocation().getURI().toString());
result.append('}');
}
} catch (CoreException e) {
CCorePlugin.log(e);
}
} else {
IASTNode node = ASTInternal.getDeclaredInSourceFileOnly(binding);
if (node != null) {
IPath filePath = new Path(node.getTranslationUnit().getFilePath());
URI uri = UNCPathConverter.getInstance().toURI(filePath);
result.append('{'); result.append('{');
result.append(file.getLocation().getURI().toString()); result.append(uri.toString());
result.append('}'); result.append('}');
} }
} catch (CoreException e) {
CCorePlugin.log(e);
} }
} }
if (result.length() > pos) if (result.length() > pos)

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2006, 2012 Wind River Systems, Inc. and others. * Copyright (c) 2006, 2014 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Markus Schorn - initial API and implementation * Markus Schorn - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser; package org.eclipse.cdt.internal.core.dom.parser;
@ -18,12 +19,17 @@ import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.index.IIndexBinding; import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IndexFilter; import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.index.IndexLocationFactory;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalBinding; import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalBinding;
import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalFunction; import org.eclipse.cdt.internal.core.dom.parser.c.ICInternalFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalFunction; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalFunction;
import org.eclipse.cdt.internal.core.index.IIndexFragment; import org.eclipse.cdt.internal.core.index.IIndexFragment;
import org.eclipse.cdt.internal.core.index.IndexFileSet;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
@ -76,6 +82,47 @@ public class ASTInternal {
public static IASTNode getDeclaredInSourceFileOnly(IIndexFragment forFragment, IBinding binding, public static IASTNode getDeclaredInSourceFileOnly(IIndexFragment forFragment, IBinding binding,
boolean requireDefinition, PDOMBinding glob) { boolean requireDefinition, PDOMBinding glob) {
IASTNode result = getDeclaredInSourceFileOnly(binding, requireDefinition);
if (result == null)
return null;
if (requireDefinition && glob != null) {
try {
if (glob.hasDeclaration())
return null;
} catch (CoreException e) {
}
}
IASTTranslationUnit tu= result.getTranslationUnit();
if (tu != null) {
if (tu.getIndexFileSet().containsNonLocalDeclaration(binding, forFragment))
return null;
}
return result;
}
public static IASTNode getDeclaredInSourceFileOnly(IBinding binding) {
IASTNode result = getDeclaredInSourceFileOnly(binding, false);
if (result == null)
return null;
IASTTranslationUnit ast= result.getTranslationUnit();
if (ast != null) {
ITranslationUnit tu = ast.getOriginatingTranslationUnit();
if (tu == null)
return null;
IIndexFileLocation location = IndexLocationFactory.getIFL(tu);
IIndexFileSet fileSet = ast.getIndexFileSet();
if (!(fileSet instanceof IndexFileSet))
return null;
if (((IndexFileSet) fileSet).containsNonLocalDeclaration(binding, location))
return null;
}
return result;
}
private static IASTNode getDeclaredInSourceFileOnly(IBinding binding, boolean requireDefinition) {
IASTNode[] decls; IASTNode[] decls;
IASTNode def; IASTNode def;
if (binding instanceof ICPPInternalBinding) { if (binding instanceof ICPPInternalBinding) {
@ -108,22 +155,6 @@ public class ASTInternal {
} }
} }
} }
if (result == null)
return null;
if (requireDefinition && glob != null) {
try {
if (glob.hasDeclaration())
return null;
} catch (CoreException e) {
}
}
IASTTranslationUnit tu= result.getTranslationUnit();
if (tu != null) {
if (tu.getIndexFileSet().containsNonLocalDeclaration(binding, forFragment))
return null;
}
return result; return result;
} }

View file

@ -650,15 +650,8 @@ public class SemanticUtil {
while (true) { while (true) {
for (int i = 0; b1 instanceof ICPPNamespaceAlias && i < 20; i++) for (int i = 0; b1 instanceof ICPPNamespaceAlias && i < 20; i++)
b1= ((ICPPNamespaceAlias) b1).getBinding(); b1= ((ICPPNamespaceAlias) b1).getBinding();
// TODO(sprigogin): Anonymous namespaces should not be ignored here.
// Ignore anonymous namespaces.
while (b1 instanceof ICPPNamespace && b1.getNameCharArray().length == 0)
b1= b1.getOwner();
for (int i = 0; b2 instanceof ICPPNamespaceAlias && i < 20; i++) for (int i = 0; b2 instanceof ICPPNamespaceAlias && i < 20; i++)
b2= ((ICPPNamespaceAlias) b2).getBinding(); b2= ((ICPPNamespaceAlias) b2).getBinding();
// Ignore anonymous namespaces.
while (b2 instanceof ICPPNamespace && b2.getNameCharArray().length == 0)
b2= b2.getOwner();
if (b1 == null) if (b1 == null)
return b2 == null; return b2 == null;

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2008, 2012 Wind River Systems, Inc. and others. * Copyright (c) 2008, 2014 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Markus Schorn - initial API and implementation * Markus Schorn - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.index; package org.eclipse.cdt.internal.core.index;
@ -18,13 +19,14 @@ import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.index.IIndexBinding; import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IIndexFileSet; import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
public class IndexFileSet implements IIndexFileSet { public class IndexFileSet implements IIndexFileSet {
private IIndexFileSet fInverse; private IIndexFileSet fInverse;
private HashMap<IIndexFragment, IIndexFragmentFileSet> fSubSets= new HashMap<IIndexFragment, IIndexFragmentFileSet>(); private HashMap<IIndexFragment, IIndexFragmentFileSet> fSubSets= new HashMap<>();
public IndexFileSet() { public IndexFileSet() {
} }
@ -98,6 +100,30 @@ public class IndexFileSet implements IIndexFileSet {
return false; return false;
} }
public boolean containsNonLocalDeclaration(IBinding binding, IIndexFileLocation ignore) {
for (Map.Entry<IIndexFragment, IIndexFragmentFileSet> entry : fSubSets.entrySet()) {
try {
final IIndexFragment fragment = entry.getKey();
final IIndexFragmentFileSet subset = entry.getValue();
IIndexFragmentName[] names =
fragment.findNames(binding, IIndexFragment.FIND_DECLARATIONS_DEFINITIONS | IIndexFragment.FIND_NON_LOCAL_ONLY);
for (IIndexFragmentName name : names) {
try {
IIndexFile file = name.getFile();
if (!file.getLocation().equals(ignore) && subset.contains((IIndexFragmentFile) file)) {
return true;
}
} catch (CoreException e) {
CCorePlugin.log(e);
}
}
} catch (CoreException e) {
CCorePlugin.log(e);
}
}
return false;
}
@Override @Override
public IBinding[] filterFileLocalBindings(IBinding[] bindings) { public IBinding[] filterFileLocalBindings(IBinding[] bindings) {
return filterFileLocalBindings(bindings, false); return filterFileLocalBindings(bindings, false);

View file

@ -1166,12 +1166,10 @@ class PDOMCPPLinkage extends PDOMLinkage implements IIndexCPPBindingConstants {
} }
if (file == null && !(binding instanceof IIndexBinding)) { if (file == null && !(binding instanceof IIndexBinding)) {
IBinding owner= binding.getOwner(); IBinding owner= binding.getOwner();
if (owner instanceof ICPPNamespace) { if (owner instanceof ICPPNamespace && owner.getNameCharArray().length == 0) {
if (owner.getNameCharArray().length == 0) { IASTNode node= ASTInternal.getDeclaredInSourceFileOnly(getPDOM(), binding, false, glob);
IASTNode node= ASTInternal.getDeclaredInSourceFileOnly(getPDOM(), binding, false, glob); if (node != null) {
if (node != null) { file= wpdom.getFileForASTNode(getLinkageID(), node);
file= wpdom.getFileForASTNode(getLinkageID(), node);
}
} }
} }
} }