Skip to content

Commit 1c222cb

Browse files
author
Enrico Steffinlongo
committed
Fixed UNREACHABLE missing return warning on GCC 12
GCC 12 when using `-O0` fails because of a missing-return-statement warning caused by the implementation of `INVARIANT(false)`. This is because the condition `if (true)` is not considered exahustive. The solution involves marking the unreachable path as `__builtin_unreachable()`.
1 parent 68af7a4 commit 1c222cb

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/util/invariant.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,26 @@ CBMC_NORETURN void report_invariant_failure(
500500
EXPAND_MACRO(INVARIANT_STRUCTURED(CONDITION, TYPENAME, __VA_ARGS__))
501501

502502
/// This should be used to mark dead code
503-
#define UNREACHABLE INVARIANT(false, "Unreachable")
504-
#define UNREACHABLE_STRUCTURED(TYPENAME, ...) \
505-
EXPAND_MACRO(INVARIANT_STRUCTURED(false, TYPENAME, __VA_ARGS__))
503+
#ifdef __GNUC__
504+
// GCC 12 with -O0 fails reporting missing return when using UNREACHABLE.
505+
// Using __builtin_unreachable fixes this without breaking the invariant.
506+
# define UNREACHABLE \
507+
do \
508+
{ \
509+
INVARIANT(false, "Unreachable"); \
510+
__builtin_unreachable(); \
511+
} while(false)
512+
# define UNREACHABLE_STRUCTURED(TYPENAME, ...) \
513+
do \
514+
{ \
515+
EXPAND_MACRO(INVARIANT_STRUCTURED(false, TYPENAME, __VA_ARGS__)); \
516+
__builtin_unreachable(); \
517+
} while(false)
518+
#else
519+
# define UNREACHABLE INVARIANT(false, "Unreachable")
520+
# define UNREACHABLE_STRUCTURED(TYPENAME, ...) \
521+
EXPAND_MACRO(INVARIANT_STRUCTURED(false, TYPENAME, __VA_ARGS__))
522+
#endif
506523

507524
/// This condition should be used to document that assumptions that are
508525
/// made on goto_functions, goto_programs, exprts, etc. being well formed.

0 commit comments

Comments
 (0)