[PATCH] D66186: Fix warning on printf('%hd', [char])

Nathan Huckleberry via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 13 16:13:53 PDT 2019


Nathan-Huckleberry created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Nathan-Huckleberry added a reviewer: rsmith.
Nathan-Huckleberry added a subscriber: nickdesaulniers.

Link: https://bugs.llvm.org/show_bug.cgi?id=41467


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66186

Files:
  clang/lib/AST/FormatString.cpp
  clang/test/FixIt/format.m
  clang/test/Sema/format-strings-enum-fixed-type.cpp
  clang/test/Sema/format-strings.c


Index: clang/test/Sema/format-strings.c
===================================================================
--- clang/test/Sema/format-strings.c
+++ clang/test/Sema/format-strings.c
@@ -277,8 +277,8 @@
 
 void should_understand_small_integers() {
   printf("%hhu", (short) 10); // expected-warning{{format specifies type 'unsigned char' but the argument has type 'short'}}
-  printf("%hu\n", (unsigned char) 1); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'unsigned char'}}
-  printf("%hu\n", (uint8_t)1); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'uint8_t'}}
+  printf("%hu\n", (unsigned char)1);
+  printf("%hu\n", (uint8_t)1);
 }
 
 void test11(void *p, char *s) {
Index: clang/test/Sema/format-strings-enum-fixed-type.cpp
===================================================================
--- clang/test/Sema/format-strings-enum-fixed-type.cpp
+++ clang/test/Sema/format-strings-enum-fixed-type.cpp
@@ -80,9 +80,9 @@
   printf("%hhd", CharConstant); // no-warning
 
   // This is not correct but it is safe. We warn because '%hd' shows intent.
-  printf("%hd", input); // expected-warning{{format specifies type 'short' but the argument has underlying type 'char'}}
-  printf("%hd", CharConstant); // expected-warning{{format specifies type 'short'}}
-  
+  printf("%hd", input);        // no-warning
+  printf("%hd", CharConstant); // no-warning
+
   // This is not correct but it matches the promotion rules (and is safe).
   printf("%d", input); // no-warning
   printf("%d", CharConstant); // no-warning
Index: clang/test/FixIt/format.m
===================================================================
--- clang/test/FixIt/format.m
+++ clang/test/FixIt/format.m
@@ -205,9 +205,7 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%f"
   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"(unichar)"
 
-  NSLog(@"%C", (char)0x260300); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'char'}}
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"
-  // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:22}:"(unichar)"
+  NSLog(@"%C", (char)0x260300);
 
   NSLog(@"%C", 'a'); // expected-warning{{format specifies type 'unichar' (aka 'unsigned short') but the argument has type 'char'}}
   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:13}:"%c"
Index: clang/lib/AST/FormatString.cpp
===================================================================
--- clang/lib/AST/FormatString.cpp
+++ clang/lib/AST/FormatString.cpp
@@ -386,8 +386,10 @@
           case BuiltinType::SChar:
           case BuiltinType::Char_U:
           case BuiltinType::UChar:
-            return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
-                                                                : NoMatch;
+            return T == C.UnsignedCharTy || T == C.SignedCharTy ||
+                           T == C.UnsignedShortTy || T == C.ShortTy
+                       ? Match
+                       : NoMatch;
           case BuiltinType::Short:
             return T == C.UnsignedShortTy ? Match : NoMatch;
           case BuiltinType::UShort:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66186.214965.patch
Type: text/x-patch
Size: 3240 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190813/f293d3a1/attachment.bin>


More information about the cfe-commits mailing list