]> granicus.if.org Git - clang/commitdiff
Use the argument location instead of the format string location when warning
authorMatt Beaumont-Gay <matthewbg@google.com>
Thu, 17 May 2012 00:03:16 +0000 (00:03 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Thu, 17 May 2012 00:03:16 +0000 (00:03 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156968 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/Sema/format-strings.c

index 62f45f5c916b8a6239d370ae2f9e4141d7b3f897..769705419e88c7cafa92b6e2d0bee78bdef7812f 100644 (file)
@@ -2423,8 +2423,8 @@ CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
         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 @@ CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
           << 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 @@ bool CheckScanfHandler::HandleScanfSpecifier(
         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 @@ bool CheckScanfHandler::HandleScanfSpecifier(
         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));
     }
   }
index afc9e9720ce176980a2037f26df9cff34827ace9..5c30849059983a5367dea12b794c273022258374 100644 (file)
@@ -502,8 +502,13 @@ void pr9751() {
   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