From 84585ab48e5f7643ab56723ed76d3415b79a3692 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Mon, 23 Jan 2012 15:29:33 +0000 Subject: [PATCH] Downgrade C++11 narrowing conversion errors to warnings default-mapped to an error, so that users can turn them off if necessary. Note that this does *not* change the behavior of in a SFINAE context, where we still flag an error even if the warning is disabled. This matches GCC's behavior. llvm-svn: 148701 --- .../clang/Basic/DiagnosticSemaKinds.td | 15 +- clang/lib/Sema/SemaInit.cpp | 24 +- .../dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp | 14 ++ .../dcl.init.list/p7-cxx11-nowarn.cpp | 209 ++++++++++++++++++ 4 files changed, 250 insertions(+), 12 deletions(-) create mode 100644 clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 56fca58475d8..dfe49108acb9 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -2942,13 +2942,22 @@ def warn_cxx98_compat_empty_scalar_initializer : Warning< def err_illegal_initializer : Error< "illegal initializer (only variables can be initialized)">; def err_illegal_initializer_type : Error<"illegal initializer type %0">; -def err_init_list_type_narrowing : Error< +def err_init_list_type_narrowing_sfinae : Error< "type %0 cannot be narrowed to %1 in initializer list">; -def err_init_list_variable_narrowing : Error< +def err_init_list_type_narrowing : Warning< + "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_constant_narrowing : Error< +def err_init_list_variable_narrowing : Warning< + "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< + "constant expression evaluates to %0 which cannot be narrowed to type %1">, + InGroup, DefaultError; def warn_init_list_type_narrowing : Warning< "type %0 cannot be narrowed to %1 in initializer list in C++11">, InGroup, DefaultIgnore; diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index f4f2663bc8bc..355cf46464d4 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -5817,9 +5817,11 @@ static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, // narrowing conversion even if the value is a constant and can be // represented exactly as an integer. S.Diag(PostInit->getLocStart(), - S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt - ? diag::err_init_list_type_narrowing - : diag::warn_init_list_type_narrowing) + S.getLangOptions().MicrosoftExt || !S.getLangOptions().CPlusPlus0x? + diag::warn_init_list_type_narrowing + : S.isSFINAEContext()? + diag::err_init_list_type_narrowing_sfinae + : diag::err_init_list_type_narrowing) << PostInit->getSourceRange() << PreNarrowingType.getLocalUnqualifiedType() << EntityType.getLocalUnqualifiedType(); @@ -5828,9 +5830,11 @@ static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, case NK_Constant_Narrowing: // A constant value was narrowed. S.Diag(PostInit->getLocStart(), - S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt - ? diag::err_init_list_constant_narrowing - : diag::warn_init_list_constant_narrowing) + S.getLangOptions().MicrosoftExt || !S.getLangOptions().CPlusPlus0x? + diag::warn_init_list_constant_narrowing + : S.isSFINAEContext()? + diag::err_init_list_constant_narrowing_sfinae + : diag::err_init_list_constant_narrowing) << PostInit->getSourceRange() << ConstantValue.getAsString(S.getASTContext(), EntityType) << EntityType.getLocalUnqualifiedType(); @@ -5839,9 +5843,11 @@ static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, case NK_Variable_Narrowing: // A variable's value may have been narrowed. S.Diag(PostInit->getLocStart(), - S.getLangOptions().CPlusPlus0x && !S.getLangOptions().MicrosoftExt - ? diag::err_init_list_variable_narrowing - : diag::warn_init_list_variable_narrowing) + S.getLangOptions().MicrosoftExt || !S.getLangOptions().CPlusPlus0x? + diag::warn_init_list_variable_narrowing + : S.isSFINAEContext()? + diag::err_init_list_variable_narrowing_sfinae + : diag::err_init_list_variable_narrowing) << PostInit->getSourceRange() << PreNarrowingType.getLocalUnqualifiedType() << EntityType.getLocalUnqualifiedType(); diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp index 6b830ba5a513..db20ea6426e0 100644 --- a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp @@ -193,3 +193,17 @@ void test_qualifiers(int i) { // Template arguments make it harder to avoid printing qualifiers: Agg c2 = {j}; // expected-error {{from type 'int' to 'const unsigned char' in}} expected-note {{override}} } + +// Test SFINAE checks. +template struct Value { }; + +template +int &check_narrowed(Value); + +template +float &check_narrowed(...); + +void test_narrowed(Value vi, Value vd) { + int &ir = check_narrowed(vd); + float &fr = check_narrowed(vi); +} diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp new file mode 100644 index 000000000000..53a775b581d1 --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-cxx11-nowarn.cpp @@ -0,0 +1,209 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wno-error=c++11-narrowing -triple x86_64-apple-macosx10.6.7 -verify %s + +// Verify that narrowing conversions in initializer lists cause errors in C++0x +// mode. + +void std_example() { + int x = 999; // x is not a constant expression + const int y = 999; + const int z = 99; + char c1 = x; // OK, though it might narrow (in this case, it does narrow) + char c2{x}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + char c3{y}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + char c4{z}; // OK: no narrowing needed + unsigned char uc1 = {5}; // OK: no narrowing needed + unsigned char uc2 = {-1}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + unsigned int ui1 = {-1}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + signed int si1 = + { (unsigned int)-1 }; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + int ii = {2.0}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + float f1 { x }; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + float f2 { 7 }; // OK: 7 can be exactly represented as a float + int f(int); + int a[] = + { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level +} + +// Test each rule individually. + +template +struct Agg { + T t; +}; + +template +struct Convert { + constexpr Convert(T v) : v(v) {} + constexpr operator T() const { return v; } + T v; +}; +template Convert ConvertVar(); + +// C++0x [dcl.init.list]p7: A narrowing conversion is an implicit conversion +// +// * from a floating-point type to an integer type, or + +void float_to_int() { + Agg a1 = {1.0F}; // expected-warning {{type 'float' cannot be narrowed to 'char'}} expected-note {{override}} + Agg a2 = {1.0}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg a3 = {1.0L}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + float f = 1.0; + double d = 1.0; + long double ld = 1.0; + Agg a4 = {f}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg a5 = {d}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg a6 = {ld}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + Agg ce1 = { Convert(1.0) }; // expected-warning {{type 'float' cannot be narrowed to 'char'}} expected-note {{override}} + Agg ce2 = { ConvertVar() }; // expected-warning {{type 'double' cannot be narrowed to 'char'}} expected-note {{override}} +} + +// * from long double to double or float, or from double to float, except where +// the source is a constant expression and the actual value after conversion +// is within the range of values that can be represented (even if it cannot be +// represented exactly), or + +void shrink_float() { + // These aren't constant expressions. + float f = 1.0; + double d = 1.0; + long double ld = 1.0; + + // Variables. + Agg f1 = {f}; // OK (no-op) + Agg f2 = {d}; // expected-warning {{non-constant-expression cannot be narrowed from type 'double' to 'float'}} expected-note {{override}} + Agg f3 = {ld}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + // Exact constants. + Agg f4 = {1.0}; // OK (double constant represented exactly) + Agg f5 = {1.0L}; // OK (long double constant represented exactly) + // Inexact but in-range constants. + Agg f6 = {0.1}; // OK (double constant in range but rounded) + Agg f7 = {0.1L}; // OK (long double constant in range but rounded) + // Out of range constants. + Agg f8 = {1E50}; // expected-warning {{constant expression evaluates to 1.000000e+50 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg f9 = {1E50L}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + // More complex constant expression. + constexpr long double e40 = 1E40L, e30 = 1E30L, e39 = 1E39L; + Agg f10 = {e40 - 5 * e39 + e30 - 5 * e39}; // OK + + // Variables. + Agg d1 = {f}; // OK (widening) + Agg d2 = {d}; // OK (no-op) + Agg d3 = {ld}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + // Exact constant. + Agg d4 = {1.0L}; // OK (long double constant represented exactly) + // Inexact but in-range constant. + Agg d5 = {0.1L}; // OK (long double constant in range but rounded) + // Out of range constant. + Agg d6 = {1E315L}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + // More complex constant expression. + constexpr long double e315 = 1E315L, e305 = 1E305L, e314 = 1E314L; + Agg d7 = {e315 - 5 * e314 + e305 - 5 * e314}; // OK + + Agg ce1 = { Convert(1e300) }; // expected-warning {{constant expression evaluates to 1.000000e+300 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg ce2 = { ConvertVar() }; // expected-warning {{non-constant-expression cannot be narrowed from type 'long double' to 'double'}} expected-note {{override}} +} + +// * from an integer type or unscoped enumeration type to a floating-point type, +// except where the source is a constant expression and the actual value after +// conversion will fit into the target type and will produce the original +// value when converted back to the original type, or +void int_to_float() { + // Not a constant expression. + char c = 1; + + // Variables. Yes, even though all char's will fit into any floating type. + Agg f1 = {c}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg f2 = {c}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg f3 = {c}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + // Constants. + Agg f4 = {12345678}; // OK (exactly fits in a float) + Agg f5 = {123456789}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + Agg ce1 = { Convert(123456789) }; // expected-warning {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{override}} + Agg ce2 = { ConvertVar() }; // expected-warning {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{override}} +} + +// * from an integer type or unscoped enumeration type to an integer type that +// cannot represent all the values of the original type, except where the +// source is a constant expression and the actual value after conversion will +// fit into the target type and will produce the original value when converted +// back to the original type. +void shrink_int() { + // Not a constant expression. + short s = 1; + unsigned short us = 1; + Agg c1 = {s}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg s1 = {s}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg s2 = {us}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + // "that cannot represent all the values of the original type" means that the + // validity of the program depends on the relative sizes of integral types. + // This test compiles with -m64, so sizeof(int) i1 = {l1}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + long long ll = 1; + Agg l2 = {ll}; // OK + + // Constants. + Agg c2 = {127}; // OK + Agg c3 = {300}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + + Agg i2 = {0x7FFFFFFFU}; // OK + Agg i3 = {0x80000000U}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg i4 = {-0x80000000L}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + // Bool is also an integer type, but conversions to it are a different AST + // node. + Agg b1 = {0}; // OK + Agg b2 = {1}; // OK + Agg b3 = {-1}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + + // Conversions from pointers to booleans aren't narrowing conversions. + Agg b = {&b1}; // OK + + Agg ce1 = { Convert(100000) }; // expected-warning {{constant expression evaluates to 100000 which cannot be narrowed to type 'short'}} expected-note {{override}} expected-warning {{changes value from 100000 to -31072}} + Agg ce2 = { ConvertVar() }; // expected-warning {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{override}} +} + +// Be sure that type- and value-dependent expressions in templates get the warning +// too. + +template +void maybe_shrink_int(T t) { + Agg s1 = {t}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} + Agg s2 = {I}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} + Agg t2 = {700}; // expected-warning {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}} +} + +void test_template() { + maybe_shrink_int<15>((int)3); // expected-note {{in instantiation}} + maybe_shrink_int<70000>((char)3); // expected-note {{in instantiation}} +} + + +// We don't want qualifiers on the types in the diagnostic. + +void test_qualifiers(int i) { + const int j = i; + struct {const unsigned char c;} c1 = {j}; // expected-warning {{from type 'int' to 'unsigned char' in}} expected-note {{override}} + // Template arguments make it harder to avoid printing qualifiers: + Agg c2 = {j}; // expected-warning {{from type 'int' to 'const unsigned char' in}} expected-note {{override}} +} + +// Make sure we still get the right SFINAE behavior. +template struct Value { }; + +template +int &check_narrowed(Value); + +template +float &check_narrowed(...); + +void test_narrowed(Value vi, Value vd) { + int &ir = check_narrowed(vd); + float &fr = check_narrowed(vi); +}