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

Benji Smith via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 13:13:56 PDT 2024


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

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

>From f7e6cbb4ecd7b95431c72d868030dd1674f26970 Mon Sep 17 00:00:00 2001
From: Benji Smith <benjsith at gmail.com>
Date: Tue, 30 Jul 2024 15:51:39 -0400
Subject: [PATCH] [BOLT] Fix C2360 compiler error with MSVC in Relocation.cpp

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
---
 bolt/lib/Core/Relocation.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

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();



More information about the llvm-commits mailing list