[PATCH] Analysis: Add support for MS specific printf format specifiers

Charles Davis cdavis5x at gmail.com
Wed Aug 21 13:08:07 PDT 2013

  Table of format specifiers and their associated character widths with `MSVCRT.DLL`:

  | Function | `"%s"`/`"%c"` | `"%S"`/`"%C"` | `"%hs"`/`"%hc"` | `"%ls"`/`"%lc"`
  | ------ | ------ | ------ | ------ | ------
  | `printf` | Narrow | Wide | Narrow | Wide
  | `wprintf` | Wide | Narrow | Narrow | Wide

Comment at: lib/Analysis/FormatString.cpp:544-546
@@ -516,5 +543,5 @@
-  // MacOS X unicode extensions.
+  // POSIX unicode extensions.
   case CArg: return "C";
   case SArg: return "S";
Jordan Rose wrote:
> Huh, I didn't realize these were present elsewhere. They don't quite behave the same on Windows, right? See http://llvm.org/bugs/show_bug.cgi?id=13565
Yeah, they don't. In fact, the behavior of these on Windows is very... uh, weird, to say the least. It depends on which function you're calling (`printf` or `wprintf`). There's a handy table above.

This is so, if you include `<tchar.h>` and call `_tprintf`, you can pass `_TCHAR` strings and it will behave correctly. (The macros and types in `<tchar.h>` behave differently depending on whether or not `_UNICODE` is `#define`d. Same for `<windows.h>` and `UNICODE`.) Note the non-standard `"%hs"` and `"%hc"` formats. They're needed because, in this scheme, there's otherwise no way to always pass a plain ol' `char` string!

Comment at: include/clang/Analysis/Analyses/FormatString.h:77
@@ +76,3 @@
+    AsInt32,      // 'I32' (MSVCRT, like __int32)
+    AsInt3264,    // 'I'   (MSVCRT, like size_t/ptrdiff_t)
+    AsInt64,      // 'I64' (MSVCRT, like __int64)
Jordan Rose wrote:
> I don't like this name. How about AsIntWord or something? (As I understand it "word" is an overloaded term in MSVC, though.)
This type is in fact called `__int3264` in MIDL-land.

What about `AsIntPtr`?


More information about the cfe-commits mailing list