[cfe-commits] r142342 - in /cfe/trunk: include/clang/Analysis/Analyses/FormatString.h lib/Analysis/PrintfFormatString.cpp lib/Sema/SemaChecking.cpp test/Sema/format-strings-fixit.c

Hans Wennborg hans at hanshq.net
Tue Oct 18 01:10:06 PDT 2011


Author: hans
Date: Tue Oct 18 03:10:06 2011
New Revision: 142342

URL: http://llvm.org/viewvc/llvm-project?rev=142342&view=rev
Log:
Suggest %zu for size_t args to printf.

For PR11152. Make PrintSpecifier::fixType() suggest "%zu" for size_t, etc.
rather than looking at the underlying type and suggesting "%llu" or other
platform-specific length modifiers. Applies to C99 and C++11.

Modified:
    cfe/trunk/include/clang/Analysis/Analyses/FormatString.h
    cfe/trunk/lib/Analysis/PrintfFormatString.cpp
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/Sema/format-strings-fixit.c

Modified: cfe/trunk/include/clang/Analysis/Analyses/FormatString.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/Analyses/FormatString.h?rev=142342&r1=142341&r2=142342&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/Analyses/FormatString.h (original)
+++ cfe/trunk/include/clang/Analysis/Analyses/FormatString.h Tue Oct 18 03:10:06 2011
@@ -463,7 +463,7 @@
     /// Changes the specifier and length according to a QualType, retaining any
     /// flags or options. Returns true on success, or false when a conversion
     /// was not successful.
-  bool fixType(QualType QT);
+  bool fixType(QualType QT, const LangOptions &LangOpt);
 
   void toString(raw_ostream &os) const;
 

Modified: cfe/trunk/lib/Analysis/PrintfFormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp?rev=142342&r1=142341&r2=142342&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/PrintfFormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/PrintfFormatString.cpp Tue Oct 18 03:10:06 2011
@@ -355,7 +355,7 @@
   return ArgTypeResult();
 }
 
-bool PrintfSpecifier::fixType(QualType QT) {
+bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt) {
   // Handle strings first (char *, wchar_t *)
   if (QT->isPointerType() && (QT->getPointeeType()->isAnyCharacterType())) {
     CS.setKind(ConversionSpecifier::sArg);
@@ -438,6 +438,23 @@
     break;
   }
 
+  // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
+  if (isa<TypedefType>(QT) && (LangOpt.C99 || LangOpt.CPlusPlus0x)) {
+    const IdentifierInfo *Identifier = QT.getBaseTypeIdentifier();
+    if (Identifier->getName() == "size_t") {
+      LM.setKind(LengthModifier::AsSizeT);
+    } else if (Identifier->getName() == "ssize_t") {
+      // Not C99, but common in Unix.
+      LM.setKind(LengthModifier::AsSizeT);
+    } else if (Identifier->getName() == "intmax_t") {
+      LM.setKind(LengthModifier::AsIntMax);
+    } else if (Identifier->getName() == "uintmax_t") {
+      LM.setKind(LengthModifier::AsIntMax);
+    } else if (Identifier->getName() == "ptrdiff_t") {
+      LM.setKind(LengthModifier::AsPtrDiff);
+    }
+  }
+
   // Set conversion specifier and disable any flags which do not apply to it.
   // Let typedefs to char fall through to int, as %c is silly for uint8_t.
   if (isa<TypedefType>(QT) && QT->isAnyCharacterType()) {

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=142342&r1=142341&r2=142342&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Oct 18 03:10:06 2011
@@ -1966,7 +1966,7 @@
 
     // We may be able to offer a FixItHint if it is a supported type.
     PrintfSpecifier fixedFS = FS;
-    bool success = fixedFS.fixType(Ex->getType());
+    bool success = fixedFS.fixType(Ex->getType(), S.getLangOptions());
 
     if (success) {
       // Get the fix string from the fixed format specifier

Modified: cfe/trunk/test/Sema/format-strings-fixit.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings-fixit.c?rev=142342&r1=142341&r2=142342&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings-fixit.c (original)
+++ cfe/trunk/test/Sema/format-strings-fixit.c Tue Oct 18 03:10:06 2011
@@ -46,6 +46,19 @@
   // Perserve the original formatting for unsigned integers.
   unsigned long val = 42;
   printf("%X", val);
+
+  typedef __SIZE_TYPE__ size_t;
+  typedef signed long int ssize_t;
+  typedef __INTMAX_TYPE__ intmax_t;
+  typedef __UINTMAX_TYPE__ uintmax_t;
+  typedef __PTRDIFF_TYPE__ ptrdiff_t;
+
+  // size_t, etc.
+  printf("%c", (size_t) 42);
+  printf("%c", (ssize_t) 42);
+  printf("%c", (intmax_t) 42);
+  printf("%c", (uintmax_t) 42);
+  printf("%c", (ptrdiff_t) 42);
 }
 
 // Validate the fixes...
@@ -68,3 +81,8 @@
 // CHECK: printf("%s", "foo");
 // CHECK: printf("%1$p", (void *)0);
 // CHECK: printf("%lX", val);
+// CHECK: printf("%zu", (size_t) 42);
+// CHECK: printf("%zd", (ssize_t) 42);
+// CHECK: printf("%jd", (intmax_t) 42);
+// CHECK: printf("%ju", (uintmax_t) 42);
+// CHECK: printf("%td", (ptrdiff_t) 42);





More information about the cfe-commits mailing list