From ff07af12df83c0112d702f61557bf9541584074f Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Mon, 12 Dec 2011 19:10:03 +0000 Subject: [PATCH] Clean up diagnostic wording for disallowed casts in C++11 constant expressions. llvm-svn: 146395 --- clang/include/clang/Basic/DiagnosticASTKinds.td | 4 ++-- clang/lib/AST/ExprConstant.cpp | 9 +++++++-- clang/test/SemaCXX/constant-expression-cxx11.cpp | 14 +++++++------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td b/clang/include/clang/Basic/DiagnosticASTKinds.td index e16f768b2a9b..9f0da2d6eb72 100644 --- a/clang/include/clang/Basic/DiagnosticASTKinds.td +++ b/clang/include/clang/Basic/DiagnosticASTKinds.td @@ -13,8 +13,8 @@ let Component = "AST" in { // "C does not permit evaluated commas in an integer constant expression">; def note_expr_divide_by_zero : Note<"division by zero">; def note_constexpr_invalid_cast : Note< - "%select{reinterpret_cast|dynamic_cast|reinterpreting cast}0 not allowed " - "in a constant expression">; + "%select{reinterpret_cast|dynamic_cast|cast interpreted as a " + "reinterpret_cast|cast from %1}0 is not allowed in a constant expression">; // inline asm related. let CategoryName = "Inline Assembly Issue" in { diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 62d70dd97828..3a897ab34842 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -2226,8 +2226,13 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are // permitted in constant expressions in C++11. Bitcasts from cv void* are // also static_casts, but we disallow them as a resolution to DR1312. - if (!E->getType()->isVoidPointerType()) - CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; + if (!E->getType()->isVoidPointerType()) { + if (SubExpr->getType()->isVoidPointerType()) + CCEDiag(E, diag::note_constexpr_invalid_cast) + << 3 << SubExpr->getType(); + else + CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; + } if (!Visit(SubExpr)) return false; Result.Designator.setInvalid(); diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp index 55762b4ea4ce..3ba76d42997c 100644 --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -301,25 +301,25 @@ struct Str { // FIXME: In C++ mode, we should say 'integral' not 'integer' int a : dynamic_cast(sptr) == dynamic_cast(sptr); // \ expected-warning {{not integer constant expression}} \ - expected-note {{dynamic_cast not allowed in a constant expression}} + expected-note {{dynamic_cast is not allowed in a constant expression}} int b : reinterpret_cast(sptr) == reinterpret_cast(sptr); // \ expected-warning {{not integer constant expression}} \ - expected-note {{reinterpret_cast not allowed in a constant expression}} + expected-note {{reinterpret_cast is not allowed in a constant expression}} int c : (S*)(long)(sptr) == (S*)(long)(sptr); // \ expected-warning {{not integer constant expression}} \ - expected-note {{reinterpreting cast not allowed in a constant expression}} + expected-note {{cast interpreted as a reinterpret_cast is not allowed in a constant expression}} int d : (S*)(42) == (S*)(42); // \ expected-warning {{not integer constant expression}} \ - expected-note {{reinterpreting cast not allowed in a constant expression}} + expected-note {{cast interpreted as a reinterpret_cast is not allowed in a constant expression}} int e : (Str*)(sptr) == (Str*)(sptr); // \ expected-warning {{not integer constant expression}} \ - expected-note {{reinterpreting cast not allowed in a constant expression}} + expected-note {{cast interpreted as a reinterpret_cast is not allowed in a constant expression}} int f : &(Str&)(*sptr) == &(Str&)(*sptr); // \ expected-warning {{not integer constant expression}} \ - expected-note {{reinterpreting cast not allowed in a constant expression}} + expected-note {{cast interpreted as a reinterpret_cast is not allowed in a constant expression}} int g : (S*)(void*)(sptr) == sptr; // \ expected-warning {{not integer constant expression}} \ - expected-note {{reinterpreting cast not allowed in a constant expression}} + expected-note {{cast from 'void *' is not allowed in a constant expression}} }; extern char externalvar[];