Skip to content

Conversation

@sstwcw
Copy link
Contributor

@sstwcw sstwcw commented Oct 29, 2025

When formatting Verilog code, the program changes the hash to kw_verilogHash and the backtick to tok::hash. The developer did not take that into account when writing the part for recognizing the #-# and #=# operators. The part did not work. The program would add a space within the operator.

after

##[0 : 5] done #-# always !rst;

before

##[0 : 5] done #- #always !rst;

When formatting Verilog code, the program changes the hash to
`kw_verilogHash` and the backtick to `tok::hash`.  The developer did not
take that into account when writing the part for recognizing the `#-#`
and `#=#` operators.  The part did not work.  The program would add a
space within the operator.

after

```SystemVerilog
##[0 : 5] done #-# always !rst;
```

before

```SystemVerilog
##[0 : 5] done #- #always !rst;
```
@llvmbot
Copy link
Member

llvmbot commented Oct 29, 2025

@llvm/pr-subscribers-clang-format

Author: None (sstwcw)

Changes

When formatting Verilog code, the program changes the hash to kw_verilogHash and the backtick to tok::hash. The developer did not take that into account when writing the part for recognizing the #-# and #=# operators. The part did not work. The program would add a space within the operator.

after

##[0 : 5] done #-# always !rst;

before

##[0 : 5] done #- #always !rst;

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

2 Files Affected:

  • (modified) clang/lib/Format/FormatTokenLexer.cpp (+10-3)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+1)
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index ab3293841a2a4..a9ea5ec9009c4 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -318,14 +318,21 @@ void FormatTokenLexer::tryMergePreviousTokens() {
                            {tok::equal, tok::greater},
                            {tok::star, tok::greater},
                            {tok::pipeequal, tok::greater},
-                           {tok::pipe, tok::arrow},
-                           {tok::hash, tok::minus, tok::hash},
-                           {tok::hash, tok::equal, tok::hash}},
+                           {tok::pipe, tok::arrow}},
                           TT_BinaryOperator) ||
         Tokens.back()->is(tok::arrow)) {
       Tokens.back()->ForcedPrecedence = prec::Comma;
       return;
     }
+    if (Tokens.size() >= 3 &&
+        Tokens[Tokens.size() - 3]->is(Keywords.kw_verilogHash) &&
+        Tokens[Tokens.size() - 2]->isOneOf(tok::minus, tok::equal) &&
+        Tokens[Tokens.size() - 1]->is(Keywords.kw_verilogHash) &&
+        tryMergeTokens(3, TT_BinaryOperator)) {
+      Tokens.back()->setFinalizedType(TT_BinaryOperator);
+      Tokens.back()->ForcedPrecedence = prec::Comma;
+      return;
+    }
   } else if (Style.isTableGen()) {
     // TableGen's Multi line string starts with [{
     if (tryMergeTokens({tok::l_square, tok::l_brace},
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index c046142c613b0..6c05feaeb13e0 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2686,6 +2686,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) {
   // precedence.
   std::pair<prec::Level, std::string> JoinedBinary[] = {
       {prec::Comma, "->"},        {prec::Comma, "<->"},
+      {prec::Comma, "#-#"},       {prec::Comma, "#=#"},
       {prec::Assignment, "+="},   {prec::Assignment, "-="},
       {prec::Assignment, "*="},   {prec::Assignment, "/="},
       {prec::Assignment, "%="},   {prec::Assignment, "&="},

@sstwcw sstwcw merged commit 43177bf into llvm:main Nov 3, 2025
12 checks passed
@sstwcw sstwcw deleted the format-verilog-operator branch November 3, 2025 03:55
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.

3 participants