[PATCH] D35427: [clang] Fix handling of "%zd" format specifier

Alexander Shaposhnikov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 14 11:13:47 PDT 2017


alexshap created this revision.

This diff addresses FIXME in lib/Analysis/PrintfFormatString.cpp
and makes Clang warn on incorrect using of "%zd" format specifier.

Test plan: make check-all


Repository:
  rL LLVM

https://reviews.llvm.org/D35427

Files:
  lib/Analysis/PrintfFormatString.cpp
  test/FixIt/format.m


Index: test/FixIt/format.m
===================================================================
--- test/FixIt/format.m
+++ test/FixIt/format.m
@@ -229,6 +229,13 @@
   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:11-[[@LINE-2]]:14}:"%+ld"
 }
 
+void testSizeTypes() {
+  printf("%zu", 0.f); // expected-warning{{format specifies type 'size_t' (aka 'unsigned long') but the argument has type 'float'}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%f"
+  printf("%zd", 0.f); // expected-warning{{format specifies type 'ssize_t' (aka 'long') but the argument has type 'float'}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%f"
+}
+
 void testEnum() {
   typedef enum {
     ImplicitA = 1,
Index: lib/Analysis/PrintfFormatString.cpp
===================================================================
--- lib/Analysis/PrintfFormatString.cpp
+++ lib/Analysis/PrintfFormatString.cpp
@@ -466,8 +466,7 @@
       case LengthModifier::AsIntMax:
         return ArgType(Ctx.getIntMaxType(), "intmax_t");
       case LengthModifier::AsSizeT:
-        // FIXME: How to get the corresponding signed version of size_t?
-        return ArgType();
+        return ArgType(Ctx.getSignedSizeType(), "ssize_t");
       case LengthModifier::AsInt3264:
         return Ctx.getTargetInfo().getTriple().isArch64Bit()
                    ? ArgType(Ctx.LongLongTy, "__int64")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35427.106672.patch
Type: text/x-patch
Size: 1377 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170714/48a33603/attachment.bin>


More information about the cfe-commits mailing list