r217195 - MS format strings: support the 'w' length modifier (PR20808)

Hans Wennborg hans at hanshq.net
Thu Sep 4 14:39:46 PDT 2014


Author: hans
Date: Thu Sep  4 16:39:46 2014
New Revision: 217195

URL: http://llvm.org/viewvc/llvm-project?rev=217195&view=rev
Log:
MS format strings: support the 'w' length modifier (PR20808)

Modified:
    cfe/trunk/include/clang/Analysis/Analyses/FormatString.h
    cfe/trunk/lib/Analysis/FormatString.cpp
    cfe/trunk/lib/Analysis/PrintfFormatString.cpp
    cfe/trunk/lib/Analysis/ScanfFormatString.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=217195&r1=217194&r2=217195&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/Analyses/FormatString.h (original)
+++ cfe/trunk/include/clang/Analysis/Analyses/FormatString.h Thu Sep  4 16:39:46 2014
@@ -79,6 +79,7 @@ public:
     AsLongDouble, // 'L'
     AsAllocate,   // for '%as', GNU extension to C90 scanf
     AsMAllocate,  // for '%ms', GNU extension to scanf
+    AsWide,       // 'w' (MSVCRT, like l but only for c, C, s, S, or Z
     AsWideChar = AsLong // for '%ls', only makes sense for printf
   };
 

Modified: cfe/trunk/lib/Analysis/FormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/FormatString.cpp?rev=217195&r1=217194&r2=217195&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/FormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/FormatString.cpp Thu Sep  4 16:39:46 2014
@@ -244,6 +244,8 @@ clang::analyze_format_string::ParseLengt
       ++I;
       lmKind = LengthModifier::AsInt3264;
       break;
+    case 'w':
+      lmKind = LengthModifier::AsWide; ++I; break;
   }
   LengthModifier lm(lmPosition, lmKind);
   FS.setLengthModifier(lm);
@@ -504,6 +506,8 @@ analyze_format_string::LengthModifier::t
     return "a";
   case AsMAllocate:
     return "m";
+  case AsWide:
+    return "w";
   case None:
     return "";
   }
@@ -719,6 +723,16 @@ bool FormatSpecifier::hasValidLengthModi
         default:
           return false;
       }
+    case LengthModifier::AsWide:
+      switch (CS.getKind()) {
+        case ConversionSpecifier::cArg:
+        case ConversionSpecifier::CArg:
+        case ConversionSpecifier::sArg:
+        case ConversionSpecifier::SArg: // FIXME: Or Z.
+          return Target.getTriple().isOSMSVCRT();
+        default:
+          return false;
+      }
   }
   llvm_unreachable("Invalid LengthModifier Kind!");
 }
@@ -741,6 +755,7 @@ bool FormatSpecifier::hasStandardLengthM
     case LengthModifier::AsInt32:
     case LengthModifier::AsInt3264:
     case LengthModifier::AsInt64:
+    case LengthModifier::AsWide:
       return false;
   }
   llvm_unreachable("Invalid LengthModifier Kind!");

Modified: cfe/trunk/lib/Analysis/PrintfFormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp?rev=217195&r1=217194&r2=217195&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/PrintfFormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/PrintfFormatString.cpp Thu Sep  4 16:39:46 2014
@@ -268,6 +268,7 @@ ArgType PrintfSpecifier::getArgType(ASTC
     switch (LM.getKind()) {
       case LengthModifier::None: return Ctx.IntTy;
       case LengthModifier::AsLong:
+      case LengthModifier::AsWide:
         return ArgType(ArgType::WIntTy, "wint_t");
       default:
         return ArgType::Invalid();
@@ -303,6 +304,7 @@ ArgType PrintfSpecifier::getArgType(ASTC
         return ArgType(Ctx.getPointerDiffType(), "ptrdiff_t");
       case LengthModifier::AsAllocate:
       case LengthModifier::AsMAllocate:
+      case LengthModifier::AsWide:
         return ArgType::Invalid();
     }
 
@@ -337,6 +339,7 @@ ArgType PrintfSpecifier::getArgType(ASTC
         return ArgType();
       case LengthModifier::AsAllocate:
       case LengthModifier::AsMAllocate:
+      case LengthModifier::AsWide:
         return ArgType::Invalid();
     }
 
@@ -372,6 +375,7 @@ ArgType PrintfSpecifier::getArgType(ASTC
       case LengthModifier::AsInt32:
       case LengthModifier::AsInt3264:
       case LengthModifier::AsInt64:
+      case LengthModifier::AsWide:
         return ArgType::Invalid();
     }
   }
@@ -384,6 +388,8 @@ ArgType PrintfSpecifier::getArgType(ASTC
                          "const unichar *");
         return ArgType(ArgType::WCStrTy, "wchar_t *");
       }
+      if (LM.getKind() == LengthModifier::AsWide)
+        return ArgType(ArgType::WCStrTy, "wchar_t *");
       return ArgType::CStrTy;
     case ConversionSpecifier::SArg:
       if (IsObjCLiteral)

Modified: cfe/trunk/lib/Analysis/ScanfFormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ScanfFormatString.cpp?rev=217195&r1=217194&r2=217195&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ScanfFormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/ScanfFormatString.cpp Thu Sep  4 16:39:46 2014
@@ -257,6 +257,7 @@ ArgType ScanfSpecifier::getArgType(ASTCo
         case LengthModifier::AsMAllocate:
         case LengthModifier::AsInt32:
         case LengthModifier::AsInt3264:
+        case LengthModifier::AsWide:
           return ArgType::Invalid();
       }
 
@@ -295,6 +296,7 @@ ArgType ScanfSpecifier::getArgType(ASTCo
         case LengthModifier::AsMAllocate:
         case LengthModifier::AsInt32:
         case LengthModifier::AsInt3264:
+        case LengthModifier::AsWide:
           return ArgType::Invalid();
       }
 
