Skip to content

Commit 2b959bd

Browse files
authored
[Clang] Error on extraneous template headers by default. (#104046)
As discussed here #99296 (comment) Fixes #99296 Fixes #50294
1 parent 539bf49 commit 2b959bd

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ C++ Specific Potentially Breaking Changes
6565
`-Wno-enum-constexpr-conversion`, to allow for a transition period for users.
6666
Now, in Clang 20, **it is no longer possible to suppress the diagnostic**.
6767

68+
- Extraneous template headers are now ill-formed by default.
69+
This error can be disable with ``-Wno-error=extraneous-template-head``.
70+
71+
.. code-block:: c++
72+
73+
template <> // error: extraneous template head
74+
template <typename T>
75+
void f();
76+
6877
ABI Changes in This Version
6978
---------------------------
7079

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5428,7 +5428,8 @@ def err_template_spec_extra_headers : Error<
54285428
"extraneous template parameter list in template specialization or "
54295429
"out-of-line template definition">;
54305430
def ext_template_spec_extra_headers : ExtWarn<
5431-
"extraneous template parameter list in template specialization">;
5431+
"extraneous template parameter list in template specialization">,
5432+
InGroup<DiagGroup<"extraneous-template-head">>, DefaultError;
54325433
def note_explicit_template_spec_does_not_need_header : Note<
54335434
"'template<>' header not required for explicitly-specialized class %0 "
54345435
"declared here">;

clang/test/Misc/warning-flags.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ This test serves two purposes:
1818

1919
The list of warnings below should NEVER grow. It should gradually shrink to 0.
2020

21-
CHECK: Warnings without flags (65):
21+
CHECK: Warnings without flags (64):
2222

2323
CHECK-NEXT: ext_expected_semi_decl_list
2424
CHECK-NEXT: ext_missing_whitespace_after_macro_name
2525
CHECK-NEXT: ext_new_paren_array_nonconst
2626
CHECK-NEXT: ext_plain_complex
2727
CHECK-NEXT: ext_template_arg_extra_parens
28-
CHECK-NEXT: ext_template_spec_extra_headers
2928
CHECK-NEXT: ext_typecheck_cond_incompatible_operands
3029
CHECK-NEXT: ext_typecheck_ordered_comparison_of_pointer_integer
3130
CHECK-NEXT: ext_using_undefined_std

clang/test/SemaTemplate/temp_explicit.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat %s
22
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat -std=c++98 %s
33
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++11 %s
4+
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++20 %s
45
//
56
// Tests explicit instantiation of templates.
67
template<typename T, typename U = T> class X0 { };
@@ -128,11 +129,15 @@ struct Foo<int> // expected-note{{header not required for explicitly-specialized
128129
{};
129130
};
130131

131-
template <> // expected-warning{{extraneous template parameter list}}
132+
template <> // expected-error{{extraneous template parameter list}}
132133
template <>
133134
struct Foo<int>::Bar<void>
134135
{};
135136

137+
#if __cplusplus >= 202002L
138+
template<> void f(auto); // expected-error{{extraneous template parameter list}}
139+
#endif
140+
136141
namespace N1 {
137142

138143
template<typename T> struct X7 { }; // expected-note{{here}}

0 commit comments

Comments
 (0)