From 43e07a0ee0ee1111186a69bf32f65a0a2a24e387 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 24 Apr 2023 07:41:57 -0700 Subject: Fix handling of `CHECK` macros in static analysis tools. Currently static analysis tools built using the Clang Dataflow Analysis framework can't prove that code under `switch (0) case 0:` is executed on all paths. The Clang Dataflow Analysis framework should ultimately be improved to handle these cases. In the meantime, to enable the use of such tools in their current state, we add a `default` case back to the `switch` statement in `ABSL_LOG_INTERNAL_STATELESS_CONDITION` to help them understand that the code is executed on all paths. PiperOrigin-RevId: 526638852 Change-Id: I49490dd477ba777aae2530697b75b583242aebc5 --- absl/log/internal/conditions.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'absl/log') diff --git a/absl/log/internal/conditions.h b/absl/log/internal/conditions.h index b89f1dfd..b3ce2574 100644 --- a/absl/log/internal/conditions.h +++ b/absl/log/internal/conditions.h @@ -56,9 +56,15 @@ // the ternary expression does a better job avoiding spurious diagnostics // (dangling else, missing switch case) and preserving noreturn semantics (e.g. // on `LOG(FATAL)`) without requiring braces. +// +// The `switch` ensures that this expansion is the begnning of a statement (as +// opposed to an expression) and prevents shenanigans like +// `AFunction(LOG(INFO))` and `decltype(LOG(INFO))`. The apparently-redundant +// `default` case makes the condition more amenable to Clang dataflow analysis. #define ABSL_LOG_INTERNAL_STATELESS_CONDITION(condition) \ switch (0) \ case 0: \ + default: \ !(condition) ? (void)0 : ::absl::log_internal::Voidify()&& // `ABSL_LOG_INTERNAL_STATEFUL_CONDITION` applies a condition like -- cgit v1.2.3