Previously, the printf warnings would say your arguments type was
`int` when it was really a `char` or a `short`. This fixes that and
allows the hints to suggest `h` modifiers for small ints.

Typedef'd `char` types are treated as ints, since the most common case
of uint8_t shouldn't normally be formatted as a character.
---
 lib/Analysis/PrintfFormatString.cpp |   16 +++++++++++++++-
 lib/Sema/SemaChecking.cpp           |    6 ++++--
 test/Sema/format-strings.c          |   11 ++++++++++-
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/lib/Analysis/PrintfFormatString.cpp b/lib/Analysis/PrintfFormatString.cpp
index b8c327c..4d17c59 100644
--- a/lib/Analysis/PrintfFormatString.cpp
+++ b/lib/Analysis/PrintfFormatString.cpp
@@ -382,6 +382,18 @@ bool PrintfSpecifier::fixType(QualType QT) {
     LM.setKind(LengthModifier::None);
     break;
 
+  case BuiltinType::Char_U:
+  case BuiltinType::UChar:
+  case BuiltinType::Char_S:
+  case BuiltinType::SChar:
+    LM.setKind(LengthModifier::AsChar);
+    break;
+
+  case BuiltinType::Short:
+  case BuiltinType::UShort:
+    LM.setKind(LengthModifier::AsShort);
+    break;
+
   case BuiltinType::WChar:
   case BuiltinType::Long:
   case BuiltinType::ULong:
@@ -399,8 +411,10 @@ bool PrintfSpecifier::fixType(QualType QT) {
   }
 
   // Set conversion specifier and disable any flags which do not apply to it.
-  if (QT->isAnyCharacterType()) {
+  // Let typedefs to char fall through to int, as %c is silly for uint8_t
+  if (!TypedefType::classof(QT.getTypePtr()) && QT->isAnyCharacterType()) {
     CS.setKind(ConversionSpecifier::cArg);
+    LM.setKind(LengthModifier::None);
     Precision.setHowSpecified(OptionalAmount::NotSpecified);
     HasAlternativeForm = 0;
     HasLeadingZeroes = 0;
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index c4e8687..eaaa204 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -1606,9 +1606,11 @@ CheckPrintfHandler::HandlePrintfSpecifier(const analyze_printf::PrintfSpecifier
     // or 'short' to an 'int'.  This is done because printf is a varargs
     // function.
     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Ex))
-      if (ICE->getType() == S.Context.IntTy)
-        if (ATR.matchesType(S.Context, ICE->getSubExpr()->getType()))
+      if (ICE->getType() == S.Context.IntTy) {
+        Ex = ICE->getSubExpr();
+        if (ATR.matchesType(S.Context, Ex->getType()))
           return true;
+      }
 
     // We may be able to offer a FixItHint if it is a supported type.
     PrintfSpecifier fixedFS = FS;
diff --git a/test/Sema/format-strings.c b/test/Sema/format-strings.c
index 1bfab25..11313d3 100644
--- a/test/Sema/format-strings.c
+++ b/test/Sema/format-strings.c
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral %s
 
 #include <stdarg.h>
+
 typedef __typeof(sizeof(int)) size_t;
 typedef struct _FILE FILE;
 int fprintf(FILE *, const char *restrict, ...);
@@ -174,7 +175,15 @@ void test10(int x, float f, int i, long long lli) {
   printf("%.0Lf", (long double) 1.0); // no-warning
   printf("%c\n", "x"); // expected-warning{{conversion specifies type 'int' but the argument has type 'char *'}}
   printf("%c\n", 1.23); // expected-warning{{conversion specifies type 'int' but the argument has type 'double'}}
-} 
+}
+
+#define unsigned char uint8_t;
+
+void should_understand_small_integers() {
+      printf("%hhu", (short) 10); // expected-warning{{conversion specifies type 'unsigned char' but the argument has type 'short'}}
+      printf("%hu\n", (unsigned char)1); // expected-warning{{conversion specifies type 'unsigned short' but the argument has type 'unsigned char'}}
+      printf("%hu\n", (uint8_t)1); // expected-warning{{conversion specifies type 'unsigned short' but the argument has type 'uint8_t'}}
+}
 
 void test11(void *p, char *s) {
   printf("%p", p); // no-warning
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to