[Sema] -Wformat: recognize %lb for the printf/scanf family of functions
authorFangrui Song <i@maskray.me>
Thu, 20 Apr 2023 16:34:34 +0000 (09:34 -0700)
committerFangrui Song <i@maskray.me>
Thu, 20 Apr 2023 16:34:34 +0000 (09:34 -0700)
Fix https://github.com/llvm/llvm-project/issues/62247

D131057 added `bArg` and `BArg` in the `AsLongLong` label in
`FormatSpecifier::hasValidLengthModifier`, but missed the `AsLong` label,
therefore `%llb` is allowed while `%lb` (e.g. `printf("%lb", (long)10)`) has a
spurious warning. Add the missing case labels.

Reviewed By: aaron.ballman, enh

Differential Revision: https://reviews.llvm.org/D148779

clang/docs/ReleaseNotes.rst
clang/lib/AST/FormatString.cpp
clang/test/Sema/format-strings-fixit.c
clang/test/Sema/format-strings.c

index febd47c1ef3fcfa01a0251245981e1ce9a5e6d61..7beae03247796091531be1fad4d0338ffc8c5037 100644 (file)
@@ -228,6 +228,9 @@ Improvements to Clang's diagnostics
 - Clang's "static assertion failed" diagnostic now points to the static assertion
   expression instead of pointing to the ``static_assert`` token.
   (`#61951 <https://github.com/llvm/llvm-project/issues/61951>`_)
+- ``-Wformat`` now recognizes ``%lb`` for the ``printf``/``scanf`` family of
+  functions.
+  (`#62247: <https://github.com/llvm/llvm-project/issues/62247>`_).
 
 Bug Fixes in This Version
 -------------------------
index c7dee2d421bbe1f62aaefa94dfd71f9b8140bd51..d42e4ea2ea083bdf3f579c0cf9183534cf973a87 100644 (file)
@@ -848,6 +848,8 @@ bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target,
       }
 
       switch (CS.getKind()) {
+        case ConversionSpecifier::bArg:
+        case ConversionSpecifier::BArg:
         case ConversionSpecifier::dArg:
         case ConversionSpecifier::DArg:
         case ConversionSpecifier::iArg:
index 64614e1c53fcdfff5c0539bac953be43ed485cfc..5e37ec76fed21dea2081c1b4e2400f10698c9a6f 100644 (file)
@@ -22,6 +22,7 @@ void test(void) {
   printf("abc%0f", "testing testing 123");
   printf("%u", (long) -12);
   printf("%b", (long) -13);
+  printf("%d", (long) -14);
   printf("%p", 123);
   printf("%c\n", "x");
   printf("%c\n", 1.23);
@@ -162,6 +163,7 @@ void test2(int intSAParm[static 2]) {
 
   // Preserve the original formatting.
   scanf("%b", &longVar);
+  scanf("%d", &longVar);
   scanf("%o", &longVar);
   scanf("%u", &longVar);
   scanf("%x", &longVar);
@@ -181,7 +183,8 @@ void test2(int intSAParm[static 2]) {
 // CHECK: printf("%d", (int) 123);
 // CHECK: printf("abc%s", "testing testing 123");
 // CHECK: printf("%ld", (long) -12);
-// CHECK: printf("%ld", (long) -13);
+// CHECK: printf("%lb", (long) -13);
+// CHECK: printf("%ld", (long) -14);
 // CHECK: printf("%d", 123);
 // CHECK: printf("%s\n", "x");
 // CHECK: printf("%f\n", 1.23);
@@ -249,6 +252,7 @@ void test2(int intSAParm[static 2]) {
 // CHECK: scanf("%ju", (my_uintmax_type*)&uIntmaxVar);
 // CHECK: scanf("%td", (my_ptrdiff_type*)&ptrdiffVar);
 // CHECK: scanf("%d", (my_int_type*)&intVar);
+// CHECK: scanf("%lb", &longVar);
 // CHECK: scanf("%ld", &longVar);
 // CHECK: scanf("%lo", &longVar);
 // CHECK: scanf("%lu", &longVar);
index 36dd88cea946a12fe71f69b3f9b6046f0bef2707..56d3056d55756db20c0e4c5ad0348576e0c0bb79 100644 (file)
@@ -304,6 +304,7 @@ void test10(int x, float f, int i, long long lli) {
   printf("%qp", (void *)0); // expected-warning{{length modifier 'q' results in undefined behavior or no effect with 'p' conversion specifier}}
   printf("hhX %hhX", (unsigned char)10); // no-warning
   printf("llX %llX", (long long) 10); // no-warning
+  printf("%lb %lB", (long) 10, (long) 10); // no-warning
   printf("%llb %llB", (long long) 10, (long long) 10); // no-warning
   // This is fine, because there is an implicit conversion to an int.
   printf("%d", (unsigned char) 10); // no-warning