Skip to content

[Clang] Fix the warning group of several compatibilty diagnostics #138872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ Improvements to Clang's diagnostics
- ``-Wreserved-identifier`` now fires on reserved parameter names in a function
declaration which is not a definition.

- Several compatibility diagnostics that were incorrectly being grouped under
``-Wpre-c++20-compat`` are now part of ``-Wc++20-compat``. (#GH138775)

Improvements to Clang's time-trace
----------------------------------

Expand Down
8 changes: 5 additions & 3 deletions clang/include/clang/Basic/DiagnosticCommonKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ def err_attribute_not_type_attr : Error<
"%0%select{ attribute|}1 cannot be applied to types">;
def err_enum_template : Error<"enumeration cannot be a template">;

def warn_cxx20_compat_consteval : Warning<
"'consteval' specifier is incompatible with C++ standards before C++20">,
InGroup<CXX20Compat>, DefaultIgnore;
def warn_cxx20_compat_consteval
: Warning<"'consteval' specifier is incompatible with C++ standards before "
"C++20">,
InGroup<CXXPre20Compat>,
DefaultIgnore;
def warn_missing_type_specifier : Warning<
"type specifier missing, defaults to 'int'">,
InGroup<ImplicitInt>, DefaultIgnore;
Expand Down
22 changes: 9 additions & 13 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ defm adl_only_template_id : CXX20Compat<
"with explicit template arguments is">;
defm ctad_for_alias_templates
: CXX20Compat<"class template argument deduction for alias templates is">;
defm implicit_typename
: CXX20Compat<"missing 'typename' prior to dependent type name %0 is">;

// C++23 compatibility with C++20 and earlier.
defm constexpr_static_var : CXX23Compat<
Expand Down Expand Up @@ -5867,16 +5869,8 @@ def ext_typename_missing
def err_typename_refers_to_using_value_decl : Error<
"typename specifier refers to a dependent using declaration for a value "
"%0 in %1">;
def note_using_value_decl_missing_typename : Note<
"add 'typename' to treat this using declaration as a type">;
def warn_cxx17_compat_implicit_typename : Warning<"use of implicit 'typename' is "
"incompatible with C++ standards before C++20">, InGroup<CXX20Compat>,
DefaultIgnore;
def ext_implicit_typename
: ExtWarn<"missing 'typename' prior to dependent "
"type name %0; implicit 'typename' is a C++20 extension">,
InGroup<CXX20>;

def note_using_value_decl_missing_typename
: Note<"add 'typename' to treat this using declaration as a type">;
def err_template_kw_refers_to_non_template : Error<
"%0%select{| following the 'template' keyword}1 "
"does not refer to a template">;
Expand Down Expand Up @@ -9572,9 +9566,11 @@ def err_incomplete_type_used_in_type_trait_expr : Error<
"incomplete type %0 used in type trait expression">, NoSFINAE;

// C++20 constinit and require_constant_initialization attribute
def warn_cxx20_compat_constinit : Warning<
"'constinit' specifier is incompatible with C++ standards before C++20">,
InGroup<CXX20Compat>, DefaultIgnore;
def warn_cxx20_compat_constinit
: Warning<"'constinit' specifier is incompatible with C++ standards before "
"C++20">,
InGroup<CXXPre20Compat>,
DefaultIgnore;
def err_constinit_local_variable : Error<
"local variable cannot be declared 'constinit'">;
def err_require_constant_init_failed : Error<
Expand Down
11 changes: 5 additions & 6 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,11 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
if (AllowImplicitTypename == ImplicitTypenameContext::No)
return nullptr;
SourceLocation QualifiedLoc = SS->getRange().getBegin();
if (getLangOpts().CPlusPlus20)
Diag(QualifiedLoc, diag::warn_cxx17_compat_implicit_typename);
else
Diag(QualifiedLoc, diag::ext_implicit_typename)
<< NestedNameSpecifier::Create(Context, SS->getScopeRep(), &II)
<< FixItHint::CreateInsertion(QualifiedLoc, "typename ");
auto DB =
DiagCompat(QualifiedLoc, diag_compat::implicit_typename)
<< NestedNameSpecifier::Create(Context, SS->getScopeRep(), &II);
if (!getLangOpts().CPlusPlus20)
DB << FixItHint::CreateInsertion(QualifiedLoc, "typename ");
}

