Author: Kristina Bessonova Date: 2023-09-05T12:48:38+02:00 New Revision: 2fbd1323e7bf6ec204867774db8aa9b7dc976da5
URL: https://github.com/llvm/llvm-project/commit/2fbd1323e7bf6ec204867774db8aa9b7dc976da5 DIFF: https://github.com/llvm/llvm-project/commit/2fbd1323e7bf6ec204867774db8aa9b7dc976da5.diff LOG: [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 Added: Modified: clang/docs/ReleaseNotes.rst clang/lib/Sema/SemaCast.cpp clang/test/Sema/warn-cast-qual.c Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 84681ec9554d21..b2b68cc2aeb5f6 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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 ------------------------- diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp index d4648b2d16eaac..98b5879456e217 100644 --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -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)); diff --git a/clang/test/Sema/warn-cast-qual.c b/clang/test/Sema/warn-cast-qual.c index 104f9a3faf47c8..ec140180ffa12f 100644 --- a/clang/test/Sema/warn-cast-qual.c +++ b/clang/test/Sema/warn-cast-qual.c @@ -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 } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits