]> granicus.if.org Git - clang/commitdiff
Add a warning for direct-list-initialization of a variable with a deduced type
authorRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 11 Feb 2015 02:41:33 +0000 (02:41 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 11 Feb 2015 02:41:33 +0000 (02:41 +0000)
(or of a lambda init-capture, which is sort-of such a variable). The semantics
of such constructs will change when we implement N3922, so we intend to warn on
this in Clang 3.6 then change the semantics in Clang 3.7.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@228792 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticParseKinds.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Parse/ParseExprCXX.cpp
lib/Sema/SemaDecl.cpp
test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp
test/Parser/cxx0x-lambda-expressions.cpp
test/Parser/objcxx0x-lambda-expressions.mm
test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp

index 75d40b17a67c482d3145a587e95bcc51ffc2e3d5..215ff06ddb26ee35035032a732c2184e1f08b848 100644 (file)
@@ -750,3 +750,6 @@ def SerializedDiagnostics : DiagGroup<"serialized-diagnostics">;
 // A warning group for warnings about code that clang accepts when
 // compiling CUDA C/C++ but which is not compatible with the CUDA spec.
 def CudaCompat : DiagGroup<"cuda-compat">;
+
+// A warning group for things that will change semantics in the future.
+def FutureCompat : DiagGroup<"future-compat">;
index 39d3c182fc49d799c05a73b872da3ada5ac26608..c8915a739b759e9382b5685a470ac95048be0c0b 100644 (file)
@@ -804,6 +804,10 @@ def warn_cxx98_compat_lambda : Warning<
 def err_lambda_missing_parens : Error<
   "lambda requires '()' before %select{'mutable'|return type|"
   "attribute specifier}0">;
+def warn_init_capture_direct_list_init : Warning<
+  "direct list initialization of a lambda init-capture will change meaning in "
+  "a future version of Clang; insert an '=' to avoid a change in behavior">,
+  InGroup<FutureCompat>;
 
 // Availability attribute
 def err_expected_version : Error<
index 5c66952979a91ccedf1638e4f7a2b1ba1225f18c..f14d7097b78f520a5e8a99cdc1e202a62f581f1b 100644 (file)
@@ -1645,6 +1645,10 @@ def err_auto_var_init_multiple_expressions : Error<
 def err_auto_var_init_paren_braces : Error<
   "cannot deduce type for variable %0 with type %1 from "
   "parenthesized initializer list">;
+def warn_auto_var_direct_list_init : Warning<
+  "direct list initialization of a variable with a deduced type will change "
+  "meaning in a future version of Clang; insert an '=' to avoid a change in "
+  "behavior">, InGroup<FutureCompat>;
 def err_auto_new_ctor_multiple_expressions : Error<
   "new expression for type %0 contains multiple constructor arguments">;
 def err_auto_missing_trailing_return : Error<
index 445967bfab8f69c0730fd58699fa0526a8c55b33..f21d5be24d02b7825d5f520745526377475d96d7 100644 (file)
@@ -894,11 +894,16 @@ Optional<unsigned> Parser::ParseLambdaIntroducer(LambdaIntroducer &Intro,
         // to save the necessary state, and restore it later.
         EnterExpressionEvaluationContext EC(Actions,
                                             Sema::PotentiallyEvaluated);
-        TryConsumeToken(tok::equal);
+        bool HadEquals = TryConsumeToken(tok::equal);
 
-        if (!SkippedInits)
+        if (!SkippedInits) {
+          // Warn on constructs that will change meaning when we implement N3922
+          if (!HadEquals && Tok.is(tok::l_brace)) {
+            Diag(Tok, diag::warn_init_capture_direct_list_init)
+              << FixItHint::CreateInsertion(Tok.getLocation(), "=");
+          }
           Init = ParseInitializer();
-        else if (Tok.is(tok::l_brace)) {
+        else if (Tok.is(tok::l_brace)) {
           BalancedDelimiterTracker Braces(*this, tok::l_brace);
           Braces.consumeOpen();
           Braces.skipToEnd();
index f187ee2474b3b434eb14de767167a3b70ff972d0..c05877843fb2aad6574215b97296e260774b90c4 100644 (file)
@@ -8716,6 +8716,14 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init,
     CheckVariableDeclarationType(VDecl);
     if (VDecl->isInvalidDecl())
       return;
+
+    // If all looks well, warn if this is a case that will change meaning when
+    // we implement N3922.
+    if (DirectInit && !CXXDirectInit && isa<InitListExpr>(Init)) {
+      Diag(Init->getLocStart(),
+           diag::warn_auto_var_direct_list_init)
+        << FixItHint::CreateInsertion(Init->getLocStart(), "=");
+    }
   }
 
   // dllimport cannot be used on variable definitions.
index 6be200dd54e52ecc480b73db7249a3897c984f50..1228c74b0709beae4717225318f5f7579e9bbfeb 100644 (file)
@@ -48,7 +48,7 @@ auto bad_init_2 = [a(1, 2)] {}; // expected-error {{initializer for lambda captu
 auto bad_init_3 = [&a(void_fn())] {}; // expected-error {{cannot form a reference to 'void'}}
 auto bad_init_4 = [a(void_fn())] {}; // expected-error {{has incomplete type 'void'}}
 auto bad_init_5 = [a(overload_fn)] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer of type '<overloaded function}}
-auto bad_init_6 = [a{overload_fn}] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer list}}
+auto bad_init_6 = [a{overload_fn}] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer list}} expected-warning {{will change meaning in a future version of Clang}}
 
 template<typename...T> void pack_1(T...t) { (void)[a(t...)] {}; } // expected-error {{initializer missing for lambda capture 'a'}}
 template void pack_1<>(); // expected-note {{instantiation of}}
