Skip to content

[clang-format] Preserve compact TableGen bit ranges - #215837

Open
fyrsta7 wants to merge 1 commit into
llvm:mainfrom
fyrsta7:codex/clang-format-tablegen-range-177051
Open

[clang-format] Preserve compact TableGen bit ranges#215837
fyrsta7 wants to merge 1 commit into
llvm:mainfrom
fyrsta7:codex/clang-format-tablegen-range-177051

Conversation

@fyrsta7

@fyrsta7 fyrsta7 commented Aug 12, 2026

Copy link
Copy Markdown

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

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 llvm#177051

Assisted-by: OpenAI Codex
@github-actions

Copy link
Copy Markdown

Hello @fyrsta7 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-clang-format

Author: Yuwei Zhao (fyrsta7)

Changes

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


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.md (+3)
  • (modified) clang/lib/Format/TokenAnnotator.cpp (+6)
  • (modified) clang/unittests/Format/FormatTestTableGen.cpp (+7)
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; }");
 

@fyrsta7

fyrsta7 commented Aug 13, 2026

Copy link
Copy Markdown
Author

I have read the linked LLVM policies, reviewed and understand all code and text in this PR, and disclosed the AI assistance in the PR description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang-format] Range assignments in TableGen are formatted oddly

1 participant