https://github.com/fyrsta7 updated 
https://github.com/llvm/llvm-project/pull/215837

>From 725ffa78cb2e2b7419aa25a03abee2bd7f79b6ab Mon Sep 17 00:00:00 2001
From: Yuwei Zhao <[email protected]>
Date: Wed, 12 Aug 2026 23:41:26 +0800
Subject: [PATCH] [clang-format] Preserve compact TableGen bit ranges

Keep hyphenated TableGen bit ranges such as {24-20} compact instead of
inserting a space before the hyphen. Add coverage for single and
comma-separated bit ranges.

Fixes #177051

Assisted-by: OpenAI Codex
---
 clang/lib/Format/TokenAnnotator.cpp           | 6 ++++++
 clang/unittests/Format/FormatTestTableGen.cpp | 7 +++++++
 2 files changed, 13 insertions(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index b6c33279b0aca..be938bd239040 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5208,6 +5208,12 @@ bool TokenAnnotator::spaceRequiredBefore(const 
AnnotatedLine &Line,
   const bool IsVerilog = Style.isVerilog();
   assert(!IsVerilog || !IsCpp);
 
+  // Keep TableGen bit ranges such as {24-20} compact.
+  if (Style.isTableGen() && Left.is(tok::numeric_constant) &&
+      Right.is(tok::numeric_constant) && Right.TokenText.starts_with("-")) {
+    return false;
+  }
+
   // Never ever merge two words.
   if (Keywords.isWordLike(Right, IsVerilog) &&
       Keywords.isWordLike(Left, IsVerilog)) {
diff --git a/clang/unittests/Format/FormatTestTableGen.cpp 
b/clang/unittests/Format/FormatTestTableGen.cpp
index df20cc26e1094..da0f1b155f2f1 100644
--- a/clang/unittests/Format/FormatTestTableGen.cpp
+++ b/clang/unittests/Format/FormatTestTableGen.cpp
@@ -216,6 +216,13 @@ TEST_F(FormatTestTableGen, ValueSuffix) {
                "}");
 }
 
+TEST_F(FormatTestTableGen, BitRanges) {
+  verifyFormat("def Ranges {\n"
+               "  let Inst{24-20} = rs2;\n"
+               "  let Inst{19-15, 11-7} = rs1;\n"
+               "}");
+}
+
 TEST_F(FormatTestTableGen, PasteOperator) {
   verifyFormat("def Paste#\"Operator\" { string Paste = \"Paste\"#operator; 
}");
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to