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(Language: FormatStyle::LK_Verilog); |
21 | } |
22 | std::string messUp(llvm::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 mynet = enable;" ); |
395 | verifyFormat("wire mynet = enable, mynet1;" ); |
396 | verifyFormat("wire mynet = enable, //\n" |
397 | " mynet1;" ); |
398 | verifyFormat("wire mynet, mynet1 = enable;" ); |
399 | verifyFormat("wire mynet, //\n" |
400 | " mynet1 = enable;" ); |
401 | verifyFormat("wire mynet = enable, mynet1 = enable;" ); |
402 | verifyFormat("wire mynet = enable, //\n" |
403 | " mynet1 = enable;" ); |
404 | verifyFormat("wire (strong1, pull0) mynet;" ); |
405 | verifyFormat("wire (strong1, pull0) mynet, mynet1;" ); |
406 | verifyFormat("wire (strong1, pull0) mynet, //\n" |
407 | " mynet1;" ); |
408 | verifyFormat("wire (strong1, pull0) mynet = enable;" ); |
409 | verifyFormat("wire (strong1, pull0) mynet = enable, mynet1;" ); |
410 | verifyFormat("wire (strong1, pull0) mynet = enable, //\n" |
411 | " mynet1;" ); |
412 | verifyFormat("wire (strong1, pull0) mynet, mynet1 = enable;" ); |
413 | verifyFormat("wire (strong1, pull0) mynet, //\n" |
414 | " mynet1 = enable;" ); |
415 | } |
416 | |
417 | TEST_F(FormatTestVerilog, Delay) { |
418 | // Delay by the default unit. |
419 | verifyFormat("#0;" ); |
420 | verifyFormat("#1;" ); |
421 | verifyFormat("#10;" ); |
422 | verifyFormat("#1.5;" ); |
423 | // Explicit unit. |
424 | verifyFormat("#1fs;" ); |
425 | verifyFormat("#1.5fs;" ); |
426 | verifyFormat("#1ns;" ); |
427 | verifyFormat("#1.5ns;" ); |
428 | verifyFormat("#1us;" ); |
429 | verifyFormat("#1.5us;" ); |
430 | verifyFormat("#1ms;" ); |
431 | verifyFormat("#1.5ms;" ); |
432 | verifyFormat("#1s;" ); |
433 | verifyFormat("#1.5s;" ); |
434 | // The following expression should be on the same line. |
435 | verifyFormat("#1 x = x;" ); |
436 | verifyFormat("#1 x = x;" , "#1\n" |
437 | "x = x;" ); |
438 | } |
439 | |
440 | TEST_F(FormatTestVerilog, Enum) { |
441 | verifyFormat("enum { x } x;" ); |
442 | verifyFormat("typedef enum { x } x;" ); |
443 | verifyFormat("enum { red, yellow, green } x;" ); |
444 | verifyFormat("typedef enum { red, yellow, green } x;" ); |
445 | verifyFormat("enum integer { x } x;" ); |
446 | verifyFormat("typedef enum { x = 0 } x;" ); |
447 | verifyFormat("typedef enum { red = 0, yellow = 1, green = 2 } x;" ); |
448 | verifyFormat("typedef enum integer { x } x;" ); |
449 | verifyFormat("typedef enum bit [0 : 1] { x } x;" ); |
450 | verifyFormat("typedef enum { add = 10, sub[5], jmp[6 : 8] } E1;" ); |
451 | verifyFormat("typedef enum { add = 10, sub[5] = 0, jmp[6 : 8] = 1 } E1;" ); |
452 | } |
453 | |
454 | TEST_F(FormatTestVerilog, Headers) { |
455 | // Test headers with multiple ports. |
456 | verifyFormat("module mh1\n" |
457 | " (input var int in1,\n" |
458 | " input var shortreal in2,\n" |
459 | " output tagged_st out);\n" |
460 | "endmodule" ); |
461 | // There should be a space following the type but not the variable name. |
462 | verifyFormat("module test\n" |
463 | " (input wire [7 : 0] a,\n" |
464 | " input wire b[7 : 0],\n" |
465 | " input wire [7 : 0] c[7 : 0]);\n" |
466 | "endmodule" ); |
467 | // Ports should be grouped by types. |
468 | verifyFormat("module test\n" |
469 | " (input [7 : 0] a,\n" |
470 | " input signed [7 : 0] b, c, d);\n" |
471 | "endmodule" ); |
472 | verifyFormat("module test\n" |
473 | " (input [7 : 0] a,\n" |
474 | " (* x = x *) input signed [7 : 0] b, c, d);\n" |
475 | "endmodule" ); |
476 | verifyFormat("module test\n" |
477 | " (input [7 : 0] a = 0,\n" |
478 | " input signed [7 : 0] b = 0, c = 0, d = 0);\n" |
479 | "endmodule" ); |
480 | verifyFormat("module test\n" |
481 | " #(parameter x)\n" |
482 | " (input [7 : 0] a,\n" |
483 | " input signed [7 : 0] b, c, d);\n" |
484 | "endmodule" ); |
485 | // When a line needs to be broken, ports of the same type should be aligned to |
486 | // the same column. |
487 | verifyFormat("module test\n" |
488 | " (input signed [7 : 0] b, c, //\n" |
489 | " d);\n" |
490 | "endmodule" ); |
491 | verifyFormat("module test\n" |
492 | " ((* x = x *) input signed [7 : 0] b, c, //\n" |
493 | " d);\n" |
494 | "endmodule" ); |
495 | verifyFormat("module test\n" |
496 | " (input signed [7 : 0] b = 0, c, //\n" |
497 | " d);\n" |
498 | "endmodule" ); |
499 | verifyFormat("module test\n" |
500 | " (input signed [7 : 0] b, c = 0, //\n" |
501 | " d);\n" |
502 | "endmodule" ); |
503 | verifyFormat("module test\n" |
504 | " (input signed [7 : 0] b, c, //\n" |
505 | " d = 0);\n" |
506 | "endmodule" ); |
507 | verifyFormat("module test\n" |
508 | " (input wire logic signed [7 : 0][0 : 1] b, c, //\n" |
509 | " d);\n" |
510 | "endmodule" ); |
511 | verifyFormat("module test\n" |
512 | " (input signed [7 : 0] b, //\n" |
513 | " c, //\n" |
514 | " d);\n" |
515 | "endmodule" ); |
516 | verifyFormat("module test\n" |
517 | " (input [7 : 0] a,\n" |
518 | " input signed [7 : 0] b, //\n" |
519 | " c, //\n" |
520 | " d);\n" |
521 | "endmodule" ); |
522 | verifyFormat("module test\n" |
523 | " (input signed [7 : 0] b, //\n" |
524 | " c, //\n" |
525 | " d,\n" |
526 | " output signed [7 : 0] h);\n" |
527 | "endmodule" ); |
528 | // With a modport. |
529 | verifyFormat("module m\n" |
530 | " (i2.master i);\n" |
531 | "endmodule" ); |
532 | verifyFormat("module m\n" |
533 | " (i2.master i, ii);\n" |
534 | "endmodule" ); |
535 | verifyFormat("module m\n" |
536 | " (i2.master i, //\n" |
537 | " ii);\n" |
538 | "endmodule" ); |
539 | verifyFormat("module m\n" |
540 | " (i2.master i,\n" |
541 | " input ii);\n" |
542 | "endmodule" ); |
543 | verifyFormat("module m\n" |
544 | " (i2::i2.master i);\n" |
545 | "endmodule" ); |
546 | verifyFormat("module m\n" |
547 | " (i2::i2.master i, ii);\n" |
548 | "endmodule" ); |
549 | verifyFormat("module m\n" |
550 | " (i2::i2.master i, //\n" |
551 | " ii);\n" |
552 | "endmodule" ); |
553 | verifyFormat("module m\n" |
554 | " (i2::i2.master i,\n" |
555 | " input ii);\n" |
556 | "endmodule" ); |
557 | verifyFormat("module m\n" |
558 | " (i2::i2 i);\n" |
559 | "endmodule" ); |
560 | verifyFormat("module m\n" |
561 | " (i2::i2 i, ii);\n" |
562 | "endmodule" ); |
563 | verifyFormat("module m\n" |
564 | " (i2::i2 i, //\n" |
565 | " ii);\n" |
566 | "endmodule" ); |
567 | verifyFormat("module m\n" |
568 | " (i2::i2 i,\n" |
569 | " input ii);\n" |
570 | "endmodule" ); |
571 | // With a macro in the names. |
572 | verifyFormat("module m\n" |
573 | " (input var `x a, b);\n" |
574 | "endmodule" ); |
575 | verifyFormat("module m\n" |
576 | " (input var `x a, //\n" |
577 | " b);\n" |
578 | "endmodule" ); |
579 | verifyFormat("module m\n" |
580 | " (input var x `a, b);\n" |
581 | "endmodule" ); |
582 | verifyFormat("module m\n" |
583 | " (input var x `a, //\n" |
584 | " b);\n" |
585 | "endmodule" ); |
586 | // A line comment shouldn't disrupt the indentation of the port list. |
587 | verifyFormat("extern module x\n" |
588 | " (//\n" |
589 | " output y);" ); |
590 | verifyFormat("extern module x\n" |
591 | " #(//\n" |
592 | " parameter x)\n" |
593 | " (//\n" |
594 | " output y);" ); |
595 | // With a concatenation in the names. |
596 | auto Style = getDefaultStyle(); |
597 | Style.ColumnLimit = 40; |
598 | verifyFormat("`define X(x) \\\n" |
599 | " module test \\\n" |
600 | " (input var x``x a, b);" , |
601 | Style); |
602 | verifyFormat("`define X(x) \\\n" |
603 | " module test \\\n" |
604 | " (input var x``x aaaaaaaaaaaaaaa, \\\n" |
605 | " b);" , |
606 | Style); |
607 | verifyFormat("`define X(x) \\\n" |
608 | " module test \\\n" |
609 | " (input var x a``x, b);" , |
610 | Style); |
611 | verifyFormat("`define X(x) \\\n" |
612 | " module test \\\n" |
613 | " (input var x aaaaaaaaaaaaaaa``x, \\\n" |
614 | " b);" , |
615 | Style); |
616 | // When the ports line is not to be formatted, following lines should not take |
617 | // on its indentation. |
618 | verifyFormat("module x\n" |
619 | " (output x);\n" |
620 | " assign x = 0;\n" |
621 | "endmodule" , |
622 | "module x\n" |
623 | " (output x);\n" |
624 | " assign x = 0;\n" |
625 | "endmodule" , |
626 | getDefaultStyle(), {tooling::Range(25, 18)}); |
627 | } |
628 | |
629 | TEST_F(FormatTestVerilog, Hierarchy) { |
630 | verifyFormat("module x;\n" |
631 | "endmodule" ); |
632 | // Test that the end label is on the same line as the end keyword. |
633 | verifyFormat("module x;\n" |
634 | "endmodule : x" ); |
635 | // Test that things inside are indented. |
636 | verifyFormat("module x;\n" |
637 | " generate\n" |
638 | " endgenerate\n" |
639 | "endmodule" ); |
640 | verifyFormat("program x;\n" |
641 | " generate\n" |
642 | " endgenerate\n" |
643 | "endprogram" ); |
644 | verifyFormat("interface x;\n" |
645 | " generate\n" |
646 | " endgenerate\n" |
647 | "endinterface" ); |
648 | verifyFormat("task x;\n" |
649 | " generate\n" |
650 | " endgenerate\n" |
651 | "endtask" ); |
652 | verifyFormat("function x;\n" |
653 | " generate\n" |
654 | " endgenerate\n" |
655 | "endfunction" ); |
656 | verifyFormat("class x;\n" |
657 | " generate\n" |
658 | " endgenerate\n" |
659 | "endclass" ); |
660 | // Test that they nest. |
661 | verifyFormat("module x;\n" |
662 | " program x;\n" |
663 | " program x;\n" |
664 | " endprogram\n" |
665 | " endprogram\n" |
666 | "endmodule" ); |
667 | // Test that an extern declaration doesn't change the indentation. |
668 | verifyFormat("extern module x;\n" |
669 | "x = x;" ); |
670 | // Test complex headers |
671 | verifyFormat("extern module x\n" |
672 | " import x.x::x::*;\n" |
673 | " import x;\n" |
674 | " #(parameter x)\n" |
675 | " (output x);" ); |
676 | verifyFormat("module x\n" |
677 | " import x.x::x::*;\n" |
678 | " import x;\n" |
679 | " #(parameter x)\n" |
680 | " (output x);\n" |
681 | " generate\n" |
682 | " endgenerate\n" |
683 | "endmodule : x" ); |
684 | verifyFormat("virtual class x\n" |
685 | " (x)\n" |
686 | " extends x(x)\n" |
687 | " implements x, x, x;\n" |
688 | " generate\n" |
689 | " endgenerate\n" |
690 | "endclass : x" ); |
691 | verifyFormat("function automatic logic [1 : 0] x\n" |
692 | " (input x);\n" |
693 | " generate\n" |
694 | " endgenerate\n" |
695 | "endfunction : x" ); |
696 | } |
697 | |
698 | TEST_F(FormatTestVerilog, Identifiers) { |
699 | // Escaped identifiers should not be split. |
700 | verifyFormat("\\busa+index" ); |
701 | verifyFormat("\\-clock" ); |
702 | verifyFormat("\\***error-condition***" ); |
703 | verifyFormat("\\net1\\/net2" ); |
704 | verifyFormat("\\{a,b}" ); |
705 | verifyFormat("\\a*(b+c)" ); |
706 | // Escaped identifiers can't be joined with the next token. Extra space |
707 | // should be removed. |
708 | verifyFormat("\\busa+index ;" , "\\busa+index\n" |
709 | ";" ); |
710 | verifyFormat("\\busa+index ;" , "\\busa+index\r\n" |
711 | ";" ); |
712 | verifyFormat("\\busa+index ;" , "\\busa+index ;" ); |
713 | verifyFormat("\\busa+index ;" , "\\busa+index\n" |
714 | " ;" ); |
715 | verifyFormat("\\busa+index ;" ); |
716 | verifyFormat("(\\busa+index );" ); |
717 | verifyFormat("\\busa+index \\busa+index ;" ); |
718 | } |
719 | |
720 | TEST_F(FormatTestVerilog, If) { |
721 | verifyFormat("if (x)\n" |
722 | " x = x;" ); |
723 | verifyFormat("unique if (x)\n" |
724 | " x = x;" ); |
725 | verifyFormat("unique0 if (x)\n" |
726 | " x = x;" ); |
727 | verifyFormat("priority if (x)\n" |
728 | " x = x;" ); |
729 | verifyFormat("if (x)\n" |
730 | " x = x;\n" |
731 | "x = x;" ); |
732 | |
733 | // Test else |
734 | verifyFormat("if (x)\n" |
735 | " x = x;\n" |
736 | "else if (x)\n" |
737 | " x = x;\n" |
738 | "else\n" |
739 | " x = x;" ); |
740 | verifyFormat("if (x) begin\n" |
741 | " x = x;\n" |
742 | "end else if (x) begin\n" |
743 | " x = x;\n" |
744 | "end else begin\n" |
745 | " x = x;\n" |
746 | "end" ); |
747 | verifyFormat("if (x) begin : x\n" |
748 | " x = x;\n" |
749 | "end : x else if (x) begin : x\n" |
750 | " x = x;\n" |
751 | "end : x else begin : x\n" |
752 | " x = x;\n" |
753 | "end : x" ); |
754 | |
755 | // Test block keywords. |
756 | verifyFormat("if (x) begin\n" |
757 | " x = x;\n" |
758 | "end" ); |
759 | verifyFormat("if (x) begin : x\n" |
760 | " x = x;\n" |
761 | "end : x" ); |
762 | verifyFormat("if (x) begin\n" |
763 | " x = x;\n" |
764 | " x = x;\n" |
765 | "end" ); |
766 | verifyFormat("if (x) fork\n" |
767 | " x = x;\n" |
768 | "join" ); |
769 | verifyFormat("if (x) fork\n" |
770 | " x = x;\n" |
771 | "join_any" ); |
772 | verifyFormat("if (x) fork\n" |
773 | " x = x;\n" |
774 | "join_none" ); |
775 | verifyFormat("if (x) generate\n" |
776 | " x = x;\n" |
777 | "endgenerate" ); |
778 | verifyFormat("if (x) generate : x\n" |
779 | " x = x;\n" |
780 | "endgenerate : x" ); |
781 | |
782 | // Test that concatenation braces don't get regarded as blocks. |
783 | verifyFormat("if (x)\n" |
784 | " {x} = x;" ); |
785 | verifyFormat("if (x)\n" |
786 | " x = {x};" ); |
787 | verifyFormat("if (x)\n" |
788 | " x = {x};\n" |
789 | "else\n" |
790 | " {x} = {x};" ); |
791 | |
792 | // With attributes. |
793 | verifyFormat("(* x *) if (x)\n" |
794 | " x = x;" ); |
795 | verifyFormat("(* x = \"x\" *) if (x)\n" |
796 | " x = x;" ); |
797 | verifyFormat("(* x, x = \"x\" *) if (x)\n" |
798 | " x = x;" ); |
799 | |
800 | // Assert are treated similar to if. But the else parts should not be |
801 | // chained. |
802 | verifyFormat("assert (x);" ); |
803 | verifyFormat("assert (x)\n" |
804 | " $info();" ); |
805 | verifyFormat("assert (x)\n" |
806 | " $info();\n" |
807 | "else\n" |
808 | " $error();" ); |
809 | verifyFormat("assert (x)\n" |
810 | "else\n" |
811 | " $error();" ); |
812 | verifyFormat("assert (x)\n" |
813 | "else begin\n" |
814 | "end" ); |
815 | verifyFormat("assert (x)\n" |
816 | "else\n" |
817 | " if (x)\n" |
818 | " $error();" ); |
819 | verifyFormat("assert (x)\n" |
820 | " $info();\n" |
821 | "else\n" |
822 | " if (x)\n" |
823 | " $error();" ); |
824 | verifyFormat("assert (x)\n" |
825 | " $info();\n" |
826 | "else\n" |
827 | " if (x)\n" |
828 | " $error();\n" |
829 | " else\n" |
830 | " $error();" ); |
831 | verifyFormat("assert (x)\n" |
832 | " $info();\n" |
833 | "else\n" |
834 | " if (x)\n" |
835 | " $error();\n" |
836 | " else if (x)\n" |
837 | " $error();\n" |
838 | " else\n" |
839 | " $error();" ); |
840 | // The body is optional for asserts. The next line should not be indented if |
841 | // the statement already ended with a semicolon. |
842 | verifyFormat("assert (x);\n" |
843 | "x = x;" ); |
844 | verifyFormat("if (x)\n" |
845 | " assert (x);\n" |
846 | "else if (x) begin\n" |
847 | "end else begin\n" |
848 | "end" ); |
849 | verifyFormat("if (x)\n" |
850 | " assert (x);\n" |
851 | "else begin\n" |
852 | "end" ); |
853 | verifyFormat("if (x)\n" |
854 | " assert (x)\n" |
855 | " else begin\n" |
856 | " end" ); |
857 | // Other keywords. |
858 | verifyFormat("assume (x)\n" |
859 | " $info();" ); |
860 | verifyFormat("cover (x)\n" |
861 | " $info();" ); |
862 | verifyFormat("restrict (x)\n" |
863 | " $info();" ); |
864 | verifyFormat("assert #0 (x)\n" |
865 | " $info();" ); |
866 | verifyFormat("assert final (x)\n" |
867 | " $info();" ); |
868 | verifyFormat("cover #0 (x)\n" |
869 | " $info();" ); |
870 | verifyFormat("cover final (x)\n" |
871 | " $info();" ); |
872 | |
873 | // The space around parentheses options should work. |
874 | auto Style = getDefaultStyle(); |
875 | verifyFormat("if (x)\n" |
876 | " x = x;\n" |
877 | "else if (x)\n" |
878 | " x = x;" , |
879 | Style); |
880 | verifyFormat("assert (x);" , Style); |
881 | verifyFormat("assert #0 (x);" , Style); |
882 | verifyFormat("assert (x)\n" |
883 | "else\n" |
884 | " if (x)\n" |
885 | " x = x;" , |
886 | Style); |
887 | Style.SpacesInParens = FormatStyle::SIPO_Custom; |
888 | Style.SpacesInParensOptions.InConditionalStatements = true; |
889 | verifyFormat("if ( x )\n" |
890 | " x = x;\n" |
891 | "else if ( x )\n" |
892 | " x = x;" , |
893 | Style); |
894 | verifyFormat("assert ( x );" , Style); |
895 | verifyFormat("assert #0 ( x );" , Style); |
896 | verifyFormat("assert ( x )\n" |
897 | "else\n" |
898 | " if ( x )\n" |
899 | " x = x;" , |
900 | Style); |
901 | } |
902 | |
903 | TEST_F(FormatTestVerilog, Instantiation) { |
904 | // Without ports. |
905 | verifyFormat("ffnand ff1;" ); |
906 | // With named ports. |
907 | verifyFormat("ffnand ff1(.qbar(out1),\n" |
908 | " .clear(in1),\n" |
909 | " .preset(in2));" ); |
910 | // With wildcard. |
911 | verifyFormat("ffnand ff1(.qbar(out1),\n" |
912 | " .clear(in1),\n" |
913 | " .preset(in2),\n" |
914 | " .*);" ); |
915 | verifyFormat("ffnand ff1(.*,\n" |
916 | " .qbar(out1),\n" |
917 | " .clear(in1),\n" |
918 | " .preset(in2));" ); |
919 | // With unconnected ports. |
920 | verifyFormat("ffnand ff1(.q(),\n" |
921 | " .qbar(out1),\n" |
922 | " .clear(in1),\n" |
923 | " .preset(in2));" ); |
924 | verifyFormat("ffnand ff1(.q(),\n" |
925 | " .qbar(),\n" |
926 | " .clear(),\n" |
927 | " .preset());" ); |
928 | verifyFormat("ffnand ff1(,\n" |
929 | " .qbar(out1),\n" |
930 | " .clear(in1),\n" |
931 | " .preset(in2));" ); |
932 | // With positional ports. |
933 | verifyFormat("ffnand ff1(out1,\n" |
934 | " in1,\n" |
935 | " in2);" ); |
936 | verifyFormat("ffnand ff1(,\n" |
937 | " out1,\n" |
938 | " in1,\n" |
939 | " in2);" ); |
940 | // Multiple instantiations. |
941 | verifyFormat("ffnand ff1(.q(),\n" |
942 | " .qbar(out1),\n" |
943 | " .clear(in1),\n" |
944 | " .preset(in2)),\n" |
945 | " ff1(.q(),\n" |
946 | " .qbar(out1),\n" |
947 | " .clear(in1),\n" |
948 | " .preset(in2));" ); |
949 | verifyFormat("ffnand //\n" |
950 | " 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 | // With breaking between instance ports disabled. |
959 | auto Style = getDefaultStyle(); |
960 | Style.VerilogBreakBetweenInstancePorts = false; |
961 | verifyFormat("ffnand ff1;" , Style); |
962 | verifyFormat("ffnand ff1(.qbar(out1), .clear(in1), .preset(in2), .*);" , |
963 | Style); |
964 | verifyFormat("ffnand ff1(out1, in1, in2);" , Style); |
965 | verifyFormat("ffnand ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n" |
966 | " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));" , |
967 | Style); |
968 | verifyFormat("ffnand //\n" |
969 | " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n" |
970 | " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));" , |
971 | Style); |
972 | } |
973 | |
974 | TEST_F(FormatTestVerilog, Loop) { |
975 | verifyFormat("foreach (x[x])\n" |
976 | " x = x;" ); |
977 | verifyFormat("repeat (x)\n" |
978 | " x = x;" ); |
979 | verifyFormat("foreach (x[x]) begin\n" |
980 | "end" ); |
981 | verifyFormat("repeat (x) begin\n" |
982 | "end" ); |
983 | auto Style = getDefaultStyle(); |
984 | Style.SpacesInParens = FormatStyle::SIPO_Custom; |
985 | Style.SpacesInParensOptions.InConditionalStatements = true; |
986 | verifyFormat("foreach ( x[x] )\n" |
987 | " x = x;" , |
988 | Style); |
989 | verifyFormat("repeat ( x )\n" |
990 | " x = x;" , |
991 | Style); |
992 | } |
993 | |
994 | TEST_F(FormatTestVerilog, Operators) { |
995 | // Test that unary operators are not followed by space. |
996 | verifyFormat("x = +x;" ); |
997 | verifyFormat("x = -x;" ); |
998 | verifyFormat("x = !x;" ); |
999 | verifyFormat("x = ~x;" ); |
1000 | verifyFormat("x = &x;" ); |
1001 | verifyFormat("x = ~&x;" ); |
1002 | verifyFormat("x = |x;" ); |
1003 | verifyFormat("x = ~|x;" ); |
1004 | verifyFormat("x = ^x;" ); |
1005 | verifyFormat("x = ~^x;" ); |
1006 | verifyFormat("x = ^~x;" ); |
1007 | verifyFormat("x = ++x;" ); |
1008 | verifyFormat("x = --x;" ); |
1009 | |
1010 | // Test that `*` and `*>` are binary. |
1011 | verifyFormat("x = x * x;" ); |
1012 | verifyFormat("x = (x * x);" ); |
1013 | verifyFormat("(opcode *> o1) = 6.1;" ); |
1014 | verifyFormat("(C, D *> Q) = 18;" ); |
1015 | // The wildcard import is not a binary operator. |
1016 | verifyFormat("import p::*;" ); |
1017 | |
1018 | // Test that operators don't get split. |
1019 | verifyFormat("x = x++;" ); |
1020 | verifyFormat("x = x--;" ); |
1021 | verifyFormat("x = x ** x;" ); |
1022 | verifyFormat("x = x << x;" ); |
1023 | verifyFormat("x = x >> x;" ); |
1024 | verifyFormat("x = x <<< x;" ); |
1025 | verifyFormat("x = x >>> x;" ); |
1026 | verifyFormat("x = x <= x;" ); |
1027 | verifyFormat("x = x >= x;" ); |
1028 | verifyFormat("x = x == x;" ); |
1029 | verifyFormat("x = 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;" ); |
1041 | verifyFormat("x -= x;" ); |
1042 | verifyFormat("x *= x;" ); |
1043 | verifyFormat("x /= x;" ); |
1044 | verifyFormat("x %= x;" ); |
1045 | verifyFormat("x &= x;" ); |
1046 | verifyFormat("x ^= x;" ); |
1047 | verifyFormat("x |= x;" ); |
1048 | verifyFormat("x <<= x;" ); |
1049 | verifyFormat("x >>= x;" ); |
1050 | verifyFormat("x <<<= x;" ); |
1051 | verifyFormat("x >>>= x;" ); |
1052 | verifyFormat("x <= x;" ); |
1053 | |
1054 | // Test that space is added between operators. |
1055 | verifyFormat("x = x < -x;" , "x=x<-x;" ); |
1056 | verifyFormat("x = x << -x;" , "x=x<<-x;" ); |
1057 | verifyFormat("x = x <<< -x;" , "x=x<<<-x;" ); |
1058 | |
1059 | // Test that operators that are C++ identifiers get treated as operators. |
1060 | verifyFormat("solve s before d;" ); // before |
1061 | verifyFormat("binsof(i) intersect {0};" ); // intersect |
1062 | verifyFormat("req dist {1};" ); // dist |
1063 | verifyFormat("a inside {b, c};" ); // inside |
1064 | verifyFormat("bus.randomize() with { atype == low; };" ); // with |
1065 | } |
1066 | |
1067 | TEST_F(FormatTestVerilog, Preprocessor) { |
1068 | auto Style = getDefaultStyle(); |
1069 | Style.ColumnLimit = 20; |
1070 | |
1071 | // Macro definitions. |
1072 | verifyFormat("`define X \\\n" |
1073 | " if (x) \\\n" |
1074 | " x = x;" , |
1075 | "`define X if(x)x=x;" , Style); |
1076 | verifyFormat("`define X(x) \\\n" |
1077 | " if (x) \\\n" |
1078 | " x = x;" , |
1079 | "`define X(x) if(x)x=x;" , Style); |
1080 | verifyFormat("`define X \\\n" |
1081 | " x = x; \\\n" |
1082 | " x = x;" , |
1083 | "`define X x=x;x=x;" , Style); |
1084 | // Macro definitions with invocations inside. |
1085 | verifyFormat("`define LIST \\\n" |
1086 | " `ENTRY \\\n" |
1087 | " `ENTRY" , |
1088 | "`define LIST \\\n" |
1089 | "`ENTRY \\\n" |
1090 | "`ENTRY" , |
1091 | Style); |
1092 | verifyFormat("`define LIST \\\n" |
1093 | " `x = `x; \\\n" |
1094 | " `x = `x;" , |
1095 | "`define LIST \\\n" |
1096 | "`x = `x; \\\n" |
1097 | "`x = `x;" , |
1098 | Style); |
1099 | verifyFormat("`define LIST \\\n" |
1100 | " `x = `x; \\\n" |
1101 | " `x = `x;" , |
1102 | "`define LIST `x=`x;`x=`x;" , Style); |
1103 | // Macro invocations. |
1104 | verifyFormat("`x = (`x1 + `x2 + x);" ); |
1105 | // Lines starting with a preprocessor directive should not be indented. |
1106 | std::string Directives[] = { |
1107 | "begin_keywords" , |
1108 | "celldefine" , |
1109 | "default_nettype" , |
1110 | "define" , |
1111 | "else" , |
1112 | "elsif" , |
1113 | "end_keywords" , |
1114 | "endcelldefine" , |
1115 | "endif" , |
1116 | "ifdef" , |
1117 | "ifndef" , |
1118 | "include" , |
1119 | "line" , |
1120 | "nounconnected_drive" , |
1121 | "pragma" , |
1122 | "resetall" , |
1123 | "timescale" , |
1124 | "unconnected_drive" , |
1125 | "undef" , |
1126 | "undefineall" , |
1127 | }; |
1128 | for (auto &Name : Directives) { |
1129 | verifyFormat("if (x)\n" |
1130 | "`" + |
1131 | Name + |
1132 | "\n" |
1133 | " ;" , |
1134 | "if (x)\n" |
1135 | "`" + |
1136 | Name + |
1137 | "\n" |
1138 | ";" , |
1139 | Style); |
1140 | } |
1141 | // Lines starting with a regular macro invocation should be indented as a |
1142 | // normal line. |
1143 | verifyFormat("if (x)\n" |
1144 | " `x = `x;\n" |
1145 | "`timescale 1ns / 1ps" , |
1146 | "if (x)\n" |
1147 | "`x = `x;\n" |
1148 | "`timescale 1ns / 1ps" , |
1149 | Style); |
1150 | verifyFormat("if (x)\n" |
1151 | "`timescale 1ns / 1ps\n" |
1152 | " `x = `x;" , |
1153 | "if (x)\n" |
1154 | "`timescale 1ns / 1ps\n" |
1155 | "`x = `x;" , |
1156 | Style); |
1157 | std::string NonDirectives[] = { |
1158 | // For `__FILE__` and `__LINE__`, although the standard classifies them as |
1159 | // preprocessor directives, they are used like regular macros. |
1160 | "__FILE__" , "__LINE__" , "elif" , "foo" , "x" , |
1161 | }; |
1162 | for (auto &Name : NonDirectives) { |
1163 | verifyFormat("if (x)\n" |
1164 | " `" + |
1165 | Name + ";" , |
1166 | "if (x)\n" |
1167 | "`" + |
1168 | Name + |
1169 | "\n" |
1170 | ";" , |
1171 | Style); |
1172 | } |
1173 | } |
1174 | |
1175 | TEST_F(FormatTestVerilog, Primitive) { |
1176 | verifyFormat("primitive multiplexer\n" |
1177 | " (mux, control, dataA, dataB);\n" |
1178 | " output mux;\n" |
1179 | " input control, dataA, dataB;\n" |
1180 | " table\n" |
1181 | " 0 1 ? : 1;\n" |
1182 | " 0 0 ? : 0;\n" |
1183 | " 1 ? 1 : 1;\n" |
1184 | " 1 ? 0 : 0;\n" |
1185 | " x 0 0 : 0;\n" |
1186 | " x 1 1 : 1;\n" |
1187 | " endtable\n" |
1188 | "endprimitive" ); |
1189 | verifyFormat("primitive latch\n" |
1190 | " (q, ena_, data);\n" |
1191 | " output q;\n" |
1192 | " reg q;\n" |
1193 | " input ena_, data;\n" |
1194 | " table\n" |
1195 | " 0 1 : ? : 1;\n" |
1196 | " 0 0 : ? : 0;\n" |
1197 | " 1 ? : ? : -;\n" |
1198 | " ? * : ? : -;\n" |
1199 | " endtable\n" |
1200 | "endprimitive" ); |
1201 | verifyFormat("primitive d\n" |
1202 | " (q, clock, data);\n" |
1203 | " output q;\n" |
1204 | " reg q;\n" |
1205 | " input clock, data;\n" |
1206 | " table\n" |
1207 | " (01) 0 : ? : 0;\n" |
1208 | " (01) 1 : ? : 1;\n" |
1209 | " (0?) 1 : 1 : 1;\n" |
1210 | " (0?) 0 : 0 : 0;\n" |
1211 | " (?0) ? : ? : -;\n" |
1212 | " (?\?) ? : ? : -;\n" |
1213 | " endtable\n" |
1214 | "endprimitive" ); |
1215 | } |
1216 | |
1217 | TEST_F(FormatTestVerilog, Streaming) { |
1218 | verifyFormat("x = {>>{j}};" ); |
1219 | verifyFormat("x = {>>byte{j}};" ); |
1220 | verifyFormat("x = {<<{j}};" ); |
1221 | verifyFormat("x = {<<byte{j}};" ); |
1222 | verifyFormat("x = {<<16{j}};" ); |
1223 | verifyFormat("x = {<<{8'b0011_0101}};" ); |
1224 | verifyFormat("x = {<<4{6'b11_0101}};" ); |
1225 | verifyFormat("x = {>>4{6'b11_0101}};" ); |
1226 | verifyFormat("x = {<<2{{<<{4'b1101}}}};" ); |
1227 | verifyFormat("bit [96 : 1] y = {>>{a, b, c}};" ); |
1228 | verifyFormat("int j = {>>{a, b, c}};" ); |
1229 | verifyFormat("{>>{a, b, c}} = 23'b1;" ); |
1230 | verifyFormat("{>>{a, b, c}} = x;" ); |
1231 | verifyFormat("{>>{j}} = x;" ); |
1232 | verifyFormat("{>>byte{j}} = x;" ); |
1233 | verifyFormat("{<<{j}} = x;" ); |
1234 | verifyFormat("{<<byte{j}} = x;" ); |
1235 | } |
1236 | |
1237 | TEST_F(FormatTestVerilog, StringLiteral) { |
1238 | // Long strings should be broken. |
1239 | verifyFormat(R"(x({"xxxxxxxxxxxxxxxx ", |
1240 | "xxxx"});)" , |
1241 | R"(x({"xxxxxxxxxxxxxxxx xxxx"});)" , |
1242 | getStyleWithColumns(getDefaultStyle(), 23)); |
1243 | verifyFormat(R"(x({"xxxxxxxxxxxxxxxx", |
1244 | " xxxx"});)" , |
1245 | R"(x({"xxxxxxxxxxxxxxxx xxxx"});)" , |
1246 | getStyleWithColumns(getDefaultStyle(), 22)); |
1247 | // Braces should be added when they don't already exist. |
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 | verifyFormat(R"(x({{"xxxxxxxxxxxxxxxx ", |
1257 | "xxxx"} == x});)" , |
1258 | R"(x({"xxxxxxxxxxxxxxxx xxxx" == x});)" , |
1259 | getStyleWithColumns(getDefaultStyle(), 24)); |
1260 | verifyFormat(R"(string x = {"xxxxxxxxxxxxxxxx ", |
1261 | "xxxxxxxx"};)" , |
1262 | R"(string x = "xxxxxxxxxxxxxxxx xxxxxxxx";)" , |
1263 | getStyleWithColumns(getDefaultStyle(), 32)); |
1264 | // Space around braces should be correct. |
1265 | auto Style = getStyleWithColumns(Style: getDefaultStyle(), ColumnLimit: 24); |
1266 | Style.Cpp11BracedListStyle = false; |
1267 | verifyFormat(R"(x({ "xxxxxxxxxxxxxxxx ", |
1268 | "xxxx" });)" , |
1269 | R"(x("xxxxxxxxxxxxxxxx xxxx");)" , Style); |
1270 | // Braces should not be added twice. |
1271 | verifyFormat(R"(x({"xxxxxxxx", |
1272 | "xxxxxxxx", |
1273 | "xxxxxx"});)" , |
1274 | R"(x("xxxxxxxxxxxxxxxxxxxxxx");)" , |
1275 | getStyleWithColumns(getDefaultStyle(), 14)); |
1276 | verifyFormat(R"(x({"xxxxxxxxxxxxxxxx ", |
1277 | "xxxxxxxxxxxxxxxx ", |
1278 | "xxxx"});)" , |
1279 | R"(x("xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx xxxx");)" , |
1280 | getStyleWithColumns(getDefaultStyle(), 23)); |
1281 | verifyFormat(R"(x({"xxxxxxxxxxxxxxxx ", |
1282 | "xxxxxxxxxxxxxxxx ", |
1283 | "xxxx"});)" , |
1284 | R"(x({"xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxx ", "xxxx"});)" , |
1285 | getStyleWithColumns(getDefaultStyle(), 23)); |
1286 | // import/export "DPI"/"DPI-C" cannot be split. |
1287 | verifyFormat(R"(import |
1288 | "DPI-C" function void foo |
1289 | ();)" , |
1290 | R"(import "DPI-C" function void foo();)" , |
1291 | getStyleWithColumns(getDefaultStyle(), 23)); |
1292 | verifyFormat(R"(export "DPI-C" function foo;)" , |
1293 | R"(export "DPI-C" function foo;)" , |
1294 | getStyleWithColumns(getDefaultStyle(), 23)); |
1295 | // These kinds of strings don't exist in Verilog. |
1296 | verifyNoCrash(Code: R"(x(@"xxxxxxxxxxxxxxxx xxxx");)" , |
1297 | Style: getStyleWithColumns(Style: getDefaultStyle(), ColumnLimit: 23)); |
1298 | verifyNoCrash(Code: R"(x(u"xxxxxxxxxxxxxxxx xxxx");)" , |
1299 | Style: getStyleWithColumns(Style: getDefaultStyle(), ColumnLimit: 23)); |
1300 | verifyNoCrash(Code: R"(x(u8"xxxxxxxxxxxxxxxx xxxx");)" , |
1301 | Style: getStyleWithColumns(Style: getDefaultStyle(), ColumnLimit: 23)); |
1302 | verifyNoCrash(Code: R"(x(_T("xxxxxxxxxxxxxxxx xxxx"));)" , |
1303 | Style: getStyleWithColumns(Style: getDefaultStyle(), ColumnLimit: 23)); |
1304 | } |
1305 | |
1306 | TEST_F(FormatTestVerilog, StructLiteral) { |
1307 | verifyFormat("c = '{0, 0.0};" ); |
1308 | verifyFormat("c = '{'{1, 1.0}, '{2, 2.0}};" ); |
1309 | verifyFormat("c = '{a: 0, b: 0.0};" ); |
1310 | verifyFormat("c = '{a: 0, b: 0.0, default: 0};" ); |
1311 | verifyFormat("d = {int: 1, shortreal: 1.0};" ); |
1312 | verifyFormat("c = '{default: 0};" ); |
1313 | |
1314 | // The identifier before the quote can be either a tag or a type case. There |
1315 | // should be a space between the tag and the quote. |
1316 | verifyFormat("c = ab'{a: 0, b: 0.0};" ); |
1317 | verifyFormat("c = ab'{cd: cd'{1, 1.0}, ef: ef'{2, 2.0}};" ); |
1318 | verifyFormat("c = ab'{cd'{1, 1.0}, ef'{2, 2.0}};" ); |
1319 | verifyFormat("d = ab'{int: 1, shortreal: 1.0};" ); |
1320 | verifyFormat("x = tagged Add '{e1, 4, ed};" ); |
1321 | |
1322 | auto Style = getDefaultStyle(); |
1323 | Style.SpacesInContainerLiterals = true; |
1324 | verifyFormat("c = '{a : 0, b : 0.0};" , Style); |
1325 | verifyFormat("c = '{a : 0, b : 0.0, default : 0};" , Style); |
1326 | verifyFormat("c = ab'{a : 0, b : 0.0};" , Style); |
1327 | verifyFormat("c = ab'{cd : cd'{1, 1.0}, ef : ef'{2, 2.0}};" , Style); |
1328 | |
1329 | // It should be indented correctly when the line has to break. |
1330 | verifyFormat("c = //\n" |
1331 | " '{default: 0};" ); |
1332 | Style = getDefaultStyle(); |
1333 | Style.ContinuationIndentWidth = 2; |
1334 | verifyFormat("c = //\n" |
1335 | " '{default: 0};" , |
1336 | Style); |
1337 | } |
1338 | |
1339 | TEST_F(FormatTestVerilog, StructuredProcedure) { |
1340 | // Blocks should be indented correctly. |
1341 | verifyFormat("initial begin\n" |
1342 | "end" ); |
1343 | verifyFormat("initial begin\n" |
1344 | " x <= x;\n" |
1345 | " x <= x;\n" |
1346 | "end" ); |
1347 | verifyFormat("initial\n" |
1348 | " x <= x;\n" |
1349 | "x <= x;" ); |
1350 | verifyFormat("always @(x) begin\n" |
1351 | "end" ); |
1352 | verifyFormat("always @(x) begin\n" |
1353 | " x <= x;\n" |
1354 | " x <= x;\n" |
1355 | "end" ); |
1356 | verifyFormat("always @(x)\n" |
1357 | " x <= x;\n" |
1358 | "x <= x;" ); |
1359 | // Various keywords. |
1360 | verifyFormat("always @(x)\n" |
1361 | " x <= x;" ); |
1362 | verifyFormat("always @(posedge x)\n" |
1363 | " x <= x;" ); |
1364 | verifyFormat("always @(posedge x or posedge y)\n" |
1365 | " x <= x;" ); |
1366 | verifyFormat("always @(posedge x, posedge y)\n" |
1367 | " x <= x;" ); |
1368 | verifyFormat("always @(negedge x, negedge y)\n" |
1369 | " x <= x;" ); |
1370 | verifyFormat("always @(edge x, edge y)\n" |
1371 | " x <= x;" ); |
1372 | verifyFormat("always\n" |
1373 | " x <= x;" ); |
1374 | verifyFormat("always @*\n" |
1375 | " x <= x;" ); |
1376 | verifyFormat("always @(*)\n" |
1377 | " x <= x;" ); |
1378 | verifyFormat("always_comb\n" |
1379 | " x <= x;" ); |
1380 | verifyFormat("always_latch @(x)\n" |
1381 | " x <= x;" ); |
1382 | verifyFormat("always_ff @(posedge x)\n" |
1383 | " x <= x;" ); |
1384 | verifyFormat("initial\n" |
1385 | " x <= x;" ); |
1386 | verifyFormat("final\n" |
1387 | " x <= x;" ); |
1388 | verifyFormat("forever\n" |
1389 | " x <= x;" ); |
1390 | } |
1391 | } // namespace |
1392 | } // namespace test |
1393 | } // namespace format |
1394 | } // namespace clang |
1395 | |