1 //===- unittest/Format/FormatTestVerilog.cpp ------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "format-test"
17 class FormatTestVerilog
: public test::FormatTestBase
{
19 FormatStyle
getDefaultStyle() const override
{
20 return getLLVMStyle(FormatStyle::LK_Verilog
);
22 std::string
messUp(StringRef Code
) const override
{
23 return test::messUp(Code
, /*HandleHash=*/false);
27 TEST_F(FormatTestVerilog
, Align
) {
28 FormatStyle Style
= getDefaultStyle();
29 Style
.AlignConsecutiveAssignments
.Enabled
= true;
30 verifyFormat("x <= x;\n"
31 "sfdbddfbdfbb <= x;\n"
34 verifyFormat("x = x;\n"
38 // Compound assignments are not aligned by default. '<=' is not a compound
40 verifyFormat("x <= x;\n"
43 verifyFormat("x += x;\n"
46 verifyFormat("x <<= x;\n"
49 verifyFormat("x <<<= x;\n"
52 verifyFormat("x >>= x;\n"
55 verifyFormat("x >>>= x;\n"
58 Style
.AlignConsecutiveAssignments
.AlignCompound
= true;
59 verifyFormat("x <= x;\n"
62 verifyFormat("x += x;\n"
65 verifyFormat("x <<= x;\n"
68 verifyFormat("x <<<= x;\n"
71 verifyFormat("x >>= x;\n"
74 verifyFormat("x >>>= x;\n"
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);");
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?;");
117 TEST_F(FormatTestVerilog
, Block
) {
118 verifyFormat("begin\n"
121 verifyFormat("begin : x\n"
124 verifyFormat("begin\n"
128 verifyFormat("fork\n"
131 verifyFormat("fork\n"
134 verifyFormat("fork\n"
137 verifyFormat("generate\n"
140 verifyFormat("generate : x\n"
144 verifyFormat("begin\n"
148 verifyFormat("begin : x\n"
152 verifyFormat("begin : x\n"
156 verifyFormat("begin\n"
160 // Test that 'disable fork' and 'rand join' don't get mistaken as blocks.
161 verifyFormat("disable fork;\n"
163 verifyFormat("rand join x x;\n"
165 // The begin keyword should not be indented if it is too long to fit on the
167 verifyFormat("while (true) //\n"
173 verifyFormat("while (true) //\n"
179 verifyFormat("while (true) //\n"
185 auto Style
= getDefaultStyle();
186 Style
.ColumnLimit
= 17;
187 verifyFormat("while (true)\n"
193 "while (true) begin\n"
194 " while (true) begin"
200 TEST_F(FormatTestVerilog
, Case
) {
201 verifyFormat("case (data)\n"
203 verifyFormat("casex (data)\n"
205 verifyFormat("casez (data)\n"
207 verifyFormat("case (data) inside\n"
209 verifyFormat("case (data)\n"
211 " result = 10'b0111111111;\n"
213 verifyFormat("case (data)\n"
215 " result = 10'b0111111111;\n"
217 // Test labels with multiple options.
218 verifyFormat("case (data)\n"
220 " result = 10'b0111111111;\n"
222 verifyFormat("case (data)\n"
225 " result = 10'b0111111111;\n"
227 // Test that blocks following labels are indented.
228 verifyFormat("case (data)\n"
230 " result = 10'b1011111111;\n"
233 verifyFormat("case (data)\n"
235 " result = 10'b1011111111;\n"
239 verifyFormat("case (data)\n"
241 " result = 10'b1011111111;\n"
243 verifyFormat("case (data)\n"
245 " result = 10'b1011111111;\n"
247 // Test that question marks and colons don't get mistaken as labels.
248 verifyFormat("case (data)\n"
250 " instruction1(ir);\n"
252 verifyFormat("case (data)\n"
253 " x ? 8'b1??????? : 1:\n"
254 " instruction3(ir);\n"
256 // Test indention options.
257 auto Style
= getDefaultStyle();
258 Style
.IndentCaseLabels
= false;
259 verifyFormat("case (data)\n"
261 " result = 10'b0111111111;\n"
264 verifyFormat("case (data)\n"
266 " result = 10'b0111111111;\n"
270 Style
.IndentCaseLabels
= true;
271 verifyFormat("case (data)\n"
273 " result = 10'b0111111111;\n"
276 verifyFormat("case (data)\n"
278 " result = 10'b0111111111;\n"
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"
288 verifyFormat("default:\n"
291 Style
.BitFieldColonSpacing
= FormatStyle::BFCS_Both
;
292 verifyFormat("case (x[1 : 0])\n"
295 verifyFormat("default:\n"
296 " x[1 : 0] = x[1 : 0];",
298 Style
= getDefaultStyle();
299 Style
.SpacesInContainerLiterals
= true;
300 verifyFormat("case ('{x : x, default : 9})\n"
303 verifyFormat("x = '{x : x, default : 9};", Style
);
304 verifyFormat("default:\n"
305 " x = '{x : x, default : 9};",
307 Style
.SpacesInContainerLiterals
= false;
308 verifyFormat("case ('{x: x, default: 9})\n"
311 verifyFormat("x = '{x: x, default: 9};", Style
);
312 verifyFormat("default:\n"
313 " x = '{x: x, default: 9};",
315 // When the line following the case label needs to be broken, the continuation
316 // should be indented correctly.
317 verifyFormat("case (data)\n"
322 verifyFormat("case (data)\n"
328 verifyFormat("case (data)\n"
330 " result = (10'b0111111111 + //\n"
331 " 10'b0111111111 + //\n"
332 " 10'b0111111111);\n"
334 verifyFormat("case (data)\n"
337 " (10'b0111111111 + //\n"
338 " 10'b0111111111 + //\n"
339 " 10'b0111111111);\n"
341 verifyFormat("case (data)\n"
344 " longfunction( //\n"
347 verifyFormat("case (data)\n"
353 verifyFormat("case (data)\n"
361 Style
= getDefaultStyle();
362 Style
.ContinuationIndentWidth
= 1;
363 verifyFormat("case (data)\n"
369 verifyFormat("case (data)\n"
372 " longfunction( //\n"
377 verifyFormat("case (v) matches\n"
378 " tagged Valid .n:\n"
383 TEST_F(FormatTestVerilog
, Coverage
) {
384 verifyFormat("covergroup x\n"
389 TEST_F(FormatTestVerilog
, Declaration
) {
390 verifyFormat("wire mynet;");
391 verifyFormat("wire mynet, mynet1;");
392 verifyFormat("wire mynet, //\n"
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"
399 verifyFormat("wire logic #0 mynet, //\n"
401 verifyFormat("wire #(1, 2, 3) mynet, //\n"
403 verifyFormat("wire mynet = enable;");
404 verifyFormat("wire mynet = enable, mynet1;");
405 verifyFormat("wire mynet = enable, //\n"
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"
417 verifyFormat("wire (strong1, pull0) mynet = enable;");
418 verifyFormat("wire (strong1, pull0) mynet = enable, mynet1;");
419 verifyFormat("wire (strong1, pull0) mynet = enable, //\n"
421 verifyFormat("wire (strong1, pull0) mynet, mynet1 = enable;");
422 verifyFormat("wire (strong1, pull0) mynet, //\n"
423 " mynet1 = enable;");
426 TEST_F(FormatTestVerilog
, Delay
) {
427 // Delay by the default unit.
430 verifyFormat("#10;");
431 verifyFormat("#1.5;");
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"
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;");
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"
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"
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"
481 verifyFormat("module test\n"
482 " (input [7 : 0] a,\n"
483 " (* x = x *) input signed [7 : 0] b, c, d);\n"
485 verifyFormat("module test\n"
486 " (input [7 : 0] a = 0,\n"
487 " input signed [7 : 0] b = 0, c = 0, d = 0);\n"
489 verifyFormat("module test\n"
491 " (input [7 : 0] a,\n"
492 " input signed [7 : 0] b, c, d);\n"
494 // When a line needs to be broken, ports of the same type should be aligned to
496 verifyFormat("module test\n"
497 " (input signed [7 : 0] b, c, //\n"
500 verifyFormat("module test\n"
501 " ((* x = x *) input signed [7 : 0] b, c, //\n"
504 verifyFormat("module test\n"
505 " (input signed [7 : 0] b = 0, c, //\n"
508 verifyFormat("module test\n"
509 " (input signed [7 : 0] b, c = 0, //\n"
512 verifyFormat("module test\n"
513 " (input signed [7 : 0] b, c, //\n"
516 verifyFormat("module test\n"
517 " (input wire logic signed [7 : 0][0 : 1] b, c, //\n"
520 verifyFormat("module test\n"
521 " (input signed [7 : 0] b, //\n"
525 verifyFormat("module test\n"
526 " (input [7 : 0] a,\n"
527 " input signed [7 : 0] b, //\n"
531 verifyFormat("module test\n"
532 " (input signed [7 : 0] b, //\n"
535 " output signed [7 : 0] h);\n"
538 verifyFormat("module m\n"
541 verifyFormat("module m\n"
542 " (i2.master i, ii);\n"
544 verifyFormat("module m\n"
545 " (i2.master i, //\n"
548 verifyFormat("module m\n"
552 verifyFormat("module m\n"
553 " (i2::i2.master i);\n"
555 verifyFormat("module m\n"
556 " (i2::i2.master i, ii);\n"
558 verifyFormat("module m\n"
559 " (i2::i2.master i, //\n"
562 verifyFormat("module m\n"
563 " (i2::i2.master i,\n"
566 verifyFormat("module m\n"
569 verifyFormat("module m\n"
572 verifyFormat("module m\n"
576 verifyFormat("module m\n"
580 // With a macro in the names.
581 verifyFormat("module m\n"
582 " (input var `x a, b);\n"
584 verifyFormat("module m\n"
585 " (input var `x a, //\n"
588 verifyFormat("module m\n"
589 " (input var x `a, b);\n"
591 verifyFormat("module m\n"
592 " (input var x `a, //\n"
595 // A line comment shouldn't disrupt the indentation of the port list.
596 verifyFormat("extern module x\n"
599 verifyFormat("extern module x\n"
604 // With a concatenation in the names.
605 auto Style
= getDefaultStyle();
606 Style
.ColumnLimit
= 40;
607 verifyFormat("`define X(x) \\\n"
609 " (input var x``x a, b);",
611 verifyFormat("`define X(x) \\\n"
613 " (input var x``x aaaaaaaaaaaaaaa, \\\n"
616 verifyFormat("`define X(x) \\\n"
618 " (input var x a``x, b);",
620 verifyFormat("`define X(x) \\\n"
622 " (input var x aaaaaaaaaaaaaaa``x, \\\n"
625 // When the ports line is not to be formatted, following lines should not take
626 // on its indentation.
627 verifyFormat("module x\n"
635 getDefaultStyle(), {tooling::Range(25, 18)});
638 TEST_F(FormatTestVerilog
, Hierarchy
) {
639 verifyFormat("module x;\n"
641 // Test that the end label is on the same line as the end keyword.
642 verifyFormat("module x;\n"
644 // Test that things inside are indented.
645 verifyFormat("module x;\n"
649 verifyFormat("program x;\n"
653 verifyFormat("interface x;\n"
657 verifyFormat("task x;\n"
661 verifyFormat("function x;\n"
665 verifyFormat("class x;\n"
669 // Test that they nest.
670 verifyFormat("module x;\n"
676 // Test that an extern declaration doesn't change the indentation.
677 verifyFormat("extern module x;\n"
679 // Test complex headers
680 verifyFormat("extern module x\n"
681 " import x.x::x::*;\n"
685 verifyFormat("module x\n"
686 " import x.x::x::*;\n"
693 verifyFormat("virtual class x\n"
696 " implements x, x, x;\n"
700 verifyFormat("function automatic logic [1 : 0] x\n"
705 // Type names with '::' should be recognized.
706 verifyFormat("function automatic x::x x\n"
709 // Names having to do macros should be recognized.
710 verifyFormat("function automatic x::x x``x\n"
713 verifyFormat("function automatic x::x `x\n"
716 verifyNoCrash("x x(x x, x x);");
719 TEST_F(FormatTestVerilog
, Identifiers
) {
720 // Escaped identifiers should not be split.
721 verifyFormat("\\busa+index");
722 verifyFormat("\\-clock");
723 verifyFormat("\\***error-condition***");
724 verifyFormat("\\net1\\/net2");
725 verifyFormat("\\{a,b}");
726 verifyFormat("\\a*(b+c)");
727 // Escaped identifiers can't be joined with the next token. Extra space
728 // should be removed.
729 verifyFormat("\\busa+index ;", "\\busa+index\n"
731 verifyFormat("\\busa+index ;", "\\busa+index\r\n"
733 verifyFormat("\\busa+index ;", "\\busa+index ;");
734 verifyFormat("\\busa+index ;", "\\busa+index\n"
736 verifyFormat("\\busa+index ;");
737 verifyFormat("(\\busa+index );");
738 verifyFormat("\\busa+index \\busa+index ;");
741 TEST_F(FormatTestVerilog
, If
) {
742 verifyFormat("if (x)\n"
744 verifyFormat("unique if (x)\n"
746 verifyFormat("unique0 if (x)\n"
748 verifyFormat("priority if (x)\n"
750 verifyFormat("if (x)\n"
755 verifyFormat("if (x)\n"
761 verifyFormat("if (x) begin\n"
763 "end else if (x) begin\n"
768 verifyFormat("if (x) begin : x\n"
770 "end : x else if (x) begin : x\n"
772 "end : x else begin : x\n"
776 // Test block keywords.
777 verifyFormat("if (x) begin\n"
780 verifyFormat("if (x) begin : x\n"
783 verifyFormat("if (x) begin\n"
787 verifyFormat("if (x) fork\n"
790 verifyFormat("if (x) fork\n"
793 verifyFormat("if (x) fork\n"
796 verifyFormat("if (x) generate\n"
799 verifyFormat("if (x) generate : x\n"
803 // Test that concatenation braces don't get regarded as blocks.
804 verifyFormat("if (x)\n"
806 verifyFormat("if (x)\n"
808 verifyFormat("if (x)\n"
814 verifyFormat("(* x *) if (x)\n"
816 verifyFormat("(* x = \"x\" *) if (x)\n"
818 verifyFormat("(* x, x = \"x\" *) if (x)\n"
821 // Assert are treated similar to if. But the else parts should not be
823 verifyFormat("assert (x);");
824 verifyFormat("assert (x)\n"
826 verifyFormat("assert (x)\n"
830 verifyFormat("assert (x)\n"
833 verifyFormat("assert (x)\n"
836 verifyFormat("assert (x)\n"
840 verifyFormat("assert (x)\n"
845 verifyFormat("assert (x)\n"
852 verifyFormat("assert (x)\n"
861 // The body is optional for asserts. The next line should not be indented if
862 // the statement already ended with a semicolon.
863 verifyFormat("assert (x);\n"
865 verifyFormat("if (x)\n"
867 "else if (x) begin\n"
870 verifyFormat("if (x)\n"
874 verifyFormat("if (x)\n"
879 verifyFormat("assume (x)\n"
881 verifyFormat("cover (x)\n"
883 verifyFormat("restrict (x)\n"
885 verifyFormat("assert #0 (x)\n"
887 verifyFormat("assert final (x)\n"
889 verifyFormat("cover #0 (x)\n"
891 verifyFormat("cover final (x)\n"
894 // The space around parentheses options should work.
895 auto Style
= getDefaultStyle();
896 verifyFormat("if (x)\n"
901 verifyFormat("assert (x);", Style
);
902 verifyFormat("assert #0 (x);", Style
);
903 verifyFormat("assert (x)\n"
908 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
909 Style
.SpacesInParensOptions
.InConditionalStatements
= true;
910 verifyFormat("if ( x )\n"
915 verifyFormat("assert ( x );", Style
);
916 verifyFormat("assert #0 ( x );", Style
);
917 verifyFormat("assert ( x )\n"
924 TEST_F(FormatTestVerilog
, Instantiation
) {
926 verifyFormat("ffnand ff1;");
928 verifyFormat("ffnand ff1(.qbar(out1),\n"
932 verifyFormat("ffnand ff1(.qbar(out1),\n"
936 verifyFormat("ffnand ff1(.*,\n"
940 // With unconnected ports.
941 verifyFormat("ffnand ff1(.q(),\n"
945 verifyFormat("ffnand ff1(.q(),\n"
949 verifyFormat("ffnand ff1(,\n"
953 // With positional ports.
954 verifyFormat("ffnand ff1(out1,\n"
957 verifyFormat("ffnand ff1(,\n"
961 // Multiple instantiations.
962 verifyFormat("ffnand ff1(.q(),\n"
970 verifyFormat("ffnand //\n"
979 verifyNoCrash(", ff1();");
980 // With breaking between instance ports disabled.
981 auto Style
= getDefaultStyle();
982 Style
.VerilogBreakBetweenInstancePorts
= false;
983 verifyFormat("ffnand ff1;", Style
);
984 verifyFormat("ffnand ff1(.qbar(out1), .clear(in1), .preset(in2), .*);",
986 verifyFormat("ffnand ff1(out1, in1, in2);", Style
);
987 verifyFormat("ffnand ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n"
988 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));",
990 verifyFormat("ffnand //\n"
991 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n"
992 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));",
996 TEST_F(FormatTestVerilog
, Loop
) {
997 verifyFormat("foreach (x[x])\n"
999 verifyFormat("repeat (x)\n"
1001 verifyFormat("foreach (x[x]) begin\n"
1003 verifyFormat("repeat (x) begin\n"
1005 auto Style
= getDefaultStyle();
1006 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
1007 Style
.SpacesInParensOptions
.InConditionalStatements
= true;
1008 verifyFormat("foreach ( x[x] )\n"
1011 verifyFormat("repeat ( x )\n"
1016 TEST_F(FormatTestVerilog
, Operators
) {
1017 // Test that unary operators are not followed by space.
1018 verifyFormat("x = +x;");
1019 verifyFormat("x = -x;");
1020 verifyFormat("x = !x;");
1021 verifyFormat("x = ~x;");
1022 verifyFormat("x = &x;");
1023 verifyFormat("x = ~&x;");
1024 verifyFormat("x = |x;");
1025 verifyFormat("x = ~|x;");
1026 verifyFormat("x = ^x;");
1027 verifyFormat("x = ~^x;");
1028 verifyFormat("x = ^~x;");
1029 verifyFormat("x = ++x;");
1030 verifyFormat("x = --x;");
1032 // Test that `*` and `*>` are binary.
1033 verifyFormat("x = x * x;");
1034 verifyFormat("x = (x * x);");
1035 verifyFormat("(opcode *> o1) = 6.1;");
1036 verifyFormat("(C, D *> Q) = 18;");
1037 // The wildcard import is not a binary operator.
1038 verifyFormat("import p::*;");
1040 // Test that operators don't get split.
1041 verifyFormat("x = x++;");
1042 verifyFormat("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 >= x;");
1050 verifyFormat("x = x == x;");
1051 verifyFormat("x = x != x;");
1052 verifyFormat("x = x === x;");
1053 verifyFormat("x = x !== x;");
1054 verifyFormat("x = x ==? x;");
1055 verifyFormat("x = x !=? x;");
1056 verifyFormat("x = x ~^ x;");
1057 verifyFormat("x = x ^~ x;");
1058 verifyFormat("x = x && x;");
1059 verifyFormat("x = x || x;");
1060 verifyFormat("x = x -> x;");
1061 verifyFormat("x = x <-> x;");
1062 verifyFormat("x += x;");
1063 verifyFormat("x -= x;");
1064 verifyFormat("x *= x;");
1065 verifyFormat("x /= x;");
1066 verifyFormat("x %= x;");
1067 verifyFormat("x &= x;");
1068 verifyFormat("x ^= x;");
1069 verifyFormat("x |= x;");
1070 verifyFormat("x <<= x;");
1071 verifyFormat("x >>= x;");
1072 verifyFormat("x <<<= x;");
1073 verifyFormat("x >>>= x;");
1074 verifyFormat("x <= x;");
1076 // Test that space is added between operators.
1077 verifyFormat("x = x < -x;", "x=x<-x;");
1078 verifyFormat("x = x << -x;", "x=x<<-x;");
1079 verifyFormat("x = x <<< -x;", "x=x<<<-x;");
1081 // Test that operators that are C++ identifiers get treated as operators.
1082 verifyFormat("solve s before d;"); // before
1083 verifyFormat("binsof(i) intersect {0};"); // intersect
1084 verifyFormat("req dist {1};"); // dist
1085 verifyFormat("a inside {b, c};"); // inside
1086 verifyFormat("bus.randomize() with { atype == low; };"); // with
1089 TEST_F(FormatTestVerilog
, Preprocessor
) {
1090 auto Style
= getDefaultStyle();
1091 Style
.ColumnLimit
= 20;
1093 // Macro definitions.
1094 verifyFormat("`define X \\\n"
1097 "`define X if(x)x=x;", Style
);
1098 verifyFormat("`define X(x) \\\n"
1101 "`define X(x) if(x)x=x;", Style
);
1102 verifyFormat("`define X \\\n"
1105 "`define X x=x;x=x;", Style
);
1106 // Macro definitions with invocations inside.
1107 verifyFormat("`define LIST \\\n"
1114 verifyFormat("`define LIST \\\n"
1121 verifyFormat("`define LIST \\\n"
1124 "`define LIST `x=`x;`x=`x;", Style
);
1125 // Macro invocations.
1126 verifyFormat("`x = (`x1 + `x2 + x);");
1127 // Lines starting with a preprocessor directive should not be indented.
1128 std::string Directives
[] = {
1142 "nounconnected_drive",
1146 "unconnected_drive",
1150 for (auto &Name
: Directives
) {
1151 verifyFormat("if (x)\n"
1163 // Lines starting with a regular macro invocation should be indented as a
1165 verifyFormat("if (x)\n"
1167 "`timescale 1ns / 1ps",
1170 "`timescale 1ns / 1ps",
1172 verifyFormat("if (x)\n"
1173 "`timescale 1ns / 1ps\n"
1176 "`timescale 1ns / 1ps\n"
1179 std::string NonDirectives
[] = {
1180 // For `__FILE__` and `__LINE__`, although the standard classifies them as
1181 // preprocessor directives, they are used like regular macros.
1182 "__FILE__", "__LINE__", "elif", "foo", "x",
1184 for (auto &Name
: NonDirectives
) {
1185 verifyFormat("if (x)\n"
1197 TEST_F(FormatTestVerilog
, Primitive
) {
1198 verifyFormat("primitive multiplexer\n"
1199 " (mux, control, dataA, dataB);\n"
1201 " input control, dataA, dataB;\n"
1211 verifyFormat("primitive latch\n"
1212 " (q, ena_, data);\n"
1215 " input ena_, data;\n"
1223 verifyFormat("primitive d\n"
1224 " (q, clock, data);\n"
1227 " input clock, data;\n"
1229 " (01) 0 : ? : 0;\n"
1230 " (01) 1 : ? : 1;\n"
1231 " (0?) 1 : 1 : 1;\n"
1232 " (0?) 0 : 0 : 0;\n"
1233 " (?0) ? : ? : -;\n"
1234 " (?\?) ? : ? : -;\n"
1239 TEST_F(FormatTestVerilog
, Streaming
) {
1240 verifyFormat("x = {>>{j}};");
1241 verifyFormat("x = {>>byte{j}};");
1242 verifyFormat("x = {<<{j}};");
1243 verifyFormat("x = {<<byte{j}};");
1244 verifyFormat("x = {<<16{j}};");
1245 verifyFormat("x = {<<{8'b0011_0101}};");
1246 verifyFormat("x = {<<4{6'b11_0101}};");
1247 verifyFormat("x = {>>4{6'b11_0101}};");
1248 verifyFormat("x = {<<2{{<<{4'b1101}}}};");
1249 verifyFormat("bit [96 : 1] y = {>>{a, b, c}};");
1250 verifyFormat("int j = {>>{a, b, c}};");
1251 verifyFormat("{>>{a, b, c}} = 23'b1;");
1252 verifyFormat("{>>{a, b, c}} = x;");
1253 verifyFormat("{>>{j}} = x;");
1254 verifyFormat("{>>byte{j}} = x;");
1255 verifyFormat("{<<{j}} = x;");
1256 verifyFormat("{<<byte{j}} = x;");
1259 TEST_F(FormatTestVerilog
, StringLiteral
) {
1260 // Long strings should be broken.
1261 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1263 R
"(x({"xxxxxxxxxxxxxxxx xxxx
"});)",
1264 getStyleWithColumns(getDefaultStyle(), 23));
1265 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1267 R
"(x({"xxxxxxxxxxxxxxxx xxxx
"});)",
1268 getStyleWithColumns(getDefaultStyle(), 22));
1269 // Braces should be added when they don't already exist.
1270 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1272 R
"(x("xxxxxxxxxxxxxxxx xxxx
");)",
1273 getStyleWithColumns(getDefaultStyle(), 23));
1274 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1276 R
"(x("xxxxxxxxxxxxxxxx xxxx
");)",
1277 getStyleWithColumns(getDefaultStyle(), 22));
1278 verifyFormat(R
"(x({{"xxxxxxxxxxxxxxxx
",
1280 R
"(x({"xxxxxxxxxxxxxxxx xxxx
" == x});)",
1281 getStyleWithColumns(getDefaultStyle(), 24));
1282 verifyFormat(R
"(string x = {"xxxxxxxxxxxxxxxx
",
1284 R
"(string x = "xxxxxxxxxxxxxxxx xxxxxxxx
";)",
1285 getStyleWithColumns(getDefaultStyle(), 32));
1286 // Space around braces should be correct.
1287 auto Style
= getStyleWithColumns(getDefaultStyle(), 24);
1288 Style
.Cpp11BracedListStyle
= false;
1289 verifyFormat(R
"(x({ "xxxxxxxxxxxxxxxx
",
1291 R
"(x("xxxxxxxxxxxxxxxx xxxx
");)", Style
);
1292 // Braces should not be added twice.
1293 verifyFormat(R
"(x({"xxxxxxxx
",
1296 R
"(x("xxxxxxxxxxxxxxxxxxxxxx
");)",
1297 getStyleWithColumns(getDefaultStyle(), 14));
1298 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1299 "xxxxxxxxxxxxxxxx
",
1301 R
"(x("xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx xxxx
");)",
1302 getStyleWithColumns(getDefaultStyle(), 23));
1303 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1304 "xxxxxxxxxxxxxxxx
",
1306 R
"(x({"xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx
", "xxxx
"});)",
1307 getStyleWithColumns(getDefaultStyle(), 23));
1308 // import/export "DPI"/"DPI-C" cannot be split.
1309 verifyFormat(R
"(import
1310 "DPI
-C
" function void foo
1312 R
"(import "DPI
-C
" function void foo();)",
1313 getStyleWithColumns(getDefaultStyle(), 23));
1314 verifyFormat(R
"(export "DPI
-C
" function foo;)",
1315 R
"(export "DPI
-C
" function foo;)",
1316 getStyleWithColumns(getDefaultStyle(), 23));
1317 // These kinds of strings don't exist in Verilog.
1318 verifyNoCrash(R
"(x(@"xxxxxxxxxxxxxxxx xxxx
");)",
1319 getStyleWithColumns(getDefaultStyle(), 23));
1320 verifyNoCrash(R
"(x(u"xxxxxxxxxxxxxxxx xxxx
");)",
1321 getStyleWithColumns(getDefaultStyle(), 23));
1322 verifyNoCrash(R
"(x(u8"xxxxxxxxxxxxxxxx xxxx
");)",
1323 getStyleWithColumns(getDefaultStyle(), 23));
1324 verifyNoCrash(R
"(x(_T("xxxxxxxxxxxxxxxx xxxx
"));)",
1325 getStyleWithColumns(getDefaultStyle(), 23));
1328 TEST_F(FormatTestVerilog
, StructLiteral
) {
1329 verifyFormat("c = '{0, 0.0};");
1330 verifyFormat("c = '{'{1, 1.0}, '{2, 2.0}};");
1331 verifyFormat("c = '{a: 0, b: 0.0};");
1332 verifyFormat("c = '{a: 0, b: 0.0, default: 0};");
1333 verifyFormat("d = {int: 1, shortreal: 1.0};");
1334 verifyFormat("c = '{default: 0};");
1336 // The identifier before the quote can be either a tag or a type case. There
1337 // should be a space between the tag and the quote.
1338 verifyFormat("c = ab'{a: 0, b: 0.0};");
1339 verifyFormat("c = ab'{cd: cd'{1, 1.0}, ef: ef'{2, 2.0}};");
1340 verifyFormat("c = ab'{cd'{1, 1.0}, ef'{2, 2.0}};");
1341 verifyFormat("d = ab'{int: 1, shortreal: 1.0};");
1342 verifyFormat("x = tagged Add '{e1, 4, ed};");
1344 auto Style
= getDefaultStyle();
1345 Style
.SpacesInContainerLiterals
= true;
1346 verifyFormat("c = '{a : 0, b : 0.0};", Style
);
1347 verifyFormat("c = '{a : 0, b : 0.0, default : 0};", Style
);
1348 verifyFormat("c = ab'{a : 0, b : 0.0};", Style
);
1349 verifyFormat("c = ab'{cd : cd'{1, 1.0}, ef : ef'{2, 2.0}};", Style
);
1351 // It should be indented correctly when the line has to break.
1352 verifyFormat("c = //\n"
1354 Style
= getDefaultStyle();
1355 Style
.ContinuationIndentWidth
= 2;
1356 verifyFormat("c = //\n"
1361 TEST_F(FormatTestVerilog
, StructuredProcedure
) {
1362 // Blocks should be indented correctly.
1363 verifyFormat("initial begin\n"
1365 verifyFormat("initial begin\n"
1369 verifyFormat("initial\n"
1372 verifyFormat("always @(x) begin\n"
1374 verifyFormat("always @(x) begin\n"
1378 verifyFormat("always @(x)\n"
1381 // Various keywords.
1382 verifyFormat("always @(x)\n"
1384 verifyFormat("always @(posedge x)\n"
1386 verifyFormat("always @(posedge x or posedge y)\n"
1388 verifyFormat("always @(posedge x, posedge y)\n"
1390 verifyFormat("always @(negedge x, negedge y)\n"
1392 verifyFormat("always @(edge x, edge y)\n"
1394 verifyFormat("always\n"
1396 verifyFormat("always @*\n"
1398 verifyFormat("always @(*)\n"
1400 verifyFormat("always_comb\n"
1402 verifyFormat("always_latch @(x)\n"
1404 verifyFormat("always_ff @(posedge x)\n"
1406 verifyFormat("initial\n"
1408 verifyFormat("final\n"
1410 verifyFormat("forever\n"
1415 } // namespace format
1416 } // namespace clang