summaryrefslogtreecommitdiff
path: root/absl/base
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-03-15 11:12:25 -0700
committerGravatar Andy Getz <durandal@google.com>2022-03-15 20:21:30 -0400
commit231c393a170ce3c6d83e8456bc87fe917c333ecf (patch)
treeb14a1ef390e86aec82c3552ba837fcd2553574b7 /absl/base
parent5ed77665c4d697f657fa1362b5a482450f858cdc (diff)
Export of internal Abseil changes
-- 5dc885f2b62993bccf33a3f3b99f7e460c819c89 by Derek Mauro <dmauro@google.com>: Remove the internal-only ABSL_INTERNAL_ASSUME now that ABSL_ASSUME is available and ABSL_INTERNAL_ASSUME has no more users. Improve the documentation to ABSL_ASSUME somewhat. PiperOrigin-RevId: 434803125 Change-Id: I7c27418463ffc1c7e10ecd50e2d17f348f686af7 -- 4aea19a0ef596228c9136a4c2446e6f25085f23c by Derek Mauro <dmauro@google.com>: Update documentation to warn against using absl::Hash across dynamically loaded libraries Fixes #1128 PiperOrigin-RevId: 434723247 Change-Id: Ib0c7ba03b2cab98b42e19e85be6833192d4b4067 GitOrigin-RevId: 5dc885f2b62993bccf33a3f3b99f7e460c819c89
Diffstat (limited to 'absl/base')
-rw-r--r--absl/base/optimization.h51
1 files changed, 28 insertions, 23 deletions
diff --git a/absl/base/optimization.h b/absl/base/optimization.h
index 6fe03c43..db5cc097 100644
--- a/absl/base/optimization.h
+++ b/absl/base/optimization.h
@@ -181,32 +181,21 @@
#define ABSL_PREDICT_TRUE(x) (x)
#endif
-// Platform and compilation mode dependent implementation of ABSL_ASSUME.
-#if !defined(NDEBUG)
-#define ABSL_INTERNAL_ASSUME(cond) assert(cond)
-#elif ABSL_HAVE_BUILTIN(__builtin_assume)
-#define ABSL_INTERNAL_ASSUME(cond) __builtin_assume(cond)
-#elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
-#define ABSL_INTERNAL_ASSUME(cond) \
- do { \
- if (!(cond)) __builtin_unreachable(); \
- } while (0)
-#elif defined(_MSC_VER)
-#define ABSL_INTERNAL_ASSUME(cond) __assume(cond)
-#else
-#define ABSL_INTERNAL_ASSUME(cond) \
- do { \
- static_cast<void>(false && (cond)); \
- } while (0)
-#endif
-
// ABSL_ASSUME(cond)
+//
// Informs the compiler that a condition is always true and that it can assume
-// it to be true for optimization purposes. The call has undefined behavior if
-// the condition is false.
+// it to be true for optimization purposes.
+//
+// WARNING: If the condition is false, the program can produce undefined and
+// potentially dangerous behavior.
+//
// In !NDEBUG mode, the condition is checked with an assert().
+//
// NOTE: The expression must not have side effects, as it may only be evaluated
-// in some compilation modes and not others.
+// in some compilation modes and not others. Some compilers may issue a warning
+// if the compiler cannot prove the expression has no side effects. For example,
+// the expression should not use a function call since the compiler cannot prove
+// that a function call does not have side effects.
//
// Example:
//
@@ -216,7 +205,23 @@
// // assumption specified above.
// int y = x / 16;
//
-#define ABSL_ASSUME(cond) ABSL_INTERNAL_ASSUME(cond)
+#if !defined(NDEBUG)
+#define ABSL_ASSUME(cond) assert(cond)
+#elif ABSL_HAVE_BUILTIN(__builtin_assume)
+#define ABSL_ASSUME(cond) __builtin_assume(cond)
+#elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
+#define ABSL_ASSUME(cond) \
+ do { \
+ if (!(cond)) __builtin_unreachable(); \
+ } while (0)
+#elif defined(_MSC_VER)
+#define ABSL_ASSUME(cond) __assume(cond)
+#else
+#define ABSL_ASSUME(cond) \
+ do { \
+ static_cast<void>(false && (cond)); \
+ } while (0)
+#endif
// ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond)
// This macro forces small unique name on a static file level symbols like