diff options
| -rw-r--r-- | clang/include/clang/Basic/DiagnosticGroups.td | 7 | ||||
| -rw-r--r-- | clang/include/clang/Basic/DiagnosticSemaKinds.td | 10 | ||||
| -rw-r--r-- | clang/lib/Sema/SemaLambda.cpp | 20 | ||||
| -rw-r--r-- | clang/test/CXX/expr/expr.prim/expr.prim.lambda/p8.cpp | 4 | ||||
| -rw-r--r-- | clang/test/FixIt/fixit-cxx0x.cpp | 1 | ||||
| -rw-r--r-- | clang/test/SemaCXX/cxx2a-lambda-equals-this.cpp | 15 | ||||
| -rw-r--r-- | clang/www/cxx_status.html | 7 |
7 files changed, 44 insertions, 20 deletions
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index e941d9dda2f..dd0ed0b13cc 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -168,6 +168,9 @@ def CXXPre14CompatPedantic : DiagGroup<"c++98-c++11-compat-pedantic", def CXXPre1zCompat : DiagGroup<"c++98-c++11-c++14-compat">; def CXXPre1zCompatPedantic : DiagGroup<"c++98-c++11-c++14-compat-pedantic", [CXXPre1zCompat]>; +def CXXPre2aCompat : DiagGroup<"c++98-c++11-c++14-c++17-compat">; +def CXXPre2aCompatPedantic : DiagGroup<"c++98-c++11-c++14-c++17-compat-pedantic", + [CXXPre2aCompat]>; def CXX98CompatBindToTemporaryCopy : DiagGroup<"c++98-compat-bind-to-temporary-copy">; @@ -784,6 +787,10 @@ def CXX14 : DiagGroup<"c++14-extensions", [CXX14BinaryLiteral]>; // earlier C++ versions. def CXX17 : DiagGroup<"c++17-extensions">; +// A warning group for warnings about using C++2a features as extensions in +// earlier C++ versions. +def CXX2a : DiagGroup<"c++2a-extensions">; + def : DiagGroup<"c++0x-extensions", [CXX11]>; def : DiagGroup<"c++1y-extensions", [CXX14]>; def : DiagGroup<"c++1z-extensions", [CXX17]>; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 856bd6a337b..20bfc8631c0 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -6470,8 +6470,6 @@ let CategoryName = "Lambda Issue" in { "%0 can appear only once in a capture list">; def err_reference_capture_with_reference_default : Error< "'&' cannot precede a capture when the capture default is '&'">; - def err_this_capture_with_copy_default : Error< - "'this' cannot be explicitly captured when the capture default is '='">; def err_copy_capture_with_copy_default : Error< "'&' must precede a capture when the capture default is '='">; def err_capture_does_not_name_variable : Error< @@ -6544,6 +6542,14 @@ let CategoryName = "Lambda Issue" in { InGroup<CXXPre1zCompat>, DefaultIgnore; def ext_star_this_lambda_capture_cxx17 : ExtWarn< "capture of '*this' by copy is a C++17 extension">, InGroup<CXX17>; + + // C++2a [=, this] captures. + def warn_cxx17_compat_equals_this_lambda_capture : Warning< + "explicit capture of 'this' with a capture default of '=' is incompatible " + "with C++ standards before C++2a">, InGroup<CXXPre2aCompat>, DefaultIgnore; + def ext_equals_this_lambda_capture_cxx2a : ExtWarn< + "explicit capture of 'this' with a capture default of '=' " + "is a C++2a extension">, InGroup<CXX2a>; } def err_return_in_captured_stmt : Error< diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp index 97575587c33..bbd7c4a0937 100644 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -948,17 +948,15 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, continue; } - // C++1z [expr.prim.lambda]p8: - // If a lambda-capture includes a capture-default that is =, each - // simple-capture of that lambda-capture shall be of the form "& - // identifier" or "* this". [ Note: The form [&,this] is redundant but - // accepted for compatibility with ISO C++14. --end note ] - if (Intro.Default == LCD_ByCopy && C->Kind != LCK_StarThis) { - Diag(C->Loc, diag::err_this_capture_with_copy_default) - << FixItHint::CreateRemoval( - SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc)); - continue; - } + // C++2a [expr.prim.lambda]p8: + // If a lambda-capture includes a capture-default that is =, + // each simple-capture of that lambda-capture shall be of the form + // "&identifier", "this", or "* this". [ Note: The form [&,this] is + // redundant but accepted for compatibility with ISO C++14. --end note ] + if (Intro.Default == LCD_ByCopy && C->Kind != LCK_StarThis) + Diag(C->Loc, !getLangOpts().CPlusPlus2a + ? diag::ext_equals_this_lambda_capture_cxx2a + : diag::warn_cxx17_compat_equals_this_lambda_capture); // C++11 [expr.prim.lambda]p12: // If this is captured by a local lambda expression, its nearest diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p8.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p8.cpp index b9b8cd76c01..1cc1fd974ca 100644 --- a/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p8.cpp +++ b/clang/test/CXX/expr/expr.prim/expr.prim.lambda/p8.cpp @@ -8,7 +8,7 @@ class X0 { (void)[this, this] () {}; // expected-error {{'this' can appear only once}} (void)[=, foo] () {}; // expected-error {{'&' must precede a capture when}} (void)[=, &foo] () {}; - (void)[=, this] () {}; // expected-error {{'this' cannot be explicitly captured}} + (void)[=, this] () {}; // expected-warning {{C++2a extension}} (void)[&, foo] () {}; (void)[&, &foo] () {}; // expected-error {{'&' cannot precede a capture when}} (void)[&, this] () {}; @@ -23,7 +23,7 @@ struct S2 { void S2::f(int i) { (void)[&, i]{ }; (void)[&, &i]{ }; // expected-error{{'&' cannot precede a capture when the capture default is '&'}} - (void)[=, this]{ }; // expected-error{{'this' cannot be explicitly captured}} + (void)[=, this]{ }; // expected-warning{{C++2a extension}} (void)[=]{ this->g(i); }; (void)[i, i]{ }; // expected-error{{'i' can appear only once in a capture list}} (void)[i(0), i(1)]{ }; // expected-error{{'i' can appear only once in a capture list}} diff --git a/clang/test/FixIt/fixit-cxx0x.cpp b/clang/test/FixIt/fixit-cxx0x.cpp index 5aebcb3defa..337b5d62bed 100644 --- a/clang/test/FixIt/fixit-cxx0x.cpp +++ b/clang/test/FixIt/fixit-cxx0x.cpp @@ -54,7 +54,6 @@ struct S2 { void S2::f(int i) { (void)[&, &i, &i]{}; // expected-error 2{{'&' cannot precede a capture when the capture default is '&'}} - (void)[=, this]{ this->g(5); }; // expected-error{{'this' cannot be explicitly captured}} (void)[i, i]{ }; // expected-error{{'i' can appear only once in a capture list}} (void)[&, i, i]{ }; // expected-error{{'i' can appear only once in a capture list}} (void)[] mutable { }; // expected-error{{lambda requires '()' before 'mutable'}} diff --git a/clang/test/SemaCXX/cxx2a-lambda-equals-this.cpp b/clang/test/SemaCXX/cxx2a-lambda-equals-this.cpp new file mode 100644 index 00000000000..ce6916322e5 --- /dev/null +++ b/clang/test/SemaCXX/cxx2a-lambda-equals-this.cpp @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -std=c++2a -verify %s +// expected-no-diagnostics + +// This test does two things. +// Deleting the copy constructor ensures that an [=, this] capture doesn't copy the object. +// Accessing a member variable from the lambda ensures that the capture actually works. +class A { + A(const A &) = delete; + int i; + + void func() { + auto L = [=, this]() -> int { return i; }; + L(); + } +}; diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html index 539af819e0b..db5a93c91ad 100644 --- a/clang/www/cxx_status.html +++ b/clang/www/cxx_status.html @@ -777,13 +777,12 @@ code. This issue is expected to be rectified soon. <h2 id="cxx20">C++2a implementation status</h2> -<p>Clang does not yet support any of the proposed features of -<!--<p>Clang has <b>experimental</b> support for some proposed features of--> +<p>Clang has <b>experimental</b> support for some proposed features of the C++ standard following C++17, provisionally named C++2a. Note that support for these features may change or be removed without notice, as the draft C++2a standard evolves. -<!--<p>You can use Clang in C++2a mode with the <code>-std=c++2a</code> option.</p>--> +<p>You can use Clang in C++2a mode with the <code>-std=c++2a</code> option.</p> <details open> <summary>List of features and minimum Clang version with support</summary> @@ -808,7 +807,7 @@ as the draft C++2a standard evolves. <tr> <td>Allow <i>lambda-capture</i> <tt>[=, this]</tt></td> <td><a href="http://wg21.link/p0409r2">P0409R2</a></td> - <td class="none" align="center">No</td> + <td class="svn" align="center">SVN</td> </tr> <tr> <td><tt>__VA_OPT__</tt> for preprocessor comma elision</td> |

