[llvm] [MC] Avoid arithmetic between different enumeration types. NFC (PR #228966)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 4 19:53:26 PDT 2026


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/228966

They are deprecated in C++20 and ill-formed in C++26 (P2864R2).

* Make FirstLiteralRelocationKind and FirstTargetFixupKind constants,
  fixing code like `FirstLiteralRelocationKind + ELF::R_*`
* Make the MachO section attributes constants, fixing
  `MachO::S_COALESCED | MachO::S_ATTR_*` in MC and lld/MachO.

>From 22208d883db673155206902acf4452b8487ec695 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sun, 4 Oct 2026 18:11:16 -0700
Subject: [PATCH] [MC] Avoid arithmetic between different enumeration types.
 NFC

They are deprecated in C++20 and ill-formed in C++26 (P2864R2).

* Make FirstLiteralRelocationKind and FirstTargetFixupKind constants,
  fixing code like `FirstLiteralRelocationKind + ELF::R_*`
* Make the MachO section attributes constants, fixing
  `MachO::S_COALESCED | MachO::S_ATTR_*` in MC and lld/MachO.
---
 llvm/include/llvm/BinaryFormat/MachO.h | 72 ++++++++++++--------------
 llvm/include/llvm/MC/MCFixup.h         | 20 ++++---
 2 files changed, 46 insertions(+), 46 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/MachO.h b/llvm/include/llvm/BinaryFormat/MachO.h
index 0c80b5d458dbc58..ecf9f36fdffcf3b 100644
--- a/llvm/include/llvm/BinaryFormat/MachO.h
+++ b/llvm/include/llvm/BinaryFormat/MachO.h
@@ -183,44 +183,40 @@ enum SectionType : uint32_t {
   LAST_KNOWN_SECTION_TYPE = S_INIT_FUNC_OFFSETS
 };
 
-enum : uint32_t {
-  // Constant masks for the "flags[31:24]" field in llvm::MachO::section and
-  // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
-
-  /// S_ATTR_PURE_INSTRUCTIONS - Section contains only true machine
-  /// instructions.
-  S_ATTR_PURE_INSTRUCTIONS = 0x80000000u,
-  /// S_ATTR_NO_TOC - Section contains coalesced symbols that are not to be
-  /// in a ranlib table of contents.
-  S_ATTR_NO_TOC = 0x40000000u,
-  /// S_ATTR_STRIP_STATIC_SYMS - Ok to strip static symbols in this section
-  /// in files with the MY_DYLDLINK flag.
-  S_ATTR_STRIP_STATIC_SYMS = 0x20000000u,
-  /// S_ATTR_NO_DEAD_STRIP - No dead stripping.
-  S_ATTR_NO_DEAD_STRIP = 0x10000000u,
-  /// S_ATTR_LIVE_SUPPORT - Blocks are live if they reference live blocks.
-  S_ATTR_LIVE_SUPPORT = 0x08000000u,
-  /// S_ATTR_SELF_MODIFYING_CODE - Used with i386 code stubs written on by
-  /// dyld.
-  S_ATTR_SELF_MODIFYING_CODE = 0x04000000u,
-  /// S_ATTR_DEBUG - A debug section.
-  S_ATTR_DEBUG = 0x02000000u,
-
-  // Constant masks for the "flags[23:8]" field in llvm::MachO::section and
-  // llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
-
-  /// S_ATTR_SOME_INSTRUCTIONS - Section contains some machine instructions.
-  S_ATTR_SOME_INSTRUCTIONS = 0x00000400u,
-  /// S_ATTR_EXT_RELOC - Section has external relocation entries.
-  S_ATTR_EXT_RELOC = 0x00000200u,
-  /// S_ATTR_LOC_RELOC - Section has local relocation entries.
-  S_ATTR_LOC_RELOC = 0x00000100u,
-
-  // Constant masks for the value of an indirect symbol in an indirect
-  // symbol table
-  INDIRECT_SYMBOL_LOCAL = 0x80000000u,
-  INDIRECT_SYMBOL_ABS = 0x40000000u
-};
+// Constant masks for the "flags[31:24]" field in llvm::MachO::section and
+// llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_USR)
+
+/// Section contains only true machine instructions.
+constexpr uint32_t S_ATTR_PURE_INSTRUCTIONS = 0x80000000u;
+/// Section contains coalesced symbols that are not to be in a ranlib table of
+/// contents.
+constexpr uint32_t S_ATTR_NO_TOC = 0x40000000u;
+/// Ok to strip static symbols in this section in files with the MY_DYLDLINK
+/// flag.
+constexpr uint32_t S_ATTR_STRIP_STATIC_SYMS = 0x20000000u;
+/// No dead stripping.
+constexpr uint32_t S_ATTR_NO_DEAD_STRIP = 0x10000000u;
+/// Blocks are live if they reference live blocks.
+constexpr uint32_t S_ATTR_LIVE_SUPPORT = 0x08000000u;
+/// Used with i386 code stubs written on by dyld.
+constexpr uint32_t S_ATTR_SELF_MODIFYING_CODE = 0x04000000u;
+/// A debug section.
+constexpr uint32_t S_ATTR_DEBUG = 0x02000000u;
+
+// Constant masks for the "flags[23:8]" field in llvm::MachO::section and
+// llvm::MachO::section_64 (mask "flags" with SECTION_ATTRIBUTES_SYS)
+
+/// Section contains some machine instructions.
+constexpr uint32_t S_ATTR_SOME_INSTRUCTIONS = 0x00000400u;
+/// Section has external relocation entries.
+constexpr uint32_t S_ATTR_EXT_RELOC = 0x00000200u;
+/// Section has local relocation entries.
+constexpr uint32_t S_ATTR_LOC_RELOC = 0x00000100u;
+
+// Constant masks for the value of an indirect symbol in an indirect symbol
+// table.
+constexpr uint32_t INDIRECT_SYMBOL_LOCAL = 0x80000000u;
+constexpr uint32_t INDIRECT_SYMBOL_ABS = 0x40000000u;
 
 enum DataRegionType {
   // Constants for the "kind" field in a data_in_code_entry structure
diff --git a/llvm/include/llvm/MC/MCFixup.h b/llvm/include/llvm/MC/MCFixup.h
index aaf75102fb9edfb..115a9f0a7430fb6 100644
--- a/llvm/include/llvm/MC/MCFixup.h
+++ b/llvm/include/llvm/MC/MCFixup.h
@@ -20,14 +20,18 @@ class MCExpr;
 
 /// Extensible enumeration to represent the type of a fixup.
 using MCFixupKind = uint16_t;
-enum {
-  // [0, FirstLiteralRelocationKind) encodes raw relocation types.
 
-  // [FirstLiteralRelocationKind, FK_NONE) encodes raw relocation types coming
-  // from .reloc directives. Fixup kind
-  // FirstLiteralRelocationKind+t encodes relocation type t.
-  FirstLiteralRelocationKind = 2000,
+// [0, FirstLiteralRelocationKind) encodes raw relocation types.
+//
+// [FirstLiteralRelocationKind, FK_NONE) encodes raw relocation types coming
+// from .reloc directives. Fixup kind FirstLiteralRelocationKind+t encodes
+// relocation type t.
+//
+// The range boundaries are not enumerators, so that arithmetic with ELF::R_*
+// or target fixup enumerators does not mix enumeration types.
+constexpr MCFixupKind FirstLiteralRelocationKind = 2000;
 
+enum {
   // Other kinds indicate the fixup may resolve to a constant, allowing the
   // assembler to update the instruction or data directly without a relocation.
   FK_NONE = 4000, ///< A no-op fixup.
@@ -40,10 +44,10 @@ enum {
   FK_SecRel_2,    ///< A two-byte section relative fixup.
   FK_SecRel_4,    ///< A four-byte section relative fixup.
   FK_SecRel_8,    ///< A eight-byte section relative fixup.
-
-  FirstTargetFixupKind,
 };
 
+constexpr MCFixupKind FirstTargetFixupKind = FK_SecRel_8 + 1;
+
 /// Encode information on a single operation to perform on a byte
 /// sequence (e.g., an encoded instruction) which requires assemble- or run-
 /// time patching.



More information about the llvm-commits mailing list