[PATCH] D66856: [Sema] Suppress -Wformat diagnostics for bool types when printed using %hhd

Erik Pilkington via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 27 18:20:22 PDT 2019


erik.pilkington created this revision.
erik.pilkington added reviewers: aaron.ballman, Nathan-Huckleberry.
Herald added subscribers: ributzka, dexonsmith, jkorous.
Herald added a project: clang.

Instead, emit them under -Wformat-pedantic. This is particularly important for us because we have a lot of code that uses `%hhd` to print BOOL values, since on macOS BOOL is a typedef for signed char, and we emit fix-its recommending `%hhd` to print it. Now that this code is compiled on a target where BOOL is a typedef for `_Bool`, we're getting these -Wformat warnings. These aren't all that useful.

rdar://problem/54579473 Clang should not warn when using %hhd or %hd format specifiers on BOOL values


Repository:
  rC Clang

https://reviews.llvm.org/D66856

Files:
  clang/lib/AST/FormatString.cpp
  clang/test/Sema/format-bool.c


Index: clang/test/Sema/format-bool.c
===================================================================
--- /dev/null
+++ clang/test/Sema/format-bool.c
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -xc %s -verify -DBOOL=_Bool
+// RUN: %clang_cc1 -xc++ %s -verify -DBOOL=bool
+// RUN: %clang_cc1 -xc %s -verify -DBOOL=_Bool -Wformat-pedantic -DPEDANTIC
+// RUN: %clang_cc1 -xc++ %s -verify -DBOOL=bool -Wformat-pedantic -DPEDANTIC
+
+__attribute__((format(__printf__, 1, 2)))
+int p(const char *fmt, ...);
+
+BOOL b;
+
+int main() {
+  p("%d", b);
+  p("%hd", b);
+  p("%hhd", b);
+#ifdef PEDANTIC
+  // expected-warning at -3 {{format specifies type 'short' but the argument has type}}
+  // expected-warning at -3 {{format specifies type 'char' but the argument has type}}
+#endif
+  p("%u", b);
+  p("%hu", b);
+  p("%hhu", b);
+#ifdef PEDANTIC
+  // expected-warning at -3 {{format specifies type 'unsigned short' but the argument has type}}
+  // expected-warning at -3 {{format specifies type 'unsigned char' but the argument has type}}
+#endif
+  p("%c", b);
+  p("%f", b); // expected-warning{{format specifies type 'double' but the argument has type}}
+  p("%ld", b); // expected-warning{{format specifies type 'long' but the argument has type}}
+}
Index: clang/lib/AST/FormatString.cpp
===================================================================
--- clang/lib/AST/FormatString.cpp
+++ clang/lib/AST/FormatString.cpp
@@ -360,7 +360,9 @@
           case BuiltinType::UChar:
           case BuiltinType::Char_U:
             return Match;
-        }
+          case BuiltinType::Bool:
+            return NoMatchPedantic;
+          }
       return NoMatch;
     }
 
@@ -390,6 +392,11 @@
               return NoMatchPedantic;
             return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
                                                                 : NoMatch;
+          case BuiltinType::Bool:
+            if (T == C.UnsignedShortTy || T == C.ShortTy ||
+                T == C.UnsignedCharTy || T == C.SignedCharTy)
+              return NoMatchPedantic;
+            return NoMatch;
           case BuiltinType::Short:
             return T == C.UnsignedShortTy ? Match : NoMatch;
           case BuiltinType::UShort:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66856.217553.patch
Type: text/x-patch
Size: 2232 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190828/8836263e/attachment.bin>


More information about the cfe-commits mailing list