Skip to content

Commit f3b5fc2

Browse files
committed
[Clang] emit -Wignored-qualifiers diagnostic for cv-qualified base classes
1 parent 0897373 commit f3b5fc2

File tree

8 files changed

+39
-10
lines changed

8 files changed

+39
-10
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ Improvements to Clang's diagnostics
733733
scope.Unlock();
734734
require(scope); // Warning! Requires mu1.
735735
}
736+
- Clang now emits a ``-Wignored-qualifiers`` diagnostic when a base class includes cv-qualifiers (#GH55474).
736737

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

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ def err_noreturn_non_function : Error<
487487
def warn_qual_return_type : Warning<
488488
"'%0' type qualifier%s1 on return type %plural{1:has|:have}1 no effect">,
489489
InGroup<IgnoredQualifiers>, DefaultIgnore;
490+
def ext_warn_qual_base_type : ExtWarn<
491+
"'%0' qualifier%s1 on base class type %2 have no effect">,
492+
InGroup<IgnoredQualifiers>;
493+
490494
def warn_deprecated_redundant_constexpr_static_def : Warning<
491495
"out-of-line definition of constexpr static data member is redundant "
492496
"in C++17 and is deprecated">,

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,15 @@ CXXBaseSpecifier *Sema::CheckBaseSpecifier(CXXRecordDecl *Class,
26552655
return nullptr;
26562656
}
26572657

2658+
if (BaseType.hasQualifiers()) {
2659+
auto Quals =
2660+
BaseType.getQualifiers().getAsString(Context.getPrintingPolicy());
2661+
Diag(BaseLoc, diag::ext_warn_qual_base_type)
2662+
<< Quals << std::count(Quals.begin(), Quals.end(), ' ') + 1
2663+
<< BaseType;
2664+
Diag(BaseLoc, diag::note_base_class_specified_here) << BaseType;
2665+
}
2666+
26582667
// For the MS ABI, propagate DLL attributes to base class templates.
26592668
if (Context.getTargetInfo().getCXXABI().isMicrosoft() ||
26602669
Context.getTargetInfo().getTriple().isPS()) {

clang/test/CXX/drs/cwg4xx.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,8 @@ namespace cwg484 { // cwg484: yes
13091309
}
13101310
CA::A() {}
13111311

1312-
struct B : CA {
1312+
struct B : CA { // expected-error {{'const' qualifier on base class type 'CA' (aka 'const cwg484::A') have no effect}} \
1313+
// expected-note {{base class 'CA' (aka 'const cwg484::A') specified here}}
13131314
B() : CA() {}
13141315
void f() { return CA::f(); }
13151316
};

clang/test/CXX/special/class.inhctor/elsewhere.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ template<typename T> struct F : D<bool> {
5757
F<bool> fb; // expected-note {{here}}
5858

5959
template<typename T>
60-
struct G : T {
60+
struct G : T { // expected-warning {{'const' qualifier on base class type 'const B1' have no effect}} \
61+
// expected-note {{base class 'const B1' specified here}}
6162
using T::T;
6263
G(int &) : G(0) {}
6364
};
6465
G<B1> g(123);
65-
G<const B1> g2(123);
66+
G<const B1> g2(123); // expected-note {{in instantiation of template class 'G<const B1>' requested here}}

clang/test/Sema/GH70594.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
22
// RUN: %clang_cc1 -fsyntax-only -std=c++23 %s -verify
33

4-
// expected-no-diagnostics
5-
64
struct A {};
75
using CA = const A;
86

9-
struct S1 : CA {
7+
struct S1 : CA { // expected-warning {{'const' qualifier on base class type 'CA' (aka 'const A') have no effect}} \
8+
// expected-note {{base class 'CA' (aka 'const A') specified here}}
109
constexpr S1() : CA() {}
1110
};
1211

1312
struct S2 : A {
1413
constexpr S2() : CA() {}
1514
};
1615

17-
struct S3 : CA {
16+
struct S3 : CA { // expected-warning {{'const' qualifier on base class type 'CA' (aka 'const A') have no effect}} \
17+
// expected-note {{base class 'CA' (aka 'const A') specified here}}
1818
constexpr S3() : A() {}
1919
};
2020

2121
struct Int {};
2222

2323
template <class _Hp>
24-
struct __tuple_leaf : _Hp {
24+
struct __tuple_leaf : _Hp { // expected-warning {{'const' qualifier on base class type 'const Int' have no effect}} \
25+
// expected-note {{base class 'const Int' specified here}}
2526
constexpr __tuple_leaf() : _Hp() {}
2627
};
2728

28-
constexpr __tuple_leaf<const Int> t;
29+
constexpr __tuple_leaf<const Int> t; // expected-note {{in instantiation of template class '__tuple_leaf<const Int>' requested here}}

clang/test/SemaCXX/class-base-member-init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ namespace PR16596 {
103103
class A { public: virtual ~A(); };
104104
typedef const A Foo;
105105
void Apply(Foo processor);
106-
struct Bar : public Foo {};
106+
struct Bar : public Foo {}; // expected-warning {{'const' qualifier on base class type 'Foo' (aka 'const PR16596::A') have no effect}}\
107+
// expected-note {{base class 'Foo' (aka 'const PR16596::A') specified here}}
107108
void Fetch() {
108109
Apply(Bar());
109110
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 %s -std=c++11 -Wignored-qualifiers -verify
2+
3+
class A { };
4+
5+
typedef const A A_Const;
6+
class B : public A_Const { }; // expected-warning {{'const' qualifier on base class type 'A_Const' (aka 'const A') have no effect}} \
7+
// expected-note {{base class 'A_Const' (aka 'const A') specified here}}
8+
9+
typedef const volatile A A_Const_Volatile;
10+
class C : public A_Const_Volatile { }; // expected-warning {{'const volatile' qualifiers on base class type 'A_Const_Volatile' (aka 'const volatile A') have no effect}} \
11+
// expected-note {{base class 'A_Const_Volatile' (aka 'const volatile A') specified here}}

0 commit comments

Comments
 (0)