From c8f935aa5e5e118efae5d143f435bdfc5f6f5c37 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Thu, 21 Sep 2017 17:52:02 -0700 Subject: [PATCH] Base: Warn on using ostream<< with std::string* In most reasonable cases, this is actually a bug. So add a diagnostic. Test: m Change-Id: Ib506b45dbdbafcb1893486b08ef13ec8f11d0357 --- base/include/android-base/logging.h | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/base/include/android-base/logging.h b/base/include/android-base/logging.h index 548b286c4..f93c69615 100644 --- a/base/include/android-base/logging.h +++ b/base/include/android-base/logging.h @@ -438,4 +438,36 @@ class ScopedLogSeverity { } // namespace base } // namespace android +namespace std { + +// Emit a warning of ostream<< with std::string*. The intention was most likely to print *string. +// +// Note: for this to work, we need to have this in a namespace. +// Note: lots of ifdef magic to make this work with Clang (platform) vs GCC (windows tools) +// Note: using diagnose_if(true) under Clang and nothing under GCC/mingw as there is no common +// attribute support. +// Note: using a pragma because "-Wgcc-compat" (included in "-Weverything") complains about +// diagnose_if. +// Note: to print the pointer, use "<< static_cast(string_pointer)" instead. +// Note: a not-recommended alternative is to let Clang ignore the warning by adding +// -Wno-user-defined-warnings to CPPFLAGS. +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgcc-compat" +#define OSTREAM_STRING_POINTER_USAGE_WARNING \ + __attribute__((diagnose_if(true, "Unexpected logging of string pointer", "warning"))) +#else +#define OSTREAM_STRING_POINTER_USAGE_WARNING /* empty */ +#endif +inline std::ostream& operator<<(std::ostream& stream, const std::string* string_pointer) + OSTREAM_STRING_POINTER_USAGE_WARNING { + return stream << static_cast(string_pointer); +} +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +#undef OSTREAM_STRING_POINTER_USAGE_WARNING + +} // namespace std + #endif // ANDROID_BASE_LOGGING_H