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 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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,8 @@ Improvements to Clang's diagnostics
}
- Diagnose invalid declarators in the declaration of constructors and destructors (#GH121706).

- 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()) {
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
35 changes: 35 additions & 0 deletions clang/test/SemaCXX/warn-base-type-qualifiers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %clang_cc1 %s -std=c++11 -Wignored-qualifiers -verify

template <typename T> struct add_const {
using type = const T;
};
template <typename T> using add_const_t = typename add_const<T>::type;

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 { // expected-warning {{'const' qualifier on base class type 'const D' have no effect}} \
// expected-note {{base class 'const D' specified here}}
using T::T;
E(int &) : E(0) {}
};
E<const D> e(1); // expected-note {{in instantiation of template class 'E<const D>' requested here}}

template <typename T>
struct G : add_const<T>::type { // expected-warning {{'const' qualifier on base class type 'add_const<D>::type' (aka 'const D') have no effect}} \
// expected-note {{base class 'add_const<D>::type' (aka 'const D') specified here}}
using T::T;
G(int &) : G(0) {}
};
G<D> g(1); // expected-note {{in instantiation of template class 'G<D>' requested here}}
3 changes: 2 additions & 1 deletion libcxx/include/tuple
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ template <class... Types>
# include <__type_traits/maybe_const.h>
# include <__type_traits/nat.h>
# include <__type_traits/negation.h>
# include <__type_traits/remove_cv.h>
# include <__type_traits/remove_cvref.h>
# include <__type_traits/remove_reference.h>
# include <__type_traits/unwrap_ref.h>
Expand Down Expand Up @@ -390,7 +391,7 @@ public:
};

template <size_t _Ip, class _Hp>
class __tuple_leaf<_Ip, _Hp, true> : private _Hp {
class __tuple_leaf<_Ip, _Hp, true> : private __remove_cv_t<_Hp> {
public:
_LIBCPP_CONSTEXPR_SINCE_CXX14 __tuple_leaf& operator=(const __tuple_leaf&) = delete;

Expand Down
Loading