Use VerifyIntegerConstantExpression for case values.

llvm-svn: 60317
This commit is contained in:
Anders Carlsson 2008-12-01 02:13:02 +00:00
parent 564730a857
commit f7fba46331
3 changed files with 10 additions and 25 deletions

View File

@ -126,31 +126,16 @@ Sema::ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval,
Expr *LHSVal = ((Expr *)lhsval), *RHSVal = ((Expr *)rhsval);
assert((LHSVal != 0) && "missing expression in case statement");
SourceLocation ExpLoc;
// C99 6.8.4.2p3: The expression shall be an integer constant.
// However, GCC allows any evaluatable integer expression.
// FIXME: Should we warn if this is evaluatable but not an I-C-E?
APValue Result;
bool isEvaluated;
if (!LHSVal->Evaluate(Result, Context, &isEvaluated) || !Result.isInt() ||
!isEvaluated) {
// FIXME: Evaluate doesn't return the SourceLocation that it failed to
// evaluate.
ExpLoc = LHSVal->getExprLoc();
Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr)
<< LHSVal->getSourceRange();
if (VerifyIntegerConstantExpression(LHSVal))
return SubStmt;
}
// GCC extension: The expression shall be an integer constant.
if (RHSVal && !RHSVal->Evaluate(Result, Context, &isEvaluated) ||
!Result.isInt() || !isEvaluated) {
ExpLoc = RHSVal->getExprLoc();
Diag(ExpLoc, diag::err_case_label_not_integer_constant_expr)
<< RHSVal->getSourceRange();
if (RHSVal && VerifyIntegerConstantExpression(RHSVal))
RHSVal = 0; // Recover by just forgetting about it.
}
if (SwitchStack.empty()) {
Diag(CaseLoc, diag::err_case_not_in_switch);

View File

@ -16,8 +16,8 @@ result = arr*brr;
result = xx*yy;
switch (arr) { // expected-error{{statement requires expression of integer type ('_Complex int' invalid)}}
case brr: ; // expected-error{{case label does not reduce to an integer constant}}
case xx: ; // expected-error{{case label does not reduce to an integer constant}}
case brr: ; // expected-error{{expression is not an integer constant expression}}
case xx: ; // expected-error{{expression is not an integer constant expression}}
}
}

View File

@ -39,8 +39,8 @@ void test4()
}
switch(1) {
case g(): // expected-error {{case label does not reduce to an integer constant}}
case 0 ... g(): // expected-error {{case label does not reduce to an integer constant}}
case g(): // expected-error {{expression is not an integer constant expression}}
case 0 ... g(): // expected-error {{expression is not an integer constant expression}}
break;
}
@ -50,12 +50,12 @@ void test4()
}
switch (1) {
case g() && 0: // expected-error {{case label does not reduce to an integer constant}}
case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
break;
}
switch (1) {
case 0 ... g() || 1: // expected-error {{case label does not reduce to an integer constant}}
case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
break;
}
}