r228792 - Add a warning for direct-list-initialization of a variable with a deduced type

Richard Smith richard at metafoo.co.uk
Tue Feb 10 19:17:31 PST 2015


Hi Hans,

I'd really like to ship this change as part of Clang 3.6: there's an
upcoming change to our behavior that will cause us to reject or differently
interpret some code in C++11 and C++14 modes, and this patch adds a warning
for the affected cases with a fix-it hint.

On Tue, Feb 10, 2015 at 6:41 PM, Richard Smith <richard-llvm at metafoo.co.uk>
wrote:

> Author: rsmith
> Date: Tue Feb 10 20:41:33 2015
> New Revision: 228792
>
> URL: http://llvm.org/viewvc/llvm-project?rev=228792&view=rev
> Log:
> Add a warning for direct-list-initialization of a variable with a deduced
> type
> (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.
>
> Modified:
>     cfe/trunk/include/clang/Basic/DiagnosticGroups.td
>     cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
>     cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>     cfe/trunk/lib/Parse/ParseExprCXX.cpp
>     cfe/trunk/lib/Sema/SemaDecl.cpp
>     cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp
>     cfe/trunk/test/Parser/cxx0x-lambda-expressions.cpp
>     cfe/trunk/test/Parser/objcxx0x-lambda-expressions.mm
>     cfe/trunk/test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Feb 10 20:41:33
> 2015
> @@ -750,3 +750,6 @@ def SerializedDiagnostics : DiagGroup<"s
>  // 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">;
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Tue Feb 10
> 20:41:33 2015
> @@ -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<
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Feb 10
> 20:41:33 2015
> @@ -1645,6 +1645,10 @@ def err_auto_var_init_multiple_expressio
>  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<
>
> Modified: cfe/trunk/lib/Parse/ParseExprCXX.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExprCXX.cpp?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Parse/ParseExprCXX.cpp (original)
> +++ cfe/trunk/lib/Parse/ParseExprCXX.cpp Tue Feb 10 20:41:33 2015
> @@ -894,11 +894,16 @@ Optional<unsigned> Parser::ParseLambdaIn
>          // 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();
>
> Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Feb 10 20:41:33 2015
> @@ -8716,6 +8716,14 @@ void Sema::AddInitializerToDecl(Decl *Re
>      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.
>
> Modified: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp
> (original)
> +++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp Tue Feb
> 10 20:41:33 2015
> @@ -48,7 +48,7 @@ auto bad_init_2 = [a(1, 2)] {}; // expec
>  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<
>    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; };
>
> Modified: cfe/trunk/test/Parser/cxx0x-lambda-expressions.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx0x-lambda-expressions.cpp?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Parser/cxx0x-lambda-expressions.cpp (original)
> +++ cfe/trunk/test/Parser/cxx0x-lambda-expressions.cpp Tue Feb 10 20:41:33
> 2015
> @@ -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}}
>
> Modified: cfe/trunk/test/Parser/objcxx0x-lambda-expressions.mm
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/objcxx0x-lambda-expressions.mm?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/Parser/objcxx0x-lambda-expressions.mm (original)
> +++ cfe/trunk/test/Parser/objcxx0x-lambda-expressions.mm Tue Feb 10
> 20:41:33 2015
> @@ -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}}
>
> Modified: cfe/trunk/test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp?rev=228792&r1=228791&r2=228792&view=diff
>
> ==============================================================================
> --- cfe/trunk/test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp
> (original)
> +++ cfe/trunk/test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp Tue
> Feb 10 20:41:33 2015
> @@ -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}}
>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150210/4ea5ecdb/attachment.html>


More information about the cfe-commits mailing list