r217196 - MS format strings: allow the 'h' length modifier with C, C, s and S (PR20808)

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


Author: hans
Date: Thu Sep  4 16:39:52 2014
New Revision: 217196

URL: http://llvm.org/viewvc/llvm-project?rev=217196&view=rev
Log:
MS format strings: allow the 'h' length modifier with C, C, s and S (PR20808)

Modified:
    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/lib/Analysis/FormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/FormatString.cpp?rev=217196&r1=217195&r2=217196&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/FormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/FormatString.cpp Thu Sep  4 16:39:52 2014
@@ -612,8 +612,20 @@ bool FormatSpecifier::hasValidLengthModi
       return true;
       
     // Handle most integer flags
-    case LengthModifier::AsChar:
     case LengthModifier::AsShort:
+      if (Target.getTriple().isOSMSVCRT()) {
+        switch (CS.getKind()) {
+          case ConversionSpecifier::cArg:
+          case ConversionSpecifier::CArg:
+          case ConversionSpecifier::sArg:
+          case ConversionSpecifier::SArg:
+            return true;
+          default:
+            break;
+        }
+      }
+      // Fall through.
+    case LengthModifier::AsChar:
     case LengthModifier::AsLongLong:
     case LengthModifier::AsQuad:
     case LengthModifier::AsIntMax:

Modified: cfe/trunk/lib/Analysis/PrintfFormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/PrintfFormatString.cpp?rev=217196&r1=217195&r2=217196&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/PrintfFormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/PrintfFormatString.cpp Thu Sep  4 16:39:52 2014
@@ -266,10 +266,14 @@ ArgType PrintfSpecifier::getArgType(ASTC
 
   if (CS.getKind() == ConversionSpecifier::cArg)
     switch (LM.getKind()) {
-      case LengthModifier::None: return Ctx.IntTy;
+      case LengthModifier::None:
+        return Ctx.IntTy;
       case LengthModifier::AsLong:
       case LengthModifier::AsWide:
         return ArgType(ArgType::WIntTy, "wint_t");
+      case LengthModifier::AsShort:
+        if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
+          return Ctx.IntTy;
       default:
         return ArgType::Invalid();
     }
@@ -395,10 +399,16 @@ ArgType PrintfSpecifier::getArgType(ASTC
       if (IsObjCLiteral)
         return ArgType(Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()),
                        "const unichar *");
+      if (Ctx.getTargetInfo().getTriple().isOSMSVCRT() &&
+          LM.getKind() == LengthModifier::AsShort)
+        return ArgType::CStrTy;
       return ArgType(ArgType::WCStrTy, "wchar_t *");
     case ConversionSpecifier::CArg:
       if (IsObjCLiteral)
         return ArgType(Ctx.UnsignedShortTy, "unichar");
+      if (Ctx.getTargetInfo().getTriple().isOSMSVCRT() &&
+          LM.getKind() == LengthModifier::AsShort)
+        return Ctx.IntTy;
       return ArgType(Ctx.WideCharTy, "wchar_t");
     case ConversionSpecifier::pArg:
       return ArgType::CPointerTy;

Modified: cfe/trunk/lib/Analysis/ScanfFormatString.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ScanfFormatString.cpp?rev=217196&r1=217195&r2=217196&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ScanfFormatString.cpp (original)
+++ cfe/trunk/lib/Analysis/ScanfFormatString.cpp Thu Sep  4 16:39:52 2014
@@ -333,6 +333,9 @@ ArgType ScanfSpecifier::getArgType(ASTCo
         case LengthModifier::AsAllocate:
         case LengthModifier::AsMAllocate:
           return ArgType::PtrTo(ArgType::CStrTy);
+        case LengthModifier::AsShort:
+          if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
+            return ArgType::PtrTo(ArgType::AnyCharTy);
         default:
           return ArgType::Invalid();
       }
@@ -346,6 +349,9 @@ ArgType ScanfSpecifier::getArgType(ASTCo
         case LengthModifier::AsAllocate:
         case LengthModifier::AsMAllocate:
           return ArgType::PtrTo(ArgType(ArgType::WCStrTy, "wchar_t *"));
+        case LengthModifier::AsShort:
+          if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
+            return ArgType::PtrTo(ArgType::AnyCharTy);
         default:
           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=217196&r1=217195&r2=217196&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings-ms.c (original)
+++ cfe/trunk/test/Sema/format-strings-ms.c Thu Sep  4 16:39:52 2014
@@ -63,4 +63,16 @@ void w_test(wchar_t c, wchar_t *s) {
 
 }
 
+void h_test(char c, char* s) {
+  double bad;
+  printf("%hc", bad); // expected-warning{{format specifies type 'int' but the argument has type 'double'}}
+  printf("%hC", bad); // expected-warning{{format specifies type 'int' but the argument has type 'double'}}
+  printf("%hs", bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double'}}
+  printf("%hS", bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double'}}
+  scanf("%hc", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
+  scanf("%hC", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
+  scanf("%hs", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
+  scanf("%hS", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
+}
+
 #endif





More information about the cfe-commits mailing list