[cfe-commits] r97482 - in /cfe/trunk: lib/Analysis/PrintfFormatString.cpp test/Sema/format-strings.c

Ted Kremenek kremenek at apple.com
Mon Mar 1 11:22:34 PST 2010


Author: kremenek
Date: Mon Mar  1 13:22:33 2010
New Revision: 97482

URL: http://llvm.org/viewvc/llvm-project?rev=97482&view=rev
Log:
Allow a '0' precision in format strings (as the man page says it is okay).
Fixes <rdar://problem/7700339>.

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

Modified: cfe/trunk/lib/Analysis/PrintfFormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp?rev=97482&r1=97481&r2=97482&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/PrintfFormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/PrintfFormatString.cpp Mon Mar  1 13:22:33 2010
@@ -69,18 +69,17 @@
   UpdateOnReturn <const char*> UpdateBeg(Beg, I);
 
   unsigned accumulator = 0;
+  bool hasDigits = false;
 
   for ( ; I != E; ++I) {
     char c = *I;
     if (c >= '0' && c <= '9') {
-      // Ignore '0' on the first character.
-      if (c == '0' && I == Beg)
-        break;
+      hasDigits = true;
       accumulator += (accumulator * 10) + (c - '0');
       continue;
     }
 
-    if (accumulator)
+    if (hasDigits)
       return OptionalAmount(OptionalAmount::Constant, accumulator, Beg);
 
     break;
@@ -118,9 +117,18 @@
       return OptionalAmount(false);
     }
 
+    assert(Amt.getHowSpecified() == OptionalAmount::Constant);
+
     if (*I == '$') {
+      // Special case: '*0$', since this is an easy mistake.
+      if (Amt.getConstantAmount() == 0) {
+        H.HandleZeroPosition(Beg, I - Beg + 1);
+        return OptionalAmount(false);
+      }
+
       const char *Tmp = Beg;
       Beg = ++I;
+
       return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
                             Tmp);
     }
@@ -182,6 +190,12 @@
   }
 
   if (Amt.getHowSpecified() == OptionalAmount::Constant && *(I++) == '$') {
+    // Special case: '%0$', since this is an easy mistake.
+    if (Amt.getConstantAmount() == 0) {
+      H.HandleZeroPosition(Start, I - Start);
+      return true;
+    }
+
     FS.setArgIndex(Amt.getConstantAmount() - 1);
     FS.setUsesPositionalArg();
     // Update the caller's pointer if we decided to consume
@@ -190,12 +204,6 @@
     return false;
   }
 
-  // Special case: '%0$', since this is an easy mistake.
-  if (*I == '0' && (I+1) != E && *(I+1) == '$') {
-    H.HandleZeroPosition(Start, I - Start + 2);
-    return true;
-  }
-
   return false;
 }
 

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=97482&r1=97481&r2=97482&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Mon Mar  1 13:22:33 2010
@@ -170,6 +170,8 @@
   printf("%d", (long long) 10); // expected-warning{{conversion specifies type 'int' but the argument has type 'long long'}}
   printf("%Lf\n", (long double) 1.0); // no-warning
   printf("%f\n", (long double) 1.0); // expected-warning{{conversion specifies type 'double' but the argument has type 'long double'}}
+  // The man page says that a zero precision is okay.
+  printf("%.0Lf", (long double) 1.0); // no-warning
 } 
 
 void test11(void *p, char *s) {
@@ -227,6 +229,7 @@
 // FIXME: This is probably not portable everywhere.
 void test_positional_arguments() {
   printf("%0$", (int)2); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
+  printf("%1$*0$d", (int) 2); // expected-warning{{position arguments in format strings start counting at 1 (not 0)}}
   printf("%1$d", (int) 2); // no-warning
   printf("%1$d", (int) 2, 2); // expected-warning{{data argument not used by format string}}
   printf("%1$d%1$f", (int) 2); // expected-warning{{conversion specifies type 'double' but the argument has type 'int'}}





More information about the cfe-commits mailing list