diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -165,6 +165,7 @@ (`#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 ------------------------- diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -3374,6 +3374,9 @@ if (auto *ConstructExpr = dyn_cast(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)); diff --git a/clang/test/Sema/warn-cast-qual.c b/clang/test/Sema/warn-cast-qual.c --- a/clang/test/Sema/warn-cast-qual.c +++ b/clang/test/Sema/warn-cast-qual.c @@ -36,6 +36,39 @@ 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 @@ *(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 @@ *(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 }