[PATCH] D159133: [Sema] Make C++ functional-style cast warn about dropped qualifiers (-Wcast-qual)
Kristina Bessonova via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 5 04:44:00 PDT 2023
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2fbd1323e7bf: [Sema] Make C++ functional-style cast warn about dropped qualifiers (-Wcast… (authored by krisb).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D159133/new/
https://reviews.llvm.org/D159133
Files:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaCast.cpp
clang/test/Sema/warn-cast-qual.c
Index: clang/test/Sema/warn-cast-qual.c
===================================================================
--- clang/test/Sema/warn-cast-qual.c
+++ 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
}
Index: clang/lib/Sema/SemaCast.cpp
===================================================================
--- clang/lib/Sema/SemaCast.cpp
+++ clang/lib/Sema/SemaCast.cpp
@@ -3374,6 +3374,9 @@
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));
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -165,6 +165,7 @@
(`#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
-------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159133.555850.patch
Type: text/x-patch
Size: 4758 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230905/e67d3c3b/attachment-0001.bin>
More information about the cfe-commits
mailing list