From: sstwcw Date: Sun, 30 Apr 2023 22:26:41 +0000 (+0000) Subject: [clang-format] Recognize Verilog type dimension in module header X-Git-Tag: upstream/17.0.6~9924 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4134f836103ebc70cc23a80c7a966d1d5f3a6353;p=platform%2Fupstream%2Fllvm.git [clang-format] Recognize Verilog type dimension in module header We had the function `verilogGroupDecl` for that. However, the type name would be incorrectly annotated in `isStartOfName` when it was not a C++ keyword and followed another identifier. Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D149352 --- diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 0bf7e9a..be73485 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2165,6 +2165,10 @@ private: /// This is a heuristic based on whether \p Tok is an identifier following /// something that is likely a type. bool isStartOfName(const FormatToken &Tok) { + // Handled in ExpressionParser for Verilog. + if (Style.isVerilog()) + return false; + if (Tok.isNot(tok::identifier) || !Tok.Previous) return false; diff --git a/clang/unittests/Format/FormatTestVerilog.cpp b/clang/unittests/Format/FormatTestVerilog.cpp index ff6a1cd..a7ba3c5 100644 --- a/clang/unittests/Format/FormatTestVerilog.cpp +++ b/clang/unittests/Format/FormatTestVerilog.cpp @@ -359,6 +359,12 @@ TEST_F(FormatTestVerilog, Headers) { " input var shortreal in2,\n" " output tagged_st out);\n" "endmodule"); + // There should be a space following the type but not the variable name. + verifyFormat("module test\n" + " (input wire [7 : 0] a,\n" + " input wire b[7 : 0],\n" + " input wire [7 : 0] c[7 : 0]);\n" + "endmodule"); // Ports should be grouped by types. verifyFormat("module test\n" " (input [7 : 0] a,\n" diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 62a8359..908a81d 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1615,6 +1615,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) { Tokens = Annotate("extern function [1 : 0] x;"); ASSERT_EQ(Tokens.size(), 10u) << Tokens; EXPECT_TOKEN(Tokens[4], tok::colon, TT_BitFieldColon); + Tokens = Annotate("module test\n" + " (input wire [7 : 0] a[7 : 0]);\n" + "endmodule"); + ASSERT_EQ(Tokens.size(), 20u) << Tokens; + EXPECT_TOKEN(Tokens[4], tok::identifier, TT_VerilogDimensionedTypeName); + EXPECT_TOKEN(Tokens[7], tok::colon, TT_BitFieldColon); + EXPECT_TOKEN(Tokens[13], tok::colon, TT_BitFieldColon); // Test case labels and ternary operators. Tokens = Annotate("case (x)\n" " x:\n"