]> granicus.if.org Git - clang/commitdiff
Format strings: don't ever convert %+d to %lu.
authorJordan Rose <jordan_rose@apple.com>
Thu, 17 Jan 2013 22:34:10 +0000 (22:34 +0000)
committerJordan Rose <jordan_rose@apple.com>
Thu, 17 Jan 2013 22:34:10 +0000 (22:34 +0000)
Presumably, if the printf format has the sign explicitly requested, the user
wants to treat the data as signed.

This is a fix-up for r172739, and also includes several test changes that
didn't make it into that commit.

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

lib/Analysis/PrintfFormatString.cpp
test/FixIt/format.m
test/Index/fix-its.c
test/Misc/caret-diags-macros.c
test/Sema/format-strings-fixit.c

index 5d37de362a41fd95943f5150522a65bbf27db828..8f151b9358e6d1c23e8b864f5fd380e3d826d3e0 100644 (file)
@@ -511,7 +511,7 @@ bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
     case ConversionSpecifier::dArg:
     case ConversionSpecifier::DArg:
     case ConversionSpecifier::iArg:
-      if (QT->isUnsignedIntegerType())
+      if (QT->isUnsignedIntegerType() && !HasPlusPrefix)
         CS.setKind(clang::analyze_format_string::ConversionSpecifier::uArg);
       break;
     default:
index f8ca0e124de7736c7359f38c0e376c2c15fe136c..919212b30678367f5b320c85be1a1cd5002f6fd2 100644 (file)
@@ -223,4 +223,8 @@ void testSignedness(long i, unsigned long u) {
   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:11-[[@LINE-4]]:13}:"%lu"
   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:11-[[@LINE-4]]:13}:"%lu"
   // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:11-[[@LINE-4]]:13}:"%ld"
+
+  printf("%+d", u); // expected-warning{{format specifies type 'int' but the argument has type 'unsigned long'}}
+
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:11-[[@LINE-2]]:14}:"%+ld"
 }
index d5cb1af854cf351b6697e9f2158b41d4170d0e77..1e710c28afccae77aafdea44fb4efa45469041c9 100644 (file)
@@ -22,6 +22,6 @@ int printf(const char *restrict, ...);
 void f2() {
   unsigned long index;
   // CHECK: warning: format specifies type 'int' but the argument has type 'unsigned long'
-  // CHECK: FIX-IT: Replace [26:17 - 26:19] with "%ld"
+  // CHECK: FIX-IT: Replace [26:17 - 26:19] with "%lu"
   MACRO(printf("%d", index));
 }
index 538431a17a0e1bb0a89c062886b0ffa2b0419b5d..316454c513b648d1f72864a5a061c5fc45e14e84 100644 (file)
@@ -218,7 +218,7 @@ Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ", Cstrlen(pKeepBuf));
 // CHECK:         {{.*}}:216:62: warning: format specifies type 'int' but the argument has type 'unsigned long'
 // CHECK-NEXT:    Csprintf(pMsgBuf,"\nEnter minimum anagram length (2-%1d): ", Cstrlen(pKeepBuf));
 // CHECK-NEXT: {{^                                                    ~~~      \^}}
-// CHECK-NEXT: {{^                                                    %1ld}}
+// CHECK-NEXT: {{^                                                    %1lu}}
 // CHECK-NEXT:    {{.*}}:213:21: note: expanded from macro 'Cstrlen'
 // CHECK-NEXT:    #define Cstrlen(a)  strlen_test(a)
 // CHECK-NEXT: {{^                    \^}}
index 15ac71342876446d981821587bfe1db3c1265be2..31274185cbc566f8275f74222b99a37d362bd16d 100644 (file)
@@ -165,7 +165,7 @@ void test2() {
 // Validate the fixes.
 // CHECK: printf("%d", (int) 123);
 // CHECK: printf("abc%s", "testing testing 123");
-// CHECK: printf("%lu", (long) -12);
+// CHECK: printf("%ld", (long) -12);
 // CHECK: printf("%d", 123);
 // CHECK: printf("%s\n", "x");
 // CHECK: printf("%f\n", 1.23);
@@ -193,11 +193,11 @@ void test2() {
 // CHECK: printf("%d", (my_int_type) 42);
 // CHECK: printf("%s", "foo");
 // CHECK: printf("%lo", (long) 42);
-// CHECK: printf("%lu", (long) 42);
+// CHECK: printf("%ld", (long) 42);
 // CHECK: printf("%lx", (long) 42);
 // CHECK: printf("%lX", (long) 42);
-// CHECK: printf("%li", (unsigned long) 42);
-// CHECK: printf("%ld", (unsigned long) 42);
+// CHECK: printf("%lu", (unsigned long) 42);
+// CHECK: printf("%lu", (unsigned long) 42);
 // CHECK: printf("%LF", (long double) 42);
 // CHECK: printf("%Le", (long double) 42);
 // CHECK: printf("%LE", (long double) 42);