[clang] [Clang] Error on extraneous template headers by default. (PR #104046)

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 14 10:56:25 PDT 2024


https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/104046

>From 5c724a55dda16dcf5dfd8e5be8c86001fa6c9fe7 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Wed, 14 Aug 2024 17:25:29 +0200
Subject: [PATCH 1/2] [Clang] Error on extraneous template headers by default.

As discussed here
https://github.com/llvm/llvm-project/issues/99296#issuecomment-2240807413
Fixes #99296
Fixes #50294
---
 clang/docs/ReleaseNotes.rst                      | 9 +++++++++
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 ++-
 clang/test/Misc/warning-flags.c                  | 3 +--
 clang/test/SemaTemplate/temp_explicit.cpp        | 4 +++-
 4 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..cec724e464e859 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -49,6 +49,15 @@ C++ Specific Potentially Breaking Changes
   few users and can be written as ``__is_same(__remove_cv(T), decltype(nullptr))``,
   which GCC supports as well.
 
+- Extraneous template headers are now ill-formed by default.
+  This error can be disable with ``-Wno-error=extraneous-template-head``.
+
+  .. code-block:: c++
+
+    template <> // error: extraneous template head
+    template <typename T>
+    void f();
+
 ABI Changes in This Version
 ---------------------------
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 554dbaff2ce0d8..cc9eeb5e9fa6af 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5426,7 +5426,8 @@ def err_template_spec_extra_headers : Error<
   "extraneous template parameter list in template specialization or "
   "out-of-line template definition">;
 def ext_template_spec_extra_headers : ExtWarn<
-  "extraneous template parameter list in template specialization">;
+  "extraneous template parameter list in template specialization">,
+   InGroup<DiagGroup<"extraneous-template-head">>, DefaultError;
 def note_explicit_template_spec_does_not_need_header : Note<
   "'template<>' header not required for explicitly-specialized class %0 "
   "declared here">;
diff --git a/clang/test/Misc/warning-flags.c b/clang/test/Misc/warning-flags.c
index cdbe1e95cba965..35543e6a49ffda 100644
--- a/clang/test/Misc/warning-flags.c
+++ b/clang/test/Misc/warning-flags.c
@@ -18,14 +18,13 @@ This test serves two purposes:
 
 The list of warnings below should NEVER grow.  It should gradually shrink to 0.
 
-CHECK: Warnings without flags (65):
+CHECK: Warnings without flags (64):
 
 CHECK-NEXT:   ext_expected_semi_decl_list
 CHECK-NEXT:   ext_missing_whitespace_after_macro_name
 CHECK-NEXT:   ext_new_paren_array_nonconst
 CHECK-NEXT:   ext_plain_complex
 CHECK-NEXT:   ext_template_arg_extra_parens
-CHECK-NEXT:   ext_template_spec_extra_headers
 CHECK-NEXT:   ext_typecheck_cond_incompatible_operands
 CHECK-NEXT:   ext_typecheck_ordered_comparison_of_pointer_integer
 CHECK-NEXT:   ext_using_undefined_std
diff --git a/clang/test/SemaTemplate/temp_explicit.cpp b/clang/test/SemaTemplate/temp_explicit.cpp
index 0bb0cfad61fdb0..e19d75e5b5c65d 100644
--- a/clang/test/SemaTemplate/temp_explicit.cpp
+++ b/clang/test/SemaTemplate/temp_explicit.cpp
@@ -128,11 +128,13 @@ struct Foo<int> // expected-note{{header not required for explicitly-specialized
     {};
 };
 
-template <> // expected-warning{{extraneous template parameter list}}
+template <> // expected-error{{extraneous template parameter list}}
 template <>
 struct Foo<int>::Bar<void>
 {};
 
+template<> void f(auto); // expected-error{{extraneous template parameter list}}
+
 namespace N1 {
 
   template<typename T> struct X7 { }; // expected-note{{here}}

>From 9d561d51202c0e77e9474ea24e171480829cac17 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Wed, 14 Aug 2024 19:43:04 +0200
Subject: [PATCH 2/2] fix tests

---
 clang/test/SemaTemplate/temp_explicit.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/test/SemaTemplate/temp_explicit.cpp b/clang/test/SemaTemplate/temp_explicit.cpp
index e19d75e5b5c65d..4612e4a57e90e0 100644
--- a/clang/test/SemaTemplate/temp_explicit.cpp
+++ b/clang/test/SemaTemplate/temp_explicit.cpp
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat %s
 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat -std=c++98 %s
 // RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++20 %s
 //
 // Tests explicit instantiation of templates.
 template<typename T, typename U = T> class X0 { };
@@ -133,7 +134,9 @@ template <>
 struct Foo<int>::Bar<void>
 {};
 
+#if __cplusplus >= 202002L
 template<> void f(auto); // expected-error{{extraneous template parameter list}}
+#endif
 
 namespace N1 {
 



More information about the cfe-commits mailing list