Skip to content

Commit 902d4f0

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

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
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 warn_qual_base_type : Warning<
491+
"'%0' qualifier%s1 on base class type %2 have no effect">,
492+
InGroup<IgnoredQualifiers>, DefaultIgnore;
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::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()) {
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)