@@ -61,7 +61,7 @@ auto a = [a(4), b = 5, &c = static_cast<const int&&>(0)] {
   using T = decltype(c);
   using T = const int &;
 };
-auto b = [a{0}] {}; // expected-error {{include <initializer_list>}}
+auto b = [a{0}] {}; // expected-error {{include <initializer_list>}} expected-warning {{will change meaning in a future version of Clang}}
 
 struct S { S(); S(S&&); };
 template<typename T> struct remove_reference { typedef T type; };
index 6f69d8096e225ea20db54d3b59a37f107aa16081..c2bf6fd3960a62c475918dfd1559335597784e6e 100644 (file)
@@ -61,7 +61,7 @@ class C {
   int z;
   void init_capture() {
     [n(0)] () mutable -> int { return ++n; }; // expected-warning{{extension}}
-    [n{0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
+    [n{0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}} expected-warning{{will change meaning in a future version}}
     [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}} expected-warning{{extension}}
     [n = {0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
     [a([&b = z]{})](){}; // expected-warning 2{{extension}}
index bef576a9d20458fb150f5926983af44cd64ea460..3954a807a5fb7d98384359cb6008e59b58ce0647 100644 (file)
@@ -21,7 +21,7 @@ class C {
 
     [foo(bar)] () {};
     [foo = bar] () {};
-    [foo{bar}] () {}; // expected-error {{<initializer_list>}}
+    [foo{bar}] () {}; // expected-error {{<initializer_list>}} expected-warning {{will change meaning}}
     [foo = {bar}] () {}; // expected-error {{<initializer_list>}}
 
     [foo(bar) baz] () {}; // expected-error {{called object type 'int' is not a function}}
index e7b515305d4ff68df920d37f9734ee39560083e7..a78f022e84c92f982e71c966bfd652c6f03a2a28 100644 (file)
@@ -117,6 +117,7 @@ void argument_deduction() {
 
 void auto_deduction() {
   auto l = {1, 2, 3, 4};
+  auto l2 {1, 2, 3, 4}; // expected-warning {{will change meaning in a future version of Clang}}
   static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
   auto bl = {1, 2.0}; // expected-error {{cannot deduce}}