Skip to content

Commit ec583f2

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Fixing macro expansion changes in new logging macros.
This was an unintentional behavior change when we added a new layer of macros. Not using function-like macro aliases would get around this, but unfortunately that would flood the macro namespace downstream with CHECK and LOG (and break existing code). Note, the old behavior only applied to CHECK and QCHECK. Other CHECK macros already had multiple layers of function-like macros and were unaffected. PiperOrigin-RevId: 493984662 Change-Id: I9a050dcaf01f2b6935f02cd42e23bc3a4d5fc62a
1 parent c353e25 commit ec583f2

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

absl/log/absl_check.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
#include "absl/log/internal/check_impl.h"
3939

40-
#define ABSL_CHECK(condition) ABSL_CHECK_IMPL(condition)
41-
#define ABSL_QCHECK(condition) ABSL_QCHECK_IMPL(condition)
40+
#define ABSL_CHECK(condition) ABSL_CHECK_IMPL(condition, #condition)
41+
#define ABSL_QCHECK(condition) ABSL_QCHECK_IMPL(condition, #condition)
4242
#define ABSL_PCHECK(condition) ABSL_PCHECK_IMPL(condition)
4343
#define ABSL_DCHECK(condition) ABSL_DCHECK_IMPL(condition)
4444

absl/log/check.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@
5454
// Might produce a message like:
5555
//
5656
// Check failed: !cheese.empty() Out of Cheese
57-
#define CHECK(condition) ABSL_CHECK_IMPL(condition)
57+
#define CHECK(condition) ABSL_CHECK_IMPL(condition, #condition)
5858

5959
// QCHECK()
6060
//
6161
// `QCHECK` behaves like `CHECK` but does not print a full stack trace and does
6262
// not run registered error handlers (as `QFATAL`). It is useful when the
6363
// problem is definitely unrelated to program flow, e.g. when validating user
6464
// input.
65-
#define QCHECK(condition) ABSL_QCHECK_IMPL(condition)
65+
#define QCHECK(condition) ABSL_QCHECK_IMPL(condition, #condition)
6666

6767
// PCHECK()
6868
//

absl/log/check_test_impl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ TEST(CHECKDeathTest, TestChecksWithSideEffects) {
120120

121121
#if GTEST_HAS_DEATH_TEST
122122

123+
TEST(CHECKTest, TestMacroExpansionInMessage) {
124+
#define MACRO(x) x
125+
auto MessageGen = []() { ABSL_TEST_CHECK(MACRO(false)); };
126+
EXPECT_DEATH(MessageGen(), HasSubstr("MACRO(false)"));
127+
#undef MACRO
128+
}
129+
130+
TEST(CHECKTest, TestNestedMacroExpansionInMessage) {
131+
#define MACRO(x) x
132+
EXPECT_DEATH(ABSL_TEST_CHECK(MACRO(false)), HasSubstr("MACRO(false)"));
133+
#undef MACRO
134+
}
135+
123136
TEST(CHECKDeachTest, TestOrderOfInvocationsBetweenCheckAndMessage) {
124137
int counter = 0;
125138

absl/log/internal/check_impl.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@
2222
#include "absl/log/internal/strip.h"
2323

2424
// CHECK
25-
#define ABSL_CHECK_IMPL(condition) \
25+
#define ABSL_CHECK_IMPL(condition, condition_str) \
2626
ABSL_LOG_INTERNAL_CONDITION_FATAL(STATELESS, \
2727
ABSL_PREDICT_FALSE(!(condition))) \
28-
ABSL_LOG_INTERNAL_CHECK(#condition).InternalStream()
28+
ABSL_LOG_INTERNAL_CHECK(condition_str).InternalStream()
2929

30-
#define ABSL_QCHECK_IMPL(condition) \
30+
#define ABSL_QCHECK_IMPL(condition, condition_str) \
3131
ABSL_LOG_INTERNAL_CONDITION_QFATAL(STATELESS, \
3232
ABSL_PREDICT_FALSE(!(condition))) \
33-
ABSL_LOG_INTERNAL_QCHECK(#condition).InternalStream()
33+
ABSL_LOG_INTERNAL_QCHECK(condition_str).InternalStream()
3434

35-
#define ABSL_PCHECK_IMPL(condition) ABSL_CHECK_IMPL(condition).WithPerror()
35+
#define ABSL_PCHECK_IMPL(condition) \
36+
ABSL_CHECK_IMPL(condition, #condition).WithPerror()
3637

3738
#ifndef NDEBUG
38-
#define ABSL_DCHECK_IMPL(condition) ABSL_CHECK_IMPL(condition)
39+
#define ABSL_DCHECK_IMPL(condition) ABSL_CHECK_IMPL(condition, #condition)
3940
#else
40-
#define ABSL_DCHECK_IMPL(condition) ABSL_CHECK_IMPL(true || (condition))
41+
#define ABSL_DCHECK_IMPL(condition) ABSL_CHECK_IMPL(true || (condition), "true")
4142
#endif
4243

4344
// CHECK_EQ

0 commit comments

Comments
 (0)