[clang-format] Preserve compact TableGen bit ranges - #215837
Conversation
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
|
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.
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 questionsHow 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 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, |
|
@llvm/pr-subscribers-clang-format Author: Yuwei Zhao (fyrsta7) ChangesClang-format currently changes compact TableGen bit ranges such as Keep adjacent numeric tokens together in TableGen when the second token is a Tests:
Fixes #177051 Assisted-by: OpenAI Codex Full diff: https://github.com/llvm/llvm-project/pull/215837.diff 3 Files Affected:
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; }");
|
|
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. |
Clang-format currently changes compact TableGen bit ranges such as
Inst{24-20}toInst{24 -20}. TableGen lexes the lower bound as a negativeinteger 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/maingit diff --check origin/main...HEADFixes #177051
Assisted-by: OpenAI Codex