Seems like gcc (5.0) agrees with you and only emits one warning. I modified
the patch to only report the inner most cast.

New patch attached.

On Wed, Nov 12, 2014 at 12:39:20PM -0800, David Blaikie wrote:
> [+Richard for oversight]
> 
> char **y1 = (char **)ptrptr; // expected-warning {{cast from 'const char
> *const *' to 'char **' drops const qualifier}} expected-warning {{cast from
> 'const char *const' to 'char *' drops const qualifier}}
> 
> I think if we're going to warn on multiple layers (I'm not sure that's
> ideal - is that consistent with GCC's warning? Does GCC warn on mismatched
> types too - "const T*" -> "U*"? - do we warn there too, or only when
> there's a valid implicit conversion like the void* example?) then we should
> probably drop the top level const, "const char *const" -> "char*" - the top
> level const on the first type is confusing/misleading, it's only relevant
> to show "const char*" and "char*".
> 
> 
> On Wed, Nov 12, 2014 at 10:41 AM, Roman Divacky <[email protected]> wrote:
> 
> > I expanded the testcase and fixed the grammar in the actual warning.
> >
> > New patch attached.
> >
> > On Tue, Nov 11, 2014 at 05:03:33PM -0800, David Blaikie wrote:
> > > (it's a bit easier if you include the test in the same patch file - also
> > > you can use Phabricator if you like - some reviewers perefer it)
> > >
> > > Since you've got the loop there for seeing through multiple levels of
> > > pointer, should you have a test case that exercises that on a > 1 level
> > of
> > > depth? Demonstrate that we warn on both levels (if that's the right thing
> > > to do?)?
> > >
> > > Optionally (probably in a separate follow-up patch) you could add a note
> > > with a fixit to include the missing consts.
> > >
> > > On Tue, Nov 11, 2014 at 10:58 AM, Roman Divacky <[email protected]>
> > wrote:
> > >
> > > > 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
> > > >
> > > >
> > > > _______________________________________________
> > > > cfe-commits mailing list
> > > > [email protected]
> > > > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
> > > >
> > > >
> >
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td	(revision 221694)
+++ 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 221694)
+++ 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 drops const qualifier">,
+  InGroup<CastQual>, DefaultIgnore;
 } // End of general sema category.
 
 // inline asm.
Index: lib/Sema/SemaCast.cpp
===================================================================
--- lib/Sema/SemaCast.cpp	(revision 221694)
+++ lib/Sema/SemaCast.cpp	(working copy)
@@ -2371,6 +2371,25 @@
 
   if (Kind == CK_BitCast)
     checkCastAlign();
+
+  // -Wcast-qual
+  const PointerType *DestPtr = nullptr;
+  const PointerType *SrcPtr = nullptr;
+  const QualType *InnerMostDestType = nullptr;
+  const QualType *InnerMostSrcType = 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()) {
+      InnerMostDestType = &OrigDestType;
+      InnerMostSrcType = &OrigSrcType;
+    }
+  }
+  if (InnerMostDestType && InnerMostSrcType)
+    Self.Diag(SrcExpr.get()->getLocStart(), diag::warn_cast_qual) << *InnerMostSrcType << *InnerMostDestType;
 }
 
 ExprResult Sema::BuildCStyleCastExpr(SourceLocation LPLoc,
Index: test/Sema/warn-cast-qual.c
===================================================================
--- test/Sema/warn-cast-qual.c	(revision 0)
+++ test/Sema/warn-cast-qual.c	(working copy)
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -Wcast-qual -verify %s
+
+#include <stdint.h>
+
+void foo() {
+  const char * const ptr = 0;
+  const char * const *ptrptr = 0;
+  char *y = (char *)ptr;	// expected-warning {{cast from 'const char *' to 'char *' drops const qualifier}}
+  char **y1 = (char **)ptrptr;	// expected-warning {{cast from 'const char *const' to 'char *' drops const qualifier}}
+  const char **y2 = (const char **)ptrptr;	// expected-warning {{cast from 'const char *const' to 'const char *' drops const qualifier}}
+
+  char *z = (char *)(uintptr_t)(const void *)ptr;	// no warning
+  char *z1 = (char *)(const void *)ptr;	// expected-warning {{cast from 'const void *' to 'char *' drops const qualifier}}
+}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to