[clang] [clang][Sema] Emit more specific diagnostic for auto in lambda before C++14 (#46059) (PR #68540)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 10 14:18:24 PDT 2023


https://github.com/weltschildkroete updated https://github.com/llvm/llvm-project/pull/68540

>From 613ea6809b478ff7391614b24ec177fc19339cdd Mon Sep 17 00:00:00 2001
From: Leonardo Duarte <weltschildkroete at gmail.com>
Date: Sun, 8 Oct 2023 12:59:15 +0200
Subject: [PATCH 1/4] [clang][Sema] Emit more specific diagnostic for auto in
 lambda before C++14 (#46059)

Namely, we specify that `auto` in a lambda parameter is a C++14
extension in the error message, which now reads:

`'auto' not allowed in lambda parameter until C++14`

This does not change the behavior for `decltype(auto)` and `__auto_type`
though.

The relevant change to `SemaType.cpp` is the addition of a branch that
sets `Error = 24`, whilst the bulk of the change comes from formatting.
---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 clang/lib/Sema/SemaType.cpp                      | 7 +++++--
 clang/test/SemaCXX/auto-cxx0x.cpp                | 4 ++--
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index c1a6e3831127e56..815f78675c72ec2 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2393,7 +2393,7 @@ def err_auto_not_allowed : Error<
   "|in type allocated by 'new'|in K&R-style function parameter"
   "|in template parameter|in friend declaration|in function prototype that is "
   "not a function declaration|in requires expression parameter"
-  "|in array declaration}1">;
+  "|in array declaration|in lambda parameter until C++14}1">;
 def err_dependent_deduced_tst : Error<
   "typename specifier refers to "
   "%select{class template|function template|variable template|alias template|"
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 068971f8130a4aa..52a8161797e15e1 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3605,8 +3605,11 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
         Info = &SemaRef.InventedParameterInfos.back();
       } else {
         // In C++14, generic lambdas allow 'auto' in their parameters.
-        if (!SemaRef.getLangOpts().CPlusPlus14 || !Auto ||
-            Auto->getKeyword() != AutoTypeKeyword::Auto) {
+        if (!SemaRef.getLangOpts().CPlusPlus14 && Auto &&
+            Auto->getKeyword() == AutoTypeKeyword::Auto) {
+          Error = 24;
+          break;
+        } else if (!Auto || Auto->getKeyword() != AutoTypeKeyword::Auto) {
           Error = 16;
           break;
         }
diff --git a/clang/test/SemaCXX/auto-cxx0x.cpp b/clang/test/SemaCXX/auto-cxx0x.cpp
index b4da3f9330c1045..65398de28e10cfb 100644
--- a/clang/test/SemaCXX/auto-cxx0x.cpp
+++ b/clang/test/SemaCXX/auto-cxx0x.cpp
@@ -12,7 +12,7 @@ thread_local auto x; // expected-error {{requires an initializer}}
 void g() {
   [](auto){}(0);
 #if __cplusplus == 201103L
-  // expected-error at -2 {{'auto' not allowed in lambda parameter}}
+  // expected-error at -2 {{'auto' not allowed in lambda parameter until C++14}}
 #endif
 }
 
@@ -20,6 +20,6 @@ void rdar47689465() {
   int x = 0;
   [](auto __attribute__((noderef)) *){}(&x);
 #if __cplusplus == 201103L
-  // expected-error at -2 {{'auto' not allowed in lambda parameter}}
+  // expected-error at -2 {{'auto' not allowed in lambda parameter until C++14}}
 #endif
 }

>From b884f2b79d57343dea6e7f9b16dfc96984ec1f5b Mon Sep 17 00:00:00 2001
From: Leonardo Duarte <weltschildkroete at gmail.com>
Date: Tue, 10 Oct 2023 22:52:54 +0200
Subject: [PATCH 2/4] Slightly change wording in diagnostic

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 815f78675c72ec2..02644a84b4f4ea4 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2393,7 +2393,7 @@ def err_auto_not_allowed : Error<
   "|in type allocated by 'new'|in K&R-style function parameter"
   "|in template parameter|in friend declaration|in function prototype that is "
   "not a function declaration|in requires expression parameter"
-  "|in array declaration|in lambda parameter until C++14}1">;
+  "|in array declaration|in lambda parameter before C++14}1">;
 def err_dependent_deduced_tst : Error<
   "typename specifier refers to "
   "%select{class template|function template|variable template|alias template|"

>From 99bc3b6cb8d1a812b6dcd573261a77a2d50a79e2 Mon Sep 17 00:00:00 2001
From: Leonardo Duarte <weltschildkroete at gmail.com>
Date: Tue, 10 Oct 2023 23:09:07 +0200
Subject: [PATCH 3/4] Add release note

---
 clang/docs/ReleaseNotes.rst | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2d918967e7f0b02..41d83791cb5f287 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -301,6 +301,9 @@ Improvements to Clang's diagnostics
 - Clang now always diagnoses when using non-standard layout types in ``offsetof`` .
   (`#64619: <https://github.com/llvm/llvm-project/issues/64619>`_)
 
+- Clang now specifies that using ``auto`` in a lambda parameter is a C++14 extension when
+  appropriate. (`#46059: <https://github.com/llvm/llvm-project/issues/46059>`_).
+
 Bug Fixes in This Version
 -------------------------
 - Fixed an issue where a class template specialization whose declaration is

>From b620007bc17c46468f0046ca135163cdaa519783 Mon Sep 17 00:00:00 2001
From: Leonardo Duarte <weltschildkroete at gmail.com>
Date: Tue, 10 Oct 2023 23:17:41 +0200
Subject: [PATCH 4/4] Update wording on tests

---
 clang/test/SemaCXX/auto-cxx0x.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/SemaCXX/auto-cxx0x.cpp b/clang/test/SemaCXX/auto-cxx0x.cpp
index 65398de28e10cfb..07687b6066790f7 100644
--- a/clang/test/SemaCXX/auto-cxx0x.cpp
+++ b/clang/test/SemaCXX/auto-cxx0x.cpp
@@ -12,7 +12,7 @@ thread_local auto x; // expected-error {{requires an initializer}}
 void g() {
   [](auto){}(0);
 #if __cplusplus == 201103L
-  // expected-error at -2 {{'auto' not allowed in lambda parameter until C++14}}
+  // expected-error at -2 {{'auto' not allowed in lambda parameter before C++14}}
 #endif
 }
 
@@ -20,6 +20,6 @@ void rdar47689465() {
   int x = 0;
   [](auto __attribute__((noderef)) *){}(&x);
 #if __cplusplus == 201103L
-  // expected-error at -2 {{'auto' not allowed in lambda parameter until C++14}}
+  // expected-error at -2 {{'auto' not allowed in lambda parameter before C++14}}
 #endif
 }



More information about the cfe-commits mailing list