https://github.com/fyrsta7 created
https://github.com/llvm/llvm-project/pull/215837
Clang-format currently changes compact TableGen bit ranges such as
`Inst{24-20}` to `Inst{24 -20}`. TableGen lexes the lower bound as a negative
integer token, so the generic rule that separates adjacent word-like tokens
inserts the unwanted space.
Keep adjacent numeric tokens together in TableGen when the second token is a
negative integer. This preserves the established compact spelling of
hyphenated bit ranges. Add coverage for both a single range and a
comma-separated range list, plus a release note for the user-visible
formatting fix.
Tests:
- `build-pr/tools/clang/unittests/Format/FormatTests` (1276 passed)
- `ninja -C build-pr check-clang-format` (33 passed)
- `python3 clang/tools/clang-format/git-clang-format --binary
build-pr/bin/clang-format --diff origin/main`
- `git diff --check origin/main...HEAD`
Fixes #177051
Assisted-by: OpenAI Codex
>From d10cd6bb26ca4ee652779adaecadecb9974bf97f 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
TableGen lexes the lower bound of a hyphenated bit range as a negative
integer token. Avoid separating adjacent numeric tokens in TableGen so ranges
such as {24-20} remain compact.
Fixes #177051
Assisted-by: OpenAI Codex
---
clang/docs/ReleaseNotes.md | 3 +++
clang/lib/Format/TokenAnnotator.cpp | 6 ++++++
clang/unittests/Format/FormatTestTableGen.cpp | 7 +++++++
3 files changed, 16 insertions(+)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 9648ad429d040..daaf7ca983465 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -568,6 +568,9 @@ features cannot lower the translation-unit ABI level;
- Add `SpacesInBlockComments` option to control spacing after `/*` and
before `*/` in ordinary block comments.
+- Fixed formatting of TableGen bit ranges so that hyphen separators remain
+ compact, as in `{24-20}`. (#GH177051)
+
### libclang
- visit identifier initializers in lambda capture as VarDecl instead of
VariableRef. Warning: this changes behaviour.
diff --git a/clang/lib/Format/TokenAnnotator.cpp
b/clang/lib/Format/TokenAnnotator.cpp
index b6c33279b0aca..349ce2b28ae4a 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);
+ // TableGen lexes the lower bound of a bit range as a negative integer.
+ 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