From cdbe1e0d85d7d32452dd1c52758d7bfaa1c0663b Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Thu, 17 Jan 2013 22:34:10 +0000 Subject: [PATCH] Format strings: don't ever convert %+d to %lu. 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 | 2 +- test/FixIt/format.m | 4 ++++ test/Index/fix-its.c | 2 +- test/Misc/caret-diags-macros.c | 2 +- test/Sema/format-strings-fixit.c | 8 ++++---- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/Analysis/PrintfFormatString.cpp b/lib/Analysis/PrintfFormatString.cpp index 5d37de362a..8f151b9358 100644 --- a/lib/Analysis/PrintfFormatString.cpp +++ b/lib/Analysis/PrintfFormatString.cpp @@ -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: diff --git a/test/FixIt/format.m b/test/FixIt/format.m index f8ca0e124d..919212b306 100644 --- a/test/FixIt/format.m +++ b/test/FixIt/format.m @@ -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" } diff --git a/test/Index/fix-its.c b/test/Index/fix-its.c index d5cb1af854..1e710c28af 100644 --- a/test/Index/fix-its.c +++ b/test/Index/fix-its.c @@ -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)); } diff --git a/test/Misc/caret-diags-macros.c b/test/Misc/caret-diags-macros.c index 538431a17a..316454c513 100644 --- a/test/Misc/caret-diags-macros.c +++ b/test/Misc/caret-diags-macros.c @@ -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: {{^ \^}} diff --git a/test/Sema/format-strings-fixit.c b/test/Sema/format-strings-fixit.c index 15ac713428..31274185cb 100644 --- a/test/Sema/format-strings-fixit.c +++ b/test/Sema/format-strings-fixit.c @@ -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); -- 2.40.0