aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBilly Donahue <BillyDonahue@users.noreply.github.com>2017-05-07 01:07:16 -0400
committerGitHub <noreply@github.com>2017-05-07 01:07:16 -0400
commit887d569eb0bac3760cb3adf5877c8284e4d7768e (patch)
tree8897d6e42dd8da0ac178e7749ffbafc855cd01d3
parent0ad83afdaa3319dadd9f03299bd62f93e6afe2d3 (diff)
parent53c478d639b8eebd2942e88266610ebc79c541f6 (diff)
downloadgoogletest-887d569eb0bac3760cb3adf5877c8284e4d7768e.tar.gz
Merge pull request #965 from davidben/format-attr
Annotate ColoredPrintf with the format attribute and fix bugs.
-rw-r--r--googletest/include/gtest/internal/gtest-port.h17
-rw-r--r--googletest/src/gtest.cc7
2 files changed, 21 insertions, 3 deletions
diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h
index 38a83655..863f6e7b 100644
--- a/googletest/include/gtest/internal/gtest-port.h
+++ b/googletest/include/gtest/internal/gtest-port.h
@@ -874,6 +874,23 @@ using ::std::tuple_size;
# define GTEST_ATTRIBUTE_UNUSED_
#endif
+// Use this annotation before a function that takes a printf format string.
+#if defined(__GNUC__) && !defined(COMPILER_ICC)
+# if defined(__MINGW_PRINTF_FORMAT)
+// MinGW has two different printf implementations. Ensure the format macro
+// matches the selected implementation. See
+// https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.
+# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
+ __attribute__((__format__(__MINGW_PRINTF_FORMAT, string_index, \
+ first_to_check)))
+# else
+# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \
+ __attribute__((__format__(__printf__, string_index, first_to_check)))
+# endif
+#else
+# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)
+#endif
+
// A macro to disallow operator=
// This should be used in the private: declarations for a class.
#define GTEST_DISALLOW_ASSIGN_(type)\
diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc
index d6fd8a63..b0c98e8d 100644
--- a/googletest/src/gtest.cc
+++ b/googletest/src/gtest.cc
@@ -2953,6 +2953,7 @@ bool ShouldUseColor(bool stdout_is_tty) {
// cannot simply emit special characters and have the terminal change colors.
// This routine must actually emit the characters rather than return a string
// that would be colored when printed, as can be done on Linux.
+GTEST_ATTRIBUTE_PRINTF_(2, 3)
void ColoredPrintf(GTestColor color, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
@@ -4729,7 +4730,7 @@ bool ShouldShard(const char* total_shards_env,
<< "Invalid environment variables: you have "
<< kTestShardIndex << " = " << shard_index
<< ", but have left " << kTestTotalShards << " unset.\n";
- ColoredPrintf(COLOR_RED, msg.GetString().c_str());
+ ColoredPrintf(COLOR_RED, "%s", msg.GetString().c_str());
fflush(stdout);
exit(EXIT_FAILURE);
} else if (total_shards != -1 && shard_index == -1) {
@@ -4737,7 +4738,7 @@ bool ShouldShard(const char* total_shards_env,
<< "Invalid environment variables: you have "
<< kTestTotalShards << " = " << total_shards
<< ", but have left " << kTestShardIndex << " unset.\n";
- ColoredPrintf(COLOR_RED, msg.GetString().c_str());
+ ColoredPrintf(COLOR_RED, "%s", msg.GetString().c_str());
fflush(stdout);
exit(EXIT_FAILURE);
} else if (shard_index < 0 || shard_index >= total_shards) {
@@ -4746,7 +4747,7 @@ bool ShouldShard(const char* total_shards_env,
<< kTestShardIndex << " < " << kTestTotalShards
<< ", but you have " << kTestShardIndex << "=" << shard_index
<< ", " << kTestTotalShards << "=" << total_shards << ".\n";
- ColoredPrintf(COLOR_RED, msg.GetString().c_str());
+ ColoredPrintf(COLOR_RED, "%s", msg.GetString().c_str());
fflush(stdout);
exit(EXIT_FAILURE);
}