[llvm] [BOLT] Fix C2360 compiler error with MSVC in Relocation.cpp (PR #101235)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 13:14:29 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-bolt

Author: Benji Smith (Benjins)

<details>
<summary>Changes</summary>

Since 67e733d36ef0c5ec9fab899ecf9f191d83c7d418, `Relocation::print` has defined arrays for relocation names on X86 and AArch64 inside the switch cases. Since these arrays are static, this should be legal according to the spec. However, MSVC still emits an error when compiling these

The fix is to add brackets around the relevant case statements, the same as for if the declared variables were not static

---

There is a recently-opened issue on the Visual Studio feedback forums about this: https://developercommunity.visualstudio.com/t/C2360-and-C2361-should-not-apply-to-stat/10712105  But even if it does get fixed in an update to MSVC, existing versions would still run into this issue when compiling BOLT. The workaround seems small and unobtrusive enough

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


1 Files Affected:

- (modified) bolt/lib/Core/Relocation.cpp (+4-2) 


``````````diff
diff --git a/bolt/lib/Core/Relocation.cpp b/bolt/lib/Core/Relocation.cpp
index 4e888a5b147ac..cc12fd0b03f57 100644
--- a/bolt/lib/Core/Relocation.cpp
+++ b/bolt/lib/Core/Relocation.cpp
@@ -1070,7 +1070,7 @@ void Relocation::print(raw_ostream &OS) const {
     OS << "RType:" << Twine::utohexstr(Type);
     break;
 
-  case Triple::aarch64:
+  case Triple::aarch64: {
     static const char *const AArch64RelocNames[] = {
 #define ELF_RELOC(name, value) #name,
 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
@@ -1079,6 +1079,7 @@ void Relocation::print(raw_ostream &OS) const {
     assert(Type < ArrayRef(AArch64RelocNames).size());
     OS << AArch64RelocNames[Type];
     break;
+  }
 
   case Triple::riscv64:
     // RISC-V relocations are not sequentially numbered so we cannot use an
@@ -1095,7 +1096,7 @@ void Relocation::print(raw_ostream &OS) const {
     }
     break;
 
-  case Triple::x86_64:
+  case Triple::x86_64: {
     static const char *const X86RelocNames[] = {
 #define ELF_RELOC(name, value) #name,
 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
@@ -1105,6 +1106,7 @@ void Relocation::print(raw_ostream &OS) const {
     OS << X86RelocNames[Type];
     break;
   }
+  }
   OS << ", 0x" << Twine::utohexstr(Offset);
   if (Symbol) {
     OS << ", " << Symbol->getName();

``````````

</details>


https://github.com/llvm/llvm-project/pull/101235


More information about the llvm-commits mailing list