mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Fixed StackOverflowError in auto type calculation for "for (auto a : a)"
This commit is contained in:
parent
b7807b8a16
commit
8a97926f2b
3 changed files with 97 additions and 85 deletions
|
@ -713,19 +713,20 @@ public class AST2BaseTest extends BaseTestCase {
|
|||
}
|
||||
|
||||
private IBinding binding(String section, int len) {
|
||||
IASTName name = findName(section, len);
|
||||
IASTName astName = findName(section, len);
|
||||
final String selection = section.substring(0, len);
|
||||
assertNotNull("Did not find \"" + selection + "\"", name);
|
||||
assertEquals(selection, name.getRawSignature());
|
||||
assertNotNull("No AST name for \"" + selection + "\"", astName);
|
||||
assertEquals(selection, astName.getRawSignature());
|
||||
|
||||
IBinding binding = name.resolveBinding();
|
||||
assertNotNull("No binding for " + name.getRawSignature(), binding);
|
||||
IBinding binding = astName.resolveBinding();
|
||||
assertNotNull("No binding for " + astName.getRawSignature(), binding);
|
||||
|
||||
return name.resolveBinding();
|
||||
return astName.resolveBinding();
|
||||
}
|
||||
|
||||
private IBinding binding(String context, String name) {
|
||||
IASTName astName = findName(context, name);
|
||||
assertNotNull("No AST name for \"" + name + "\"", astName);
|
||||
assertEquals(name, astName.getRawSignature());
|
||||
|
||||
IBinding binding = astName.resolveBinding();
|
||||
|
|
|
@ -8436,13 +8436,20 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
assertEquals(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE, pt.getID());
|
||||
}
|
||||
|
||||
// auto x = x; // Self referring type.
|
||||
// auto x = y + z;
|
||||
// auto y = x;
|
||||
// auto z = x;
|
||||
// void test() {
|
||||
// for (auto a : a) {}
|
||||
// }
|
||||
public void testAutoType_305970() throws Exception {
|
||||
String code= getAboveComment();
|
||||
BindingAssertionHelper bh= new BindingAssertionHelper(code, true);
|
||||
ICPPVariable x= bh.assertNonProblem("x =", 1);
|
||||
IProblemType pt= (IProblemType) x.getType();
|
||||
assertEquals(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE, pt.getID());
|
||||
BindingAssertionHelper bh = getAssertionHelper();
|
||||
ICPPVariable x= bh.assertNonProblem("x =", 1, ICPPVariable.class);
|
||||
IProblemType xt= (IProblemType) x.getType();
|
||||
assertEquals(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE, xt.getID());
|
||||
ICPPVariable a= bh.assertNonProblem("a :", "a", ICPPVariable.class);
|
||||
IProblemType at= (IProblemType) a.getType();
|
||||
assertEquals(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE, at.getID());
|
||||
}
|
||||
|
||||
// struct A { auto a = 1; }; // Auto-typed non-static fields are not allowed.
|
||||
|
|
|
@ -1980,7 +1980,13 @@ public class CPPVisitor extends ASTQueries {
|
|||
return type;
|
||||
}
|
||||
|
||||
private static IType createAutoType(IASTDeclSpecifier declSpec, IASTDeclarator declarator) {
|
||||
private static IType createAutoType(final IASTDeclSpecifier declSpec, IASTDeclarator declarator) {
|
||||
Set<IASTDeclSpecifier> recursionProtectionSet = autoTypeDeclSpecs.get();
|
||||
try {
|
||||
if (!recursionProtectionSet.add(declSpec)) {
|
||||
// Detected a self referring auto type, e.g.: auto x = x;
|
||||
return new ProblemType(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE);
|
||||
}
|
||||
if (declarator instanceof ICPPASTFunctionDeclarator) {
|
||||
return createAutoFunctionType(declSpec, (ICPPASTFunctionDeclarator) declarator);
|
||||
}
|
||||
|
@ -2038,13 +2044,15 @@ public class CPPVisitor extends ASTQueries {
|
|||
}
|
||||
}
|
||||
return createAutoType(autoInitClause, declSpec, declarator);
|
||||
} finally {
|
||||
recursionProtectionSet.remove(declSpec);
|
||||
}
|
||||
}
|
||||
|
||||
private static IType createAutoType(ICPPASTInitializerClause initClause, IASTDeclSpecifier declSpec,
|
||||
IASTDeclarator declarator) {
|
||||
// C++0x: 7.1.6.4
|
||||
if (initClause == null || !autoTypeDeclSpecs.get().add(declSpec)) {
|
||||
// Detected a self referring auto type, e.g.: auto x = x;
|
||||
if (initClause == null) {
|
||||
return new ProblemType(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE);
|
||||
}
|
||||
|
||||
|
@ -2052,7 +2060,6 @@ public class CPPVisitor extends ASTQueries {
|
|||
IType initType = null;
|
||||
ValueCategory valueCat= null;
|
||||
ICPPClassTemplate initializer_list_template = null;
|
||||
try {
|
||||
if (initClause instanceof ICPPASTInitializerList) {
|
||||
initializer_list_template = get_initializer_list(declSpec);
|
||||
if (initializer_list_template == null) {
|
||||
|
@ -2068,12 +2075,9 @@ public class CPPVisitor extends ASTQueries {
|
|||
final ICPPEvaluation evaluation = initClause.getEvaluation();
|
||||
initType= evaluation.getTypeOrFunctionSet(declarator);
|
||||
valueCat= evaluation.getValueCategory(declarator);
|
||||
if (initType == null) {
|
||||
if (initType == null || initType instanceof ISemanticProblem) {
|
||||
return new ProblemType(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE);
|
||||
}
|
||||
} finally {
|
||||
autoTypeDeclSpecs.get().remove(declSpec);
|
||||
}
|
||||
ICPPFunctionTemplate template = new AutoTypeResolver(type);
|
||||
CPPTemplateParameterMap paramMap = new CPPTemplateParameterMap(1);
|
||||
TemplateArgumentDeduction.deduceFromFunctionArgs(template, Collections.singletonList(initType),
|
||||
|
|
Loading…
Add table
Reference in a new issue