mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 06:32:10 +02:00
Content assist for typedefs using same name as struct, bug 279931.
This commit is contained in:
parent
1cb9ea94e6
commit
ada1ba048c
4 changed files with 53 additions and 18 deletions
|
@ -11,8 +11,6 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.core.parser.tests.prefix;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTCompletionNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
|
@ -137,11 +135,31 @@ public class BasicCompletionTest extends CompletionTestBase {
|
|||
// b.
|
||||
public void testBug267911() throws Exception {
|
||||
String code = getAboveComment();
|
||||
IASTCompletionNode node = getGPPCompletionNode(code);
|
||||
assertNotNull(node);
|
||||
List<IBinding> bindings= proposeBindings(node);
|
||||
String[] names= getSortedNames(bindings);
|
||||
assertEquals("B", names[0]);
|
||||
assertEquals("doit", names[1]);
|
||||
String[] expected= {"B", "doit"};
|
||||
checkCompletion(code, true, expected);
|
||||
}
|
||||
|
||||
// typedef struct MyType {
|
||||
// int aField;
|
||||
// } MyType;
|
||||
// M
|
||||
public void testBug279931() throws Exception {
|
||||
String code = getAboveComment();
|
||||
String[] expected= {"MyType", "MyType"};
|
||||
checkCompletion(code, true, expected);
|
||||
expected= new String[] {"MyType"};
|
||||
checkCompletion(code, false, expected);
|
||||
}
|
||||
|
||||
// typedef struct MyType {
|
||||
// int aField;
|
||||
// } MyType;
|
||||
// struct M
|
||||
public void testBug279931a() throws Exception {
|
||||
String code = getAboveComment();
|
||||
String[] expected= {"MyType"};
|
||||
checkCompletion(code, true, expected);
|
||||
checkCompletion(code, false, expected);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ public class CompletionTestBase extends BaseTestCase {
|
|||
private static final IParserLogService NULL_LOG = new NullLogService();
|
||||
|
||||
protected IASTCompletionNode getCompletionNode(String code, ParserLanguage lang, boolean useGNUExtensions) throws ParserException {
|
||||
CodeReader codeReader = new CodeReader(code.toCharArray());
|
||||
CodeReader codeReader = new CodeReader(code.trim().toCharArray());
|
||||
ScannerInfo scannerInfo = new ScannerInfo();
|
||||
IScanner scanner= AST2BaseTest.createScanner(codeReader, lang, ParserMode.COMPLETE_PARSE, scannerInfo);
|
||||
|
||||
|
@ -93,6 +93,18 @@ public class CompletionTestBase extends BaseTestCase {
|
|||
return getCompletionNode(code, ParserLanguage.C, true);
|
||||
}
|
||||
|
||||
protected void checkCompletion(String code, boolean isCpp, String[] expected) throws ParserException {
|
||||
IASTCompletionNode node = isCpp ? getGPPCompletionNode(code) : getGCCCompletionNode(code);
|
||||
assertNotNull(node);
|
||||
List<IBinding> bindings= proposeBindings(node);
|
||||
String[] names= getSortedNames(bindings);
|
||||
int len= Math.min(expected.length, names.length);
|
||||
for (int i = 0; i < len; i++) {
|
||||
assertEquals(expected[i], names[i]);
|
||||
}
|
||||
assertEquals(expected.length, names.length);
|
||||
}
|
||||
|
||||
private static class BindingsComparator implements Comparator {
|
||||
public int compare(Object o1, Object o2) {
|
||||
IBinding b1 = (IBinding)o1;
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
|||
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.ITypedef;
|
||||
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
|
||||
|
||||
/**
|
||||
|
@ -138,6 +139,8 @@ public class CASTElaboratedTypeSpecifier extends CASTBaseDeclSpecifier implement
|
|||
} else if (b instanceof IEnumeration) {
|
||||
if (getKind() != k_enum)
|
||||
b= null;
|
||||
} else if (b instanceof ITypedef) {
|
||||
b= null;
|
||||
}
|
||||
if (b != null) {
|
||||
result[nextPos++]= b;
|
||||
|
|
|
@ -85,7 +85,7 @@ import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTSimpleDeclSpecifier;
|
|||
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||
import org.eclipse.cdt.core.index.IIndexFileSet;
|
||||
import org.eclipse.cdt.core.parser.util.ArrayUtil;
|
||||
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
|
||||
import org.eclipse.cdt.core.parser.util.CharArraySet;
|
||||
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
||||
|
@ -1123,28 +1123,30 @@ public class CVisitor extends ASTQueries {
|
|||
}
|
||||
}
|
||||
|
||||
CharArrayObjectMap prefixMap = new CharArrayObjectMap(2);
|
||||
IBinding[] result = null;
|
||||
CharArraySet handled= new CharArraySet(1);
|
||||
while (scope != null) {
|
||||
try {
|
||||
if (!(scope instanceof ICCompositeTypeScope)) {
|
||||
IBinding[] bindings= scope.getBindings(name, true, true, fileSet);
|
||||
for (IBinding b : bindings) {
|
||||
final char[] n= b.getNameCharArray();
|
||||
if (!prefixMap.containsKey(n)) {
|
||||
prefixMap.put(n, b);
|
||||
// consider binding only if no binding with the same name was found in another scope.
|
||||
if (!handled.containsKey(n)) {
|
||||
result= (IBinding[]) ArrayUtil.append(IBinding.class, result, b);
|
||||
}
|
||||
}
|
||||
// store names of bindings
|
||||
for (IBinding b : bindings) {
|
||||
final char[] n= b.getNameCharArray();
|
||||
handled.put(n);
|
||||
}
|
||||
}
|
||||
} catch (DOMException e) {
|
||||
}
|
||||
scope= scope.getParent();
|
||||
}
|
||||
|
||||
IBinding[] result = null;
|
||||
Object[] vals = prefixMap.valueArray();
|
||||
for (Object val : vals) {
|
||||
result = (IBinding[]) ArrayUtil.append(IBinding.class, result, val);
|
||||
}
|
||||
return (IBinding[]) ArrayUtil.trim(IBinding.class, result);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue