[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
Thu May 16 05:44:43 PDT 2024


https://github.com/cor3ntin 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/6] [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 c1a6e3831127e..815f78675c72e 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 068971f8130a4..52a8161797e15 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 b4da3f9330c10..65398de28e10c 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/6] 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 815f78675c72e..02644a84b4f4e 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/6] 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 2d918967e7f0b..41d83791cb5f2 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/6] 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 65398de28e10c..07687b6066790 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
 }

>From 031e76900f6e63c97ed8666066ff98d967305af4 Mon Sep 17 00:00:00 2001
From: Leonardo Duarte <weltschildkroete at gmail.com>
Date: Wed, 11 Oct 2023 22:50:59 +0200
Subject: [PATCH 5/6] Add comments for which diagnostic to emit

---
 clang/lib/Sema/SemaType.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 52a8161797e15..da759ba340c72 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3607,10 +3607,10 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
         // In C++14, generic lambdas allow 'auto' in their parameters.
         if (!SemaRef.getLangOpts().CPlusPlus14 && Auto &&
             Auto->getKeyword() == AutoTypeKeyword::Auto) {
-          Error = 24;
+          Error = 24; // auto not allowed in lambda parameter (before C++14)
           break;
         } else if (!Auto || Auto->getKeyword() != AutoTypeKeyword::Auto) {
-          Error = 16;
+          Error = 16; // __auto_type or decltype(auto) not allowed in lambda parameter
           break;
         }
         Info = SemaRef.getCurLambda();

>From 86e53efbed1387c9743e3ec4d8c074119821f917 Mon Sep 17 00:00:00 2001
From: Leonardo Duarte <weltschildkroete at gmail.com>
Date: Wed, 11 Oct 2023 23:15:16 +0200
Subject: [PATCH 6/6] Apply clang-format

---
 clang/lib/Sema/SemaType.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index da759ba340c72..4f54645a9c148 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3610,7 +3610,8 @@ static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state,
           Error = 24; // auto not allowed in lambda parameter (before C++14)
           break;
         } else if (!Auto || Auto->getKeyword() != AutoTypeKeyword::Auto) {
-          Error = 16; // __auto_type or decltype(auto) not allowed in lambda parameter
+          Error = 16; // __auto_type or decltype(auto) not allowed in lambda
+                      // parameter
           break;
         }
         Info = SemaRef.getCurLambda();



More information about the cfe-commits mailing list