summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorDan Albert <danalbert@google.com>2015-07-08 13:50:42 -0700
committerDan Albert <danalbert@google.com>2015-07-09 10:47:24 -0700
commit459df8f3a14d4c614f0211049800cf7cad6d30ad (patch)
tree29b1d8281e4355d70bf32b4efffa5e5a9350da2b /base
parenta6241a0298de5e773ef113626ca4686f9c038868 (diff)
Turn on -Wformat-nonliteral.
Apparently there are two classes of this warning in clang. -Wformat-security is only emitted for cases of `func(nonliteral_fmt_string)` (no args), and -Wformat-nonliteral is emitted for cases *with* arguments. For whatever reason, the latter isn't included in -Wextra and must be manually enabled. To make this more easily portable to Windows, move the existing gnu_printf/__printf__ decision into base/macros.h as ATTRIBUTE_FORMAT. Change-Id: I3b0990e1d1f0a2e9c13b32f5cd60478946cb5fc6
Diffstat (limited to 'base')
-rw-r--r--base/include/base/macros.h13
-rw-r--r--base/include/base/stringprintf.h24
2 files changed, 18 insertions, 19 deletions
diff --git a/base/include/base/macros.h b/base/include/base/macros.h
index b1ce7c6ba..35db83a09 100644
--- a/base/include/base/macros.h
+++ b/base/include/base/macros.h
@@ -185,4 +185,17 @@ void UNUSED(const T&...) {
} while (0)
#endif
+// 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, so use `gnu_printf' which allows z
+// in %zd and PRIu64 (and related) to be recognized by the compile-time
+// checking.
+#if defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO
+#define ATTRIBUTE_FORMAT(fmt, args) \
+ __attribute__((format(gnu_printf, fmt, args)))
+#else
+#define ATTRIBUTE_FORMAT(fmt, args) \
+ __attribute__((format(__printf__, fmt, args)))
+#endif
+
#endif // UTILS_MACROS_H
diff --git a/base/include/base/stringprintf.h b/base/include/base/stringprintf.h
index d68af8713..e17a2b521 100644
--- a/base/include/base/stringprintf.h
+++ b/base/include/base/stringprintf.h
@@ -20,35 +20,21 @@
#include <stdarg.h>
#include <string>
+#include "base/macros.h"
+
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__(FORMAT_ARCHETYPE, 1, 2)));
+std::string StringPrintf(const char* fmt, ...) ATTRIBUTE_FORMAT(1, 2);
// Appends a printf-like formatting of the arguments to 'dst'.
void StringAppendF(std::string* dst, const char* fmt, ...)
- __attribute__((__format__(FORMAT_ARCHETYPE, 2, 3)));
+ ATTRIBUTE_FORMAT(2, 3);
// Appends a printf-like formatting of the arguments to 'dst'.
void StringAppendV(std::string* dst, const char* format, va_list ap)
- __attribute__((__format__(FORMAT_ARCHETYPE, 2, 0)));
-
-#undef FORMAT_ARCHETYPE
+ ATTRIBUTE_FORMAT(2, 0);
} // namespace base
} // namespace android