Skip to content

Commit

Permalink
ABSL_ASSUME: Use a ternary operator instead of do-while in the
Browse files Browse the repository at this point in the history
implementations that use a branch marked unreachable so that it is
usable in more contexts.

The unimplemented case now uses the same definition as ABSL_ASSERT
when NDEBUG is defined.

Fixes #1814

PiperOrigin-RevId: 720236336
Change-Id: I4f4f6e8a384dd11a07626b085a001fc2f5b7db21
  • Loading branch information
derekmauro authored and copybara-github committed Jan 27, 2025
1 parent e118b39 commit c64f219
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
20 changes: 8 additions & 12 deletions absl/base/optimization.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// new code that requires C compatibility or assume C compatibility will remain
// indefinitely.

// SKIP_ABSL_INLINE_NAMESPACE_CHECK

#ifndef ABSL_BASE_OPTIMIZATION_H_
#define ABSL_BASE_OPTIMIZATION_H_

Expand Down Expand Up @@ -271,20 +273,14 @@
#elif defined(_MSC_VER)
#define ABSL_ASSUME(cond) __assume(cond)
#elif defined(__cpp_lib_unreachable) && __cpp_lib_unreachable >= 202202L
#define ABSL_ASSUME(cond) \
do { \
if (!(cond)) std::unreachable(); \
} while (false)
#define ABSL_ASSUME(cond) ((cond) ? void() : std::unreachable())
#elif defined(__GNUC__) || ABSL_HAVE_BUILTIN(__builtin_unreachable)
#define ABSL_ASSUME(cond) \
do { \
if (!(cond)) __builtin_unreachable(); \
} while (false)
#define ABSL_ASSUME(cond) ((cond) ? void() : __builtin_unreachable())
#elif ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
// Unimplemented. Uses the same definition as ABSL_ASSERT in the NDEBUG case.
#define ABSL_ASSUME(expr) (decltype((expr) ? void() : void())())
#else
#define ABSL_ASSUME(cond) \
do { \
static_cast<void>(false && (cond)); \
} while (false)
#define ABSL_ASSUME(expr) (false ? ((expr) ? void() : void()) : void())
#endif

// ABSL_INTERNAL_UNIQUE_SMALL_NAME(cond)
Expand Down
12 changes: 12 additions & 0 deletions absl/base/optimization_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@ TEST(PredictTest, ExplicitBoolConversion) {
if (ABSL_PREDICT_FALSE(is_false)) ADD_FAILURE();
}

// This verifies that ABSL_ASSUME compiles in a variety of contexts.
// It does not test optimization.
TEST(AbslAssume, Compiles) {
int x = 0;
ABSL_ASSUME(x >= 0);
EXPECT_EQ(x, 0);

// https://github.com/abseil/abseil-cpp/issues/1814
ABSL_ASSUME(x >= 0), (x >= 0) ? ++x : --x;
EXPECT_EQ(x, 1);
}

} // namespace

0 comments on commit c64f219

Please sign in to comment.