Hi,
I implemented -Wcast-qual. The patch is actually quite short (attached + a test
case).
This fixes #13772 and also note that -Wcast-qual is used in llvm build itself.
Is this ok to be commited? Thanks
Roman
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td (revision 221679)
+++ include/clang/Basic/DiagnosticGroups.td (working copy)
@@ -60,7 +60,7 @@
def KeywordCompat : DiagGroup<"keyword-compat">;
def GNUCaseRange : DiagGroup<"gnu-case-range">;
def CastAlign : DiagGroup<"cast-align">;
-def : DiagGroup<"cast-qual">;
+def CastQual : DiagGroup<"cast-qual">;
def : DiagGroup<"char-align">;
def Comment : DiagGroup<"comment">;
def GNUComplexInteger : DiagGroup<"gnu-complex-integer">;
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td (revision 221679)
+++ include/clang/Basic/DiagnosticSemaKinds.td (working copy)
@@ -6097,6 +6097,8 @@
def warn_zero_size_struct_union_in_extern_c : Warning<"%select{|empty }0"
"%select{struct|union}1 has size 0 in C, %select{size 1|non-zero size}2 in C++">,
InGroup<ExternCCompat>;
+def warn_cast_qual : Warning<"cast from %0 to %1 casts drops const qualifier">,
+ InGroup<CastQual>, DefaultIgnore;
} // End of general sema category.
// inline asm.
Index: lib/Sema/SemaCast.cpp
===================================================================
--- lib/Sema/SemaCast.cpp (revision 221679)
+++ lib/Sema/SemaCast.cpp (working copy)
@@ -2371,7 +2371,21 @@
if (Kind == CK_BitCast)
checkCastAlign();
+
+ // -Wcast-qual
+ const PointerType *DestPtr = nullptr;
+ const PointerType *SrcPtr = nullptr;
+
+ while ((DestPtr = DestType->getAs<PointerType>()) && (SrcPtr = SrcType->getAs<PointerType>())) {
+ QualType OrigDestType = DestType;
+ QualType OrigSrcType = SrcType;
+ DestType = DestPtr->getPointeeType();
+ SrcType = SrcPtr->getPointeeType();
+ if (SrcType.isConstQualified() && !DestType.isConstQualified()) {
+ Self.Diag(SrcExpr.get()->getLocStart(), diag::warn_cast_qual) << OrigSrcType << OrigDestType;
}
+ }
+}
ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc,
TypeSourceInfo *CastTypeInfo,
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -Wcast-qual -verify %s
void foo() {
const char * const x = 0;
char *y = (char *)x; // expected-warning {{cast from 'const char *' to 'char *' casts drops const qualifier}}
const char *z = (const char *)x;
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits