xref: /llvm-project/clang/unittests/Format/FormatTestVerilog.cpp (revision ef18986b2033a44e69b7c3553a356e9037ac1413)
1 //===- unittest/Format/FormatTestVerilog.cpp ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FormatTestBase.h"
10 
11 #define DEBUG_TYPE "format-test"
12 
13 namespace clang {
14 namespace format {
15 namespace test {
16 namespace {
17 class FormatTestVerilog : public test::FormatTestBase {
18 protected:
19   FormatStyle getDefaultStyle() const override {
20     return getLLVMStyle(FormatStyle::LK_Verilog);
21   }
22   std::string messUp(StringRef Code) const override {
23     return test::messUp(Code, /*HandleHash=*/false);
24   }
25 };
26 
27 TEST_F(FormatTestVerilog, Align) {
28   FormatStyle Style = getDefaultStyle();
29   Style.AlignConsecutiveAssignments.Enabled = true;
30   verifyFormat("x            <= x;\n"
31                "sfdbddfbdfbb <= x;\n"
32                "x             = x;",
33                Style);
34   verifyFormat("x            = x;\n"
35                "sfdbddfbdfbb = x;\n"
36                "x            = x;",
37                Style);
38   // Compound assignments are not aligned by default. '<=' is not a compound
39   // assignment.
40   verifyFormat("x            <= x;\n"
41                "sfdbddfbdfbb <= x;",
42                Style);
43   verifyFormat("x += x;\n"
44                "sfdbddfbdfbb <= x;",
45                Style);
46   verifyFormat("x <<= x;\n"
47                "sfdbddfbdfbb <= x;",
48                Style);
49   verifyFormat("x <<<= x;\n"
50                "sfdbddfbdfbb <= x;",
51                Style);
52   verifyFormat("x >>= x;\n"
53                "sfdbddfbdfbb <= x;",
54                Style);
55   verifyFormat("x >>>= x;\n"
56                "sfdbddfbdfbb <= x;",
57                Style);
58   Style.AlignConsecutiveAssignments.AlignCompound = true;
59   verifyFormat("x            <= x;\n"
60                "sfdbddfbdfbb <= x;",
61                Style);
62   verifyFormat("x            += x;\n"
63                "sfdbddfbdfbb <= x;",
64                Style);
65   verifyFormat("x            <<= x;\n"
66                "sfdbddfbdfbb  <= x;",
67                Style);
68   verifyFormat("x            <<<= x;\n"
69                "sfdbddfbdfbb   <= x;",
70                Style);
71   verifyFormat("x            >>= x;\n"
72                "sfdbddfbdfbb  <= x;",
73                Style);
74   verifyFormat("x            >>>= x;\n"
75                "sfdbddfbdfbb   <= x;",
76                Style);
77 }
78 
79 TEST_F(FormatTestVerilog, Assign) {
80   verifyFormat("assign mynet = enable;");
81   verifyFormat("assign (strong1, pull0) #1 mynet = enable;");
82   verifyFormat("assign #1 mynet = enable;");
83   verifyFormat("assign mynet = enable;");
84   // Test that assignments are on separate lines.
85   verifyFormat("assign mynet = enable,\n"
86                "       mynet1 = enable1;");
87   // Test that `<=` and `,` don't confuse it.
88   verifyFormat("assign mynet = enable1 <= enable2;");
89   verifyFormat("assign mynet = enable1 <= enable2,\n"
90                "       mynet1 = enable3;");
91   verifyFormat("assign mynet = enable,\n"
92                "       mynet1 = enable2 <= enable3;");
93   verifyFormat("assign mynet = enable(enable1, enable2);");
94 }
95 
96 TEST_F(FormatTestVerilog, BasedLiteral) {
97   verifyFormat("x = '0;");
98   verifyFormat("x = '1;");
99   verifyFormat("x = 'X;");
100   verifyFormat("x = 'x;");
101   verifyFormat("x = 'Z;");
102   verifyFormat("x = 'z;");
103   verifyFormat("x = 659;");
104   verifyFormat("x = 'h837ff;");
105   verifyFormat("x = 'o7460;");
106   verifyFormat("x = 4'b1001;");
107   verifyFormat("x = 5'D3;");
108   verifyFormat("x = 3'b01x;");
109   verifyFormat("x = 12'hx;");
110   verifyFormat("x = 16'hz;");
111   verifyFormat("x = -8'd6;");
112   verifyFormat("x = 4'shf;");
113   verifyFormat("x = -4'sd15;");
114   verifyFormat("x = 16'sd?;");
115 }
116 
117 TEST_F(FormatTestVerilog, Block) {
118   verifyFormat("begin\n"
119                "  x = x;\n"
120                "end");
121   verifyFormat("begin : x\n"
122                "  x = x;\n"
123                "end : x");
124   verifyFormat("begin\n"
125                "  x = x;\n"
126                "  x = x;\n"
127                "end");
128   verifyFormat("fork\n"
129                "  x = x;\n"
130                "join");
131   verifyFormat("fork\n"
132                "  x = x;\n"
133                "join_any");
134   verifyFormat("fork\n"
135                "  x = x;\n"
136                "join_none");
137   verifyFormat("generate\n"
138                "  x = x;\n"
139                "endgenerate");
140   verifyFormat("generate : x\n"
141                "  x = x;\n"
142                "endgenerate : x");
143   // Nested blocks.
144   verifyFormat("begin\n"
145                "  begin\n"
146                "  end\n"
147                "end");
148   verifyFormat("begin : x\n"
149                "  begin\n"
150                "  end\n"
151                "end : x");
152   verifyFormat("begin : x\n"
153                "  begin : x\n"
154                "  end : x\n"
155                "end : x");
156   verifyFormat("begin\n"
157                "  begin : x\n"
158                "  end : x\n"
159                "end");
160   // Test that 'disable fork' and 'rand join' don't get mistaken as blocks.
161   verifyFormat("disable fork;\n"
162                "x = x;");
163   verifyFormat("rand join x x;\n"
164                "x = x;");
165   // The begin keyword should not be indented if it is too long to fit on the
166   // same line.
167   verifyFormat("while (true) //\n"
168                "begin\n"
169                "  while (true) //\n"
170                "  begin\n"
171                "  end\n"
172                "end");
173   verifyFormat("while (true) //\n"
174                "begin : x\n"
175                "  while (true) //\n"
176                "  begin : x\n"
177                "  end : x\n"
178                "end : x");
179   verifyFormat("while (true) //\n"
180                "fork\n"
181                "  while (true) //\n"
182                "  fork\n"
183                "  join\n"
184                "join");
185   auto Style = getDefaultStyle();
186   Style.ColumnLimit = 17;
187   verifyFormat("while (true)\n"
188                "begin\n"
189                "  while (true)\n"
190                "  begin\n"
191                "  end\n"
192                "end",
193                "while (true) begin\n"
194                "  while (true) begin"
195                "  end\n"
196                "end",
197                Style);
198 }
199 
200 TEST_F(FormatTestVerilog, Case) {
201   verifyFormat("case (data)\n"
202                "endcase");
203   verifyFormat("casex (data)\n"
204                "endcase");
205   verifyFormat("casez (data)\n"
206                "endcase");
207   verifyFormat("case (data) inside\n"
208                "endcase");
209   verifyFormat("case (data)\n"
210                "  16'd0:\n"
211                "    result = 10'b0111111111;\n"
212                "endcase");
213   verifyFormat("case (data)\n"
214                "  xxxxxxxx:\n"
215                "    result = 10'b0111111111;\n"
216                "endcase");
217   // Test labels with multiple options.
218   verifyFormat("case (data)\n"
219                "  16'd0, 16'd1:\n"
220                "    result = 10'b0111111111;\n"
221                "endcase");
222   verifyFormat("case (data)\n"
223                "  16'd0, //\n"
224                "      16'd1:\n"
225                "    result = 10'b0111111111;\n"
226                "endcase");
227   // Test that blocks following labels are indented.
228   verifyFormat("case (data)\n"
229                "  16'd1: fork\n"
230                "    result = 10'b1011111111;\n"
231                "  join\n"
232                "endcase");
233   verifyFormat("case (data)\n"
234                "  16'd1: fork : x\n"
235                "    result = 10'b1011111111;\n"
236                "  join : x\n"
237                "endcase");
238   // Test default.
239   verifyFormat("case (data)\n"
240                "  default\n"
241                "    result = 10'b1011111111;\n"
242                "endcase");
243   verifyFormat("case (data)\n"
244                "  default:\n"
245                "    result = 10'b1011111111;\n"
246                "endcase");
247   // Test that question marks and colons don't get mistaken as labels.
248   verifyFormat("case (data)\n"
249                "  8'b1???????:\n"
250                "    instruction1(ir);\n"
251                "endcase");
252   verifyFormat("case (data)\n"
253                "  x ? 8'b1??????? : 1:\n"
254                "    instruction3(ir);\n"
255                "endcase");
256   // Test indention options.
257   auto Style = getDefaultStyle();
258   Style.IndentCaseLabels = false;
259   verifyFormat("case (data)\n"
260                "16'd0:\n"
261                "  result = 10'b0111111111;\n"
262                "endcase",
263                Style);
264   verifyFormat("case (data)\n"
265                "16'd0: begin\n"
266                "  result = 10'b0111111111;\n"
267                "end\n"
268                "endcase",
269                Style);
270   Style.IndentCaseLabels = true;
271   verifyFormat("case (data)\n"
272                "  16'd0:\n"
273                "    result = 10'b0111111111;\n"
274                "endcase",
275                Style);
276   verifyFormat("case (data)\n"
277                "  16'd0: begin\n"
278                "    result = 10'b0111111111;\n"
279                "  end\n"
280                "endcase",
281                Style);
282   // Other colons should not be mistaken as case colons.
283   Style = getDefaultStyle();
284   Style.BitFieldColonSpacing = FormatStyle::BFCS_None;
285   verifyFormat("case (x[1:0])\n"
286                "endcase",
287                Style);
288   verifyFormat("default:\n"
289                "  x[1:0] = x[1:0];",
290                Style);
291   Style.BitFieldColonSpacing = FormatStyle::BFCS_Both;
292   verifyFormat("case (x[1 : 0])\n"
293                "endcase",
294                Style);
295   verifyFormat("default:\n"
296                "  x[1 : 0] = x[1 : 0];",
297                Style);
298   Style = getDefaultStyle();
299   Style.SpacesInContainerLiterals = true;
300   verifyFormat("case ('{x : x, default : 9})\n"
301                "endcase",
302                Style);
303   verifyFormat("x = '{x : x, default : 9};", Style);
304   verifyFormat("default:\n"
305                "  x = '{x : x, default : 9};",
306                Style);
307   Style.SpacesInContainerLiterals = false;
308   verifyFormat("case ('{x: x, default: 9})\n"
309                "endcase",
310                Style);
311   verifyFormat("x = '{x: x, default: 9};", Style);
312   verifyFormat("default:\n"
313                "  x = '{x: x, default: 9};",
314                Style);
315   // When the line following the case label needs to be broken, the continuation
316   // should be indented correctly.
317   verifyFormat("case (data)\n"
318                "  16'd0:\n"
319                "    result = //\n"
320                "        10'b0111111111;\n"
321                "endcase");
322   verifyFormat("case (data)\n"
323                "  16'd0, //\n"
324                "      16'd1:\n"
325                "    result = //\n"
326                "        10'b0111111111;\n"
327                "endcase");
328   verifyFormat("case (data)\n"
329                "  16'd0:\n"
330                "    result = (10'b0111111111 + //\n"
331                "              10'b0111111111 + //\n"
332                "              10'b0111111111);\n"
333                "endcase");
334   verifyFormat("case (data)\n"
335                "  16'd0:\n"
336                "    result =              //\n"
337                "        (10'b0111111111 + //\n"
338                "         10'b0111111111 + //\n"
339                "         10'b0111111111);\n"
340                "endcase");
341   verifyFormat("case (data)\n"
342                "  16'd0:\n"
343                "    result =          //\n"
344                "        longfunction( //\n"
345                "            arg);\n"
346                "endcase");
347   verifyFormat("case (data)\n"
348                "  16'd0:\n"
349                "    //\n"
350                "    result = //\n"
351                "        10'b0111111111;\n"
352                "endcase");
353   verifyFormat("case (data)\n"
354                "  16'd0:\n"
355                "    //\n"
356                "\n"
357                "    //\n"
358                "    result = //\n"
359                "        10'b0111111111;\n"
360                "endcase");
361   Style = getDefaultStyle();
362   Style.ContinuationIndentWidth = 1;
363   verifyFormat("case (data)\n"
364                "  16'd0:\n"
365                "    result = //\n"
366                "     10'b0111111111;\n"
367                "endcase",
368                Style);
369   verifyFormat("case (data)\n"
370                "  16'd0:\n"
371                "    result =       //\n"
372                "     longfunction( //\n"
373                "      arg);\n"
374                "endcase",
375                Style);
376 
377   verifyFormat("case (v) matches\n"
378                "  tagged Valid .n:\n"
379                "    ;\n"
380                "endcase");
381 }
382 
383 TEST_F(FormatTestVerilog, Coverage) {
384   verifyFormat("covergroup x\n"
385                "    @@(begin x);\n"
386                "endgroup");
387 }
388 
389 TEST_F(FormatTestVerilog, Declaration) {
390   verifyFormat("wire mynet;");
391   verifyFormat("wire mynet, mynet1;");
392   verifyFormat("wire mynet, //\n"
393                "     mynet1;");
394   verifyFormat("wire #0 mynet, mynet1;");
395   verifyFormat("wire logic #0 mynet, mynet1;");
396   verifyFormat("wire #(1, 2, 3) mynet, mynet1;");
397   verifyFormat("wire #0 mynet, //\n"
398                "        mynet1;");
399   verifyFormat("wire logic #0 mynet, //\n"
400                "              mynet1;");
401   verifyFormat("wire #(1, 2, 3) mynet, //\n"
402                "                mynet1;");
403   verifyFormat("wire mynet = enable;");
404   verifyFormat("wire mynet = enable, mynet1;");
405   verifyFormat("wire mynet = enable, //\n"
406                "     mynet1;");
407   verifyFormat("wire mynet, mynet1 = enable;");
408   verifyFormat("wire mynet, //\n"
409                "     mynet1 = enable;");
410   verifyFormat("wire mynet = enable, mynet1 = enable;");
411   verifyFormat("wire mynet = enable, //\n"
412                "     mynet1 = enable;");
413   verifyFormat("wire (strong1, pull0) mynet;");
414   verifyFormat("wire (strong1, pull0) mynet, mynet1;");
415   verifyFormat("wire (strong1, pull0) mynet, //\n"
416                "                      mynet1;");
417   verifyFormat("wire (strong1, pull0) mynet = enable;");
418   verifyFormat("wire (strong1, pull0) mynet = enable, mynet1;");
419   verifyFormat("wire (strong1, pull0) mynet = enable, //\n"
420                "                      mynet1;");
421   verifyFormat("wire (strong1, pull0) mynet, mynet1 = enable;");
422   verifyFormat("wire (strong1, pull0) mynet, //\n"
423                "                      mynet1 = enable;");
424 }
425 
426 TEST_F(FormatTestVerilog, Delay) {
427   // Delay by the default unit.
428   verifyFormat("#0;");
429   verifyFormat("#1;");
430   verifyFormat("#10;");
431   verifyFormat("#1.5;");
432   // Explicit unit.
433   verifyFormat("#1fs;");
434   verifyFormat("#1.5fs;");
435   verifyFormat("#1ns;");
436   verifyFormat("#1.5ns;");
437   verifyFormat("#1us;");
438   verifyFormat("#1.5us;");
439   verifyFormat("#1ms;");
440   verifyFormat("#1.5ms;");
441   verifyFormat("#1s;");
442   verifyFormat("#1.5s;");
443   // The following expression should be on the same line.
444   verifyFormat("#1 x = x;");
445   verifyFormat("#1 x = x;", "#1\n"
446                             "x = x;");
447 }
448 
449 TEST_F(FormatTestVerilog, Enum) {
450   verifyFormat("enum { x } x;");
451   verifyFormat("typedef enum { x } x;");
452   verifyFormat("enum { red, yellow, green } x;");
453   verifyFormat("typedef enum { red, yellow, green } x;");
454   verifyFormat("enum integer { x } x;");
455   verifyFormat("typedef enum { x = 0 } x;");
456   verifyFormat("typedef enum { red = 0, yellow = 1, green = 2 } x;");
457   verifyFormat("typedef enum integer { x } x;");
458   verifyFormat("typedef enum bit [0 : 1] { x } x;");
459   verifyFormat("typedef enum { add = 10, sub[5], jmp[6 : 8] } E1;");
460   verifyFormat("typedef enum { add = 10, sub[5] = 0, jmp[6 : 8] = 1 } E1;");
461 }
462 
463 TEST_F(FormatTestVerilog, Headers) {
464   // Test headers with multiple ports.
465   verifyFormat("module mh1\n"
466                "    (input var int in1,\n"
467                "     input var shortreal in2,\n"
468                "     output tagged_st out);\n"
469                "endmodule");
470   // There should be a space following the type but not the variable name.
471   verifyFormat("module test\n"
472                "    (input wire [7 : 0] a,\n"
473                "     input wire b[7 : 0],\n"
474                "     input wire [7 : 0] c[7 : 0]);\n"
475                "endmodule");
476   // Ports should be grouped by types.
477   verifyFormat("module test\n"
478                "    (input [7 : 0] a,\n"
479                "     input signed [7 : 0] b, c, d);\n"
480                "endmodule");
481   verifyFormat("module test\n"
482                "    (input [7 : 0] a,\n"
483                "     (* x = x *) input signed [7 : 0] b, c, d);\n"
484                "endmodule");
485   verifyFormat("module test\n"
486                "    (input [7 : 0] a = 0,\n"
487                "     input signed [7 : 0] b = 0, c = 0, d = 0);\n"
488                "endmodule");
489   verifyFormat("module test\n"
490                "    #(parameter x)\n"
491                "    (input [7 : 0] a,\n"
492                "     input signed [7 : 0] b, c, d);\n"
493                "endmodule");
494   // When a line needs to be broken, ports of the same type should be aligned to
495   // the same column.
496   verifyFormat("module test\n"
497                "    (input signed [7 : 0] b, c, //\n"
498                "                          d);\n"
499                "endmodule");
500   verifyFormat("module test\n"
501                "    ((* x = x *) input signed [7 : 0] b, c, //\n"
502                "                                      d);\n"
503                "endmodule");
504   verifyFormat("module test\n"
505                "    (input signed [7 : 0] b = 0, c, //\n"
506                "                          d);\n"
507                "endmodule");
508   verifyFormat("module test\n"
509                "    (input signed [7 : 0] b, c = 0, //\n"
510                "                          d);\n"
511                "endmodule");
512   verifyFormat("module test\n"
513                "    (input signed [7 : 0] b, c, //\n"
514                "                          d = 0);\n"
515                "endmodule");
516   verifyFormat("module test\n"
517                "    (input wire logic signed [7 : 0][0 : 1] b, c, //\n"
518                "                                            d);\n"
519                "endmodule");
520   verifyFormat("module test\n"
521                "    (input signed [7 : 0] b, //\n"
522                "                          c, //\n"
523                "                          d);\n"
524                "endmodule");
525   verifyFormat("module test\n"
526                "    (input [7 : 0] a,\n"
527                "     input signed [7 : 0] b, //\n"
528                "                          c, //\n"
529                "                          d);\n"
530                "endmodule");
531   verifyFormat("module test\n"
532                "    (input signed [7 : 0] b, //\n"
533                "                          c, //\n"
534                "                          d,\n"
535                "     output signed [7 : 0] h);\n"
536                "endmodule");
537   // With a modport.
538   verifyFormat("module m\n"
539                "    (i2.master i);\n"
540                "endmodule");
541   verifyFormat("module m\n"
542                "    (i2.master i, ii);\n"
543                "endmodule");
544   verifyFormat("module m\n"
545                "    (i2.master i, //\n"
546                "               ii);\n"
547                "endmodule");
548   verifyFormat("module m\n"
549                "    (i2.master i,\n"
550                "     input ii);\n"
551                "endmodule");
552   verifyFormat("module m\n"
553                "    (i2::i2.master i);\n"
554                "endmodule");
555   verifyFormat("module m\n"
556                "    (i2::i2.master i, ii);\n"
557                "endmodule");
558   verifyFormat("module m\n"
559                "    (i2::i2.master i, //\n"
560                "                   ii);\n"
561                "endmodule");
562   verifyFormat("module m\n"
563                "    (i2::i2.master i,\n"
564                "     input ii);\n"
565                "endmodule");
566   verifyFormat("module m\n"
567                "    (i2::i2 i);\n"
568                "endmodule");
569   verifyFormat("module m\n"
570                "    (i2::i2 i, ii);\n"
571                "endmodule");
572   verifyFormat("module m\n"
573                "    (i2::i2 i, //\n"
574                "            ii);\n"
575                "endmodule");
576   verifyFormat("module m\n"
577                "    (i2::i2 i,\n"
578                "     input ii);\n"
579                "endmodule");
580   // With a macro in the names.
581   verifyFormat("module m\n"
582                "    (input var `x a, b);\n"
583                "endmodule");
584   verifyFormat("module m\n"
585                "    (input var `x a, //\n"
586                "                  b);\n"
587                "endmodule");
588   verifyFormat("module m\n"
589                "    (input var x `a, b);\n"
590                "endmodule");
591   verifyFormat("module m\n"
592                "    (input var x `a, //\n"
593                "                 b);\n"
594                "endmodule");
595   // A line comment shouldn't disrupt the indentation of the port list.
596   verifyFormat("extern module x\n"
597                "    (//\n"
598                "     output y);");
599   verifyFormat("extern module x\n"
600                "    #(//\n"
601                "      parameter x)\n"
602                "    (//\n"
603                "     output y);");
604   // With a concatenation in the names.
605   auto Style = getDefaultStyle();
606   Style.ColumnLimit = 40;
607   verifyFormat("`define X(x)                           \\\n"
608                "  module test                          \\\n"
609                "      (input var x``x a, b);",
610                Style);
611   verifyFormat("`define X(x)                           \\\n"
612                "  module test                          \\\n"
613                "      (input var x``x aaaaaaaaaaaaaaa, \\\n"
614                "                      b);",
615                Style);
616   verifyFormat("`define X(x)                           \\\n"
617                "  module test                          \\\n"
618                "      (input var x a``x, b);",
619                Style);
620   verifyFormat("`define X(x)                           \\\n"
621                "  module test                          \\\n"
622                "      (input var x aaaaaaaaaaaaaaa``x, \\\n"
623                "                   b);",
624                Style);
625   // When the ports line is not to be formatted, following lines should not take
626   // on its indentation.
627   verifyFormat("module x\n"
628                "    (output x);\n"
629                "  assign x = 0;\n"
630                "endmodule",
631                "module x\n"
632                "    (output x);\n"
633                "    assign x = 0;\n"
634                "endmodule",
635                getDefaultStyle(), {tooling::Range(25, 18)});
636 }
637 
638 TEST_F(FormatTestVerilog, Hierarchy) {
639   verifyFormat("module x;\n"
640                "endmodule");
641   // Test that the end label is on the same line as the end keyword.
642   verifyFormat("module x;\n"
643                "endmodule : x");
644   // Test that things inside are indented.
645   verifyFormat("module x;\n"
646                "  generate\n"
647                "  endgenerate\n"
648                "endmodule");
649   verifyFormat("program x;\n"
650                "  generate\n"
651                "  endgenerate\n"
652                "endprogram");
653   verifyFormat("interface x;\n"
654                "  generate\n"
655                "  endgenerate\n"
656                "endinterface");
657   verifyFormat("task x;\n"
658                "  generate\n"
659                "  endgenerate\n"
660                "endtask");
661   verifyFormat("function x;\n"
662                "  generate\n"
663                "  endgenerate\n"
664                "endfunction");
665   verifyFormat("class x;\n"
666                "  generate\n"
667                "  endgenerate\n"
668                "endclass");
669   // Test that they nest.
670   verifyFormat("module x;\n"
671                "  program x;\n"
672                "    program x;\n"
673                "    endprogram\n"
674                "  endprogram\n"
675                "endmodule");
676   // Test that an extern declaration doesn't change the indentation.
677   verifyFormat("extern module x;\n"
678                "x = x;");
679   // Test complex headers
680   verifyFormat("extern module x\n"
681                "    import x.x::x::*;\n"
682                "    import x;\n"
683                "    #(parameter x)\n"
684                "    (output x);");
685   verifyFormat("module x\n"
686                "    import x.x::x::*;\n"
687                "    import x;\n"
688                "    #(parameter x)\n"
689                "    (output x);\n"
690                "  generate\n"
691                "  endgenerate\n"
692                "endmodule : x");
693   verifyFormat("virtual class x\n"
694                "    (x)\n"
695                "    extends x(x)\n"
696                "    implements x, x, x;\n"
697                "  generate\n"
698                "  endgenerate\n"
699                "endclass : x");
700   verifyFormat("function automatic logic [1 : 0] x\n"
701                "    (input x);\n"
702                "  generate\n"
703                "  endgenerate\n"
704                "endfunction : x");
705 }
706 
707 TEST_F(FormatTestVerilog, Identifiers) {
708   // Escaped identifiers should not be split.
709   verifyFormat("\\busa+index");
710   verifyFormat("\\-clock");
711   verifyFormat("\\***error-condition***");
712   verifyFormat("\\net1\\/net2");
713   verifyFormat("\\{a,b}");
714   verifyFormat("\\a*(b+c)");
715   // Escaped identifiers can't be joined with the next token.  Extra space
716   // should be removed.
717   verifyFormat("\\busa+index ;", "\\busa+index\n"
718                                  ";");
719   verifyFormat("\\busa+index ;", "\\busa+index\r\n"
720                                  ";");
721   verifyFormat("\\busa+index ;", "\\busa+index  ;");
722   verifyFormat("\\busa+index ;", "\\busa+index\n"
723                                  " ;");
724   verifyFormat("\\busa+index ;");
725   verifyFormat("(\\busa+index );");
726   verifyFormat("\\busa+index \\busa+index ;");
727 }
728 
729 TEST_F(FormatTestVerilog, If) {
730   verifyFormat("if (x)\n"
731                "  x = x;");
732   verifyFormat("unique if (x)\n"
733                "  x = x;");
734   verifyFormat("unique0 if (x)\n"
735                "  x = x;");
736   verifyFormat("priority if (x)\n"
737                "  x = x;");
738   verifyFormat("if (x)\n"
739                "  x = x;\n"
740                "x = x;");
741 
742   // Test else
743   verifyFormat("if (x)\n"
744                "  x = x;\n"
745                "else if (x)\n"
746                "  x = x;\n"
747                "else\n"
748                "  x = x;");
749   verifyFormat("if (x) begin\n"
750                "  x = x;\n"
751                "end else if (x) begin\n"
752                "  x = x;\n"
753                "end else begin\n"
754                "  x = x;\n"
755                "end");
756   verifyFormat("if (x) begin : x\n"
757                "  x = x;\n"
758                "end : x else if (x) begin : x\n"
759                "  x = x;\n"
760                "end : x else begin : x\n"
761                "  x = x;\n"
762                "end : x");
763 
764   // Test block keywords.
765   verifyFormat("if (x) begin\n"
766                "  x = x;\n"
767                "end");
768   verifyFormat("if (x) begin : x\n"
769                "  x = x;\n"
770                "end : x");
771   verifyFormat("if (x) begin\n"
772                "  x = x;\n"
773                "  x = x;\n"
774                "end");
775   verifyFormat("if (x) fork\n"
776                "  x = x;\n"
777                "join");
778   verifyFormat("if (x) fork\n"
779                "  x = x;\n"
780                "join_any");
781   verifyFormat("if (x) fork\n"
782                "  x = x;\n"
783                "join_none");
784   verifyFormat("if (x) generate\n"
785                "  x = x;\n"
786                "endgenerate");
787   verifyFormat("if (x) generate : x\n"
788                "  x = x;\n"
789                "endgenerate : x");
790 
791   // Test that concatenation braces don't get regarded as blocks.
792   verifyFormat("if (x)\n"
793                "  {x} = x;");
794   verifyFormat("if (x)\n"
795                "  x = {x};");
796   verifyFormat("if (x)\n"
797                "  x = {x};\n"
798                "else\n"
799                "  {x} = {x};");
800 
801   // With attributes.
802   verifyFormat("(* x *) if (x)\n"
803                "  x = x;");
804   verifyFormat("(* x = \"x\" *) if (x)\n"
805                "  x = x;");
806   verifyFormat("(* x, x = \"x\" *) if (x)\n"
807                "  x = x;");
808 
809   // Assert are treated similar to if.  But the else parts should not be
810   // chained.
811   verifyFormat("assert (x);");
812   verifyFormat("assert (x)\n"
813                "  $info();");
814   verifyFormat("assert (x)\n"
815                "  $info();\n"
816                "else\n"
817                "  $error();");
818   verifyFormat("assert (x)\n"
819                "else\n"
820                "  $error();");
821   verifyFormat("assert (x)\n"
822                "else begin\n"
823                "end");
824   verifyFormat("assert (x)\n"
825                "else\n"
826                "  if (x)\n"
827                "    $error();");
828   verifyFormat("assert (x)\n"
829                "  $info();\n"
830                "else\n"
831                "  if (x)\n"
832                "    $error();");
833   verifyFormat("assert (x)\n"
834                "  $info();\n"
835                "else\n"
836                "  if (x)\n"
837                "    $error();\n"
838                "  else\n"
839                "    $error();");
840   verifyFormat("assert (x)\n"
841                "  $info();\n"
842                "else\n"
843                "  if (x)\n"
844                "    $error();\n"
845                "  else if (x)\n"
846                "    $error();\n"
847                "  else\n"
848                "    $error();");
849   // The body is optional for asserts.  The next line should not be indented if
850   // the statement already ended with a semicolon.
851   verifyFormat("assert (x);\n"
852                "x = x;");
853   verifyFormat("if (x)\n"
854                "  assert (x);\n"
855                "else if (x) begin\n"
856                "end else begin\n"
857                "end");
858   verifyFormat("if (x)\n"
859                "  assert (x);\n"
860                "else begin\n"
861                "end");
862   verifyFormat("if (x)\n"
863                "  assert (x)\n"
864                "  else begin\n"
865                "  end");
866   // Other keywords.
867   verifyFormat("assume (x)\n"
868                "  $info();");
869   verifyFormat("cover (x)\n"
870                "  $info();");
871   verifyFormat("restrict (x)\n"
872                "  $info();");
873   verifyFormat("assert #0 (x)\n"
874                "  $info();");
875   verifyFormat("assert final (x)\n"
876                "  $info();");
877   verifyFormat("cover #0 (x)\n"
878                "  $info();");
879   verifyFormat("cover final (x)\n"
880                "  $info();");
881 
882   // The space around parentheses options should work.
883   auto Style = getDefaultStyle();
884   verifyFormat("if (x)\n"
885                "  x = x;\n"
886                "else if (x)\n"
887                "  x = x;",
888                Style);
889   verifyFormat("assert (x);", Style);
890   verifyFormat("assert #0 (x);", Style);
891   verifyFormat("assert (x)\n"
892                "else\n"
893                "  if (x)\n"
894                "    x = x;",
895                Style);
896   Style.SpacesInParens = FormatStyle::SIPO_Custom;
897   Style.SpacesInParensOptions.InConditionalStatements = true;
898   verifyFormat("if ( x )\n"
899                "  x = x;\n"
900                "else if ( x )\n"
901                "  x = x;",
902                Style);
903   verifyFormat("assert ( x );", Style);
904   verifyFormat("assert #0 ( x );", Style);
905   verifyFormat("assert ( x )\n"
906                "else\n"
907                "  if ( x )\n"
908                "    x = x;",
909                Style);
910 }
911 
912 TEST_F(FormatTestVerilog, Instantiation) {
913   // Without ports.
914   verifyFormat("ffnand ff1;");
915   // With named ports.
916   verifyFormat("ffnand ff1(.qbar(out1),\n"
917                "           .clear(in1),\n"
918                "           .preset(in2));");
919   // With wildcard.
920   verifyFormat("ffnand ff1(.qbar(out1),\n"
921                "           .clear(in1),\n"
922                "           .preset(in2),\n"
923                "           .*);");
924   verifyFormat("ffnand ff1(.*,\n"
925                "           .qbar(out1),\n"
926                "           .clear(in1),\n"
927                "           .preset(in2));");
928   // With unconnected ports.
929   verifyFormat("ffnand ff1(.q(),\n"
930                "           .qbar(out1),\n"
931                "           .clear(in1),\n"
932                "           .preset(in2));");
933   verifyFormat("ffnand ff1(.q(),\n"
934                "           .qbar(),\n"
935                "           .clear(),\n"
936                "           .preset());");
937   verifyFormat("ffnand ff1(,\n"
938                "           .qbar(out1),\n"
939                "           .clear(in1),\n"
940                "           .preset(in2));");
941   // With positional ports.
942   verifyFormat("ffnand ff1(out1,\n"
943                "           in1,\n"
944                "           in2);");
945   verifyFormat("ffnand ff1(,\n"
946                "           out1,\n"
947                "           in1,\n"
948                "           in2);");
949   // Multiple instantiations.
950   verifyFormat("ffnand ff1(.q(),\n"
951                "           .qbar(out1),\n"
952                "           .clear(in1),\n"
953                "           .preset(in2)),\n"
954                "       ff1(.q(),\n"
955                "           .qbar(out1),\n"
956                "           .clear(in1),\n"
957                "           .preset(in2));");
958   verifyFormat("ffnand //\n"
959                "    ff1(.q(),\n"
960                "        .qbar(out1),\n"
961                "        .clear(in1),\n"
962                "        .preset(in2)),\n"
963                "    ff1(.q(),\n"
964                "        .qbar(out1),\n"
965                "        .clear(in1),\n"
966                "        .preset(in2));");
967   // With breaking between instance ports disabled.
968   auto Style = getDefaultStyle();
969   Style.VerilogBreakBetweenInstancePorts = false;
970   verifyFormat("ffnand ff1;", Style);
971   verifyFormat("ffnand ff1(.qbar(out1), .clear(in1), .preset(in2), .*);",
972                Style);
973   verifyFormat("ffnand ff1(out1, in1, in2);", Style);
974   verifyFormat("ffnand ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n"
975                "       ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));",
976                Style);
977   verifyFormat("ffnand //\n"
978                "    ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n"
979                "    ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));",
980                Style);
981 }
982 
983 TEST_F(FormatTestVerilog, Loop) {
984   verifyFormat("foreach (x[x])\n"
985                "  x = x;");
986   verifyFormat("repeat (x)\n"
987                "  x = x;");
988   verifyFormat("foreach (x[x]) begin\n"
989                "end");
990   verifyFormat("repeat (x) begin\n"
991                "end");
992   auto Style = getDefaultStyle();
993   Style.SpacesInParens = FormatStyle::SIPO_Custom;
994   Style.SpacesInParensOptions.InConditionalStatements = true;
995   verifyFormat("foreach ( x[x] )\n"
996                "  x = x;",
997                Style);
998   verifyFormat("repeat ( x )\n"
999                "  x = x;",
1000                Style);
1001 }
1002 
1003 TEST_F(FormatTestVerilog, Operators) {
1004   // Test that unary operators are not followed by space.
1005   verifyFormat("x = +x;");
1006   verifyFormat("x = -x;");
1007   verifyFormat("x = !x;");
1008   verifyFormat("x = ~x;");
1009   verifyFormat("x = &x;");
1010   verifyFormat("x = ~&x;");
1011   verifyFormat("x = |x;");
1012   verifyFormat("x = ~|x;");
1013   verifyFormat("x = ^x;");
1014   verifyFormat("x = ~^x;");
1015   verifyFormat("x = ^~x;");
1016   verifyFormat("x = ++x;");
1017   verifyFormat("x = --x;");
1018 
1019   // Test that `*` and `*>` are binary.
1020   verifyFormat("x = x * x;");
1021   verifyFormat("x = (x * x);");
1022   verifyFormat("(opcode *> o1) = 6.1;");
1023   verifyFormat("(C, D *> Q) = 18;");
1024   // The wildcard import is not a binary operator.
1025   verifyFormat("import p::*;");
1026 
1027   // Test that operators don't get split.
1028   verifyFormat("x = x++;");
1029   verifyFormat("x = x--;");
1030   verifyFormat("x = x ** x;");
1031   verifyFormat("x = x << x;");
1032   verifyFormat("x = x >> x;");
1033   verifyFormat("x = x <<< x;");
1034   verifyFormat("x = x >>> x;");
1035   verifyFormat("x = x <= x;");
1036   verifyFormat("x = x >= x;");
1037   verifyFormat("x = x == x;");
1038   verifyFormat("x = x != x;");
1039   verifyFormat("x = x === x;");
1040   verifyFormat("x = x !== x;");
1041   verifyFormat("x = x ==? x;");
1042   verifyFormat("x = x !=? x;");
1043   verifyFormat("x = x ~^ x;");
1044   verifyFormat("x = x ^~ x;");
1045   verifyFormat("x = x && x;");
1046   verifyFormat("x = x || x;");
1047   verifyFormat("x = x -> x;");
1048   verifyFormat("x = x <-> x;");
1049   verifyFormat("x += x;");
1050   verifyFormat("x -= x;");
1051   verifyFormat("x *= x;");
1052   verifyFormat("x /= x;");
1053   verifyFormat("x %= x;");
1054   verifyFormat("x &= x;");
1055   verifyFormat("x ^= x;");
1056   verifyFormat("x |= x;");
1057   verifyFormat("x <<= x;");
1058   verifyFormat("x >>= x;");
1059   verifyFormat("x <<<= x;");
1060   verifyFormat("x >>>= x;");
1061   verifyFormat("x <= x;");
1062 
1063   // Test that space is added between operators.
1064   verifyFormat("x = x < -x;", "x=x<-x;");
1065   verifyFormat("x = x << -x;", "x=x<<-x;");
1066   verifyFormat("x = x <<< -x;", "x=x<<<-x;");
1067 
1068   // Test that operators that are C++ identifiers get treated as operators.
1069   verifyFormat("solve s before d;");                       // before
1070   verifyFormat("binsof(i) intersect {0};");                // intersect
1071   verifyFormat("req dist {1};");                           // dist
1072   verifyFormat("a inside {b, c};");                        // inside
1073   verifyFormat("bus.randomize() with { atype == low; };"); // with
1074 }
1075 
1076 TEST_F(FormatTestVerilog, Preprocessor) {
1077   auto Style = getDefaultStyle();
1078   Style.ColumnLimit = 20;
1079 
1080   // Macro definitions.
1081   verifyFormat("`define X          \\\n"
1082                "  if (x)           \\\n"
1083                "    x = x;",
1084                "`define X if(x)x=x;", Style);
1085   verifyFormat("`define X(x)       \\\n"
1086                "  if (x)           \\\n"
1087                "    x = x;",
1088                "`define X(x) if(x)x=x;", Style);
1089   verifyFormat("`define X          \\\n"
1090                "  x = x;           \\\n"
1091                "  x = x;",
1092                "`define X x=x;x=x;", Style);
1093   // Macro definitions with invocations inside.
1094   verifyFormat("`define LIST       \\\n"
1095                "  `ENTRY           \\\n"
1096                "  `ENTRY",
1097                "`define LIST \\\n"
1098                "`ENTRY \\\n"
1099                "`ENTRY",
1100                Style);
1101   verifyFormat("`define LIST       \\\n"
1102                "  `x = `x;         \\\n"
1103                "  `x = `x;",
1104                "`define LIST \\\n"
1105                "`x = `x; \\\n"
1106                "`x = `x;",
1107                Style);
1108   verifyFormat("`define LIST       \\\n"
1109                "  `x = `x;         \\\n"
1110                "  `x = `x;",
1111                "`define LIST `x=`x;`x=`x;", Style);
1112   // Macro invocations.
1113   verifyFormat("`x = (`x1 + `x2 + x);");
1114   // Lines starting with a preprocessor directive should not be indented.
1115   std::string Directives[] = {
1116       "begin_keywords",
1117       "celldefine",
1118       "default_nettype",
1119       "define",
1120       "else",
1121       "elsif",
1122       "end_keywords",
1123       "endcelldefine",
1124       "endif",
1125       "ifdef",
1126       "ifndef",
1127       "include",
1128       "line",
1129       "nounconnected_drive",
1130       "pragma",
1131       "resetall",
1132       "timescale",
1133       "unconnected_drive",
1134       "undef",
1135       "undefineall",
1136   };
1137   for (auto &Name : Directives) {
1138     verifyFormat("if (x)\n"
1139                  "`" +
1140                      Name +
1141                      "\n"
1142                      "  ;",
1143                  "if (x)\n"
1144                  "`" +
1145                      Name +
1146                      "\n"
1147                      ";",
1148                  Style);
1149   }
1150   // Lines starting with a regular macro invocation should be indented as a
1151   // normal line.
1152   verifyFormat("if (x)\n"
1153                "  `x = `x;\n"
1154                "`timescale 1ns / 1ps",
1155                "if (x)\n"
1156                "`x = `x;\n"
1157                "`timescale 1ns / 1ps",
1158                Style);
1159   verifyFormat("if (x)\n"
1160                "`timescale 1ns / 1ps\n"
1161                "  `x = `x;",
1162                "if (x)\n"
1163                "`timescale 1ns / 1ps\n"
1164                "`x = `x;",
1165                Style);
1166   std::string NonDirectives[] = {
1167       // For `__FILE__` and `__LINE__`, although the standard classifies them as
1168       // preprocessor directives, they are used like regular macros.
1169       "__FILE__", "__LINE__", "elif", "foo", "x",
1170   };
1171   for (auto &Name : NonDirectives) {
1172     verifyFormat("if (x)\n"
1173                  "  `" +
1174                      Name + ";",
1175                  "if (x)\n"
1176                  "`" +
1177                      Name +
1178                      "\n"
1179                      ";",
1180                  Style);
1181   }
1182 }
1183 
1184 TEST_F(FormatTestVerilog, Primitive) {
1185   verifyFormat("primitive multiplexer\n"
1186                "    (mux, control, dataA, dataB);\n"
1187                "  output mux;\n"
1188                "  input control, dataA, dataB;\n"
1189                "  table\n"
1190                "    0 1 ? : 1;\n"
1191                "    0 0 ? : 0;\n"
1192                "    1 ? 1 : 1;\n"
1193                "    1 ? 0 : 0;\n"
1194                "    x 0 0 : 0;\n"
1195                "    x 1 1 : 1;\n"
1196                "  endtable\n"
1197                "endprimitive");
1198   verifyFormat("primitive latch\n"
1199                "    (q, ena_, data);\n"
1200                "  output q;\n"
1201                "  reg q;\n"
1202                "  input ena_, data;\n"
1203                "  table\n"
1204                "    0 1 : ? : 1;\n"
1205                "    0 0 : ? : 0;\n"
1206                "    1 ? : ? : -;\n"
1207                "    ? * : ? : -;\n"
1208                "  endtable\n"
1209                "endprimitive");
1210   verifyFormat("primitive d\n"
1211                "    (q, clock, data);\n"
1212                "  output q;\n"
1213                "  reg q;\n"
1214                "  input clock, data;\n"
1215                "  table\n"
1216                "    (01) 0 : ? : 0;\n"
1217                "    (01) 1 : ? : 1;\n"
1218                "    (0?) 1 : 1 : 1;\n"
1219                "    (0?) 0 : 0 : 0;\n"
1220                "    (?0) ? : ? : -;\n"
1221                "    (?\?) ? : ? : -;\n"
1222                "  endtable\n"
1223                "endprimitive");
1224 }
1225 
1226 TEST_F(FormatTestVerilog, Streaming) {
1227   verifyFormat("x = {>>{j}};");
1228   verifyFormat("x = {>>byte{j}};");
1229   verifyFormat("x = {<<{j}};");
1230   verifyFormat("x = {<<byte{j}};");
1231   verifyFormat("x = {<<16{j}};");
1232   verifyFormat("x = {<<{8'b0011_0101}};");
1233   verifyFormat("x = {<<4{6'b11_0101}};");
1234   verifyFormat("x = {>>4{6'b11_0101}};");
1235   verifyFormat("x = {<<2{{<<{4'b1101}}}};");
1236   verifyFormat("bit [96 : 1] y = {>>{a, b, c}};");
1237   verifyFormat("int j = {>>{a, b, c}};");
1238   verifyFormat("{>>{a, b, c}} = 23'b1;");
1239   verifyFormat("{>>{a, b, c}} = x;");
1240   verifyFormat("{>>{j}} = x;");
1241   verifyFormat("{>>byte{j}} = x;");
1242   verifyFormat("{<<{j}} = x;");
1243   verifyFormat("{<<byte{j}} = x;");
1244 }
1245 
1246 TEST_F(FormatTestVerilog, StringLiteral) {
1247   // Long strings should be broken.
1248   verifyFormat(R"(x({"xxxxxxxxxxxxxxxx ",
1249    "xxxx"});)",
1250                R"(x({"xxxxxxxxxxxxxxxx xxxx"});)",
1251                getStyleWithColumns(getDefaultStyle(), 23));
1252   verifyFormat(R"(x({"xxxxxxxxxxxxxxxx",
1253    " xxxx"});)",
1254                R"(x({"xxxxxxxxxxxxxxxx xxxx"});)",
1255                getStyleWithColumns(getDefaultStyle(), 22));
1256   // Braces should be added when they don't already exist.
1257   verifyFormat(R"(x({"xxxxxxxxxxxxxxxx ",
1258    "xxxx"});)",
1259                R"(x("xxxxxxxxxxxxxxxx xxxx");)",
1260                getStyleWithColumns(getDefaultStyle(), 23));
1261   verifyFormat(R"(x({"xxxxxxxxxxxxxxxx",
1262    " xxxx"});)",
1263                R"(x("xxxxxxxxxxxxxxxx xxxx");)",
1264                getStyleWithColumns(getDefaultStyle(), 22));
1265   verifyFormat(R"(x({{"xxxxxxxxxxxxxxxx ",
1266     "xxxx"} == x});)",
1267                R"(x({"xxxxxxxxxxxxxxxx xxxx" == x});)",
1268                getStyleWithColumns(getDefaultStyle(), 24));
1269   verifyFormat(R"(string x = {"xxxxxxxxxxxxxxxx ",
1270             "xxxxxxxx"};)",
1271                R"(string x = "xxxxxxxxxxxxxxxx xxxxxxxx";)",
1272                getStyleWithColumns(getDefaultStyle(), 32));
1273   // Space around braces should be correct.
1274   auto Style = getStyleWithColumns(getDefaultStyle(), 24);
1275   Style.Cpp11BracedListStyle = false;
1276   verifyFormat(R"(x({ "xxxxxxxxxxxxxxxx ",
1277     "xxxx" });)",
1278                R"(x("xxxxxxxxxxxxxxxx xxxx");)", Style);
1279   // Braces should not be added twice.
1280   verifyFormat(R"(x({"xxxxxxxx",
1281    "xxxxxxxx",
1282    "xxxxxx"});)",
1283                R"(x("xxxxxxxxxxxxxxxxxxxxxx");)",
1284                getStyleWithColumns(getDefaultStyle(), 14));
1285   verifyFormat(R"(x({"xxxxxxxxxxxxxxxx ",
1286    "xxxxxxxxxxxxxxxx ",
1287    "xxxx"});)",
1288                R"(x("xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx xxxx");)",
1289                getStyleWithColumns(getDefaultStyle(), 23));
1290   verifyFormat(R"(x({"xxxxxxxxxxxxxxxx ",
1291    "xxxxxxxxxxxxxxxx ",
1292    "xxxx"});)",
1293                R"(x({"xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx ", "xxxx"});)",
1294                getStyleWithColumns(getDefaultStyle(), 23));
1295   // import/export "DPI"/"DPI-C" cannot be split.
1296   verifyFormat(R"(import
1297     "DPI-C" function void foo
1298     ();)",
1299                R"(import "DPI-C" function void foo();)",
1300                getStyleWithColumns(getDefaultStyle(), 23));
1301   verifyFormat(R"(export "DPI-C" function foo;)",
1302                R"(export "DPI-C" function foo;)",
1303                getStyleWithColumns(getDefaultStyle(), 23));
1304   // These kinds of strings don't exist in Verilog.
1305   verifyNoCrash(R"(x(@"xxxxxxxxxxxxxxxx xxxx");)",
1306                 getStyleWithColumns(getDefaultStyle(), 23));
1307   verifyNoCrash(R"(x(u"xxxxxxxxxxxxxxxx xxxx");)",
1308                 getStyleWithColumns(getDefaultStyle(), 23));
1309   verifyNoCrash(R"(x(u8"xxxxxxxxxxxxxxxx xxxx");)",
1310                 getStyleWithColumns(getDefaultStyle(), 23));
1311   verifyNoCrash(R"(x(_T("xxxxxxxxxxxxxxxx xxxx"));)",
1312                 getStyleWithColumns(getDefaultStyle(), 23));
1313 }
1314 
1315 TEST_F(FormatTestVerilog, StructLiteral) {
1316   verifyFormat("c = '{0, 0.0};");
1317   verifyFormat("c = '{'{1, 1.0}, '{2, 2.0}};");
1318   verifyFormat("c = '{a: 0, b: 0.0};");
1319   verifyFormat("c = '{a: 0, b: 0.0, default: 0};");
1320   verifyFormat("d = {int: 1, shortreal: 1.0};");
1321   verifyFormat("c = '{default: 0};");
1322 
1323   // The identifier before the quote can be either a tag or a type case.  There
1324   // should be a space between the tag and the quote.
1325   verifyFormat("c = ab'{a: 0, b: 0.0};");
1326   verifyFormat("c = ab'{cd: cd'{1, 1.0}, ef: ef'{2, 2.0}};");
1327   verifyFormat("c = ab'{cd'{1, 1.0}, ef'{2, 2.0}};");
1328   verifyFormat("d = ab'{int: 1, shortreal: 1.0};");
1329   verifyFormat("x = tagged Add '{e1, 4, ed};");
1330 
1331   auto Style = getDefaultStyle();
1332   Style.SpacesInContainerLiterals = true;
1333   verifyFormat("c = '{a : 0, b : 0.0};", Style);
1334   verifyFormat("c = '{a : 0, b : 0.0, default : 0};", Style);
1335   verifyFormat("c = ab'{a : 0, b : 0.0};", Style);
1336   verifyFormat("c = ab'{cd : cd'{1, 1.0}, ef : ef'{2, 2.0}};", Style);
1337 
1338   // It should be indented correctly when the line has to break.
1339   verifyFormat("c = //\n"
1340                "    '{default: 0};");
1341   Style = getDefaultStyle();
1342   Style.ContinuationIndentWidth = 2;
1343   verifyFormat("c = //\n"
1344                "  '{default: 0};",
1345                Style);
1346 }
1347 
1348 TEST_F(FormatTestVerilog, StructuredProcedure) {
1349   // Blocks should be indented correctly.
1350   verifyFormat("initial begin\n"
1351                "end");
1352   verifyFormat("initial begin\n"
1353                "  x <= x;\n"
1354                "  x <= x;\n"
1355                "end");
1356   verifyFormat("initial\n"
1357                "  x <= x;\n"
1358                "x <= x;");
1359   verifyFormat("always @(x) begin\n"
1360                "end");
1361   verifyFormat("always @(x) begin\n"
1362                "  x <= x;\n"
1363                "  x <= x;\n"
1364                "end");
1365   verifyFormat("always @(x)\n"
1366                "  x <= x;\n"
1367                "x <= x;");
1368   // Various keywords.
1369   verifyFormat("always @(x)\n"
1370                "  x <= x;");
1371   verifyFormat("always @(posedge x)\n"
1372                "  x <= x;");
1373   verifyFormat("always @(posedge x or posedge y)\n"
1374                "  x <= x;");
1375   verifyFormat("always @(posedge x, posedge y)\n"
1376                "  x <= x;");
1377   verifyFormat("always @(negedge x, negedge y)\n"
1378                "  x <= x;");
1379   verifyFormat("always @(edge x, edge y)\n"
1380                "  x <= x;");
1381   verifyFormat("always\n"
1382                "  x <= x;");
1383   verifyFormat("always @*\n"
1384                "  x <= x;");
1385   verifyFormat("always @(*)\n"
1386                "  x <= x;");
1387   verifyFormat("always_comb\n"
1388                "  x <= x;");
1389   verifyFormat("always_latch @(x)\n"
1390                "  x <= x;");
1391   verifyFormat("always_ff @(posedge x)\n"
1392                "  x <= x;");
1393   verifyFormat("initial\n"
1394                "  x <= x;");
1395   verifyFormat("final\n"
1396                "  x <= x;");
1397   verifyFormat("forever\n"
1398                "  x <= x;");
1399 }
1400 } // namespace
1401 } // namespace test
1402 } // namespace format
1403 } // namespace clang
1404