mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-09 18:56:02 +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();
|
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) {
|
if (declarator instanceof ICPPASTFunctionDeclarator) {
|
||||||
return createAutoFunctionType(declSpec, (ICPPASTFunctionDeclarator) declarator);
|
return createAutoFunctionType(declSpec, (ICPPASTFunctionDeclarator) declarator);
|
||||||
}
|
}
|
||||||
|
boolean rangeBasedFor= false;
|
||||||
parent = parent.getParent();
|
parent = parent.getParent();
|
||||||
if (parent instanceof ICPPASTNewExpression) {
|
if (parent instanceof ICPPASTNewExpression) {
|
||||||
IASTInitializer initializer = ((ICPPASTNewExpression) parent).getInitializer();
|
IASTInitializer initializer = ((ICPPASTNewExpression) parent).getInitializer();
|
||||||
|
@ -1758,12 +1758,16 @@ public class CPPVisitor extends ASTQueries {
|
||||||
initClause = arguments[0];
|
initClause = arguments[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (parent instanceof ICPPASTRangeBasedForStatement) {
|
||||||
|
ICPPASTRangeBasedForStatement forStmt= (ICPPASTRangeBasedForStatement) parent;
|
||||||
|
initClause= forStmt.getInitializerClause();
|
||||||
|
rangeBasedFor= true;
|
||||||
} else if (parent instanceof IASTCompositeTypeSpecifier &&
|
} else if (parent instanceof IASTCompositeTypeSpecifier &&
|
||||||
declSpec.getStorageClass() != IASTDeclSpecifier.sc_static) {
|
declSpec.getStorageClass() != IASTDeclSpecifier.sc_static) {
|
||||||
// Non-static auto-typed class members are not allowed.
|
// Non-static auto-typed class members are not allowed.
|
||||||
return new ProblemType(ISemanticProblem.TYPE_AUTO_FOR_NON_STATIC_FIELD);
|
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);
|
IType type = createType(declSpec);
|
||||||
|
@ -1786,7 +1790,8 @@ public class CPPVisitor extends ASTQueries {
|
||||||
return type;
|
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
|
// C++0x: 7.1.6.4
|
||||||
if (!autoTypeDeclSpecs.get().add(declSpec)) {
|
if (!autoTypeDeclSpecs.get().add(declSpec)) {
|
||||||
// Detected a self referring auto type, e.g.: auto x = x;
|
// 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,
|
type = (IType) CPPTemplates.instantiate(initializer_list_template,
|
||||||
new ICPPTemplateArgument[] { new CPPTemplateArgument(type) }, true);
|
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);
|
return decorateType(type, declSpec, declarator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue