diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index eb3a271fa59d02..b3e9987a7e6c98 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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 ---------------------------------- diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index db54312ad965e8..0175c20daf241e 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, DefaultIgnore; +def warn_qual_base_type : Warning< + "'%0' qualifier%s1 on base class type %2 have no effect">, + InGroup, 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 a867ed73bd4033..839b3a1cccdcc3 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()) { + 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()) { 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..b9fd577c574b99 --- /dev/null +++ b/clang/test/SemaCXX/warn-base-type-qualifiers.cpp @@ -0,0 +1,35 @@ +// RUN: %clang_cc1 %s -std=c++11 -Wignored-qualifiers -verify + +template struct add_const { + using type = const T; +}; +template using add_const_t = typename add_const::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 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 e(1); // expected-note {{in instantiation of template class 'E' requested here}} + +template +struct G : add_const::type { // expected-warning {{'const' qualifier on base class type 'add_const::type' (aka 'const D') have no effect}} \ + // expected-note {{base class 'add_const::type' (aka 'const D') specified here}} + using T::T; + G(int &) : G(0) {} +}; +G g(1); // expected-note {{in instantiation of template class 'G' requested here}} diff --git a/libcxx/include/tuple b/libcxx/include/tuple index 0c96786ae6d027..5d968bfd4015a1 100644 --- a/libcxx/include/tuple +++ b/libcxx/include/tuple @@ -257,6 +257,7 @@ template # 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> @@ -390,7 +391,7 @@ public: }; template -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;