1
0
Fork 0
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:
Sergey Prigogin 2012-12-05 19:22:08 -08:00
parent b7807b8a16
commit 8a97926f2b
3 changed files with 97 additions and 85 deletions

View file

@ -713,19 +713,20 @@ public class AST2BaseTest extends BaseTestCase {
} }
private IBinding binding(String section, int len) { private IBinding binding(String section, int len) {
IASTName name = findName(section, len); IASTName astName = findName(section, len);
final String selection = section.substring(0, len); final String selection = section.substring(0, len);
assertNotNull("Did not find \"" + selection + "\"", name); assertNotNull("No AST name for \"" + selection + "\"", astName);
assertEquals(selection, name.getRawSignature()); assertEquals(selection, astName.getRawSignature());
IBinding binding = name.resolveBinding(); IBinding binding = astName.resolveBinding();
assertNotNull("No binding for " + name.getRawSignature(), binding); assertNotNull("No binding for " + astName.getRawSignature(), binding);
return name.resolveBinding(); return astName.resolveBinding();
} }
private IBinding binding(String context, String name) { private IBinding binding(String context, String name) {
IASTName astName = findName(context, name); IASTName astName = findName(context, name);
assertNotNull("No AST name for \"" + name + "\"", astName);
assertEquals(name, astName.getRawSignature()); assertEquals(name, astName.getRawSignature());
IBinding binding = astName.resolveBinding(); IBinding binding = astName.resolveBinding();

View file

@ -8436,13 +8436,20 @@ public class AST2CPPTests extends AST2BaseTest {
assertEquals(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE, pt.getID()); 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 { public void testAutoType_305970() throws Exception {
String code= getAboveComment(); BindingAssertionHelper bh = getAssertionHelper();
BindingAssertionHelper bh= new BindingAssertionHelper(code, true); ICPPVariable x= bh.assertNonProblem("x =", 1, ICPPVariable.class);
ICPPVariable x= bh.assertNonProblem("x =", 1); IProblemType xt= (IProblemType) x.getType();
IProblemType pt= (IProblemType) x.getType(); assertEquals(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE, xt.getID());
assertEquals(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE, pt.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. // struct A { auto a = 1; }; // Auto-typed non-static fields are not allowed.

View file

@ -1980,7 +1980,13 @@ public class CPPVisitor extends ASTQueries {
return type; 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) { if (declarator instanceof ICPPASTFunctionDeclarator) {
return createAutoFunctionType(declSpec, (ICPPASTFunctionDeclarator) declarator); return createAutoFunctionType(declSpec, (ICPPASTFunctionDeclarator) declarator);
} }
@ -2038,13 +2044,15 @@ public class CPPVisitor extends ASTQueries {
} }
} }
return createAutoType(autoInitClause, declSpec, declarator); return createAutoType(autoInitClause, declSpec, declarator);
} finally {
recursionProtectionSet.remove(declSpec);
}
} }
private static IType createAutoType(ICPPASTInitializerClause initClause, IASTDeclSpecifier declSpec, private static IType createAutoType(ICPPASTInitializerClause initClause, IASTDeclSpecifier declSpec,
IASTDeclarator declarator) { IASTDeclarator declarator) {
// C++0x: 7.1.6.4 // C++0x: 7.1.6.4
if (initClause == null || !autoTypeDeclSpecs.get().add(declSpec)) { if (initClause == null) {
// Detected a self referring auto type, e.g.: auto x = x;
return new ProblemType(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE); return new ProblemType(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE);
} }
@ -2052,7 +2060,6 @@ public class CPPVisitor extends ASTQueries {
IType initType = null; IType initType = null;
ValueCategory valueCat= null; ValueCategory valueCat= null;
ICPPClassTemplate initializer_list_template = null; ICPPClassTemplate initializer_list_template = null;
try {
if (initClause instanceof ICPPASTInitializerList) { if (initClause instanceof ICPPASTInitializerList) {
initializer_list_template = get_initializer_list(declSpec); initializer_list_template = get_initializer_list(declSpec);
if (initializer_list_template == null) { if (initializer_list_template == null) {
@ -2068,12 +2075,9 @@ public class CPPVisitor extends ASTQueries {
final ICPPEvaluation evaluation = initClause.getEvaluation(); final ICPPEvaluation evaluation = initClause.getEvaluation();
initType= evaluation.getTypeOrFunctionSet(declarator); initType= evaluation.getTypeOrFunctionSet(declarator);
valueCat= evaluation.getValueCategory(declarator); valueCat= evaluation.getValueCategory(declarator);
if (initType == null) { if (initType == null || initType instanceof ISemanticProblem) {
return new ProblemType(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE); return new ProblemType(ISemanticProblem.TYPE_CANNOT_DEDUCE_AUTO_TYPE);
} }
} finally {
autoTypeDeclSpecs.get().remove(declSpec);
}
ICPPFunctionTemplate template = new AutoTypeResolver(type); ICPPFunctionTemplate template = new AutoTypeResolver(type);
CPPTemplateParameterMap paramMap = new CPPTemplateParameterMap(1); CPPTemplateParameterMap paramMap = new CPPTemplateParameterMap(1);
TemplateArgumentDeduction.deduceFromFunctionArgs(template, Collections.singletonList(initType), TemplateArgumentDeduction.deduceFromFunctionArgs(template, Collections.singletonList(initType),