summaryrefslogtreecommitdiffstats
path: root/base/string_util.h
diff options
context:
space:
mode:
authorevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-20 06:53:28 +0000
committerevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-20 06:53:28 +0000
commit34b2b007db875a6acb853c5cd2a247fbb32c0f88 (patch)
tree6dc39bc9f10d6e8eedcdf14821ba9e96b5ccab51 /base/string_util.h
parent24b857793e27aded8d804a112a5fe6c77e28b081 (diff)
downloadchromium_src-34b2b007db875a6acb853c5cd2a247fbb32c0f88.zip
chromium_src-34b2b007db875a6acb853c5cd2a247fbb32c0f88.tar.gz
chromium_src-34b2b007db875a6acb853c5cd2a247fbb32c0f88.tar.bz2
Add compiler-specific "examine printf format" attributes to printfs.
Functions that take a printf-style format get a new annotation, which produces a bunch of compiler warnings when you use printf impoperly. This change adds the annotations and fixes the warnings. We now must use PRId64 for 64-bit numbers and the PRIsz for size_t. Review URL: http://codereview.chromium.org/339059 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32600 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.h')
-rw-r--r--base/string_util.h36
1 files changed, 26 insertions, 10 deletions
diff --git a/base/string_util.h b/base/string_util.h
index 89caf4f..6d4a4d9 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -13,6 +13,7 @@
#include <vector>
#include "base/basictypes.h"
+#include "base/compiler_specific.h"
#include "base/string16.h"
#include "base/string_piece.h" // For implicit conversions.
@@ -42,17 +43,22 @@ int strncasecmp(const char* s1, const char* s2, size_t count);
// Wrapper for vsnprintf that always null-terminates and always returns the
// number of characters that would be in an untruncated formatted
// string, even when truncation occurs.
-int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments);
+int vsnprintf(char* buffer, size_t size, const char* format, va_list arguments)
+ PRINTF_FORMAT(3, 0);
// vswprintf always null-terminates, but when truncation occurs, it will either
// return -1 or the number of characters that would be in an untruncated
// formatted string. The actual return value depends on the underlying
// C library's vswprintf implementation.
int vswprintf(wchar_t* buffer, size_t size,
- const wchar_t* format, va_list arguments);
+ const wchar_t* format, va_list arguments) WPRINTF_FORMAT(3, 0);
// Some of these implementations need to be inlined.
+// We separate the declaration from the implementation of this inline
+// function just so the PRINTF_FORMAT works.
+inline int snprintf(char* buffer, size_t size, const char* format, ...)
+ PRINTF_FORMAT(3, 4);
inline int snprintf(char* buffer, size_t size, const char* format, ...) {
va_list arguments;
va_start(arguments, format);
@@ -61,6 +67,10 @@ inline int snprintf(char* buffer, size_t size, const char* format, ...) {
return result;
}
+// We separate the declaration from the implementation of this inline
+// function just so the WPRINTF_FORMAT works.
+inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...)
+ WPRINTF_FORMAT(3, 4);
inline int swprintf(wchar_t* buffer, size_t size, const wchar_t* format, ...) {
va_list arguments;
va_start(arguments, format);
@@ -438,22 +448,28 @@ double StringToDouble(const std::string& value);
double StringToDouble(const string16& value);
// Return a C++ string given printf-like input.
-std::string StringPrintf(const char* format, ...);
-std::wstring StringPrintf(const wchar_t* format, ...);
+std::string StringPrintf(const char* format, ...) PRINTF_FORMAT(1, 2);
+std::wstring StringPrintf(const wchar_t* format, ...) WPRINTF_FORMAT(1, 2);
// Store result into a supplied string and return it
-const std::string& SStringPrintf(std::string* dst, const char* format, ...);
+const std::string& SStringPrintf(std::string* dst, const char* format, ...)
+ PRINTF_FORMAT(2, 3);
const std::wstring& SStringPrintf(std::wstring* dst,
- const wchar_t* format, ...);
+ const wchar_t* format, ...)
+ WPRINTF_FORMAT(2, 3);
// Append result to a supplied string
-void StringAppendF(std::string* dst, const char* format, ...);
-void StringAppendF(std::wstring* dst, const wchar_t* format, ...);
+void StringAppendF(std::string* dst, const char* format, ...)
+ PRINTF_FORMAT(2, 3);
+void StringAppendF(std::wstring* dst, const wchar_t* format, ...)
+ WPRINTF_FORMAT(2, 3);
// Lower-level routine that takes a va_list and appends to a specified
// string. All other routines are just convenience wrappers around it.
-void StringAppendV(std::string* dst, const char* format, va_list ap);
-void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap);
+void StringAppendV(std::string* dst, const char* format, va_list ap)
+ PRINTF_FORMAT(2, 0);
+void StringAppendV(std::wstring* dst, const wchar_t* format, va_list ap)
+ WPRINTF_FORMAT(2, 0);
// This is mpcomplete's pattern for saving a string copy when dealing with
// a function that writes results into a wchar_t[] and wanting the result to