r217326 - MS format strings: parse the 'Z' printf conversion specifier (PR20808)

Hans Wennborg hans at hanshq.net
Sat Sep 6 20:03:51 PDT 2014


Author: hans
Date: Sat Sep  6 22:03:51 2014
New Revision: 217326

URL: http://llvm.org/viewvc/llvm-project?rev=217326&view=rev
Log:
MS format strings: parse the 'Z' printf conversion specifier (PR20808)

Modified:
    cfe/trunk/include/clang/Analysis/Analyses/FormatString.h
    cfe/trunk/lib/Analysis/FormatString.cpp
    cfe/trunk/lib/Analysis/PrintfFormatString.cpp
    cfe/trunk/test/Sema/format-strings-ms.c

Modified: cfe/trunk/include/clang/Analysis/Analyses/FormatString.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/Analyses/FormatString.h?rev=217326&r1=217325&r2=217326&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/Analyses/FormatString.h (original)
+++ cfe/trunk/include/clang/Analysis/Analyses/FormatString.h Sat Sep  6 22:03:51 2014
@@ -155,6 +155,8 @@ public:
 
     // ** Printf-specific **
 
+    ZArg, // MS extension
+
     // Objective-C specific specifiers.
     ObjCObjArg,  // '@'
     ObjCBeg = ObjCObjArg, ObjCEnd = ObjCObjArg,

Modified: cfe/trunk/lib/Analysis/FormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/FormatString.cpp?rev=217326&r1=217325&r2=217326&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/FormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/FormatString.cpp Sat Sep  6 22:03:51 2014
@@ -554,6 +554,9 @@ const char *ConversionSpecifier::toStrin
 
   // GlibC specific specifiers.
   case PrintErrno: return "m";
+
+  // MS specific specifiers.
+  case ZArg: return "Z";
   }
   return nullptr;
 }
@@ -619,6 +622,7 @@ bool FormatSpecifier::hasValidLengthModi
           case ConversionSpecifier::CArg:
           case ConversionSpecifier::sArg:
           case ConversionSpecifier::SArg:
+          case ConversionSpecifier::ZArg:
             return true;
           default:
             break;
@@ -671,6 +675,7 @@ bool FormatSpecifier::hasValidLengthModi
         case ConversionSpecifier::cArg:
         case ConversionSpecifier::sArg:
         case ConversionSpecifier::ScanListArg:
+        case ConversionSpecifier::ZArg:
           return true;
         default:
           return false;
@@ -740,7 +745,8 @@ bool FormatSpecifier::hasValidLengthModi
         case ConversionSpecifier::cArg:
         case ConversionSpecifier::CArg:
         case ConversionSpecifier::sArg:
-        case ConversionSpecifier::SArg: // FIXME: Or Z.
+        case ConversionSpecifier::SArg:
+        case ConversionSpecifier::ZArg:
           return Target.getTriple().isOSMSVCRT();
         default:
           return false;
@@ -805,6 +811,7 @@ bool FormatSpecifier::hasStandardConvers
     case ConversionSpecifier::DArg:
     case ConversionSpecifier::OArg:
     case ConversionSpecifier::UArg:
+    case ConversionSpecifier::ZArg:
       return false;
   }
   llvm_unreachable("Invalid ConversionSpecifier Kind!");

Modified: cfe/trunk/lib/Analysis/PrintfFormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp?rev=217326&r1=217325&r2=217326&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/PrintfFormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/PrintfFormatString.cpp Sat Sep  6 22:03:51 2014
@@ -198,7 +198,7 @@ static PrintfSpecifierResult ParsePrintf
     case '@': k = ConversionSpecifier::ObjCObjArg; break;
     // Glibc specific.
     case 'm': k = ConversionSpecifier::PrintErrno; break;
-    // Apple-specific
+    // Apple-specific.
     case 'D':
       if (Target.getTriple().isOSDarwin())
         k = ConversionSpecifier::DArg;
@@ -211,6 +211,10 @@ static PrintfSpecifierResult ParsePrintf
       if (Target.getTriple().isOSDarwin())
         k = ConversionSpecifier::UArg;
       break;
+    // MS specific.
+    case 'Z':
+      if (Target.getTriple().isOSMSVCRT())
+        k = ConversionSpecifier::ZArg;
   }
   PrintfConversionSpecifier CS(conversionPosition, k);
   FS.setConversionSpecifier(CS);

Modified: cfe/trunk/test/Sema/format-strings-ms.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings-ms.c?rev=217326&r1=217325&r2=217326&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings-ms.c (original)
+++ cfe/trunk/test/Sema/format-strings-ms.c Sat Sep  6 22:03:51 2014
@@ -8,11 +8,12 @@ typedef unsigned short wchar_t;
 #ifdef NON_ISO_WARNING
 
 // Split off this test to reduce the warning noise in the rest of the file.
-void non_iso_warning_test(__int32 i32, __int64 i64, wchar_t c) {
+void non_iso_warning_test(__int32 i32, __int64 i64, wchar_t c, void *p) {
   printf("%Id", i32); // expected-warning{{'I' length modifier is not supported by ISO C}}
   printf("%I32d", i32); // expected-warning{{'I32' length modifier is not supported by ISO C}}
   printf("%I64d", i64); // expected-warning{{'I64' length modifier is not supported by ISO C}}
   printf("%wc", c); // expected-warning{{'w' length modifier is not supported by ISO C}}
+  printf("%Z", p); // expected-warning{{'Z' conversion specifier is not supported by ISO C}}
 }
 
 #else
@@ -75,4 +76,13 @@ void h_test(char c, char* s) {
   scanf("%hS", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
 }
 
+void z_test(void *p) {
+  printf("%Z", p);
+  printf("%hZ", p);
+  printf("%lZ", p);
+  printf("%wZ", p);
+  printf("%hhZ", p); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 'Z' conversion specifier}}
+  scanf("%Z", p); // expected-warning{{invalid conversion specifier 'Z'}}
+}
+
 #endif





More information about the cfe-commits mailing list