mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Bug 332883: auto-type in range-based for.
This commit is contained in:
parent
1be3c0dbd9
commit
aa9690322b
2 changed files with 27 additions and 3 deletions
|
@ -9306,4 +9306,15 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
parseAndCheckBindings();
|
||||
}
|
||||
|
||||
// struct S {
|
||||
// void f();
|
||||
// };
|
||||
// void g() {
|
||||
// S array[5];
|
||||
// for (auto& s : array)
|
||||
// s.f(); // ERROR HERE
|
||||
// }
|
||||
public void testAutoTypeInRangeBasedFor_332883() throws Exception {
|
||||
parseAndCheckBindings();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1748,7 +1748,7 @@ public class CPPVisitor extends ASTQueries {
|
|||
if (declarator instanceof ICPPASTFunctionDeclarator) {
|
||||
return createAutoFunctionType(declSpec, (ICPPASTFunctionDeclarator) declarator);
|
||||
}
|
||||
|
||||
boolean rangeBasedFor= false;
|
||||
parent = parent.getParent();
|
||||
if (parent instanceof ICPPASTNewExpression) {
|
||||
IASTInitializer initializer = ((ICPPASTNewExpression) parent).getInitializer();
|
||||
|
@ -1758,12 +1758,16 @@ public class CPPVisitor extends ASTQueries {
|
|||
initClause = arguments[0];
|
||||
}
|
||||
}
|
||||
} else if (parent instanceof ICPPASTRangeBasedForStatement) {
|
||||
ICPPASTRangeBasedForStatement forStmt= (ICPPASTRangeBasedForStatement) parent;
|
||||
initClause= forStmt.getInitializerClause();
|
||||
rangeBasedFor= true;
|
||||
} else if (parent instanceof IASTCompositeTypeSpecifier &&
|
||||
declSpec.getStorageClass() != IASTDeclSpecifier.sc_static) {
|
||||
// Non-static auto-typed class members are not allowed.
|
||||
return new ProblemType(ISemanticProblem.TYPE_AUTO_FOR_NON_STATIC_FIELD);
|
||||
}
|
||||
return createAutoType(initClause, declSpec, declarator);
|
||||
return createAutoType(initClause, declSpec, declarator, rangeBasedFor);
|
||||
}
|
||||
|
||||
IType type = createType(declSpec);
|
||||
|
@ -1786,7 +1790,8 @@ public class CPPVisitor extends ASTQueries {
|
|||
return type;
|
||||
}
|
||||
|
||||
private static IType createAutoType(IASTNode initClause, IASTDeclSpecifier declSpec, IASTDeclarator declarator) {
|
||||
private static IType createAutoType(IASTNode initClause, IASTDeclSpecifier declSpec, IASTDeclarator declarator,
|
||||
boolean rangeBasedFor) {
|
||||
// C++0x: 7.1.6.4
|
||||
if (!autoTypeDeclSpecs.get().add(declSpec)) {
|
||||
// Detected a self referring auto type, e.g.: auto x = x;
|
||||
|
@ -1837,6 +1842,14 @@ public class CPPVisitor extends ASTQueries {
|
|||
type = (IType) CPPTemplates.instantiate(initializer_list_template,
|
||||
new ICPPTemplateArgument[] { new CPPTemplateArgument(type) }, true);
|
||||
}
|
||||
if (rangeBasedFor) {
|
||||
if (type instanceof IArrayType) {
|
||||
type= ((IArrayType) type).getType();
|
||||
} else {
|
||||
// todo: handle non-array types in for based range loops
|
||||
// (c++-spec 6.5.4): 'typeof (* begin(type &&))'
|
||||
}
|
||||
}
|
||||
return decorateType(type, declSpec, declarator);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue