[cfe-commits] r156968 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/Sema/format-strings.c

Matt Beaumont-Gay matthewbg at google.com
Wed May 16 17:03:16 PDT 2012


Author: matthewbg
Date: Wed May 16 19:03:16 2012
New Revision: 156968

URL: http://llvm.org/viewvc/llvm-project?rev=156968&view=rev
Log:
Use the argument location instead of the format string location when warning
about argument type mismatch.

This gives a nicer diagnostic in cases like
  printf(fmt,
         i);
where previously the snippet just pointed at 'fmt' (with a note at the
definition of fmt).

It's a wash for cases like
  printf("%f",
         i);
where previously we snippeted the offending portion of the format string,
but didn't indicate which argument was at fault.

Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/format-strings.c

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=156968&r1=156967&r2=156968&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed May 16 19:03:16 2012
@@ -2423,8 +2423,8 @@
         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
           << Ex->getSourceRange(),
-        getLocationOfByte(CS.getStart()),
-        /*IsStringLocation*/true,
+        Ex->getLocStart(),
+        /*IsStringLocation*/false,
         getSpecifierRange(startSpecifier, specifierLen),
         FixItHint::CreateReplacement(
           getSpecifierRange(startSpecifier, specifierLen),
@@ -2436,8 +2436,8 @@
           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
           << getSpecifierRange(startSpecifier, specifierLen)
           << Ex->getSourceRange(),
-        getLocationOfByte(CS.getStart()),
-        true,
+        Ex->getLocStart(),
+        /*IsStringLocation*/false,
         getSpecifierRange(startSpecifier, specifierLen));
     }
   }
@@ -2591,8 +2591,8 @@
         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
           << Ex->getSourceRange(),
-        getLocationOfByte(CS.getStart()),
-        /*IsStringLocation*/true,
+        Ex->getLocStart(),
+        /*IsStringLocation*/false,
         getSpecifierRange(startSpecifier, specifierLen),
         FixItHint::CreateReplacement(
           getSpecifierRange(startSpecifier, specifierLen),
@@ -2602,8 +2602,8 @@
         S.PDiag(diag::warn_printf_conversion_argument_type_mismatch)
           << ATR.getRepresentativeTypeName(S.Context) << Ex->getType()
           << Ex->getSourceRange(),
-        getLocationOfByte(CS.getStart()),
-        /*IsStringLocation*/true,
+        Ex->getLocStart(),
+        /*IsStringLocation*/false,
         getSpecifierRange(startSpecifier, specifierLen));
     }
   }

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=156968&r1=156967&r2=156968&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Wed May 16 19:03:16 2012
@@ -502,8 +502,13 @@
   printf("%a", (long double)0); // expected-warning{{format specifies type 'double' but the argument has type 'long double'}}
 
   // Test braced char[] initializers.
-  const char kFormat18[] = { "%lld" }; // expected-note{{format string is defined here}}}
+  const char kFormat18[] = { "%lld" }; // expected-note{{format string is defined here}}
   printf(kFormat18, 0); // expected-warning{{format specifies type}}
+
+  // Make sure we point at the offending argument rather than the format string.
+  const char kFormat19[] = "%d";  // expected-note{{format string is defined here}}
+  printf(kFormat19,
+         0.0); // expected-warning{{format specifies}}
 }
 
 // PR 9466: clang: doesn't know about %Lu, %Ld, and %Lx 





More information about the cfe-commits mailing list