From 1ef28dbcf1a8b72f590f2d73bc204e85605ab605 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Thu, 29 Mar 2012 23:39:39 +0000 Subject: [PATCH] Extend -Wc++11-narrowing to cover converted constant expressions as well as braced-initializers. . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153703 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 13 +++++++++---- lib/Sema/SemaOverload.cpp | 15 ++++++--------- test/CXX/expr/expr.const/p3-0x-nowarn.cpp | 8 ++++++++ 3 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 test/CXX/expr/expr.const/p3-0x-nowarn.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index bdaac9c26a..9dd04b99d5 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -27,7 +27,12 @@ def err_typecheck_converted_constant_expression_disallowed : Error< def err_expr_not_cce : Error< "%select{case value|enumerator value|non-type template argument}0 " "is not a constant expression">; -def err_cce_narrowing : Error< +def err_cce_narrowing : ExtWarn< + "%select{case value|enumerator value|non-type template argument}0 " + "%select{cannot be narrowed from type %2 to %3|" + "evaluates to %2, which cannot be narrowed to type %3}1">, + InGroup, DefaultError; +def err_cce_narrowing_sfinae : Error< "%select{case value|enumerator value|non-type template argument}0 " "%select{cannot be narrowed from type %2 to %3|" "evaluates to %2, which cannot be narrowed to type %3}1">; @@ -2990,18 +2995,18 @@ def err_illegal_initializer : Error< def err_illegal_initializer_type : Error<"illegal initializer type %0">; def err_init_list_type_narrowing_sfinae : Error< "type %0 cannot be narrowed to %1 in initializer list">; -def err_init_list_type_narrowing : Warning< +def err_init_list_type_narrowing : ExtWarn< "type %0 cannot be narrowed to %1 in initializer list">, InGroup, DefaultError; def err_init_list_variable_narrowing_sfinae : Error< "non-constant-expression cannot be narrowed from type %0 to %1 in " "initializer list">; -def err_init_list_variable_narrowing : Warning< +def err_init_list_variable_narrowing : ExtWarn< "non-constant-expression cannot be narrowed from type %0 to %1 in " "initializer list">, InGroup, DefaultError; def err_init_list_constant_narrowing_sfinae : Error< "constant expression evaluates to %0 which cannot be narrowed to type %1">; -def err_init_list_constant_narrowing : Warning< +def err_init_list_constant_narrowing : ExtWarn< "constant expression evaluates to %0 which cannot be narrowed to type %1">, InGroup, DefaultError; def warn_init_list_type_narrowing : Warning< diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 661f589099..e1d32056eb 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -4808,7 +4808,6 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, // Check for a narrowing implicit conversion. APValue PreNarrowingValue; QualType PreNarrowingType; - bool Diagnosed = false; switch (SCS->getNarrowingKind(Context, Result.get(), PreNarrowingValue, PreNarrowingType)) { case NK_Variable_Narrowing: @@ -4818,16 +4817,18 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, break; case NK_Constant_Narrowing: - Diag(From->getLocStart(), diag::err_cce_narrowing) + Diag(From->getLocStart(), + isSFINAEContext() ? diag::err_cce_narrowing_sfinae : + diag::err_cce_narrowing) << CCE << /*Constant*/1 << PreNarrowingValue.getAsString(Context, PreNarrowingType) << T; - Diagnosed = true; break; case NK_Type_Narrowing: - Diag(From->getLocStart(), diag::err_cce_narrowing) + Diag(From->getLocStart(), + isSFINAEContext() ? diag::err_cce_narrowing_sfinae : + diag::err_cce_narrowing) << CCE << /*Constant*/0 << From->getType() << T; - Diagnosed = true; break; } @@ -4849,10 +4850,6 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T, } } - // 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) diff --git a/test/CXX/expr/expr.const/p3-0x-nowarn.cpp b/test/CXX/expr/expr.const/p3-0x-nowarn.cpp new file mode 100644 index 0000000000..c891374519 --- /dev/null +++ b/test/CXX/expr/expr.const/p3-0x-nowarn.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wno-c++11-narrowing -verify %s + +// +void f(int x) { + switch (x) { + case 0x80000001: break; + } +} -- 2.40.0