r369791 - [Sema] Don't warn on printf('%hd', [char]) (PR41467)

Nathan Huckleberry via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 23 11:01:57 PDT 2019


Author: nathan-huckleberry
Date: Fri Aug 23 11:01:57 2019
New Revision: 369791

URL: http://llvm.org/viewvc/llvm-project?rev=369791&view=rev
Log:
[Sema] Don't warn on printf('%hd', [char]) (PR41467)

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

Reviewers: rsmith, nickdesaulniers, aaron.ballman, lebedev.ri

Reviewed By: nickdesaulniers, aaron.ballman, lebedev.ri

Subscribers: lebedev.ri, nickdesaulniers, cfe-commits

Tags: #clang

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

Added:
    cfe/trunk/test/Sema/format-strings-pedantic.c
Modified:
    cfe/trunk/lib/AST/FormatString.cpp
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/test/FixIt/format.m
    cfe/trunk/test/Sema/format-strings-enum-fixed-type.cpp
    cfe/trunk/test/Sema/format-strings.c

Modified: cfe/trunk/lib/AST/FormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/FormatString.cpp?rev=369791&r1=369790&r2=369791&view=diff
==============================================================================
--- cfe/trunk/lib/AST/FormatString.cpp (original)
+++ cfe/trunk/lib/AST/FormatString.cpp Fri Aug 23 11:01:57 2019
@@ -386,6 +386,8 @@ ArgType::matchesType(ASTContext &C, Qual
           case BuiltinType::SChar:
           case BuiltinType::Char_U:
           case BuiltinType::UChar:
+            if (T == C.UnsignedShortTy || T == C.ShortTy)
+              return NoMatchPedantic;
             return T == C.UnsignedCharTy || T == C.SignedCharTy ? Match
                                                                 : NoMatch;
           case BuiltinType::Short:

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=369791&r1=369790&r2=369791&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Aug 23 11:01:57 2019
@@ -8119,9 +8119,13 @@ CheckPrintfHandler::checkFormatExpr(cons
       // function.
       if (ICE->getType() == S.Context.IntTy ||
           ICE->getType() == S.Context.UnsignedIntTy) {
-        // All further checking is done on the subexpression.
-        if (AT.matchesType(S.Context, ExprTy))
+        // All further checking is done on the subexpression
+        const analyze_printf::ArgType::MatchKind ImplicitMatch =
+            AT.matchesType(S.Context, ExprTy);
+        if (ImplicitMatch == analyze_printf::ArgType::Match)
           return true;
+        if (ImplicitMatch == analyze_printf::ArgType::NoMatchPedantic)
+          Pedantic = true;
       }
     }
   } else if (const CharacterLiteral *CL = dyn_cast<CharacterLiteral>(E)) {

Modified: cfe/trunk/test/FixIt/format.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/format.m?rev=369791&r1=369790&r2=369791&view=diff
==============================================================================
--- cfe/trunk/test/FixIt/format.m (original)
+++ cfe/trunk/test/FixIt/format.m Fri Aug 23 11:01:57 2019
@@ -205,9 +205,7 @@ void test_percent_C() {
   // 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"

Modified: cfe/trunk/test/Sema/format-strings-enum-fixed-type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings-enum-fixed-type.cpp?rev=369791&r1=369790&r2=369791&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings-enum-fixed-type.cpp (original)
+++ cfe/trunk/test/Sema/format-strings-enum-fixed-type.cpp Fri Aug 23 11:01:57 2019
@@ -79,10 +79,10 @@ void testChar(CharEnum input) {
   printf("%hhd", input); // no-warning
   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'}}
-  
+  // This is not correct, but it is safe. Only warned in pedantic mode because '%hd' shows intent.
+  printf("%hd", input);
+  printf("%hd", CharConstant);
+
   // This is not correct but it matches the promotion rules (and is safe).
   printf("%d", input); // no-warning
   printf("%d", CharConstant); // no-warning

Added: cfe/trunk/test/Sema/format-strings-pedantic.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings-pedantic.c?rev=369791&view=auto
==============================================================================
--- cfe/trunk/test/Sema/format-strings-pedantic.c (added)
+++ cfe/trunk/test/Sema/format-strings-pedantic.c Fri Aug 23 11:01:57 2019
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wformat -Wformat-pedantic -isystem %S/Inputs %s
+
+int printf(const char *restrict, ...);
+
+typedef unsigned char uint8_t;
+
+void print_char_as_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' (aka 'unsigned char')}}
+}

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=369791&r1=369790&r2=369791&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Fri Aug 23 11:01:57 2019
@@ -277,8 +277,8 @@ typedef unsigned char uint8_t;
 
 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); // warning with -Wformat-pedantic only
+  printf("%hu\n", (uint8_t)1);       // warning with -Wformat-pedantic only
 }
 
 void test11(void *p, char *s) {




More information about the cfe-commits mailing list