diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-01-30 22:27:01 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-01-30 22:27:01 +0000 |
commit | f72fccf533bca206af8e75d041c29db99e6a7f2c (patch) | |
tree | 024c4044375c5ed02ffc50aad332ce8fb680da2f /lib/Sema/SemaOverload.cpp | |
parent | acd8c513b9db3a166b5409cd8e75712a7157b7b6 (diff) |
constexpr: disallow signed integer overflow in integral conversions in constant
expressions in C++11.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149286 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaOverload.cpp')
-rw-r--r-- | lib/Sema/SemaOverload.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 91630e8d74..f681a589cf 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -4646,6 +4646,7 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, // Check for a narrowing implicit conversion. APValue PreNarrowingValue; + bool Diagnosed = false; switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue)) { case NK_Variable_Narrowing: // Implicit conversion to a narrower type, and the value is not a constant @@ -4657,11 +4658,13 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, Diag(From->getSourceRange().getBegin(), diag::err_cce_narrowing) << CCE << /*Constant*/1 << PreNarrowingValue.getAsString(Context, QualType()) << T; + Diagnosed = true; break; case NK_Type_Narrowing: Diag(From->getSourceRange().getBegin(), diag::err_cce_narrowing) << CCE << /*Constant*/0 << From->getType() << T; + Diagnosed = true; break; } @@ -4674,12 +4677,19 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, // The expression can't be folded, so we can't keep it at this position in // the AST. Result = ExprError(); - } else if (Notes.empty()) { - // It's a constant expression. + } else { Value = Eval.Val.getInt(); - return Result; + + if (Notes.empty()) { + // It's a constant expression. + return Result; + } } + // Only issue one narrowing diagnostic. + if (Diagnosed) + return Result; + // It's not a constant expression. Produce an appropriate diagnostic. if (Notes.size() == 1 && Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) @@ -4690,7 +4700,7 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, for (unsigned I = 0; I < Notes.size(); ++I) Diag(Notes[I].first, Notes[I].second); } - return ExprError(); + return Result; } /// dropPointerConversions - If the given standard conversion sequence |