// We know from the grammar that this name refers to a type,
Expand Down
10 changes: 4 additions & 6 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3775,12 +3775,10 @@ TypeResult Sema::ActOnTemplateIdType(
NestedNameSpecifier *NNS =
NestedNameSpecifier::Create(Context, SS.getScopeRep(), TemplateII);
if (AllowImplicitTypename == ImplicitTypenameContext::Yes) {
if (getLangOpts().CPlusPlus20)
Diag(SS.getBeginLoc(), diag::warn_cxx17_compat_implicit_typename);
else
Diag(SS.getBeginLoc(), diag::ext_implicit_typename)
<< NNS
<< FixItHint::CreateInsertion(SS.getBeginLoc(), "typename ");
auto DB = DiagCompat(SS.getBeginLoc(), diag_compat::implicit_typename)
<< NNS;
if (!getLangOpts().CPlusPlus20)
DB << FixItHint::CreateInsertion(SS.getBeginLoc(), "typename ");
} else
Diag(SS.getBeginLoc(), diag::err_typename_missing_template) << NNS;

Expand Down
4 changes: 2 additions & 2 deletions clang/test/CXX/drs/cwg1xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace cwg108 { // cwg108: 2.9
template<typename T> struct A {
struct B { typedef int X; };
B::X x;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B::X'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B::X' is a C++20 extension}}
struct C : B { X x; };
// expected-error@-1 {{unknown type name 'X'}}
};
Expand Down Expand Up @@ -321,7 +321,7 @@ namespace cwg121 { // cwg121: 2.7
X::Y<T> x;
T::Y<T> y;
// expected-error@-1 {{use 'template' keyword to treat 'Y' as a dependent template name}}
// cxx98-17-error@-2 {{missing 'typename' prior to dependent type name 'T::Y'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-2 {{missing 'typename' prior to dependent type name 'T::Y' is a C++20 extension}}
};
Z<X> z;
} // namespace cwg121
Expand Down
18 changes: 9 additions & 9 deletions clang/test/CXX/drs/cwg2xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ namespace cwg224 { // cwg224: 16
A::type a;
A<T>::type b;
A<T*>::type c;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T *>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T *>::type' is a C++20 extension}}
::cwg224::example1::A<T>::type d;

class B {
Expand All @@ -435,13 +435,13 @@ namespace cwg224 { // cwg224: 16
A::type a;
A<T>::type b;
A<T*>::type c;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T *>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T *>::type' is a C++20 extension}}
::cwg224::example1::A<T>::type d;

B::type e;
A<T>::B::type f;
A<T*>::B::type g;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T *>::B::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T *>::B::type' is a C++20 extension}}
typename A<T*>::B::type h;
};
};
Expand All @@ -450,25 +450,25 @@ namespace cwg224 { // cwg224: 16
typedef int type;
A<T*>::type a;
A<T>::type b;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T>::type' is a C++20 extension}}
};

template <class T1, class T2, int I> struct B {
typedef int type;
B<T1, T2, I>::type b1;
B<T2, T1, I>::type b2;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B<T2, T1, I>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B<T2, T1, I>::type' is a C++20 extension}}

typedef T1 my_T1;
static const int my_I = I;
static const int my_I2 = I+0;
static const int my_I3 = my_I;
B<my_T1, T2, my_I>::type b3;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B<my_T1, T2, my_I>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B<my_T1, T2, my_I>::type' is a C++20 extension}}
B<my_T1, T2, my_I2>::type b4;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B<my_T1, T2, my_I2>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B<my_T1, T2, my_I2>::type' is a C++20 extension}}
B<my_T1, T2, my_I3>::type b5;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B<my_T1, T2, my_I3>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'B<my_T1, T2, my_I3>::type' is a C++20 extension}}
};
}

Expand All @@ -480,7 +480,7 @@ namespace cwg224 { // cwg224: 16
X<A::i, char>::type x;
X<A<T>::i, double>::type y;
X<A<T*>::i, long>::type z;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'X<A<T *>::i, long>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'X<A<T *>::i, long>::type' is a C++20 extension}}
int f();
};
template <class T> int A<T>::f() {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CXX/drs/cwg4xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ namespace cwg409 { // cwg409: 2.7
A::B b2;
A<T>::B b3;
A<T*>::B b4;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T *>::B'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'A<T *>::B' is a C++20 extension}}
};
} // namespace cwg409

Expand Down
6 changes: 3 additions & 3 deletions clang/test/CXX/drs/cwg5xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ namespace cwg526 { // cwg526: 2.7
typedef int type;
X<N>::type v1;
X<(N)>::type v2;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'X<(N)>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'X<(N)>::type' is a C++20 extension}}
X<+N>::type v3;
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'X<+N>::type'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'X<+N>::type' is a C++20 extension}}
};
} // namespace cwg526

