Wrap `llvm_unreachable` macro in do-while loop
authorStefan Gränitz <stefan.graenitz@gmail.com>
Thu, 4 Aug 2022 11:58:26 +0000 (13:58 +0200)
committerStefan Gränitz <stefan.graenitz@gmail.com>
Mon, 8 Aug 2022 11:15:32 +0000 (13:15 +0200)
Macros that expand into multiple terms can cause interesting preprocessor hickups depending
on the context they are used in. https://github.com/llvm/llvm-project/issues/56867 reported
a miscompilation of `llvm_unreachable(msg)` inside a `LLVM_DEBUG({ ... })` block. We were
able to fix it by wrapping the expansion in a `do {} while(false)`.

Differential Revision: https://reviews.llvm.org/D131337

llvm/include/llvm/Support/ErrorHandling.h

index 004b3b7868fb597bd3ade91de80fda2c38128ec3..9c8e3448f3a03e3540adb8b9dd730c77dd9b20ba 100644 (file)
@@ -147,7 +147,11 @@ llvm_unreachable_internal(const char *msg = nullptr, const char *file = nullptr,
 #elif LLVM_UNREACHABLE_OPTIMIZE
 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
 #else
-#define llvm_unreachable(msg) LLVM_BUILTIN_TRAP, LLVM_BUILTIN_UNREACHABLE
+#define llvm_unreachable(msg)                                                  \
+  do {                                                                         \
+    LLVM_BUILTIN_TRAP;                                                         \
+    LLVM_BUILTIN_UNREACHABLE;                                                  \
+  } while (false)
 #endif
 
 #endif