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

Ted Kremenek kremenek at apple.com
Fri Jan 29 15:32:22 PST 2010


Author: kremenek
Date: Fri Jan 29 17:32:22 2010
New Revision: 94856

URL: http://llvm.org/viewvc/llvm-project?rev=94856&view=rev
Log:
Be a little more permissive than C99: allow 'unsigned' to be used for
the field width and precision of a format specifier instead of just
'int'.  This matches GCC, and fixes <rdar://problem/6079850>.

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=94856&r1=94855&r2=94856&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Jan 29 17:32:22 2010
@@ -1151,10 +1151,14 @@
       }
       
       // Type check the data argument.  It should be an 'int'.
+      // Although not in conformance with C99, we also allow the argument to be
+      // an 'unsigned int' as that is a reasonably safe case.  GCC also
+      // doesn't emit a warning for that case.
       const Expr *Arg = getDataArg(NumConversions);
       QualType T = Arg->getType();
-      const BuiltinType *BT = T->getAs<BuiltinType>();
-      if (!BT || BT->getKind() != BuiltinType::Int) {
+      const BuiltinType *BT = T->getAs<BuiltinType>();            
+      if (!BT || (BT->getKind() != BuiltinType::Int &&
+                  BT->getKind() != BuiltinType::UInt)) {
         S.Diag(getLocationOfByte(Amt.getStart()), BadTypeDiag)
           << T
 		  << getFormatSpecifierRange(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=94856&r1=94855&r2=94856&view=diff

==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Fri Jan 29 17:32:22 2010
@@ -40,11 +40,15 @@
 
   // rdar://6079877
   printf("abc"
-         "%*d", (unsigned) 1, 1); // expected-warning {{field width should have type 'int'}}
+         "%*d", 1, 1); // no-warning
   printf("abc\
 def"
-         "%*d", (unsigned) 1, 1); // expected-warning {{field width should have type 'int'}}
-  
+         "%*d", 1, 1); // no-warning
+         
+  // <rdar://problem/6079850>, allow 'unsigned' (instead of 'int') to be used for both
+  // the field width and precision.  This deviates from C99, but is reasonably safe
+  // and is also accepted by GCC.
+  printf("%*d", (unsigned) 1, 1); // no-warning  
 }
 
 void check_conditional_literal(const char* s, int i) {
@@ -137,6 +141,7 @@
 
 void torture(va_list v8) {
   vprintf ("%*.*d", v8);  // no-warning
+  
 }
 
 void test10(int x, float f, int i) {





More information about the cfe-commits mailing list