Skip to content
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

[Clang] emit -Wignored-qualifiers diagnostic for cv-qualified base classes #121419

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------------------
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">,
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,15 @@ CXXBaseSpecifier *Sema::CheckBaseSpecifier(CXXRecordDecl *Class,
return nullptr;
}

if (BaseType.hasQualifiers() && !isa<SubstTemplateTypeParmType>(BaseType)) {
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
std::string 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()) {
Expand Down
20 changes: 20 additions & 0 deletions clang/test/SemaCXX/warn-base-type-qualifiers.cpp
Original file line number Diff line number Diff line change
@@ -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 {
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved
using T::T;
E(int &) : E(0) {}
};
E<const D> e(1);
Loading