[cfe-commits] Patch: PR9751: -Wformat warnings will point to the callsite and give a note if the format string is elsewhere.

Eli Friedman eli.friedman at gmail.com
Thu Oct 13 14:53:04 PDT 2011

On Thu, Oct 13, 2011 at 2:38 PM, Richard Trieu <rtrieu at google.com> wrote:
> http://llvm.org/bugs/show_bug.cgi?id=9751
> -Wformat warnings will point to the format string, but no message at the
> call site.  This patch will move the warning to always point at the call
> site.  If the format string is part of the function call, one warning will
> show.  If the format string is defined elsewhere, a warning at the call site
> plus a note where the format string is defined.
> Also, if a note is present, the fix-it would be moved to the note so they
> won't be applied with -fixit.  If the format string was used in multiple
> places, fixits may cause problems in other places.
> printf("%d %d", 1);
> warning: more '%' conversions than data arguments [-Wformat]
> printf("%d %d", 1);
>            ~^
> const char kFormat[] = "%d %d";
> printf(kFormat, 1);
> test2.c:8:8: warning: more '%' conversions than data arguments [-Wformat]
> printf(kFormat, 1);
>        ^~~~~~~
> test2.c:7:29: note: format string is defined here
> const char kFormat[] = "%d %d";
>                            ~^
> Patch attached an available at http://codereview.appspot.com/5277043/

+namespace {
+  class StringLiteralFinder
+    : public ConstStmtVisitor<StringLiteralFinder> {
+    Sema& S;
+    const StringLiteral *StringExpr;
+    bool StringFound;
+  public:

Why is StringLiteralFinder necessary?  The caller should know how it
found the string literal...

What do the resulting diagnostics look like if the string literal is
defined in a macro?


More information about the cfe-commits mailing list