[cfe-commits] [PATCH] -Wcast-qual

Igor Minin i.minin.llvm at gmail.com
Wed Jun 20 02:53:21 PDT 2012


Hello.

The attached patch adds support for a clang -Wcast-qual option. Semantic is
compatible with the gcc one.

Warn whenever a pointer is cast so as to remove a type qualifier from the
> target
> type. For example, warn if a "const char*" is cast to an ordinary "char*".
> Also warn when making a cast that introduces a type qualifier in an unsafe
> way.
> For example, casting "char**" to "const char**" is unsafe, as in this
> example:
> /* p is char ** value. */
> const char **q = (const char **) p;
> /* Assignment of readonly string to const char * is OK. */
> *q = "string";
> /* Now char** pointer points to read-only memory. */
> **p = ’b’;

(Gcc manual).

What is "introducing in an unsafe way" precisely described in [conv.qual]
item of C++ standard.

So, the proposed patch is very similar to the code that checks assign
constrains. Differences are:

* This patch generates only warnings, not errors, which helps to have
cleaner implementation.
* It is called only for C-style casting and functional-style casting
(because only three casts can remove qualifiers: C-sytle, functional-style
and const_cast. But const_cast in gcc is not generated warnings, so I
preserved this behaviour in clang). Other cases filtered by checks in
appropriate casts.

Please, review.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120620/8729ab4c/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: warn_cast_qual.diff
Type: application/octet-stream
Size: 6644 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120620/8729ab4c/attachment.obj>


More information about the cfe-commits mailing list