Skip to content

Commit cdfd81f

Browse files
author
vladlosev
committed
Adds a new macro simplifying use of snprinf on MS platforms.
git-svn-id: http://googletest.googlecode.com/svn/trunk@597 861a406c-534a-0410-8894-cb66d6ee9925
1 parent 524cc4e commit cdfd81f

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

include/gtest/internal/gtest-port.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,23 @@ inline void Abort() { abort(); }
16821682

16831683
} // namespace posix
16841684

1685+
// MSVC "deprecates" snprintf and issues warnings wherever it is used. In
1686+
// order to avoid these warnings, we need to use _snprintf or _snprintf_s on
1687+
// MSVC-based platforms. We map the GTEST_SNPRINTF_ macro to the appropriate
1688+
// function in order to achieve that. We use macro definition here because
1689+
// snprintf is a variadic function.
1690+
#if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE
1691+
// MSVC 2005 and above support variadic macros.
1692+
# define GTEST_SNPRINTF_(buffer, size, format, ...) \
1693+
_snprintf_s(buffer, size, size, format, __VA_ARGS__)
1694+
#elif defined(_MSC_VER)
1695+
// Windows CE does not define _snprintf_s and MSVC prior to 2005 doesn't
1696+
// complain about _snprintf.
1697+
# define GTEST_SNPRINTF_ _snprintf
1698+
#else
1699+
# define GTEST_SNPRINTF_ snprintf
1700+
#endif
1701+
16851702
// The maximum number a BiggestInt can represent. This definition
16861703
// works no matter BiggestInt is represented in one's complement or
16871704
// two's complement.

src/gtest-printers.cc

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ namespace {
5555

5656
using ::std::ostream;
5757

58-
#if GTEST_OS_WINDOWS_MOBILE // Windows CE does not define _snprintf_s.
59-
# define snprintf _snprintf
60-
#elif _MSC_VER >= 1400 // VC 8.0 and later deprecate snprintf and _snprintf.
61-
# define snprintf _snprintf_s
62-
#elif _MSC_VER
63-
# define snprintf _snprintf
64-
#endif // GTEST_OS_WINDOWS_MOBILE
65-
6658
// Prints a segment of bytes in the given object.
6759
void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
6860
size_t count, ostream* os) {
@@ -77,7 +69,7 @@ void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
7769
else
7870
*os << '-';
7971
}
80-
snprintf(text, sizeof(text), "%02X", obj_bytes[j]);
72+
GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);
8173
*os << text;
8274
}
8375
}

0 commit comments

Comments
 (0)