[Sema] Make C++ functional-style cast warn about dropped qualifiers (-Wcast-qual)

Functional-style cast (i.e. a simple-type-specifier or typename-specifier
followed by a parenthesize single expression [expr.type.conv]) is equivalent
to the C-style cast, so that makes sense they have identical behavior
including warnings.

This also matches GCC https://godbolt.org/z/b8Ma9Thjb.

Reviewed By: rnk, aaron.ballman

Differential Revision: https://reviews.llvm.org/D159133
This commit is contained in:
Kristina Bessonova 2023-09-05 12:43:52 +02:00
parent 50598f0ff4
commit 2fbd1323e7
3 changed files with 49 additions and 0 deletions

View File

@ -165,6 +165,7 @@ Improvements to Clang's diagnostics
(`#64871: <https://github.com/llvm/llvm-project/issues/64871>`_).
Also clang no longer emits false positive warnings about the output length of
``%g`` format specifier.
- Clang now emits ``-Wcast-qual`` for functional-style cast expressions.
Bug Fixes in This Version
-------------------------

View File

@ -3374,6 +3374,9 @@ ExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo,
if (auto *ConstructExpr = dyn_cast<CXXConstructExpr>(SubExpr))
ConstructExpr->setParenOrBraceRange(SourceRange(LPLoc, RPLoc));
// -Wcast-qual
DiagnoseCastQual(Op.Self, Op.SrcExpr, Op.DestType);
return Op.complete(CXXFunctionalCastExpr::Create(
Context, Op.ResultType, Op.ValueKind, CastTypeInfo, Op.Kind,
Op.SrcExpr.get(), &Op.BasePath, CurFPFeatureOverrides(), LPLoc, RPLoc));

View File

@ -36,6 +36,39 @@ void foo(void) {
char *charptr = (char *)constcharptr; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
const char *constcharptr2 = (char *)constcharptr; // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
const char *charptr2 = (char *)charptr; // no warning
#ifdef __cplusplus
using CharPtr = char *;
using CharPtrPtr = char **;
using ConstCharPtrPtr = const char **;
using CharPtrConstPtr = char *const *;
char *fy = CharPtr(ptr); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
char **fy1 = CharPtrPtr(ptrptr); // expected-warning {{cast from 'const char *const *' to 'char **' drops const qualifier}}
const char **fy2 = ConstCharPtrPtr(ptrptr); // expected-warning {{cast from 'const char *const *' to 'const char **' drops const qualifier}}
char *const *fy3 = CharPtrConstPtr(ptrptr); // expected-warning {{cast from 'const char *const' to 'char *const' drops const qualifier}}
const char **fy4 = ConstCharPtrPtr(ptrcptr); // expected-warning {{cast from 'char *const *' to 'const char **' drops const qualifier}}
using ConstVoidPtr = const void *;
char *fz = CharPtr(uintptr_t(ConstVoidPtr(ptr))); // no warning
char *fz1 = CharPtr(ConstVoidPtr(ptr)); // expected-warning {{cast from 'const void *' to 'char *' drops const qualifier}}
char *fvol2 = CharPtr(vol); // expected-warning {{cast from 'volatile char *' to 'char *' drops volatile qualifier}}
char *fvolc2 = CharPtr(volc); // expected-warning {{cast from 'const volatile char *' to 'char *' drops const and volatile qualifiers}}
using ConstIntPtrPtr = const int **;
using VolitileIntPtrPtr = volatile int **;
const int **fintptrptrc = ConstIntPtrPtr(intptrptr); // expected-warning {{cast from 'int **' to 'ConstIntPtrPtr' (aka 'const int **') must have all intermediate pointers const qualified}}
volatile int **fintptrptrv = VolitileIntPtrPtr(intptrptr); // expected-warning {{cast from 'int **' to 'VolitileIntPtrPtr' (aka 'volatile int **') must have all intermediate pointers const qualified}}
using ConstIntPtr = const int *;
const int *fintptrc = ConstIntPtr(intptr); // no warning
char **fcharptrptr = CharPtrPtr(charptrptrc); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
char *fcharptr = CharPtr(constcharptr); // expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
const char *fcharptr2 = CharPtr(charptr); // no warning
#endif
}
void bar_0(void) {
@ -48,6 +81,12 @@ void bar_0(void) {
*(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
*(int *)(&S.b) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
#ifdef __cplusplus
using IntPtr = int *;
*(IntPtr(&S.a)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
*(IntPtr(&S.b)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
#endif
}
void bar_1(void) {
@ -61,4 +100,10 @@ void bar_1(void) {
*(int *)(&S.a) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
*(int *)(&S.b) = 0; // no warning
#ifdef __cplusplus
using IntPtr = int *;
*(IntPtr(&S.a)) = 0; // expected-warning {{cast from 'const int *' to 'int *' drops const qualifier}}
*(IntPtr(&S.b)) = 0; // no warning
#endif
}