diff options
author | Andreas Gampe | 2017-09-21 19:52:02 -0500 |
---|---|---|
committer | Andreas Gampe | 2017-09-22 18:25:58 -0500 |
commit | c8f935aa5e5e118efae5d143f435bdfc5f6f5c37 (patch) | |
tree | c0d57897d3205824100257de6e51c40d0c76c286 /base/include | |
parent | 643e9a536cc27a81a77eb8497ed05dcf0292249b (diff) | |
download | platform-system-core-c8f935aa5e5e118efae5d143f435bdfc5f6f5c37.tar.gz platform-system-core-c8f935aa5e5e118efae5d143f435bdfc5f6f5c37.tar.xz platform-system-core-c8f935aa5e5e118efae5d143f435bdfc5f6f5c37.zip |
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
Diffstat (limited to 'base/include')
-rw-r--r-- | base/include/android-base/logging.h | 32 |
1 files changed, 32 insertions, 0 deletions
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 { | |||
438 | } // namespace base | 438 | } // namespace base |
439 | } // namespace android | 439 | } // namespace android |
440 | 440 | ||
441 | namespace std { | ||
442 | |||
443 | // Emit a warning of ostream<< with std::string*. The intention was most likely to print *string. | ||
444 | // | ||
445 | // Note: for this to work, we need to have this in a namespace. | ||
446 | // Note: lots of ifdef magic to make this work with Clang (platform) vs GCC (windows tools) | ||
447 | // Note: using diagnose_if(true) under Clang and nothing under GCC/mingw as there is no common | ||
448 | // attribute support. | ||
449 | // Note: using a pragma because "-Wgcc-compat" (included in "-Weverything") complains about | ||
450 | // diagnose_if. | ||
451 | // Note: to print the pointer, use "<< static_cast<const void*>(string_pointer)" instead. | ||
452 | // Note: a not-recommended alternative is to let Clang ignore the warning by adding | ||
453 | // -Wno-user-defined-warnings to CPPFLAGS. | ||
454 | #ifdef __clang__ | ||
455 | #pragma clang diagnostic push | ||
456 | #pragma clang diagnostic ignored "-Wgcc-compat" | ||
457 | #define OSTREAM_STRING_POINTER_USAGE_WARNING \ | ||
458 | __attribute__((diagnose_if(true, "Unexpected logging of string pointer", "warning"))) | ||
459 | #else | ||
460 | #define OSTREAM_STRING_POINTER_USAGE_WARNING /* empty */ | ||
461 | #endif | ||
462 | inline std::ostream& operator<<(std::ostream& stream, const std::string* string_pointer) | ||
463 | OSTREAM_STRING_POINTER_USAGE_WARNING { | ||
464 | return stream << static_cast<const void*>(string_pointer); | ||
465 | } | ||
466 | #ifdef __clang__ | ||
467 | #pragma clang diagnostic pop | ||
468 | #endif | ||
469 | #undef OSTREAM_STRING_POINTER_USAGE_WARNING | ||
470 | |||
471 | } // namespace std | ||
472 | |||
441 | #endif // ANDROID_BASE_LOGGING_H | 473 | #endif // ANDROID_BASE_LOGGING_H |