[llvm] 7a66fe1 - Wrap `llvm_unreachable` macro in do-while loop

Stefan Gränitz via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 8 04:16:11 PDT 2022


Author: Stefan Gränitz
Date: 2022-08-08T13:15:32+02:00
New Revision: 7a66fe1075cfc7568554aeea9c40997dfb581979

URL: https://github.com/llvm/llvm-project/commit/7a66fe1075cfc7568554aeea9c40997dfb581979
DIFF: https://github.com/llvm/llvm-project/commit/7a66fe1075cfc7568554aeea9c40997dfb581979.diff

LOG: Wrap `llvm_unreachable` macro in do-while loop

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

Added: 
    

Modified: 
    llvm/include/llvm/Support/ErrorHandling.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/ErrorHandling.h b/llvm/include/llvm/Support/ErrorHandling.h
index 004b3b7868fb..9c8e3448f3a0 100644
--- a/llvm/include/llvm/Support/ErrorHandling.h
+++ b/llvm/include/llvm/Support/ErrorHandling.h
@@ -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


        


More information about the llvm-commits mailing list