Expand Down Expand Up @@ -783,7 +783,7 @@ struct Outer {
};
template <class T>
Outer<T>::Inner* Outer<T>::Inner::self() { return this; }
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'Outer<T>::Inner'; implicit 'typename' is a C++20 extension}}
// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'Outer<T>::Inner' is a C++20 extension}}

} // namespace cwg560

Expand Down
2 changes: 1 addition & 1 deletion clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Example1 {

template<class T> struct A<A<A<T>>> {
struct C {};
B<B<T>>::C bc; // expected-warning {{implicit 'typename' is a C++20 extension}}
B<B<T>>::C bc; // expected-warning {{missing 'typename' prior to dependent type name 'B<B<T>>::C' is a C++20 extension}}
};
}

Expand Down
2 changes: 1 addition & 1 deletion clang/test/FixIt/fixit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ struct MoreAccidentalCommas {
template<class T> struct Mystery;
template<class T> typedef Mystery<T>::type getMysteriousThing() { // \
expected-error {{function definition declared 'typedef'}} \
expected-warning {{implicit 'typename' is a C++20 extension}}
expected-warning {{missing 'typename' prior to dependent type name 'Mystery<T>::type' is a C++20 extension}}
return Mystery<T>::get();
}

Expand Down
12 changes: 6 additions & 6 deletions clang/test/SemaCXX/MicrosoftCompatibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ class C : private A<T>, public B<U> {
typedef B<U> Base2;
typedef A<U> Base3;

A<T>::TYPE a1; // expected-warning {{implicit 'typename' is a C++20 extension}}
Base1::TYPE a2; // expected-warning {{implicit 'typename' is a C++20 extension}}
A<T>::TYPE a1; // expected-warning {{missing 'typename' prior to dependent type name 'A<T>::TYPE' is a C++20 extension}}
Base1::TYPE a2; // expected-warning {{missing 'typename' prior to dependent type name 'Base1::TYPE' is a C++20 extension}}

B<U>::TYPE a3; // expected-warning {{implicit 'typename' is a C++20 extension}}
Base2::TYPE a4; // expected-warning {{implicit 'typename' is a C++20 extension}}
B<U>::TYPE a3; // expected-warning {{missing 'typename' prior to dependent type name 'B<U>::TYPE' is a C++20 extension}}
Base2::TYPE a4; // expected-warning {{missing 'typename' prior to dependent type name 'Base2::TYPE' is a C++20 extension}}

A<U>::TYPE a5; // expected-warning {{implicit 'typename' is a C++20 extension}}
Base3::TYPE a6; // expected-warning {{implicit 'typename' is a C++20 extension}}
A<U>::TYPE a5; // expected-warning {{missing 'typename' prior to dependent type name 'A<U>::TYPE' is a C++20 extension}}
Base3::TYPE a6; // expected-warning {{missing 'typename' prior to dependent type name 'Base3::TYPE' is a C++20 extension}}
};

class D {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/SemaCXX/MicrosoftExtensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ typedef char __unaligned *aligned_type; // expected-error {{expected ';' after t

namespace PR32750 {
template<typename T> struct A {};
template<typename T> struct B : A<A<T>> { A<T>::C::D d; }; // expected-warning {{implicit 'typename' is a C++20 extension}}
template<typename T> struct B : A<A<T>> { A<T>::C::D d; }; // expected-warning {{missing 'typename' prior to dependent type name 'A<T>::C::D' is a C++20 extension}}
}

#endif
Expand Down
8 changes: 4 additions & 4 deletions clang/test/SemaCXX/MicrosoftSuper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ struct DerivedFromDependentBase : BaseTemplate<T> {
typename __super::XXX a;
typedef typename __super::XXX b;

__super::XXX c; // expected-warning {{implicit 'typename' is a C++20 extension}}
typedef __super::XXX d; // expected-warning {{implicit 'typename' is a C++20 extension}}
__super::XXX c; // expected-warning {{missing 'typename'}}
typedef __super::XXX d; // expected-warning {{missing 'typename'}}

void foo() {
typename __super::XXX e;
Expand All @@ -127,8 +127,8 @@ struct DerivedFromTemplateParameter : T {
typename __super::XXX a;
typedef typename __super::XXX b;

__super::XXX c; // expected-warning {{implicit 'typename' is a C++20 extension}}
typedef __super::XXX d; // expected-warning {{implicit 'typename' is a C++20 extension}}
__super::XXX c; // expected-warning {{missing 'typename'}}
typedef __super::XXX d; // expected-warning {{missing 'typename'}}

void foo() {
typename __super::XXX e;
Expand Down
14 changes: 14 additions & 0 deletions clang/test/SemaCXX/gh138775.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify=cxx17 %s
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=pre-cxx20-compat -Wpre-c++20-compat %s
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=cxx20-compat -Wc++20-compat %s
// cxx20-compat-no-diagnostics

// cxx17-error@+4 {{unknown type name 'consteval'; did you mean 'constexpr'}}
// cxx17-warning@+3 {{missing 'typename' prior to dependent type name 'T::type' is a C++20 extension}}
// pre-cxx20-compat-warning@+2 {{'consteval' specifier is incompatible with C++ standards before C++20}}
// pre-cxx20-compat-warning@+1 {{missing 'typename' prior to dependent type name 'T::type' is incompatible with C++ standards before C++20}}
template<typename T> consteval T::type f();

// cxx17-error@+2 {{unknown type name 'constinit'}}
// pre-cxx20-compat-warning@+1 {{'constinit' specifier is incompatible with C++ standards before C++20}}
constinit int x = 4;
2 changes: 1 addition & 1 deletion clang/test/SemaCXX/rounding-math-crash.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-linux -fsyntax-only -frounding-math -verify %s

template <class b> b::a() {}
// expected-warning@-1 {{implicit 'typename' is a C++20 extension}}
// expected-warning@-1 {{missing 'typename' prior to dependent type name 'b::a' is a C++20 extension}}
// expected-error@-2 {{expected unqualified-id}}
16 changes: 8 additions & 8 deletions clang/test/SemaCXX/unknown-type-name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ struct A {

static int n;
static type m;
static int h(T::type, int); // expected-warning{{implicit 'typename' is a C++20 extension}}
static int h(T::type x, char); // expected-warning{{implicit 'typename' is a C++20 extension}}
static int h(T::type, int); // expected-warning{{missing 'typename'}}
static int h(T::type x, char); // expected-warning{{missing 'typename'}}
};

template<typename T>
A<T>::type g(T t) { return t; } // expected-warning{{implicit 'typename' is a C++20 extension}}
A<T>::type g(T t) { return t; } // expected-warning{{missing 'typename'}}

template<typename T>
A<T>::type A<T>::f() { return type(); } // expected-warning{{implicit 'typename' is a C++20 extension}}
A<T>::type A<T>::f() { return type(); } // expected-warning{{missing 'typename'}}

template<typename T>
void f(T::type) { } // expected-error{{missing 'typename'}}
Expand Down Expand Up @@ -84,11 +84,11 @@ int *test(UnknownType *fool) { return 0; } // expected-error{{unknown type name

template<typename T> int A<T>::n(T::value); // ok
template<typename T>
A<T>::type // expected-warning {{implicit 'typename' is a C++20 extension}}
A<T>::type // expected-warning {{missing 'typename'}}
A<T>::m(T::value, 0); // ok

template<typename T> int A<T>::h(T::type, int) {} // expected-warning{{implicit 'typename' is a C++20 extension}}
template<typename T> int A<T>::h(T::type x, char) {} // expected-warning{{implicit 'typename' is a C++20 extension}}
template<typename T> int A<T>::h(T::type, int) {} // expected-warning{{missing 'typename'}}
template<typename T> int A<T>::h(T::type x, char) {} // expected-warning{{missing 'typename'}}

template<typename T> int h(T::type, int); // expected-error{{missing 'typename'}}
template<typename T> int h(T::type x, char); // expected-error{{missing 'typename'}}
Expand Down Expand Up @@ -117,4 +117,4 @@ template<typename T> int i(T::type, int());
// a fix-it to add 'typename A<T>::type'
template<typename T>
A<T>::g() { } // expected-error{{expected unqualified-id}}
// expected-warning@-1{{implicit 'typename' is a C++20 extension}}
// expected-warning@-1{{missing 'typename'}}
2 changes: 1 addition & 1 deletion clang/test/SemaTemplate/typename-specifier-3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace PR12884_original {
typedef int arg;
};
struct C {
typedef B::X<typename B::arg> x; // precxx17-warning{{missing 'typename' prior to dependent type name 'B::X'; implicit 'typename' is a C++20 extension}}
typedef B::X<typename B::arg> x; // precxx17-warning{{missing 'typename' prior to dependent type name 'B::X' is a C++20 extension}}
};
};

Expand Down