1 //===- unittest/Format/FormatTestVerilog.cpp ------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "FormatTestBase.h" 10 11 #define DEBUG_TYPE "format-test" 12 13 namespace clang { 14 namespace format { 15 namespace test { 16 namespace { 17 class FormatTestVerilog : public test::FormatTestBase { 18 protected: 19 FormatStyle getDefaultStyle() const override { 20 return getLLVMStyle(FormatStyle::LK_Verilog); 21 } 22 std::string messUp(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\n"); 233 verifyFormat("case (data)\n" 234 " 16'd1: fork : x\n" 235 " result = 10'b1011111111;\n" 236 " join : x\n" 237 "endcase\n"); 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};\n", Style); 304 verifyFormat("default:\n" 305 " x = '{x : x, default : 9};\n", 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};\n", Style); 312 verifyFormat("default:\n" 313 " x = '{x: x, default: 9};\n", 314 Style); 315 } 316 317 TEST_F(FormatTestVerilog, Coverage) { 318 verifyFormat("covergroup x\n" 319 " @@(begin x);\n" 320 "endgroup"); 321 } 322 323 TEST_F(FormatTestVerilog, Declaration) { 324 verifyFormat("wire mynet;"); 325 verifyFormat("wire mynet, mynet1;"); 326 verifyFormat("wire mynet, //\n" 327 " mynet1;"); 328 verifyFormat("wire mynet = enable;"); 329 verifyFormat("wire mynet = enable, mynet1;"); 330 verifyFormat("wire mynet = enable, //\n" 331 " mynet1;"); 332 verifyFormat("wire mynet, mynet1 = enable;"); 333 verifyFormat("wire mynet, //\n" 334 " mynet1 = enable;"); 335 verifyFormat("wire mynet = enable, mynet1 = enable;"); 336 verifyFormat("wire mynet = enable, //\n" 337 " mynet1 = enable;"); 338 verifyFormat("wire (strong1, pull0) mynet;"); 339 verifyFormat("wire (strong1, pull0) mynet, mynet1;"); 340 verifyFormat("wire (strong1, pull0) mynet, //\n" 341 " mynet1;"); 342 verifyFormat("wire (strong1, pull0) mynet = enable;"); 343 verifyFormat("wire (strong1, pull0) mynet = enable, mynet1;"); 344 verifyFormat("wire (strong1, pull0) mynet = enable, //\n" 345 " mynet1;"); 346 verifyFormat("wire (strong1, pull0) mynet, mynet1 = enable;"); 347 verifyFormat("wire (strong1, pull0) mynet, //\n" 348 " mynet1 = enable;"); 349 } 350 351 TEST_F(FormatTestVerilog, Delay) { 352 // Delay by the default unit. 353 verifyFormat("#0;"); 354 verifyFormat("#1;"); 355 verifyFormat("#10;"); 356 verifyFormat("#1.5;"); 357 // Explicit unit. 358 verifyFormat("#1fs;"); 359 verifyFormat("#1.5fs;"); 360 verifyFormat("#1ns;"); 361 verifyFormat("#1.5ns;"); 362 verifyFormat("#1us;"); 363 verifyFormat("#1.5us;"); 364 verifyFormat("#1ms;"); 365 verifyFormat("#1.5ms;"); 366 verifyFormat("#1s;"); 367 verifyFormat("#1.5s;"); 368 // The following expression should be on the same line. 369 verifyFormat("#1 x = x;"); 370 verifyFormat("#1 x = x;", "#1\n" 371 "x = x;"); 372 } 373 374 TEST_F(FormatTestVerilog, Enum) { 375 verifyFormat("enum { x } x;"); 376 verifyFormat("typedef enum { x } x;"); 377 verifyFormat("enum { red, yellow, green } x;"); 378 verifyFormat("typedef enum { red, yellow, green } x;"); 379 verifyFormat("enum integer { x } x;"); 380 verifyFormat("typedef enum { x = 0 } x;"); 381 verifyFormat("typedef enum { red = 0, yellow = 1, green = 2 } x;"); 382 verifyFormat("typedef enum integer { x } x;"); 383 verifyFormat("typedef enum bit [0 : 1] { x } x;"); 384 verifyFormat("typedef enum { add = 10, sub[5], jmp[6 : 8] } E1;"); 385 verifyFormat("typedef enum { add = 10, sub[5] = 0, jmp[6 : 8] = 1 } E1;"); 386 } 387 388 TEST_F(FormatTestVerilog, Headers) { 389 // Test headers with multiple ports. 390 verifyFormat("module mh1\n" 391 " (input var int in1,\n" 392 " input var shortreal in2,\n" 393 " output tagged_st out);\n" 394 "endmodule"); 395 // There should be a space following the type but not the variable name. 396 verifyFormat("module test\n" 397 " (input wire [7 : 0] a,\n" 398 " input wire b[7 : 0],\n" 399 " input wire [7 : 0] c[7 : 0]);\n" 400 "endmodule"); 401 // Ports should be grouped by types. 402 verifyFormat("module test\n" 403 " (input [7 : 0] a,\n" 404 " input signed [7 : 0] b, c, d);\n" 405 "endmodule"); 406 verifyFormat("module test\n" 407 " (input [7 : 0] a,\n" 408 " (* x = x *) input signed [7 : 0] b, c, d);\n" 409 "endmodule"); 410 verifyFormat("module test\n" 411 " (input [7 : 0] a = 0,\n" 412 " input signed [7 : 0] b = 0, c = 0, d = 0);\n" 413 "endmodule"); 414 verifyFormat("module test\n" 415 " #(parameter x)\n" 416 " (input [7 : 0] a,\n" 417 " input signed [7 : 0] b, c, d);\n" 418 "endmodule"); 419 // When a line needs to be broken, ports of the same type should be aligned to 420 // the same column. 421 verifyFormat("module test\n" 422 " (input signed [7 : 0] b, c, //\n" 423 " d);\n" 424 "endmodule"); 425 verifyFormat("module test\n" 426 " ((* x = x *) input signed [7 : 0] b, c, //\n" 427 " d);\n" 428 "endmodule"); 429 verifyFormat("module test\n" 430 " (input signed [7 : 0] b = 0, c, //\n" 431 " d);\n" 432 "endmodule"); 433 verifyFormat("module test\n" 434 " (input signed [7 : 0] b, c = 0, //\n" 435 " d);\n" 436 "endmodule"); 437 verifyFormat("module test\n" 438 " (input signed [7 : 0] b, c, //\n" 439 " d = 0);\n" 440 "endmodule"); 441 verifyFormat("module test\n" 442 " (input wire logic signed [7 : 0][0 : 1] b, c, //\n" 443 " d);\n" 444 "endmodule"); 445 verifyFormat("module test\n" 446 " (input signed [7 : 0] b, //\n" 447 " c, //\n" 448 " d);\n" 449 "endmodule"); 450 verifyFormat("module test\n" 451 " (input [7 : 0] a,\n" 452 " input signed [7 : 0] b, //\n" 453 " c, //\n" 454 " d);\n" 455 "endmodule"); 456 verifyFormat("module test\n" 457 " (input signed [7 : 0] b, //\n" 458 " c, //\n" 459 " d,\n" 460 " output signed [7 : 0] h);\n" 461 "endmodule"); 462 // With a modport. 463 verifyFormat("module m\n" 464 " (i2.master i);\n" 465 "endmodule"); 466 verifyFormat("module m\n" 467 " (i2.master i, ii);\n" 468 "endmodule"); 469 verifyFormat("module m\n" 470 " (i2.master i, //\n" 471 " ii);\n" 472 "endmodule"); 473 verifyFormat("module m\n" 474 " (i2.master i,\n" 475 " input ii);\n" 476 "endmodule"); 477 verifyFormat("module m\n" 478 " (i2::i2.master i);\n" 479 "endmodule"); 480 verifyFormat("module m\n" 481 " (i2::i2.master i, ii);\n" 482 "endmodule"); 483 verifyFormat("module m\n" 484 " (i2::i2.master i, //\n" 485 " ii);\n" 486 "endmodule"); 487 verifyFormat("module m\n" 488 " (i2::i2.master i,\n" 489 " input ii);\n" 490 "endmodule"); 491 verifyFormat("module m\n" 492 " (i2::i2 i);\n" 493 "endmodule"); 494 verifyFormat("module m\n" 495 " (i2::i2 i, ii);\n" 496 "endmodule"); 497 verifyFormat("module m\n" 498 " (i2::i2 i, //\n" 499 " ii);\n" 500 "endmodule"); 501 verifyFormat("module m\n" 502 " (i2::i2 i,\n" 503 " input ii);\n" 504 "endmodule"); 505 // With a macro in the names. 506 verifyFormat("module m\n" 507 " (input var `x a, b);\n" 508 "endmodule"); 509 verifyFormat("module m\n" 510 " (input var `x a, //\n" 511 " b);\n" 512 "endmodule"); 513 verifyFormat("module m\n" 514 " (input var x `a, b);\n" 515 "endmodule"); 516 verifyFormat("module m\n" 517 " (input var x `a, //\n" 518 " b);\n" 519 "endmodule"); 520 // A line comment shouldn't disrupt the indentation of the port list. 521 verifyFormat("extern module x\n" 522 " (//\n" 523 " output y);"); 524 verifyFormat("extern module x\n" 525 " #(//\n" 526 " parameter x)\n" 527 " (//\n" 528 " output y);"); 529 // With a concatenation in the names. 530 auto Style = getDefaultStyle(); 531 Style.ColumnLimit = 40; 532 verifyFormat("`define X(x) \\\n" 533 " module test \\\n" 534 " (input var x``x a, b);", 535 Style); 536 verifyFormat("`define X(x) \\\n" 537 " module test \\\n" 538 " (input var x``x aaaaaaaaaaaaaaa, \\\n" 539 " b);", 540 Style); 541 verifyFormat("`define X(x) \\\n" 542 " module test \\\n" 543 " (input var x a``x, b);", 544 Style); 545 verifyFormat("`define X(x) \\\n" 546 " module test \\\n" 547 " (input var x aaaaaaaaaaaaaaa``x, \\\n" 548 " b);", 549 Style); 550 } 551 552 TEST_F(FormatTestVerilog, Hierarchy) { 553 verifyFormat("module x;\n" 554 "endmodule"); 555 // Test that the end label is on the same line as the end keyword. 556 verifyFormat("module x;\n" 557 "endmodule : x"); 558 // Test that things inside are indented. 559 verifyFormat("module x;\n" 560 " generate\n" 561 " endgenerate\n" 562 "endmodule"); 563 verifyFormat("program x;\n" 564 " generate\n" 565 " endgenerate\n" 566 "endprogram"); 567 verifyFormat("interface x;\n" 568 " generate\n" 569 " endgenerate\n" 570 "endinterface"); 571 verifyFormat("task x;\n" 572 " generate\n" 573 " endgenerate\n" 574 "endtask"); 575 verifyFormat("function x;\n" 576 " generate\n" 577 " endgenerate\n" 578 "endfunction"); 579 verifyFormat("class x;\n" 580 " generate\n" 581 " endgenerate\n" 582 "endclass"); 583 // Test that they nest. 584 verifyFormat("module x;\n" 585 " program x;\n" 586 " program x;\n" 587 " endprogram\n" 588 " endprogram\n" 589 "endmodule"); 590 // Test that an extern declaration doesn't change the indentation. 591 verifyFormat("extern module x;\n" 592 "x = x;"); 593 // Test complex headers 594 verifyFormat("extern module x\n" 595 " import x.x::x::*;\n" 596 " import x;\n" 597 " #(parameter x)\n" 598 " (output x);"); 599 verifyFormat("module x\n" 600 " import x.x::x::*;\n" 601 " import x;\n" 602 " #(parameter x)\n" 603 " (output x);\n" 604 " generate\n" 605 " endgenerate\n" 606 "endmodule : x"); 607 verifyFormat("virtual class x\n" 608 " (x)\n" 609 " extends x(x)\n" 610 " implements x, x, x;\n" 611 " generate\n" 612 " endgenerate\n" 613 "endclass : x\n"); 614 verifyFormat("function automatic logic [1 : 0] x\n" 615 " (input x);\n" 616 " generate\n" 617 " endgenerate\n" 618 "endfunction : x"); 619 } 620 621 TEST_F(FormatTestVerilog, Identifiers) { 622 // Escaped identifiers should not be split. 623 verifyFormat("\\busa+index"); 624 verifyFormat("\\-clock"); 625 verifyFormat("\\***error-condition***"); 626 verifyFormat("\\net1\\/net2"); 627 verifyFormat("\\{a,b}"); 628 verifyFormat("\\a*(b+c)"); 629 // Escaped identifiers can't be joined with the next token. Extra space 630 // should be removed. 631 verifyFormat("\\busa+index ;", "\\busa+index\n" 632 ";"); 633 verifyFormat("\\busa+index ;", "\\busa+index\r\n" 634 ";"); 635 verifyFormat("\\busa+index ;", "\\busa+index ;"); 636 verifyFormat("\\busa+index ;", "\\busa+index\n" 637 " ;"); 638 verifyFormat("\\busa+index ;"); 639 verifyFormat("(\\busa+index );"); 640 verifyFormat("\\busa+index \\busa+index ;"); 641 } 642 643 TEST_F(FormatTestVerilog, If) { 644 verifyFormat("if (x)\n" 645 " x = x;"); 646 verifyFormat("unique if (x)\n" 647 " x = x;"); 648 verifyFormat("unique0 if (x)\n" 649 " x = x;"); 650 verifyFormat("priority if (x)\n" 651 " x = x;"); 652 verifyFormat("if (x)\n" 653 " x = x;\n" 654 "x = x;"); 655 656 // Test else 657 verifyFormat("if (x)\n" 658 " x = x;\n" 659 "else if (x)\n" 660 " x = x;\n" 661 "else\n" 662 " x = x;"); 663 verifyFormat("if (x) begin\n" 664 " x = x;\n" 665 "end else if (x) begin\n" 666 " x = x;\n" 667 "end else begin\n" 668 " x = x;\n" 669 "end"); 670 verifyFormat("if (x) begin : x\n" 671 " x = x;\n" 672 "end : x else if (x) begin : x\n" 673 " x = x;\n" 674 "end : x else begin : x\n" 675 " x = x;\n" 676 "end : x"); 677 678 // Test block keywords. 679 verifyFormat("if (x) begin\n" 680 " x = x;\n" 681 "end"); 682 verifyFormat("if (x) begin : x\n" 683 " x = x;\n" 684 "end : x"); 685 verifyFormat("if (x) begin\n" 686 " x = x;\n" 687 " x = x;\n" 688 "end"); 689 verifyFormat("if (x) fork\n" 690 " x = x;\n" 691 "join"); 692 verifyFormat("if (x) fork\n" 693 " x = x;\n" 694 "join_any"); 695 verifyFormat("if (x) fork\n" 696 " x = x;\n" 697 "join_none"); 698 verifyFormat("if (x) generate\n" 699 " x = x;\n" 700 "endgenerate"); 701 verifyFormat("if (x) generate : x\n" 702 " x = x;\n" 703 "endgenerate : x"); 704 705 // Test that concatenation braces don't get regarded as blocks. 706 verifyFormat("if (x)\n" 707 " {x} = x;"); 708 verifyFormat("if (x)\n" 709 " x = {x};"); 710 verifyFormat("if (x)\n" 711 " x = {x};\n" 712 "else\n" 713 " {x} = {x};"); 714 715 // With attributes. 716 verifyFormat("(* x *) if (x)\n" 717 " x = x;"); 718 verifyFormat("(* x = \"x\" *) if (x)\n" 719 " x = x;"); 720 verifyFormat("(* x, x = \"x\" *) if (x)\n" 721 " x = x;"); 722 723 // Assert are treated similar to if. But the else parts should not be 724 // chained. 725 verifyFormat("assert (x);"); 726 verifyFormat("assert (x)\n" 727 " $info();"); 728 verifyFormat("assert (x)\n" 729 " $info();\n" 730 "else\n" 731 " $error();"); 732 verifyFormat("assert (x)\n" 733 "else\n" 734 " $error();"); 735 verifyFormat("assert (x)\n" 736 "else begin\n" 737 "end"); 738 verifyFormat("assert (x)\n" 739 "else\n" 740 " if (x)\n" 741 " $error();"); 742 verifyFormat("assert (x)\n" 743 " $info();\n" 744 "else\n" 745 " if (x)\n" 746 " $error();"); 747 verifyFormat("assert (x)\n" 748 " $info();\n" 749 "else\n" 750 " if (x)\n" 751 " $error();\n" 752 " else\n" 753 " $error();"); 754 verifyFormat("assert (x)\n" 755 " $info();\n" 756 "else\n" 757 " if (x)\n" 758 " $error();\n" 759 " else if (x)\n" 760 " $error();\n" 761 " else\n" 762 " $error();"); 763 // The body is optional for asserts. The next line should not be indented if 764 // the statement already ended with a semicolon. 765 verifyFormat("assert (x);\n" 766 "x = x;"); 767 verifyFormat("if (x)\n" 768 " assert (x);\n" 769 "else if (x) begin\n" 770 "end else begin\n" 771 "end"); 772 verifyFormat("if (x)\n" 773 " assert (x);\n" 774 "else begin\n" 775 "end"); 776 verifyFormat("if (x)\n" 777 " assert (x)\n" 778 " else begin\n" 779 " end"); 780 // Other keywords. 781 verifyFormat("assume (x)\n" 782 " $info();"); 783 verifyFormat("cover (x)\n" 784 " $info();"); 785 verifyFormat("restrict (x)\n" 786 " $info();"); 787 verifyFormat("assert #0 (x)\n" 788 " $info();"); 789 verifyFormat("assert final (x)\n" 790 " $info();"); 791 verifyFormat("cover #0 (x)\n" 792 " $info();"); 793 verifyFormat("cover final (x)\n" 794 " $info();"); 795 796 // The space around parentheses options should work. 797 auto Style = getDefaultStyle(); 798 verifyFormat("if (x)\n" 799 " x = x;\n" 800 "else if (x)\n" 801 " x = x;", 802 Style); 803 verifyFormat("assert (x);", Style); 804 verifyFormat("assert #0 (x);", Style); 805 verifyFormat("assert (x)\n" 806 "else\n" 807 " if (x)\n" 808 " x = x;", 809 Style); 810 Style.SpacesInConditionalStatement = true; 811 verifyFormat("if ( x )\n" 812 " x = x;\n" 813 "else if ( x )\n" 814 " x = x;", 815 Style); 816 verifyFormat("assert ( x );", Style); 817 verifyFormat("assert #0 ( x );", Style); 818 verifyFormat("assert ( x )\n" 819 "else\n" 820 " if ( x )\n" 821 " x = x;", 822 Style); 823 } 824 825 TEST_F(FormatTestVerilog, Instantiation) { 826 // Without ports. 827 verifyFormat("ffnand ff1;"); 828 // With named ports. 829 verifyFormat("ffnand ff1(.qbar(out1),\n" 830 " .clear(in1),\n" 831 " .preset(in2));"); 832 // With wildcard. 833 verifyFormat("ffnand ff1(.qbar(out1),\n" 834 " .clear(in1),\n" 835 " .preset(in2),\n" 836 " .*);"); 837 verifyFormat("ffnand ff1(.*,\n" 838 " .qbar(out1),\n" 839 " .clear(in1),\n" 840 " .preset(in2));"); 841 // With unconnected ports. 842 verifyFormat("ffnand ff1(.q(),\n" 843 " .qbar(out1),\n" 844 " .clear(in1),\n" 845 " .preset(in2));"); 846 verifyFormat("ffnand ff1(.q(),\n" 847 " .qbar(),\n" 848 " .clear(),\n" 849 " .preset());"); 850 verifyFormat("ffnand ff1(,\n" 851 " .qbar(out1),\n" 852 " .clear(in1),\n" 853 " .preset(in2));"); 854 // With positional ports. 855 verifyFormat("ffnand ff1(out1,\n" 856 " in1,\n" 857 " in2);"); 858 verifyFormat("ffnand ff1(,\n" 859 " out1,\n" 860 " in1,\n" 861 " in2);"); 862 // Multiple instantiations. 863 verifyFormat("ffnand ff1(.q(),\n" 864 " .qbar(out1),\n" 865 " .clear(in1),\n" 866 " .preset(in2)),\n" 867 " ff1(.q(),\n" 868 " .qbar(out1),\n" 869 " .clear(in1),\n" 870 " .preset(in2));"); 871 verifyFormat("ffnand //\n" 872 " ff1(.q(),\n" 873 " .qbar(out1),\n" 874 " .clear(in1),\n" 875 " .preset(in2)),\n" 876 " ff1(.q(),\n" 877 " .qbar(out1),\n" 878 " .clear(in1),\n" 879 " .preset(in2));"); 880 // With breaking between instance ports disabled. 881 auto Style = getDefaultStyle(); 882 Style.VerilogBreakBetweenInstancePorts = false; 883 verifyFormat("ffnand ff1;", Style); 884 verifyFormat("ffnand ff1(.qbar(out1), .clear(in1), .preset(in2), .*);", 885 Style); 886 verifyFormat("ffnand ff1(out1, in1, in2);", Style); 887 verifyFormat("ffnand ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n" 888 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));", 889 Style); 890 verifyFormat("ffnand //\n" 891 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n" 892 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));", 893 Style); 894 } 895 896 TEST_F(FormatTestVerilog, Loop) { 897 verifyFormat("foreach (x[x])\n" 898 " x = x;"); 899 verifyFormat("repeat (x)\n" 900 " x = x;"); 901 verifyFormat("foreach (x[x]) begin\n" 902 "end"); 903 verifyFormat("repeat (x) begin\n" 904 "end"); 905 auto Style = getDefaultStyle(); 906 Style.SpacesInConditionalStatement = true; 907 verifyFormat("foreach ( x[x] )\n" 908 " x = x;", 909 Style); 910 verifyFormat("repeat ( x )\n" 911 " x = x;", 912 Style); 913 } 914 915 TEST_F(FormatTestVerilog, Operators) { 916 // Test that unary operators are not followed by space. 917 verifyFormat("x = +x;"); 918 verifyFormat("x = -x;"); 919 verifyFormat("x = !x;"); 920 verifyFormat("x = ~x;"); 921 verifyFormat("x = &x;"); 922 verifyFormat("x = ~&x;"); 923 verifyFormat("x = |x;"); 924 verifyFormat("x = ~|x;"); 925 verifyFormat("x = ^x;"); 926 verifyFormat("x = ~^x;"); 927 verifyFormat("x = ^~x;"); 928 verifyFormat("x = ++x;"); 929 verifyFormat("x = --x;"); 930 931 // Test that `*` and `*>` are binary. 932 verifyFormat("x = x * x;"); 933 verifyFormat("x = (x * x);"); 934 verifyFormat("(opcode *> o1) = 6.1;"); 935 verifyFormat("(C, D *> Q) = 18;"); 936 // The wildcard import is not a binary operator. 937 verifyFormat("import p::*;"); 938 939 // Test that operators don't get split. 940 verifyFormat("x = x++;"); 941 verifyFormat("x = x--;"); 942 verifyFormat("x = x ** x;"); 943 verifyFormat("x = x << x;"); 944 verifyFormat("x = x >> x;"); 945 verifyFormat("x = x <<< x;"); 946 verifyFormat("x = x >>> x;"); 947 verifyFormat("x = x <= x;"); 948 verifyFormat("x = x >= x;"); 949 verifyFormat("x = x == x;"); 950 verifyFormat("x = x != x;"); 951 verifyFormat("x = x === x;"); 952 verifyFormat("x = x !== x;"); 953 verifyFormat("x = x ==? x;"); 954 verifyFormat("x = x !=? x;"); 955 verifyFormat("x = x ~^ x;"); 956 verifyFormat("x = x ^~ x;"); 957 verifyFormat("x = x && x;"); 958 verifyFormat("x = x || x;"); 959 verifyFormat("x = x->x;"); 960 verifyFormat("x = x <-> x;"); 961 verifyFormat("x += x;"); 962 verifyFormat("x -= x;"); 963 verifyFormat("x *= x;"); 964 verifyFormat("x /= x;"); 965 verifyFormat("x %= x;"); 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 975 // Test that space is added between operators. 976 verifyFormat("x = x < -x;", "x=x<-x;"); 977 verifyFormat("x = x << -x;", "x=x<<-x;"); 978 verifyFormat("x = x <<< -x;", "x=x<<<-x;"); 979 980 // Test that operators that are C++ identifiers get treated as operators. 981 verifyFormat("solve s before d;"); // before 982 verifyFormat("binsof(i) intersect {0};"); // intersect 983 verifyFormat("req dist {1};"); // dist 984 verifyFormat("a inside {b, c};"); // inside 985 verifyFormat("bus.randomize() with { atype == low; };"); // with 986 } 987 988 TEST_F(FormatTestVerilog, Preprocessor) { 989 auto Style = getDefaultStyle(); 990 Style.ColumnLimit = 20; 991 992 // Macro definitions. 993 verifyFormat("`define X \\\n" 994 " if (x) \\\n" 995 " x = x;", 996 "`define X if(x)x=x;", Style); 997 verifyFormat("`define X(x) \\\n" 998 " if (x) \\\n" 999 " x = x;", 1000 "`define X(x) if(x)x=x;", Style); 1001 verifyFormat("`define X \\\n" 1002 " x = x; \\\n" 1003 " x = x;", 1004 "`define X x=x;x=x;", Style); 1005 // Macro definitions with invocations inside. 1006 verifyFormat("`define LIST \\\n" 1007 " `ENTRY \\\n" 1008 " `ENTRY", 1009 "`define LIST \\\n" 1010 "`ENTRY \\\n" 1011 "`ENTRY", 1012 Style); 1013 verifyFormat("`define LIST \\\n" 1014 " `x = `x; \\\n" 1015 " `x = `x;", 1016 "`define LIST \\\n" 1017 "`x = `x; \\\n" 1018 "`x = `x;", 1019 Style); 1020 verifyFormat("`define LIST \\\n" 1021 " `x = `x; \\\n" 1022 " `x = `x;", 1023 "`define LIST `x=`x;`x=`x;", Style); 1024 // Macro invocations. 1025 verifyFormat("`x = (`x1 + `x2 + x);"); 1026 // Lines starting with a preprocessor directive should not be indented. 1027 std::string Directives[] = { 1028 "begin_keywords", 1029 "celldefine", 1030 "default_nettype", 1031 "define", 1032 "else", 1033 "elsif", 1034 "end_keywords", 1035 "endcelldefine", 1036 "endif", 1037 "ifdef", 1038 "ifndef", 1039 "include", 1040 "line", 1041 "nounconnected_drive", 1042 "pragma", 1043 "resetall", 1044 "timescale", 1045 "unconnected_drive", 1046 "undef", 1047 "undefineall", 1048 }; 1049 for (auto &Name : Directives) { 1050 verifyFormat("if (x)\n" 1051 "`" + 1052 Name + 1053 "\n" 1054 " ;", 1055 "if (x)\n" 1056 "`" + 1057 Name + 1058 "\n" 1059 ";", 1060 Style); 1061 } 1062 // Lines starting with a regular macro invocation should be indented as a 1063 // normal line. 1064 verifyFormat("if (x)\n" 1065 " `x = `x;\n" 1066 "`timescale 1ns / 1ps", 1067 "if (x)\n" 1068 "`x = `x;\n" 1069 "`timescale 1ns / 1ps", 1070 Style); 1071 verifyFormat("if (x)\n" 1072 "`timescale 1ns / 1ps\n" 1073 " `x = `x;", 1074 "if (x)\n" 1075 "`timescale 1ns / 1ps\n" 1076 "`x = `x;", 1077 Style); 1078 std::string NonDirectives[] = { 1079 // For `__FILE__` and `__LINE__`, although the standard classifies them as 1080 // preprocessor directives, they are used like regular macros. 1081 "__FILE__", "__LINE__", "elif", "foo", "x", 1082 }; 1083 for (auto &Name : NonDirectives) { 1084 verifyFormat("if (x)\n" 1085 " `" + 1086 Name + ";", 1087 "if (x)\n" 1088 "`" + 1089 Name + 1090 "\n" 1091 ";", 1092 Style); 1093 } 1094 } 1095 1096 TEST_F(FormatTestVerilog, Primitive) { 1097 verifyFormat("primitive multiplexer\n" 1098 " (mux, control, dataA, dataB);\n" 1099 " output mux;\n" 1100 " input control, dataA, dataB;\n" 1101 " table\n" 1102 " 0 1 ? : 1;\n" 1103 " 0 0 ? : 0;\n" 1104 " 1 ? 1 : 1;\n" 1105 " 1 ? 0 : 0;\n" 1106 " x 0 0 : 0;\n" 1107 " x 1 1 : 1;\n" 1108 " endtable\n" 1109 "endprimitive"); 1110 verifyFormat("primitive latch\n" 1111 " (q, ena_, data);\n" 1112 " output q;\n" 1113 " reg q;\n" 1114 " input ena_, data;\n" 1115 " table\n" 1116 " 0 1 : ? : 1;\n" 1117 " 0 0 : ? : 0;\n" 1118 " 1 ? : ? : -;\n" 1119 " ? * : ? : -;\n" 1120 " endtable\n" 1121 "endprimitive"); 1122 verifyFormat("primitive d\n" 1123 " (q, clock, data);\n" 1124 " output q;\n" 1125 " reg q;\n" 1126 " input clock, data;\n" 1127 " table\n" 1128 " (01) 0 : ? : 0;\n" 1129 " (01) 1 : ? : 1;\n" 1130 " (0?) 1 : 1 : 1;\n" 1131 " (0?) 0 : 0 : 0;\n" 1132 " (?0) ? : ? : -;\n" 1133 " (?\?) ? : ? : -;\n" 1134 " endtable\n" 1135 "endprimitive"); 1136 } 1137 1138 TEST_F(FormatTestVerilog, Streaming) { 1139 verifyFormat("x = {>>{j}};"); 1140 verifyFormat("x = {>>byte{j}};"); 1141 verifyFormat("x = {<<{j}};"); 1142 verifyFormat("x = {<<byte{j}};"); 1143 verifyFormat("x = {<<16{j}};"); 1144 verifyFormat("x = {<<{8'b0011_0101}};"); 1145 verifyFormat("x = {<<4{6'b11_0101}};"); 1146 verifyFormat("x = {>>4{6'b11_0101}};"); 1147 verifyFormat("x = {<<2{{<<{4'b1101}}}};"); 1148 verifyFormat("bit [96 : 1] y = {>>{a, b, c}};"); 1149 verifyFormat("int j = {>>{a, b, c}};"); 1150 verifyFormat("{>>{a, b, c}} = 23'b1;"); 1151 verifyFormat("{>>{a, b, c}} = x;"); 1152 verifyFormat("{>>{j}} = x;"); 1153 verifyFormat("{>>byte{j}} = x;"); 1154 verifyFormat("{<<{j}} = x;"); 1155 verifyFormat("{<<byte{j}} = x;"); 1156 } 1157 1158 TEST_F(FormatTestVerilog, StructLiteral) { 1159 verifyFormat("c = '{0, 0.0};"); 1160 verifyFormat("c = '{'{1, 1.0}, '{2, 2.0}};"); 1161 verifyFormat("c = '{a: 0, b: 0.0};"); 1162 verifyFormat("c = '{a: 0, b: 0.0, default: 0};"); 1163 verifyFormat("c = ab'{a: 0, b: 0.0};"); 1164 verifyFormat("c = ab'{cd: cd'{1, 1.0}, ef: ef'{2, 2.0}};"); 1165 verifyFormat("c = ab'{cd'{1, 1.0}, ef'{2, 2.0}};"); 1166 verifyFormat("d = {int: 1, shortreal: 1.0};"); 1167 verifyFormat("d = ab'{int: 1, shortreal: 1.0};"); 1168 verifyFormat("c = '{default: 0};"); 1169 auto Style = getDefaultStyle(); 1170 Style.SpacesInContainerLiterals = true; 1171 verifyFormat("c = '{a : 0, b : 0.0};", Style); 1172 verifyFormat("c = '{a : 0, b : 0.0, default : 0};", Style); 1173 verifyFormat("c = ab'{a : 0, b : 0.0};", Style); 1174 verifyFormat("c = ab'{cd : cd'{1, 1.0}, ef : ef'{2, 2.0}};", Style); 1175 1176 // It should be indented correctly when the line has to break. 1177 verifyFormat("c = //\n" 1178 " '{default: 0};"); 1179 Style = getDefaultStyle(); 1180 Style.ContinuationIndentWidth = 2; 1181 verifyFormat("c = //\n" 1182 " '{default: 0};", 1183 Style); 1184 } 1185 1186 TEST_F(FormatTestVerilog, StructuredProcedure) { 1187 // Blocks should be indented correctly. 1188 verifyFormat("initial begin\n" 1189 "end"); 1190 verifyFormat("initial begin\n" 1191 " x <= x;\n" 1192 " x <= x;\n" 1193 "end"); 1194 verifyFormat("initial\n" 1195 " x <= x;\n" 1196 "x <= x;"); 1197 verifyFormat("always @(x) begin\n" 1198 "end"); 1199 verifyFormat("always @(x) begin\n" 1200 " x <= x;\n" 1201 " x <= x;\n" 1202 "end"); 1203 verifyFormat("always @(x)\n" 1204 " x <= x;\n" 1205 "x <= x;"); 1206 // Various keywords. 1207 verifyFormat("always @(x)\n" 1208 " x <= x;"); 1209 verifyFormat("always @(posedge x)\n" 1210 " x <= x;"); 1211 verifyFormat("always @(posedge x or posedge y)\n" 1212 " x <= x;"); 1213 verifyFormat("always @(posedge x, posedge y)\n" 1214 " x <= x;"); 1215 verifyFormat("always @(negedge x, negedge y)\n" 1216 " x <= x;"); 1217 verifyFormat("always @(edge x, edge y)\n" 1218 " x <= x;"); 1219 verifyFormat("always\n" 1220 " x <= x;"); 1221 verifyFormat("always @*\n" 1222 " x <= x;"); 1223 verifyFormat("always @(*)\n" 1224 " x <= x;"); 1225 verifyFormat("always_comb\n" 1226 " x <= x;"); 1227 verifyFormat("always_latch @(x)\n" 1228 " x <= x;"); 1229 verifyFormat("always_ff @(posedge x)\n" 1230 " x <= x;"); 1231 verifyFormat("initial\n" 1232 " x <= x;"); 1233 verifyFormat("final\n" 1234 " x <= x;"); 1235 verifyFormat("forever\n" 1236 " x <= x;"); 1237 } 1238 } // namespace 1239 } // namespace test 1240 } // namespace format 1241 } // namespace clang 1242