@@ -326,6 +328,7 @@ ArgType ScanfSpecifier::getArgType(ASTCo
         case LengthModifier::None:
           return ArgType::PtrTo(ArgType::AnyCharTy);
         case LengthModifier::AsLong:
+        case LengthModifier::AsWide:
           return ArgType::PtrTo(ArgType(Ctx.getWideCharType(), "wchar_t"));
         case LengthModifier::AsAllocate:
         case LengthModifier::AsMAllocate:
@@ -338,6 +341,7 @@ ArgType ScanfSpecifier::getArgType(ASTCo
       // FIXME: Mac OS X specific?
       switch (LM.getKind()) {
         case LengthModifier::None:
+        case LengthModifier::AsWide:
           return ArgType::PtrTo(ArgType(Ctx.getWideCharType(), "wchar_t"));
         case LengthModifier::AsAllocate:
         case LengthModifier::AsMAllocate:
@@ -378,6 +382,7 @@ ArgType ScanfSpecifier::getArgType(ASTCo
         case LengthModifier::AsMAllocate:
         case LengthModifier::AsInt32:
         case LengthModifier::AsInt3264:
+        case LengthModifier::AsWide:
           return ArgType::Invalid();
         }
 

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=217195&r1=217194&r2=217195&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings-ms.c (original)
+++ cfe/trunk/test/Sema/format-strings-ms.c Thu Sep  4 16:39:46 2014
@@ -1,25 +1,66 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -fms-compatibility -triple=i386-pc-win32 -Wformat-non-iso %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fms-compatibility -triple=i386-pc-win32 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -fms-compatibility -triple=i386-pc-win32 -Wformat-non-iso -DNON_ISO_WARNING %s
 
 int printf(const char *format, ...) __attribute__((format(printf, 1, 2)));
+int scanf(const char * restrict, ...) ;
+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) {
+  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}}
+}
+
+#else
 
 void signed_test() {
   short val = 30;
-  printf("val = %I64d\n", val); // expected-warning{{'I64' length modifier is not supported by ISO C}} \
-                                // expected-warning{{format specifies type '__int64' (aka 'long long') but the argument has type 'short'}}
+  printf("val = %I64d\n", val); // expected-warning{{format specifies type '__int64' (aka 'long long') but the argument has type 'short'}}
   long long bigval = 30;
-  printf("val = %I32d\n", bigval); // expected-warning{{'I32' length modifier is not supported by ISO C}} \
-                                   // expected-warning{{format specifies type '__int32' (aka 'int') but the argument has type 'long long'}}
-  printf("val = %Id\n", bigval); // expected-warning{{'I' length modifier is not supported by ISO C}} \
-                                 // expected-warning{{format specifies type '__int32' (aka 'int') but the argument has type 'long long'}}
+  printf("val = %I32d\n", bigval); // expected-warning{{format specifies type '__int32' (aka 'int') but the argument has type 'long long'}}
+  printf("val = %Id\n", bigval); // expected-warning{{format specifies type '__int32' (aka 'int') but the argument has type 'long long'}}
 }
 
 void unsigned_test() {
   unsigned short val = 30;
-  printf("val = %I64u\n", val); // expected-warning{{'I64' length modifier is not supported by ISO C}} \
-                                // expected-warning{{format specifies type 'unsigned __int64' (aka 'unsigned long long') but the argument has type 'unsigned short'}}
+  printf("val = %I64u\n", val); // expected-warning{{format specifies type 'unsigned __int64' (aka 'unsigned long long') but the argument has type 'unsigned short'}}
   unsigned long long bigval = 30;
-  printf("val = %I32u\n", bigval); // expected-warning{{'I32' length modifier is not supported by ISO C}} \
-                                   // expected-warning{{format specifies type 'unsigned __int32' (aka 'unsigned int') but the argument has type 'unsigned long long'}}
-  printf("val = %Iu\n", bigval); // expected-warning{{'I' length modifier is not supported by ISO C}} \
-                                 // expected-warning{{format specifies type 'unsigned __int32' (aka 'unsigned int') but the argument has type 'unsigned long long'}}
+  printf("val = %I32u\n", bigval); // expected-warning{{format specifies type 'unsigned __int32' (aka 'unsigned int') but the argument has type 'unsigned long long'}}
+  printf("val = %Iu\n", bigval); // expected-warning{{format specifies type 'unsigned __int32' (aka 'unsigned int') but the argument has type 'unsigned long long'}}
 }
+
+void w_test(wchar_t c, wchar_t *s) {
+  printf("%wc", c);
+  printf("%wC", c);
+  printf("%C", c);
+  printf("%ws", s);
+  printf("%wS", s);
+  printf("%S", s);
+  scanf("%wc", &c);
+  scanf("%wC", &c);
+  scanf("%C", &c);
+  scanf("%ws", s);
+  scanf("%wS", s);
+  scanf("%S", s);
+
+  double bad;
+  printf("%wc", bad); // expected-warning{{format specifies type 'wint_t' (aka 'int') but the argument has type 'double'}}
+  printf("%wC", bad); // expected-warning{{format specifies type 'wchar_t' (aka 'unsigned short') but the argument has type 'double'}}
+  printf("%C", bad); // expected-warning{{format specifies type 'wchar_t' (aka 'unsigned short') but the argument has type 'double'}}
+  printf("%ws", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
+  printf("%wS", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
+  printf("%S", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
+  scanf("%wc", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
+  scanf("%wC", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
+  scanf("%C", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
+  scanf("%ws", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
+  scanf("%wS", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
+  scanf("%S", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
+
+}
+
+#endif





More information about the cfe-commits mailing list