Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1839)

Unified Diff: base/debug/crash_logging.h

Issue 2137863002: Make it a compile error to use a crash key with no variable name. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/debug/crash_logging.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/debug/crash_logging.h
diff --git a/base/debug/crash_logging.h b/base/debug/crash_logging.h
index 97bac754a4b626985fe26935d37de8008fb7f7d7..0f9e1148034491a5d70271961492657c3dbac89c 100644
--- a/base/debug/crash_logging.h
+++ b/base/debug/crash_logging.h
@@ -49,12 +49,35 @@ class BASE_EXPORT ScopedCrashKey {
ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value);
~ScopedCrashKey();
+ // Helper to force a static_assert when instantiating a ScopedCrashKey
+ // temporary without a name. The usual idiom is to just #define a macro that
+ // static_asserts with the message; however, that doesn't work well when the
+ // type is in a namespace.
+ //
+ // Instead, we use a templated helper to trigger the static_assert, observing
+ // two rules:
+ // - The static_assert needs to be in a normally uninstantiated template;
+ // otherwise, it will fail to compile =)
+ // - Similarly, the static_assert must be dependent on the template argument,
+ // to prevent it from being evaluated until the template is instantiated.
+ template<typename T>
+ ScopedCrashKey(const T&) {
danakj 2016/07/12 19:59:26 Why a constructor and not just a free CrashFromBad
dcheng 2016/07/13 00:34:11 This prevents me from having to "using base::debug
Robert Sesek 2016/07/13 19:46:22 What about something like this: enum class Unname
danakj 2016/07/13 19:48:16 I was just thinking similar things.. but more like
+ static_assert(
+ sizeof(T) == 0,
+ "scoped crash key objects should not be unnamed temporaries.");
+ }
+
private:
std::string key_;
DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey);
};
+// Disallow an instantation of ScopedCrashKey without a name, since this results
+// in a temporary that is immediately destroyed. Doing so will trigger the
+// static_assert in the templated constructor helper in ScopedCrashKey.
+#define ScopedCrashKey(key, value) ScopedCrashKey(0xbad)
+
// Before setting values for a key, all the keys must be registered.
struct BASE_EXPORT CrashKey {
// The name of the crash key, used in the above functions.
« no previous file with comments | « no previous file | base/debug/crash_logging.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698