1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 06:45:43 +02:00

Bug 331056: Content assist for involving using declarations.

This commit is contained in:
Markus Schorn 2010-12-02 10:23:06 +00:00
parent b30316c094
commit f8f2a9c9a3
3 changed files with 152 additions and 138 deletions

View file

@ -191,7 +191,7 @@ abstract public class CPPScope implements ICPPASTInternalScope {
}
public IBinding getBindingInAST(IASTName name, boolean forceResolve) {
IBinding[] bs= getBindingsInAST(name, forceResolve, false, false, false);
IBinding[] bs= getBindingsInAST(name, forceResolve, false, false);
return CPPSemantics.resolveAmbiguities(name, bs);
}
@ -201,7 +201,7 @@ abstract public class CPPScope implements ICPPASTInternalScope {
public IBinding[] getBindings(IASTName name, boolean resolve, boolean prefixLookup, IIndexFileSet fileSet,
boolean checkPointOfDecl) {
IBinding[] result = getBindingsInAST(name, resolve, prefixLookup, checkPointOfDecl, true);
IBinding[] result = getBindingsInAST(name, resolve, prefixLookup, checkPointOfDecl);
final IASTTranslationUnit tu = name.getTranslationUnit();
if (tu != null) {
IIndex index = tu.getIndex();
@ -242,8 +242,9 @@ abstract public class CPPScope implements ICPPASTInternalScope {
return (IBinding[]) ArrayUtil.trim(IBinding.class, result);
}
public IBinding[] getBindingsInAST(IASTName name, boolean forceResolve, boolean prefixLookup,
boolean checkPointOfDecl, boolean expandUsingDirectives) {
boolean checkPointOfDecl) {
populateCache();
final char[] c = name.getLookupKey();
IBinding[] result = null;
@ -272,17 +273,17 @@ abstract public class CPPScope implements ICPPASTInternalScope {
if (obj instanceof ObjectSet<?>) {
ObjectSet<?> os= (ObjectSet<?>) obj;
for (int j = 0; j < os.size(); j++) {
result= addCandidate(os.keyAt(j), name, forceResolve, checkPointOfDecl, expandUsingDirectives, result);
result= addCandidate(os.keyAt(j), name, forceResolve, checkPointOfDecl, result);
}
} else {
result = addCandidate(obj, name, forceResolve, checkPointOfDecl, expandUsingDirectives, result);
result = addCandidate(obj, name, forceResolve, checkPointOfDecl, result);
}
}
return (IBinding[]) ArrayUtil.trim(IBinding.class, result);
}
private IBinding[] addCandidate(Object candidate, IASTName name, boolean forceResolve,
boolean checkPointOfDecl, boolean expandUsingDirectives, IBinding[] result) {
boolean checkPointOfDecl, IBinding[] result) {
if (checkPointOfDecl) {
IASTTranslationUnit tu= name.getTranslationUnit();
if (!CPPSemantics.declaredBefore(candidate, name, tu != null && tu.getIndex() != null)) {
@ -308,13 +309,7 @@ abstract public class CPPScope implements ICPPASTInternalScope {
binding= (IBinding) candidate;
}
if (expandUsingDirectives && binding instanceof ICPPUsingDeclaration) {
IBinding[] delegates = ((ICPPUsingDeclaration) binding).getDelegates();
result= (IBinding[]) ArrayUtil.addAll(IBinding.class, result, delegates);
} else {
result = (IBinding[]) ArrayUtil.append(IBinding.class, result, binding);
}
return result;
return (IBinding[]) ArrayUtil.append(IBinding.class, result, binding);
}
public final void populateCache() {

View file

@ -1195,41 +1195,46 @@ public class CPPSemantics {
// For index scopes the point of declaration is ignored.
bindings= scope.getBindings(data.astName, true, data.prefixLookup, fileSet);
}
return expandUsingDeclarationsAndRemoveObjects(bindings, data.typesOnly);
}
if (data.typesOnly) {
return removeObjects(bindings);
private static IBinding[] expandUsingDeclarationsAndRemoveObjects(final IBinding[] bindings, boolean removeObjects) {
if (bindings == null || bindings.length == 0)
return IBinding.EMPTY_BINDING_ARRAY;
for (IBinding b : bindings) {
if (b == null)
break;
if (b instanceof ICPPUsingDeclaration || (removeObjects && isObject(b))) {
List<IBinding> result= new ArrayList<IBinding>(bindings.length);
expandUsingDeclarations(bindings, removeObjects, result);
return result.toArray(new IBinding[result.size()]);
}
}
return bindings;
}
private static IBinding[] removeObjects(final IBinding[] bindings) {
final int length = bindings.length;
IBinding[] copy= null;
int pos= 0;
for (int i = 0; i < length; i++) {
final IBinding binding= bindings[i];
IBinding check= binding;
if (binding instanceof ICPPUsingDeclaration) {
IBinding[] delegates= ((ICPPUsingDeclaration) binding).getDelegates();
if (delegates.length > 0)
check= delegates[0];
private static boolean isObject(IBinding b) {
return !(b instanceof IType || b instanceof ICPPNamespace);
}
if (check instanceof IType || check instanceof ICPPNamespace) {
if (copy != null) {
copy[pos]= binding;
}
pos++;
} else {
if (copy == null) {
copy= new IBinding[length-1];
System.arraycopy(bindings, 0, copy, 0, pos);
}
}
}
if (pos == 0)
return IBinding.EMPTY_BINDING_ARRAY;
return copy == null ? bindings : copy;
private static void expandUsingDeclarations(IBinding[] bindings, boolean removeObjects, List<IBinding> result) {
if (bindings != null) {
for (IBinding b : bindings) {
if (b == null)
return;
if (b instanceof ICPPUsingDeclaration) {
for (IBinding d : ((ICPPUsingDeclaration) b).getDelegates()) {
if (d != null && !(removeObjects && isObject(d))) {
result.add(d);
}
}
} else if (!(removeObjects && isObject(b))) {
result.add(b);
}
}
}
}
private static ICPPTemplateScope enclosingTemplateScope(IASTNode node) {

View file

@ -167,7 +167,14 @@ public class CompletionTests extends AbstractContentAssistTest {
// };
// typedef enum {__nix} _e204758;
// void _f204758(_e204758 x);
//
// // Bug 331056
// namespace _A_331056 {
// class Reference {};
// }
// namespace _B_331056 {
// using ::_A_331056::Reference;
// }
public CompletionTests(String name) {
super(name, true);
@ -747,7 +754,7 @@ public class CompletionTests extends AbstractContentAssistTest {
assertCompletionResults(fCursorOffset, expected, AbstractContentAssistTest.COMPARE_REP_STRINGS);
}
//using namespace /*cursor*/
//using namespace n/*cursor*/
public void testAutoColons2() throws Exception {
final String[] expected= {
"ns"
@ -1233,7 +1240,7 @@ public class CompletionTests extends AbstractContentAssistTest {
public void testConstructorInitializerList_EmptyInput_Bug266586() throws Exception {
final String[] expected= {"mOne", "Base",
"Base(int)", "Base(const Base<Helper> &)", "Helper",
"Helper(void)", "Helper(const Helper &)",
"Helper(void)", "Helper(const Helper &)", "_A_331056", "_B_331056",
// Namespaces must be offered as well. In order for this code
// to compile with gcc (e.g. 4.1.2), you need to write
// ::ns::Base<Helper>() instead of just Base<Helper>().
@ -1326,4 +1333,11 @@ public class CompletionTests extends AbstractContentAssistTest {
final String[] expected= { "push_back(const int & value) : void" };
assertParameterHint(expected);
}
// using namespace ::_B_331056;
// Ref/*cursor*/
public void testUsingDeclaration_Bug331056() throws Exception {
final String[] expected= { "Reference" };
assertCompletionResults(fCursorOffset, expected, COMPARE_ID_STRINGS);
}
}