diff --git a/include/clang/Analysis/Analyses/FormatString.h b/include/clang/Analysis/Analyses/FormatString.h index 2a16f98c91c47168a418c6fa3995c2704f4c48d7..52dcd03b0b9d2e1d863607cd371ff684b0fc6cd8 100644 --- a/include/clang/Analysis/Analyses/FormatString.h +++ b/include/clang/Analysis/Analyses/FormatString.h @@ -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 }; diff --git a/lib/Analysis/FormatString.cpp b/lib/Analysis/FormatString.cpp index 0a922f7508f54fc4f1f2183775bb614d9a77523b..e4d84ec670b35359a8bdb8422b67bfd5184e9528 100644 --- a/lib/Analysis/FormatString.cpp +++ b/lib/Analysis/FormatString.cpp @@ -244,6 +244,8 @@ clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS, ++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::toString() const { return "a"; case AsMAllocate: return "m"; + case AsWide: + return "w"; case None: return ""; } @@ -719,6 +723,16 @@ bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target) const { 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::hasStandardLengthModifier() const { case LengthModifier::AsInt32: case LengthModifier::AsInt3264: case LengthModifier::AsInt64: + case LengthModifier::AsWide: return false; } llvm_unreachable("Invalid LengthModifier Kind!"); diff --git a/lib/Analysis/PrintfFormatString.cpp b/lib/Analysis/PrintfFormatString.cpp index 082a8327a346766821bf8f73dca2a7f7162bd73a..38dc8ae5a0fefb393e56b4ee7a40a852b457bd85 100644 --- a/lib/Analysis/PrintfFormatString.cpp +++ b/lib/Analysis/PrintfFormatString.cpp @@ -268,6 +268,7 @@ ArgType PrintfSpecifier::getArgType(ASTContext &Ctx, 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(ASTContext &Ctx, 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(ASTContext &Ctx, return ArgType(); case LengthModifier::AsAllocate: case LengthModifier::AsMAllocate: + case LengthModifier::AsWide: return ArgType::Invalid(); } @@ -372,6 +375,7 @@ ArgType PrintfSpecifier::getArgType(ASTContext &Ctx, case LengthModifier::AsInt32: case LengthModifier::AsInt3264: case LengthModifier::AsInt64: + case LengthModifier::AsWide: return ArgType::Invalid(); } } @@ -384,6 +388,8 @@ ArgType PrintfSpecifier::getArgType(ASTContext &Ctx, "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) diff --git a/lib/Analysis/ScanfFormatString.cpp b/lib/Analysis/ScanfFormatString.cpp index ed286274950b1e648303f0a34e04b24d6157da56..5fb2d7ccedd4a9b93e0b82a936ff668df6647da8 100644 --- a/lib/Analysis/ScanfFormatString.cpp +++ b/lib/Analysis/ScanfFormatString.cpp @@ -257,6 +257,7 @@ ArgType ScanfSpecifier::getArgType(ASTContext &Ctx) const { case LengthModifier::AsMAllocate: case LengthModifier::AsInt32: case LengthModifier::AsInt3264: + case LengthModifier::AsWide: return ArgType::Invalid(); } @@ -295,6 +296,7 @@ ArgType ScanfSpecifier::getArgType(ASTContext &Ctx) const { case LengthModifier::AsMAllocate: case LengthModifier::AsInt32: case LengthModifier::AsInt3264: + case LengthModifier::AsWide: return ArgType::Invalid(); } @@ -326,6 +328,7 @@ ArgType ScanfSpecifier::getArgType(ASTContext &Ctx) const { 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(ASTContext &Ctx) const { // 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(ASTContext &Ctx) const { case LengthModifier::AsMAllocate: case LengthModifier::AsInt32: case LengthModifier::AsInt3264: + case LengthModifier::AsWide: return ArgType::Invalid(); } diff --git a/test/Sema/format-strings-ms.c b/test/Sema/format-strings-ms.c index 2ad8eae033d513bf8a09672d1df427baa142141f..3daa0e4f1d5fbf08f5eaf7610f723a3b9da6e603 100644 --- a/test/Sema/format-strings-ms.c +++ b/test/Sema/format-strings-ms.c @@ -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