Skip to content

Conversation

@jrtc27
Copy link
Collaborator

@jrtc27 jrtc27 commented Jul 22, 2025

The current implementation is dangerous if used in contexts that need a
single statement, since invokeELFT(...); is in fact two statements, a
switch statement and an empty statement.

@llvmbot
Copy link
Member

llvmbot commented Jul 22, 2025

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: Jessica Clarke (jrtc27)

Changes

The current implementation is dangerous if used in contexts that need a
single statement, since invokeELFT(...); is in fact two statements, a
switch statement and an empty statement.


Full diff: https://github.com/llvm/llvm-project/pull/150119.diff

1 Files Affected:

  • (modified) lld/ELF/Target.h (+18-16)
diff --git a/lld/ELF/Target.h b/lld/ELF/Target.h
index 93f15920bfedb..fc20dae87584c 100644
--- a/lld/ELF/Target.h
+++ b/lld/ELF/Target.h
@@ -338,21 +338,23 @@ inline uint64_t overwriteULEB128(uint8_t *bufLoc, uint64_t val) {
 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
 #endif
 #define invokeELFT(f, ...)                                                     \
-  switch (ctx.arg.ekind) {                                                     \
-  case lld::elf::ELF32LEKind:                                                  \
-    f<llvm::object::ELF32LE>(__VA_ARGS__);                                     \
-    break;                                                                     \
-  case lld::elf::ELF32BEKind:                                                  \
-    f<llvm::object::ELF32BE>(__VA_ARGS__);                                     \
-    break;                                                                     \
-  case lld::elf::ELF64LEKind:                                                  \
-    f<llvm::object::ELF64LE>(__VA_ARGS__);                                     \
-    break;                                                                     \
-  case lld::elf::ELF64BEKind:                                                  \
-    f<llvm::object::ELF64BE>(__VA_ARGS__);                                     \
-    break;                                                                     \
-  default:                                                                     \
-    llvm_unreachable("unknown ctx.arg.ekind");                                 \
-  }
+  do {                                                                         \
+      switch (ctx.arg.ekind) {                                                 \
+      case lld::elf::ELF32LEKind:                                              \
+        f<llvm::object::ELF32LE>(__VA_ARGS__);                                 \
+        break;                                                                 \
+      case lld::elf::ELF32BEKind:                                              \
+        f<llvm::object::ELF32BE>(__VA_ARGS__);                                 \
+        break;                                                                 \
+      case lld::elf::ELF64LEKind:                                              \
+        f<llvm::object::ELF64LE>(__VA_ARGS__);                                 \
+        break;                                                                 \
+      case lld::elf::ELF64BEKind:                                              \
+        f<llvm::object::ELF64BE>(__VA_ARGS__);                                 \
+        break;                                                                 \
+      default:                                                                 \
+        llvm_unreachable("unknown ctx.arg.ekind");                             \
+      }                                                                        \
+  } while (0)
 
 #endif

@github-actions
Copy link

github-actions bot commented Jul 22, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

…ction

The current implementation is dangerous if used in contexts that need a
single statement, since invokeELFT(...); is in fact two statements, a
switch statement and an empty statement.
@jrtc27 jrtc27 force-pushed the invokeELFT-function-like branch from e64d09c to 36e3aa9 Compare July 22, 2025 21:54
@jrtc27 jrtc27 merged commit f8685a8 into llvm:main Jul 26, 2025
9 checks passed
@jrtc27 jrtc27 deleted the invokeELFT-function-like branch July 26, 2025 23:21
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
…ction (llvm#150119)

The current implementation is dangerous if used in contexts that need a
single statement, since invokeELFT(...); is in fact two statements, a
switch statement and an empty statement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants