summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorSpencer Low <CompareAndSwap@gmail.com>2015-05-13 00:02:55 -0700
committerSpencer Low <CompareAndSwap@gmail.com>2015-05-13 19:30:30 -0700
commit6001c87cbcafba68975fd9b3aa69f4952396f83c (patch)
tree0908abe4dc96ed631c280f081a0762e4ff65b3da /base
parent74f0a578a8ee0957b849e811af5fc30c74be55f0 (diff)
adb: win32: fix StringPrintf format string checking of %zd and PRIu64
At runtime, vsnprintf (and android::base::StringPrintf which calls it) call a mingw version of vsnprintf, not the vsnprintf from MSVCRT.DLL. The mingw version properly understands %zd and PRIu64 (the latter, provided that you #include <inttypes.h>). The problem was that android::base::StringPrintf was causing compile-time errors saying that %zd and PRIu64 were not recognized. It seems that this was because the attribute on the function prototypes specified `printf' instead of `gnu_printf'. Once that was fixed to match vsnprintf's attribute, the warnings went away. This uses similar preprocessor techniques as <android/log.h>. Also restore a %zd usage to avoid a static_cast<>, and make print_transfer_progress()'s format string compile-time checkable (and tweak some types and %llu => PRIu64). Change-Id: I80b31b9994858a28cb7c6847143b86108b8ab842 Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Diffstat (limited to 'base')
-rw-r--r--base/include/base/stringprintf.h22
1 files changed, 19 insertions, 3 deletions
diff --git a/base/include/base/stringprintf.h b/base/include/base/stringprintf.h
index 195c1decb..d68af8713 100644
--- a/base/include/base/stringprintf.h
+++ b/base/include/base/stringprintf.h
@@ -23,16 +23,32 @@
namespace android {
namespace base {
+// These printf-like functions are implemented in terms of vsnprintf, so they
+// use the same attribute for compile-time format string checking. On Windows,
+// if the mingw version of vsnprintf is used, use `gnu_printf' which allows z
+// in %zd and PRIu64 (and related) to be recognized by the compile-time
+// checking.
+#define FORMAT_ARCHETYPE __printf__
+#ifdef __USE_MINGW_ANSI_STDIO
+#if __USE_MINGW_ANSI_STDIO
+#undef FORMAT_ARCHETYPE
+#define FORMAT_ARCHETYPE gnu_printf
+#endif
+#endif
+
// Returns a string corresponding to printf-like formatting of the arguments.
std::string StringPrintf(const char* fmt, ...)
- __attribute__((__format__(__printf__, 1, 2)));
+ __attribute__((__format__(FORMAT_ARCHETYPE, 1, 2)));
// Appends a printf-like formatting of the arguments to 'dst'.
void StringAppendF(std::string* dst, const char* fmt, ...)
- __attribute__((__format__(__printf__, 2, 3)));
+ __attribute__((__format__(FORMAT_ARCHETYPE, 2, 3)));
// Appends a printf-like formatting of the arguments to 'dst'.
-void StringAppendV(std::string* dst, const char* format, va_list ap);
+void StringAppendV(std::string* dst, const char* format, va_list ap)
+ __attribute__((__format__(FORMAT_ARCHETYPE, 2, 0)));
+
+#undef FORMAT_ARCHETYPE
} // namespace base
} // namespace android