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(llvm::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 Style
= getDefaultStyle();
348 Style
.ContinuationIndentWidth
= 1;
349 verifyFormat("case (data)\n"
355 verifyFormat("case (data)\n"
358 " longfunction( //\n"
364 TEST_F(FormatTestVerilog
, Coverage
) {
365 verifyFormat("covergroup x\n"
370 TEST_F(FormatTestVerilog
, Declaration
) {
371 verifyFormat("wire mynet;");
372 verifyFormat("wire mynet, mynet1;");
373 verifyFormat("wire mynet, //\n"
375 verifyFormat("wire mynet = enable;");
376 verifyFormat("wire mynet = enable, mynet1;");
377 verifyFormat("wire mynet = enable, //\n"
379 verifyFormat("wire mynet, mynet1 = enable;");
380 verifyFormat("wire mynet, //\n"
381 " mynet1 = enable;");
382 verifyFormat("wire mynet = enable, mynet1 = enable;");
383 verifyFormat("wire mynet = enable, //\n"
384 " mynet1 = enable;");
385 verifyFormat("wire (strong1, pull0) mynet;");
386 verifyFormat("wire (strong1, pull0) mynet, mynet1;");
387 verifyFormat("wire (strong1, pull0) mynet, //\n"
389 verifyFormat("wire (strong1, pull0) mynet = enable;");
390 verifyFormat("wire (strong1, pull0) mynet = enable, mynet1;");
391 verifyFormat("wire (strong1, pull0) mynet = enable, //\n"
393 verifyFormat("wire (strong1, pull0) mynet, mynet1 = enable;");
394 verifyFormat("wire (strong1, pull0) mynet, //\n"
395 " mynet1 = enable;");
398 TEST_F(FormatTestVerilog
, Delay
) {
399 // Delay by the default unit.
402 verifyFormat("#10;");
403 verifyFormat("#1.5;");
405 verifyFormat("#1fs;");
406 verifyFormat("#1.5fs;");
407 verifyFormat("#1ns;");
408 verifyFormat("#1.5ns;");
409 verifyFormat("#1us;");
410 verifyFormat("#1.5us;");
411 verifyFormat("#1ms;");
412 verifyFormat("#1.5ms;");
413 verifyFormat("#1s;");
414 verifyFormat("#1.5s;");
415 // The following expression should be on the same line.
416 verifyFormat("#1 x = x;");
417 verifyFormat("#1 x = x;", "#1\n"
421 TEST_F(FormatTestVerilog
, Enum
) {
422 verifyFormat("enum { x } x;");
423 verifyFormat("typedef enum { x } x;");
424 verifyFormat("enum { red, yellow, green } x;");
425 verifyFormat("typedef enum { red, yellow, green } x;");
426 verifyFormat("enum integer { x } x;");
427 verifyFormat("typedef enum { x = 0 } x;");
428 verifyFormat("typedef enum { red = 0, yellow = 1, green = 2 } x;");
429 verifyFormat("typedef enum integer { x } x;");
430 verifyFormat("typedef enum bit [0 : 1] { x } x;");
431 verifyFormat("typedef enum { add = 10, sub[5], jmp[6 : 8] } E1;");
432 verifyFormat("typedef enum { add = 10, sub[5] = 0, jmp[6 : 8] = 1 } E1;");
435 TEST_F(FormatTestVerilog
, Headers
) {
436 // Test headers with multiple ports.
437 verifyFormat("module mh1\n"
438 " (input var int in1,\n"
439 " input var shortreal in2,\n"
440 " output tagged_st out);\n"
442 // There should be a space following the type but not the variable name.
443 verifyFormat("module test\n"
444 " (input wire [7 : 0] a,\n"
445 " input wire b[7 : 0],\n"
446 " input wire [7 : 0] c[7 : 0]);\n"
448 // Ports should be grouped by types.
449 verifyFormat("module test\n"
450 " (input [7 : 0] a,\n"
451 " input signed [7 : 0] b, c, d);\n"
453 verifyFormat("module test\n"
454 " (input [7 : 0] a,\n"
455 " (* x = x *) input signed [7 : 0] b, c, d);\n"
457 verifyFormat("module test\n"
458 " (input [7 : 0] a = 0,\n"
459 " input signed [7 : 0] b = 0, c = 0, d = 0);\n"
461 verifyFormat("module test\n"
463 " (input [7 : 0] a,\n"
464 " input signed [7 : 0] b, c, d);\n"
466 // When a line needs to be broken, ports of the same type should be aligned to
468 verifyFormat("module test\n"
469 " (input signed [7 : 0] b, c, //\n"
472 verifyFormat("module test\n"
473 " ((* x = x *) input signed [7 : 0] b, c, //\n"
476 verifyFormat("module test\n"
477 " (input signed [7 : 0] b = 0, c, //\n"
480 verifyFormat("module test\n"
481 " (input signed [7 : 0] b, c = 0, //\n"
484 verifyFormat("module test\n"
485 " (input signed [7 : 0] b, c, //\n"
488 verifyFormat("module test\n"
489 " (input wire logic signed [7 : 0][0 : 1] b, c, //\n"
492 verifyFormat("module test\n"
493 " (input signed [7 : 0] b, //\n"
497 verifyFormat("module test\n"
498 " (input [7 : 0] a,\n"
499 " input signed [7 : 0] b, //\n"
503 verifyFormat("module test\n"
504 " (input signed [7 : 0] b, //\n"
507 " output signed [7 : 0] h);\n"
510 verifyFormat("module m\n"
513 verifyFormat("module m\n"
514 " (i2.master i, ii);\n"
516 verifyFormat("module m\n"
517 " (i2.master i, //\n"
520 verifyFormat("module m\n"
524 verifyFormat("module m\n"
525 " (i2::i2.master i);\n"
527 verifyFormat("module m\n"
528 " (i2::i2.master i, ii);\n"
530 verifyFormat("module m\n"
531 " (i2::i2.master i, //\n"
534 verifyFormat("module m\n"
535 " (i2::i2.master i,\n"
538 verifyFormat("module m\n"
541 verifyFormat("module m\n"
544 verifyFormat("module m\n"
548 verifyFormat("module m\n"
552 // With a macro in the names.
553 verifyFormat("module m\n"
554 " (input var `x a, b);\n"
556 verifyFormat("module m\n"
557 " (input var `x a, //\n"
560 verifyFormat("module m\n"
561 " (input var x `a, b);\n"
563 verifyFormat("module m\n"
564 " (input var x `a, //\n"
567 // A line comment shouldn't disrupt the indentation of the port list.
568 verifyFormat("extern module x\n"
571 verifyFormat("extern module x\n"
576 // With a concatenation in the names.
577 auto Style
= getDefaultStyle();
578 Style
.ColumnLimit
= 40;
579 verifyFormat("`define X(x) \\\n"
581 " (input var x``x a, b);",
583 verifyFormat("`define X(x) \\\n"
585 " (input var x``x aaaaaaaaaaaaaaa, \\\n"
588 verifyFormat("`define X(x) \\\n"
590 " (input var x a``x, b);",
592 verifyFormat("`define X(x) \\\n"
594 " (input var x aaaaaaaaaaaaaaa``x, \\\n"
599 TEST_F(FormatTestVerilog
, Hierarchy
) {
600 verifyFormat("module x;\n"
602 // Test that the end label is on the same line as the end keyword.
603 verifyFormat("module x;\n"
605 // Test that things inside are indented.
606 verifyFormat("module x;\n"
610 verifyFormat("program x;\n"
614 verifyFormat("interface x;\n"
618 verifyFormat("task x;\n"
622 verifyFormat("function x;\n"
626 verifyFormat("class x;\n"
630 // Test that they nest.
631 verifyFormat("module x;\n"
637 // Test that an extern declaration doesn't change the indentation.
638 verifyFormat("extern module x;\n"
640 // Test complex headers
641 verifyFormat("extern module x\n"
642 " import x.x::x::*;\n"
646 verifyFormat("module x\n"
647 " import x.x::x::*;\n"
654 verifyFormat("virtual class x\n"
657 " implements x, x, x;\n"
661 verifyFormat("function automatic logic [1 : 0] x\n"
668 TEST_F(FormatTestVerilog
, Identifiers
) {
669 // Escaped identifiers should not be split.
670 verifyFormat("\\busa+index");
671 verifyFormat("\\-clock");
672 verifyFormat("\\***error-condition***");
673 verifyFormat("\\net1\\/net2");
674 verifyFormat("\\{a,b}");
675 verifyFormat("\\a*(b+c)");
676 // Escaped identifiers can't be joined with the next token. Extra space
677 // should be removed.
678 verifyFormat("\\busa+index ;", "\\busa+index\n"
680 verifyFormat("\\busa+index ;", "\\busa+index\r\n"
682 verifyFormat("\\busa+index ;", "\\busa+index ;");
683 verifyFormat("\\busa+index ;", "\\busa+index\n"
685 verifyFormat("\\busa+index ;");
686 verifyFormat("(\\busa+index );");
687 verifyFormat("\\busa+index \\busa+index ;");
690 TEST_F(FormatTestVerilog
, If
) {
691 verifyFormat("if (x)\n"
693 verifyFormat("unique if (x)\n"
695 verifyFormat("unique0 if (x)\n"
697 verifyFormat("priority if (x)\n"
699 verifyFormat("if (x)\n"
704 verifyFormat("if (x)\n"
710 verifyFormat("if (x) begin\n"
712 "end else if (x) begin\n"
717 verifyFormat("if (x) begin : x\n"
719 "end : x else if (x) begin : x\n"
721 "end : x else begin : x\n"
725 // Test block keywords.
726 verifyFormat("if (x) begin\n"
729 verifyFormat("if (x) begin : x\n"
732 verifyFormat("if (x) begin\n"
736 verifyFormat("if (x) fork\n"
739 verifyFormat("if (x) fork\n"
742 verifyFormat("if (x) fork\n"
745 verifyFormat("if (x) generate\n"
748 verifyFormat("if (x) generate : x\n"
752 // Test that concatenation braces don't get regarded as blocks.
753 verifyFormat("if (x)\n"
755 verifyFormat("if (x)\n"
757 verifyFormat("if (x)\n"
763 verifyFormat("(* x *) if (x)\n"
765 verifyFormat("(* x = \"x\" *) if (x)\n"
767 verifyFormat("(* x, x = \"x\" *) if (x)\n"
770 // Assert are treated similar to if. But the else parts should not be
772 verifyFormat("assert (x);");
773 verifyFormat("assert (x)\n"
775 verifyFormat("assert (x)\n"
779 verifyFormat("assert (x)\n"
782 verifyFormat("assert (x)\n"
785 verifyFormat("assert (x)\n"
789 verifyFormat("assert (x)\n"
794 verifyFormat("assert (x)\n"
801 verifyFormat("assert (x)\n"
810 // The body is optional for asserts. The next line should not be indented if
811 // the statement already ended with a semicolon.
812 verifyFormat("assert (x);\n"
814 verifyFormat("if (x)\n"
816 "else if (x) begin\n"
819 verifyFormat("if (x)\n"
823 verifyFormat("if (x)\n"
828 verifyFormat("assume (x)\n"
830 verifyFormat("cover (x)\n"
832 verifyFormat("restrict (x)\n"
834 verifyFormat("assert #0 (x)\n"
836 verifyFormat("assert final (x)\n"
838 verifyFormat("cover #0 (x)\n"
840 verifyFormat("cover final (x)\n"
843 // The space around parentheses options should work.
844 auto Style
= getDefaultStyle();
845 verifyFormat("if (x)\n"
850 verifyFormat("assert (x);", Style
);
851 verifyFormat("assert #0 (x);", Style
);
852 verifyFormat("assert (x)\n"
857 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
858 Style
.SpacesInParensOptions
.InConditionalStatements
= true;
859 verifyFormat("if ( x )\n"
864 verifyFormat("assert ( x );", Style
);
865 verifyFormat("assert #0 ( x );", Style
);
866 verifyFormat("assert ( x )\n"
873 TEST_F(FormatTestVerilog
, Instantiation
) {
875 verifyFormat("ffnand ff1;");
877 verifyFormat("ffnand ff1(.qbar(out1),\n"
881 verifyFormat("ffnand ff1(.qbar(out1),\n"
885 verifyFormat("ffnand ff1(.*,\n"
889 // With unconnected ports.
890 verifyFormat("ffnand ff1(.q(),\n"
894 verifyFormat("ffnand ff1(.q(),\n"
898 verifyFormat("ffnand ff1(,\n"
902 // With positional ports.
903 verifyFormat("ffnand ff1(out1,\n"
906 verifyFormat("ffnand ff1(,\n"
910 // Multiple instantiations.
911 verifyFormat("ffnand ff1(.q(),\n"
919 verifyFormat("ffnand //\n"
928 // With breaking between instance ports disabled.
929 auto Style
= getDefaultStyle();
930 Style
.VerilogBreakBetweenInstancePorts
= false;
931 verifyFormat("ffnand ff1;", Style
);
932 verifyFormat("ffnand ff1(.qbar(out1), .clear(in1), .preset(in2), .*);",
934 verifyFormat("ffnand ff1(out1, in1, in2);", Style
);
935 verifyFormat("ffnand ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n"
936 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));",
938 verifyFormat("ffnand //\n"
939 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n"
940 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));",
944 TEST_F(FormatTestVerilog
, Loop
) {
945 verifyFormat("foreach (x[x])\n"
947 verifyFormat("repeat (x)\n"
949 verifyFormat("foreach (x[x]) begin\n"
951 verifyFormat("repeat (x) begin\n"
953 auto Style
= getDefaultStyle();
954 Style
.SpacesInParens
= FormatStyle::SIPO_Custom
;
955 Style
.SpacesInParensOptions
.InConditionalStatements
= true;
956 verifyFormat("foreach ( x[x] )\n"
959 verifyFormat("repeat ( x )\n"
964 TEST_F(FormatTestVerilog
, Operators
) {
965 // Test that unary operators are not followed by space.
966 verifyFormat("x = +x;");
967 verifyFormat("x = -x;");
968 verifyFormat("x = !x;");
969 verifyFormat("x = ~x;");
970 verifyFormat("x = &x;");
971 verifyFormat("x = ~&x;");
972 verifyFormat("x = |x;");
973 verifyFormat("x = ~|x;");
974 verifyFormat("x = ^x;");
975 verifyFormat("x = ~^x;");
976 verifyFormat("x = ^~x;");
977 verifyFormat("x = ++x;");
978 verifyFormat("x = --x;");
980 // Test that `*` and `*>` are binary.
981 verifyFormat("x = x * x;");
982 verifyFormat("x = (x * x);");
983 verifyFormat("(opcode *> o1) = 6.1;");
984 verifyFormat("(C, D *> Q) = 18;");
985 // The wildcard import is not a binary operator.
986 verifyFormat("import p::*;");
988 // Test that operators don't get split.
989 verifyFormat("x = x++;");
990 verifyFormat("x = x--;");
991 verifyFormat("x = x ** x;");
992 verifyFormat("x = x << x;");
993 verifyFormat("x = x >> x;");
994 verifyFormat("x = x <<< x;");
995 verifyFormat("x = x >>> x;");
996 verifyFormat("x = x <= x;");
997 verifyFormat("x = x >= x;");
998 verifyFormat("x = x == x;");
999 verifyFormat("x = x != x;");
1000 verifyFormat("x = x === x;");
1001 verifyFormat("x = x !== x;");
1002 verifyFormat("x = x ==? x;");
1003 verifyFormat("x = x !=? x;");
1004 verifyFormat("x = x ~^ x;");
1005 verifyFormat("x = x ^~ x;");
1006 verifyFormat("x = x && x;");
1007 verifyFormat("x = x || x;");
1008 verifyFormat("x = x->x;");
1009 verifyFormat("x = 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 verifyFormat("x <<= x;");
1019 verifyFormat("x >>= x;");
1020 verifyFormat("x <<<= x;");
1021 verifyFormat("x >>>= x;");
1022 verifyFormat("x <= x;");
1024 // Test that space is added between operators.
1025 verifyFormat("x = x < -x;", "x=x<-x;");
1026 verifyFormat("x = x << -x;", "x=x<<-x;");
1027 verifyFormat("x = x <<< -x;", "x=x<<<-x;");
1029 // Test that operators that are C++ identifiers get treated as operators.
1030 verifyFormat("solve s before d;"); // before
1031 verifyFormat("binsof(i) intersect {0};"); // intersect
1032 verifyFormat("req dist {1};"); // dist
1033 verifyFormat("a inside {b, c};"); // inside
1034 verifyFormat("bus.randomize() with { atype == low; };"); // with
1037 TEST_F(FormatTestVerilog
, Preprocessor
) {
1038 auto Style
= getDefaultStyle();
1039 Style
.ColumnLimit
= 20;
1041 // Macro definitions.
1042 verifyFormat("`define X \\\n"
1045 "`define X if(x)x=x;", Style
);
1046 verifyFormat("`define X(x) \\\n"
1049 "`define X(x) if(x)x=x;", Style
);
1050 verifyFormat("`define X \\\n"
1053 "`define X x=x;x=x;", Style
);
1054 // Macro definitions with invocations inside.
1055 verifyFormat("`define LIST \\\n"
1062 verifyFormat("`define LIST \\\n"
1069 verifyFormat("`define LIST \\\n"
1072 "`define LIST `x=`x;`x=`x;", Style
);
1073 // Macro invocations.
1074 verifyFormat("`x = (`x1 + `x2 + x);");
1075 // Lines starting with a preprocessor directive should not be indented.
1076 std::string Directives
[] = {
1090 "nounconnected_drive",
1094 "unconnected_drive",
1098 for (auto &Name
: Directives
) {
1099 verifyFormat("if (x)\n"
1111 // Lines starting with a regular macro invocation should be indented as a
1113 verifyFormat("if (x)\n"
1115 "`timescale 1ns / 1ps",
1118 "`timescale 1ns / 1ps",
1120 verifyFormat("if (x)\n"
1121 "`timescale 1ns / 1ps\n"
1124 "`timescale 1ns / 1ps\n"
1127 std::string NonDirectives
[] = {
1128 // For `__FILE__` and `__LINE__`, although the standard classifies them as
1129 // preprocessor directives, they are used like regular macros.
1130 "__FILE__", "__LINE__", "elif", "foo", "x",
1132 for (auto &Name
: NonDirectives
) {
1133 verifyFormat("if (x)\n"
1145 TEST_F(FormatTestVerilog
, Primitive
) {
1146 verifyFormat("primitive multiplexer\n"
1147 " (mux, control, dataA, dataB);\n"
1149 " input control, dataA, dataB;\n"
1159 verifyFormat("primitive latch\n"
1160 " (q, ena_, data);\n"
1163 " input ena_, data;\n"
1171 verifyFormat("primitive d\n"
1172 " (q, clock, data);\n"
1175 " input clock, data;\n"
1177 " (01) 0 : ? : 0;\n"
1178 " (01) 1 : ? : 1;\n"
1179 " (0?) 1 : 1 : 1;\n"
1180 " (0?) 0 : 0 : 0;\n"
1181 " (?0) ? : ? : -;\n"
1182 " (?\?) ? : ? : -;\n"
1187 TEST_F(FormatTestVerilog
, Streaming
) {
1188 verifyFormat("x = {>>{j}};");
1189 verifyFormat("x = {>>byte{j}};");
1190 verifyFormat("x = {<<{j}};");
1191 verifyFormat("x = {<<byte{j}};");
1192 verifyFormat("x = {<<16{j}};");
1193 verifyFormat("x = {<<{8'b0011_0101}};");
1194 verifyFormat("x = {<<4{6'b11_0101}};");
1195 verifyFormat("x = {>>4{6'b11_0101}};");
1196 verifyFormat("x = {<<2{{<<{4'b1101}}}};");
1197 verifyFormat("bit [96 : 1] y = {>>{a, b, c}};");
1198 verifyFormat("int j = {>>{a, b, c}};");
1199 verifyFormat("{>>{a, b, c}} = 23'b1;");
1200 verifyFormat("{>>{a, b, c}} = x;");
1201 verifyFormat("{>>{j}} = x;");
1202 verifyFormat("{>>byte{j}} = x;");
1203 verifyFormat("{<<{j}} = x;");
1204 verifyFormat("{<<byte{j}} = x;");
1207 TEST_F(FormatTestVerilog
, StringLiteral
) {
1208 // Long strings should be broken.
1209 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1211 R
"(x({"xxxxxxxxxxxxxxxx xxxx
"});)",
1212 getStyleWithColumns(getDefaultStyle(), 23));
1213 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1215 R
"(x({"xxxxxxxxxxxxxxxx xxxx
"});)",
1216 getStyleWithColumns(getDefaultStyle(), 22));
1217 // Braces should be added when they don't already exist.
1218 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1220 R
"(x("xxxxxxxxxxxxxxxx xxxx
");)",
1221 getStyleWithColumns(getDefaultStyle(), 23));
1222 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1224 R
"(x("xxxxxxxxxxxxxxxx xxxx
");)",
1225 getStyleWithColumns(getDefaultStyle(), 22));
1226 verifyFormat(R
"(x({{"xxxxxxxxxxxxxxxx
",
1228 R
"(x({"xxxxxxxxxxxxxxxx xxxx
" == x});)",
1229 getStyleWithColumns(getDefaultStyle(), 24));
1230 verifyFormat(R
"(string x = {"xxxxxxxxxxxxxxxx
",
1232 R
"(string x = "xxxxxxxxxxxxxxxx xxxxxxxx
";)",
1233 getStyleWithColumns(getDefaultStyle(), 32));
1234 // Space around braces should be correct.
1235 auto Style
= getStyleWithColumns(getDefaultStyle(), 24);
1236 Style
.Cpp11BracedListStyle
= false;
1237 verifyFormat(R
"(x({ "xxxxxxxxxxxxxxxx
",
1239 R
"(x("xxxxxxxxxxxxxxxx xxxx
");)", Style
);
1240 // Braces should not be added twice.
1241 verifyFormat(R
"(x({"xxxxxxxx
",
1244 R
"(x("xxxxxxxxxxxxxxxxxxxxxx
");)",
1245 getStyleWithColumns(getDefaultStyle(), 14));
1246 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1247 "xxxxxxxxxxxxxxxx
",
1249 R
"(x("xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx xxxx
");)",
1250 getStyleWithColumns(getDefaultStyle(), 23));
1251 verifyFormat(R
"(x({"xxxxxxxxxxxxxxxx
",
1252 "xxxxxxxxxxxxxxxx
",
1254 R
"(x({"xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx
", "xxxx
"});)",
1255 getStyleWithColumns(getDefaultStyle(), 23));
1256 // import/export "DPI"/"DPI-C" cannot be split.
1257 verifyFormat(R
"(import
1258 "DPI
-C
" function void foo
1260 R
"(import "DPI
-C
" function void foo();)",
1261 getStyleWithColumns(getDefaultStyle(), 23));
1262 verifyFormat(R
"(export "DPI
-C
" function foo;)",
1263 R
"(export "DPI
-C
" function foo;)",
1264 getStyleWithColumns(getDefaultStyle(), 23));
1265 // These kinds of strings don't exist in Verilog.
1266 verifyNoCrash(R
"(x(@"xxxxxxxxxxxxxxxx xxxx
");)",
1267 getStyleWithColumns(getDefaultStyle(), 23));
1268 verifyNoCrash(R
"(x(u"xxxxxxxxxxxxxxxx xxxx
");)",
1269 getStyleWithColumns(getDefaultStyle(), 23));
1270 verifyNoCrash(R
"(x(u8"xxxxxxxxxxxxxxxx xxxx
");)",
1271 getStyleWithColumns(getDefaultStyle(), 23));
1272 verifyNoCrash(R
"(x(_T("xxxxxxxxxxxxxxxx xxxx
"));)",
1273 getStyleWithColumns(getDefaultStyle(), 23));
1276 TEST_F(FormatTestVerilog
, StructLiteral
) {
1277 verifyFormat("c = '{0, 0.0};");
1278 verifyFormat("c = '{'{1, 1.0}, '{2, 2.0}};");
1279 verifyFormat("c = '{a: 0, b: 0.0};");
1280 verifyFormat("c = '{a: 0, b: 0.0, default: 0};");
1281 verifyFormat("c = ab'{a: 0, b: 0.0};");
1282 verifyFormat("c = ab'{cd: cd'{1, 1.0}, ef: ef'{2, 2.0}};");
1283 verifyFormat("c = ab'{cd'{1, 1.0}, ef'{2, 2.0}};");
1284 verifyFormat("d = {int: 1, shortreal: 1.0};");
1285 verifyFormat("d = ab'{int: 1, shortreal: 1.0};");
1286 verifyFormat("c = '{default: 0};");
1287 auto Style
= getDefaultStyle();
1288 Style
.SpacesInContainerLiterals
= true;
1289 verifyFormat("c = '{a : 0, b : 0.0};", Style
);
1290 verifyFormat("c = '{a : 0, b : 0.0, default : 0};", Style
);
1291 verifyFormat("c = ab'{a : 0, b : 0.0};", Style
);
1292 verifyFormat("c = ab'{cd : cd'{1, 1.0}, ef : ef'{2, 2.0}};", Style
);
1294 // It should be indented correctly when the line has to break.
1295 verifyFormat("c = //\n"
1297 Style
= getDefaultStyle();
1298 Style
.ContinuationIndentWidth
= 2;
1299 verifyFormat("c = //\n"
1304 TEST_F(FormatTestVerilog
, StructuredProcedure
) {
1305 // Blocks should be indented correctly.
1306 verifyFormat("initial begin\n"
1308 verifyFormat("initial begin\n"
1312 verifyFormat("initial\n"
1315 verifyFormat("always @(x) begin\n"
1317 verifyFormat("always @(x) begin\n"
1321 verifyFormat("always @(x)\n"
1324 // Various keywords.
1325 verifyFormat("always @(x)\n"
1327 verifyFormat("always @(posedge x)\n"
1329 verifyFormat("always @(posedge x or posedge y)\n"
1331 verifyFormat("always @(posedge x, posedge y)\n"
1333 verifyFormat("always @(negedge x, negedge y)\n"
1335 verifyFormat("always @(edge x, edge y)\n"
1337 verifyFormat("always\n"
1339 verifyFormat("always @*\n"
1341 verifyFormat("always @(*)\n"
1343 verifyFormat("always_comb\n"
1345 verifyFormat("always_latch @(x)\n"
1347 verifyFormat("always_ff @(posedge x)\n"
1349 verifyFormat("initial\n"
1351 verifyFormat("final\n"
1353 verifyFormat("forever\n"
1358 } // namespace format
1359 } // namespace clang