[clang] [Clang] emit -Wignored-qualifiers diagnostic for cv-qualified base classes (PR #121419)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 1 08:18:09 PST 2025
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/121419
>From 3f6b1d68978857035a972f49b1cfd9d9d0151be9 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 1 Jan 2025 01:47:17 +0200
Subject: [PATCH] [Clang] emit -Wignored-qualifiers diagnostic for cv-qualified
base classes
---
clang/docs/ReleaseNotes.rst | 1 +
.../clang/Basic/DiagnosticSemaKinds.td | 4 ++++
clang/lib/Sema/SemaDeclCXX.cpp | 9 +++++++++
.../SemaCXX/warn-base-type-qualifiers.cpp | 20 +++++++++++++++++++
4 files changed, 34 insertions(+)
create mode 100644 clang/test/SemaCXX/warn-base-type-qualifiers.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b7da12bcf65818..659f0ebd97fc46 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -733,6 +733,7 @@ Improvements to Clang's diagnostics
scope.Unlock();
require(scope); // Warning! Requires mu1.
}
+- Clang now emits a ``-Wignored-qualifiers`` diagnostic when a base class includes cv-qualifiers (#GH55474).
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 330ae045616aba..294cfa24975a73 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -487,6 +487,10 @@ def err_noreturn_non_function : Error<
def warn_qual_return_type : Warning<
"'%0' type qualifier%s1 on return type %plural{1:has|:have}1 no effect">,
InGroup<IgnoredQualifiers>, DefaultIgnore;
+def warn_qual_base_type : Warning<
+ "'%0' qualifier%s1 on base class type %2 have no effect">,
+ InGroup<IgnoredQualifiers>, DefaultIgnore;
+
def warn_deprecated_redundant_constexpr_static_def : Warning<
"out-of-line definition of constexpr static data member is redundant "
"in C++17 and is deprecated">,
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index c5a72cf812ebc9..b82ca1541ba249 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -2655,6 +2655,15 @@ CXXBaseSpecifier *Sema::CheckBaseSpecifier(CXXRecordDecl *Class,
return nullptr;
}
+ if (BaseType.hasQualifiers() && !isa<SubstTemplateTypeParmType>(BaseType)) {
+ auto Quals =
+ BaseType.getQualifiers().getAsString(Context.getPrintingPolicy());
+ Diag(BaseLoc, diag::warn_qual_base_type)
+ << Quals << std::count(Quals.begin(), Quals.end(), ' ') + 1
+ << BaseType;
+ Diag(BaseLoc, diag::note_base_class_specified_here) << BaseType;
+ }
+
// For the MS ABI, propagate DLL attributes to base class templates.
if (Context.getTargetInfo().getCXXABI().isMicrosoft() ||
Context.getTargetInfo().getTriple().isPS()) {
diff --git a/clang/test/SemaCXX/warn-base-type-qualifiers.cpp b/clang/test/SemaCXX/warn-base-type-qualifiers.cpp
new file mode 100644
index 00000000000000..a5af7ec7415bf5
--- /dev/null
+++ b/clang/test/SemaCXX/warn-base-type-qualifiers.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 %s -std=c++11 -Wignored-qualifiers -verify
+
+class A { };
+
+typedef const A A_Const;
+class B : public A_Const { }; // expected-warning {{'const' qualifier on base class type 'A_Const' (aka 'const A') have no effect}} \
+ // expected-note {{base class 'A_Const' (aka 'const A') specified here}}
+
+typedef const volatile A A_Const_Volatile;
+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}} \
+ // expected-note {{base class 'A_Const_Volatile' (aka 'const volatile A') specified here}}
+
+struct D {
+ D(int);
+};
+template <typename T> struct E : T {
+ using T::T;
+ E(int &) : E(0) {}
+};
+E<const D> e(1);
More information about the cfe-commits
mailing list