Skip to content

Commit d2d7f76

Browse files
authored
Merge pull request #7665 from esteffin/esteffin/gcc-12-unreachable-fix
Fixup GCC 12 errors
2 parents 40401ce + b829f63 commit d2d7f76

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

regression/invariants/driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ int main(int argc, char** argv)
112112
UNREACHABLE_STRUCTURED(structured_error_testt, 1, "Structured error"); // NOLINT
113113
else if(arg=="unreachable-string")
114114
UNREACHABLE;
115+
else if(arg == "unreachable-because")
116+
UNREACHABLE_BECAUSE("Unreachable with explanation text");
115117
else if(arg=="data-invariant-structured")
116118
DATA_INVARIANT_STRUCTURED(false, structured_error_testt, 1, "Structured error"); // NOLINT
117119
else if(arg=="data-invariant-string")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE
2+
dummy_parameter.c
3+
unreachable-because
4+
^EXIT=(0|127|134|137)$
5+
^SIGNAL=0$
6+
--- begin invariant violation report ---
7+
Unreachable with explanation text
8+
Invariant check failed
9+
^(Backtrace)|(Backtraces not supported)$
10+
--
11+
^warning: ignoring
12+
^VERIFICATION SUCCESSFUL$

src/solvers/smt2/smt2_conv.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,9 @@ constant_exprt smt2_convt::parse_literal(
516516
return from_integer(value, type);
517517
}
518518
else
519-
INVARIANT(
520-
false,
519+
UNREACHABLE_BECAUSE(
521520
"smt2_convt::parse_literal should not be of unsupported type " +
522-
type.id_string());
521+
type.id_string());
523522
}
524523

525524
exprt smt2_convt::parse_array(

src/solvers/strings/string_format_builtin_function.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ add_axioms_for_format_specifier(
232232
}
233233
}
234234

235-
INVARIANT(false, "format specifier must belong to [bBhHsScCdoxXeEfgGaAtT%n]");
235+
UNREACHABLE_BECAUSE(
236+
"format specifier must belong to [bBhHsScCdoxXeEfgGaAtT%n]");
236237
}
237238

238239
/// Deserialize an argument for format from \p string.

src/util/invariant.h

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,33 @@ 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_BECAUSE(REASON) \
513+
do \
514+
{ \
515+
INVARIANT(false, REASON); \
516+
__builtin_unreachable(); \
517+
} while(false)
518+
# define UNREACHABLE_STRUCTURED(TYPENAME, ...) \
519+
do \
520+
{ \
521+
EXPAND_MACRO(INVARIANT_STRUCTURED(false, TYPENAME, __VA_ARGS__)); \
522+
__builtin_unreachable(); \
523+
} while(false)
524+
#else
525+
# define UNREACHABLE INVARIANT(false, "Unreachable")
526+
# define UNREACHABLE_BECAUSE(REASON) INVARIANT(false, REASON)
527+
# define UNREACHABLE_STRUCTURED(TYPENAME, ...) \
528+
EXPAND_MACRO(INVARIANT_STRUCTURED(false, TYPENAME, __VA_ARGS__))
529+
#endif
506530

507531
/// This condition should be used to document that assumptions that are
508532
/// made on goto_functions, goto_programs, exprts, etc. being well formed.
@@ -514,8 +538,17 @@ CBMC_NORETURN void report_invariant_failure(
514538
#define DATA_INVARIANT_STRUCTURED(CONDITION, TYPENAME, ...) \
515539
EXPAND_MACRO(INVARIANT_STRUCTURED(CONDITION, TYPENAME, __VA_ARGS__))
516540

517-
#define UNIMPLEMENTED_FEATURE(FEATURE) \
518-
INVARIANT(false, std::string{"Reached unimplemented "} + (FEATURE))
541+
#if __GNUC__
542+
# define UNIMPLEMENTED_FEATURE(FEATURE) \
543+
do \
544+
{ \
545+
INVARIANT(false, std::string{"Reached unimplemented "} + (FEATURE)); \
546+
__builtin_unreachable(); \
547+
} while(false)
548+
#else
549+
# define UNIMPLEMENTED_FEATURE(FEATURE) \
550+
INVARIANT(false, std::string{"Reached unimplemented "} + (FEATURE))
551+
#endif
519552

520553
// Legacy annotations
521554

0 commit comments

Comments
 (0)