[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 Aug 29 12:10:06 PDT 2023
krisb created this revision.
krisb added reviewers: lebedev.ri, rnk, alexfh.
Herald added a subscriber: StephenFan.
Herald added a project: All.
krisb requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
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.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D159133
Files:
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));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159133.554452.patch
Type: text/x-patch
Size: 4263 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230829/a3b18908/attachment.bin>
More information about the cfe-commits
mailing list