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 } 166 167 TEST_F(FormatTestVerilog, Case) { 168 verifyFormat("case (data)\n" 169 "endcase"); 170 verifyFormat("casex (data)\n" 171 "endcase"); 172 verifyFormat("casez (data)\n" 173 "endcase"); 174 verifyFormat("case (data) inside\n" 175 "endcase"); 176 verifyFormat("case (data)\n" 177 " 16'd0:\n" 178 " result = 10'b0111111111;\n" 179 "endcase"); 180 verifyFormat("case (data)\n" 181 " xxxxxxxx:\n" 182 " result = 10'b0111111111;\n" 183 "endcase"); 184 // Test labels with multiple options. 185 verifyFormat("case (data)\n" 186 " 16'd0, 16'd1:\n" 187 " result = 10'b0111111111;\n" 188 "endcase"); 189 verifyFormat("case (data)\n" 190 " 16'd0, //\n" 191 " 16'd1:\n" 192 " result = 10'b0111111111;\n" 193 "endcase"); 194 // Test that blocks following labels are indented. 195 verifyFormat("case (data)\n" 196 " 16'd1: fork\n" 197 " result = 10'b1011111111;\n" 198 " join\n" 199 "endcase\n"); 200 verifyFormat("case (data)\n" 201 " 16'd1: fork : x\n" 202 " result = 10'b1011111111;\n" 203 " join : x\n" 204 "endcase\n"); 205 // Test default. 206 verifyFormat("case (data)\n" 207 " default\n" 208 " result = 10'b1011111111;\n" 209 "endcase"); 210 verifyFormat("case (data)\n" 211 " default:\n" 212 " result = 10'b1011111111;\n" 213 "endcase"); 214 // Test that question marks and colons don't get mistaken as labels. 215 verifyFormat("case (data)\n" 216 " 8'b1???????:\n" 217 " instruction1(ir);\n" 218 "endcase"); 219 verifyFormat("case (data)\n" 220 " x ? 8'b1??????? : 1:\n" 221 " instruction3(ir);\n" 222 "endcase"); 223 // Test indention options. 224 auto Style = getDefaultStyle(); 225 Style.IndentCaseLabels = false; 226 verifyFormat("case (data)\n" 227 "16'd0:\n" 228 " result = 10'b0111111111;\n" 229 "endcase", 230 Style); 231 verifyFormat("case (data)\n" 232 "16'd0: begin\n" 233 " result = 10'b0111111111;\n" 234 "end\n" 235 "endcase", 236 Style); 237 Style.IndentCaseLabels = true; 238 verifyFormat("case (data)\n" 239 " 16'd0:\n" 240 " result = 10'b0111111111;\n" 241 "endcase", 242 Style); 243 verifyFormat("case (data)\n" 244 " 16'd0: begin\n" 245 " result = 10'b0111111111;\n" 246 " end\n" 247 "endcase", 248 Style); 249 // Other colons should not be mistaken as case colons. 250 Style = getDefaultStyle(); 251 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 252 verifyFormat("case (x[1:0])\n" 253 "endcase", 254 Style); 255 verifyFormat("default:\n" 256 " x[1:0] = x[1:0];", 257 Style); 258 Style.BitFieldColonSpacing = FormatStyle::BFCS_Both; 259 verifyFormat("case (x[1 : 0])\n" 260 "endcase", 261 Style); 262 verifyFormat("default:\n" 263 " x[1 : 0] = x[1 : 0];", 264 Style); 265 Style = getDefaultStyle(); 266 Style.SpacesInContainerLiterals = true; 267 verifyFormat("case ('{x : x, default : 9})\n" 268 "endcase", 269 Style); 270 verifyFormat("x = '{x : x, default : 9};\n", Style); 271 verifyFormat("default:\n" 272 " x = '{x : x, default : 9};\n", 273 Style); 274 Style.SpacesInContainerLiterals = false; 275 verifyFormat("case ('{x: x, default: 9})\n" 276 "endcase", 277 Style); 278 verifyFormat("x = '{x: x, default: 9};\n", Style); 279 verifyFormat("default:\n" 280 " x = '{x: x, default: 9};\n", 281 Style); 282 } 283 284 TEST_F(FormatTestVerilog, Coverage) { 285 verifyFormat("covergroup x\n" 286 " @@(begin x);\n" 287 "endgroup"); 288 } 289 290 TEST_F(FormatTestVerilog, Declaration) { 291 verifyFormat("wire mynet;"); 292 verifyFormat("wire mynet, mynet1;"); 293 verifyFormat("wire mynet, //\n" 294 " mynet1;"); 295 verifyFormat("wire mynet = enable;"); 296 verifyFormat("wire mynet = enable, mynet1;"); 297 verifyFormat("wire mynet = enable, //\n" 298 " mynet1;"); 299 verifyFormat("wire mynet, mynet1 = enable;"); 300 verifyFormat("wire mynet, //\n" 301 " mynet1 = enable;"); 302 verifyFormat("wire mynet = enable, mynet1 = enable;"); 303 verifyFormat("wire mynet = enable, //\n" 304 " mynet1 = enable;"); 305 verifyFormat("wire (strong1, pull0) mynet;"); 306 verifyFormat("wire (strong1, pull0) mynet, mynet1;"); 307 verifyFormat("wire (strong1, pull0) mynet, //\n" 308 " mynet1;"); 309 verifyFormat("wire (strong1, pull0) mynet = enable;"); 310 verifyFormat("wire (strong1, pull0) mynet = enable, mynet1;"); 311 verifyFormat("wire (strong1, pull0) mynet = enable, //\n" 312 " mynet1;"); 313 verifyFormat("wire (strong1, pull0) mynet, mynet1 = enable;"); 314 verifyFormat("wire (strong1, pull0) mynet, //\n" 315 " mynet1 = enable;"); 316 } 317 318 TEST_F(FormatTestVerilog, Delay) { 319 // Delay by the default unit. 320 verifyFormat("#0;"); 321 verifyFormat("#1;"); 322 verifyFormat("#10;"); 323 verifyFormat("#1.5;"); 324 // Explicit unit. 325 verifyFormat("#1fs;"); 326 verifyFormat("#1.5fs;"); 327 verifyFormat("#1ns;"); 328 verifyFormat("#1.5ns;"); 329 verifyFormat("#1us;"); 330 verifyFormat("#1.5us;"); 331 verifyFormat("#1ms;"); 332 verifyFormat("#1.5ms;"); 333 verifyFormat("#1s;"); 334 verifyFormat("#1.5s;"); 335 // The following expression should be on the same line. 336 verifyFormat("#1 x = x;"); 337 verifyFormat("#1 x = x;", "#1\n" 338 "x = x;"); 339 } 340 341 TEST_F(FormatTestVerilog, Enum) { 342 verifyFormat("enum { x } x;"); 343 verifyFormat("typedef enum { x } x;"); 344 verifyFormat("enum { red, yellow, green } x;"); 345 verifyFormat("typedef enum { red, yellow, green } x;"); 346 verifyFormat("enum integer { x } x;"); 347 verifyFormat("typedef enum { x = 0 } x;"); 348 verifyFormat("typedef enum { red = 0, yellow = 1, green = 2 } x;"); 349 verifyFormat("typedef enum integer { x } x;"); 350 verifyFormat("typedef enum bit [0 : 1] { x } x;"); 351 verifyFormat("typedef enum { add = 10, sub[5], jmp[6 : 8] } E1;"); 352 verifyFormat("typedef enum { add = 10, sub[5] = 0, jmp[6 : 8] = 1 } E1;"); 353 } 354 355 TEST_F(FormatTestVerilog, Headers) { 356 // Test headers with multiple ports. 357 verifyFormat("module mh1\n" 358 " (input var int in1,\n" 359 " input var shortreal in2,\n" 360 " output tagged_st out);\n" 361 "endmodule"); 362 // Ports should be grouped by types. 363 verifyFormat("module test\n" 364 " (input [7 : 0] a,\n" 365 " input signed [7 : 0] b, c, d);\n" 366 "endmodule"); 367 verifyFormat("module test\n" 368 " (input [7 : 0] a,\n" 369 " (* x = x *) input signed [7 : 0] b, c, d);\n" 370 "endmodule"); 371 verifyFormat("module test\n" 372 " (input [7 : 0] a = 0,\n" 373 " input signed [7 : 0] b = 0, c = 0, d = 0);\n" 374 "endmodule"); 375 verifyFormat("module test\n" 376 " #(parameter x)\n" 377 " (input [7 : 0] a,\n" 378 " input signed [7 : 0] b, c, d);\n" 379 "endmodule"); 380 // When a line needs to be broken, ports of the same type should be aligned to 381 // the same column. 382 verifyFormat("module test\n" 383 " (input signed [7 : 0] b, c, //\n" 384 " d);\n" 385 "endmodule"); 386 verifyFormat("module test\n" 387 " ((* x = x *) input signed [7 : 0] b, c, //\n" 388 " d);\n" 389 "endmodule"); 390 verifyFormat("module test\n" 391 " (input signed [7 : 0] b = 0, c, //\n" 392 " d);\n" 393 "endmodule"); 394 verifyFormat("module test\n" 395 " (input signed [7 : 0] b, c = 0, //\n" 396 " d);\n" 397 "endmodule"); 398 verifyFormat("module test\n" 399 " (input signed [7 : 0] b, c, //\n" 400 " d = 0);\n" 401 "endmodule"); 402 verifyFormat("module test\n" 403 " (input wire logic signed [7 : 0][0 : 1] b, c, //\n" 404 " d);\n" 405 "endmodule"); 406 verifyFormat("module test\n" 407 " (input signed [7 : 0] b, //\n" 408 " c, //\n" 409 " d);\n" 410 "endmodule"); 411 verifyFormat("module test\n" 412 " (input [7 : 0] a,\n" 413 " input signed [7 : 0] b, //\n" 414 " c, //\n" 415 " d);\n" 416 "endmodule"); 417 verifyFormat("module test\n" 418 " (input signed [7 : 0] b, //\n" 419 " c, //\n" 420 " d,\n" 421 " output signed [7 : 0] h);\n" 422 "endmodule"); 423 // With a modport. 424 verifyFormat("module m\n" 425 " (i2.master i);\n" 426 "endmodule"); 427 verifyFormat("module m\n" 428 " (i2.master i, ii);\n" 429 "endmodule"); 430 verifyFormat("module m\n" 431 " (i2.master i, //\n" 432 " ii);\n" 433 "endmodule"); 434 verifyFormat("module m\n" 435 " (i2.master i,\n" 436 " input ii);\n" 437 "endmodule"); 438 verifyFormat("module m\n" 439 " (i2::i2.master i);\n" 440 "endmodule"); 441 verifyFormat("module m\n" 442 " (i2::i2.master i, ii);\n" 443 "endmodule"); 444 verifyFormat("module m\n" 445 " (i2::i2.master i, //\n" 446 " ii);\n" 447 "endmodule"); 448 verifyFormat("module m\n" 449 " (i2::i2.master i,\n" 450 " input ii);\n" 451 "endmodule"); 452 verifyFormat("module m\n" 453 " (i2::i2 i);\n" 454 "endmodule"); 455 verifyFormat("module m\n" 456 " (i2::i2 i, ii);\n" 457 "endmodule"); 458 verifyFormat("module m\n" 459 " (i2::i2 i, //\n" 460 " ii);\n" 461 "endmodule"); 462 verifyFormat("module m\n" 463 " (i2::i2 i,\n" 464 " input ii);\n" 465 "endmodule"); 466 // With a macro in the names. 467 verifyFormat("module m\n" 468 " (input var `x a, b);\n" 469 "endmodule"); 470 verifyFormat("module m\n" 471 " (input var `x a, //\n" 472 " b);\n" 473 "endmodule"); 474 verifyFormat("module m\n" 475 " (input var x `a, b);\n" 476 "endmodule"); 477 verifyFormat("module m\n" 478 " (input var x `a, //\n" 479 " b);\n" 480 "endmodule"); 481 // With a concatenation in the names. 482 auto Style = getDefaultStyle(); 483 Style.ColumnLimit = 40; 484 verifyFormat("`define X(x) \\\n" 485 " module test \\\n" 486 " (input var x``x a, b);", 487 Style); 488 verifyFormat("`define X(x) \\\n" 489 " module test \\\n" 490 " (input var x``x aaaaaaaaaaaaaaa, \\\n" 491 " b);", 492 Style); 493 verifyFormat("`define X(x) \\\n" 494 " module test \\\n" 495 " (input var x a``x, b);", 496 Style); 497 verifyFormat("`define X(x) \\\n" 498 " module test \\\n" 499 " (input var x aaaaaaaaaaaaaaa``x, \\\n" 500 " b);", 501 Style); 502 } 503 504 TEST_F(FormatTestVerilog, Hierarchy) { 505 verifyFormat("module x;\n" 506 "endmodule"); 507 // Test that the end label is on the same line as the end keyword. 508 verifyFormat("module x;\n" 509 "endmodule : x"); 510 // Test that things inside are indented. 511 verifyFormat("module x;\n" 512 " generate\n" 513 " endgenerate\n" 514 "endmodule"); 515 verifyFormat("program x;\n" 516 " generate\n" 517 " endgenerate\n" 518 "endprogram"); 519 verifyFormat("interface x;\n" 520 " generate\n" 521 " endgenerate\n" 522 "endinterface"); 523 verifyFormat("task x;\n" 524 " generate\n" 525 " endgenerate\n" 526 "endtask"); 527 verifyFormat("function x;\n" 528 " generate\n" 529 " endgenerate\n" 530 "endfunction"); 531 verifyFormat("class x;\n" 532 " generate\n" 533 " endgenerate\n" 534 "endclass"); 535 // Test that they nest. 536 verifyFormat("module x;\n" 537 " program x;\n" 538 " program x;\n" 539 " endprogram\n" 540 " endprogram\n" 541 "endmodule"); 542 // Test that an extern declaration doesn't change the indentation. 543 verifyFormat("extern module x;\n" 544 "x = x;"); 545 // Test complex headers 546 verifyFormat("extern module x\n" 547 " import x.x::x::*;\n" 548 " import x;\n" 549 " #(parameter x)\n" 550 " (output x);"); 551 verifyFormat("module x\n" 552 " import x.x::x::*;\n" 553 " import x;\n" 554 " #(parameter x)\n" 555 " (output x);\n" 556 " generate\n" 557 " endgenerate\n" 558 "endmodule : x"); 559 verifyFormat("virtual class x\n" 560 " (x)\n" 561 " extends x(x)\n" 562 " implements x, x, x;\n" 563 " generate\n" 564 " endgenerate\n" 565 "endclass : x\n"); 566 verifyFormat("function automatic logic [1 : 0] x\n" 567 " (input x);\n" 568 " generate\n" 569 " endgenerate\n" 570 "endfunction : x"); 571 } 572 573 TEST_F(FormatTestVerilog, Identifiers) { 574 // Escaped identifiers should not be split. 575 verifyFormat("\\busa+index"); 576 verifyFormat("\\-clock"); 577 verifyFormat("\\***error-condition***"); 578 verifyFormat("\\net1\\/net2"); 579 verifyFormat("\\{a,b}"); 580 verifyFormat("\\a*(b+c)"); 581 // Escaped identifiers can't be joined with the next token. Extra space 582 // should be removed. 583 verifyFormat("\\busa+index ;", "\\busa+index\n" 584 ";"); 585 verifyFormat("\\busa+index ;", "\\busa+index\r\n" 586 ";"); 587 verifyFormat("\\busa+index ;", "\\busa+index ;"); 588 verifyFormat("\\busa+index ;", "\\busa+index\n" 589 " ;"); 590 verifyFormat("\\busa+index ;"); 591 verifyFormat("(\\busa+index );"); 592 verifyFormat("\\busa+index \\busa+index ;"); 593 } 594 595 TEST_F(FormatTestVerilog, If) { 596 verifyFormat("if (x)\n" 597 " x = x;"); 598 verifyFormat("unique if (x)\n" 599 " x = x;"); 600 verifyFormat("unique0 if (x)\n" 601 " x = x;"); 602 verifyFormat("priority if (x)\n" 603 " x = x;"); 604 verifyFormat("if (x)\n" 605 " x = x;\n" 606 "x = x;"); 607 608 // Test else 609 verifyFormat("if (x)\n" 610 " x = x;\n" 611 "else if (x)\n" 612 " x = x;\n" 613 "else\n" 614 " x = x;"); 615 verifyFormat("if (x) begin\n" 616 " x = x;\n" 617 "end else if (x) begin\n" 618 " x = x;\n" 619 "end else begin\n" 620 " x = x;\n" 621 "end"); 622 verifyFormat("if (x) begin : x\n" 623 " x = x;\n" 624 "end : x else if (x) begin : x\n" 625 " x = x;\n" 626 "end : x else begin : x\n" 627 " x = x;\n" 628 "end : x"); 629 630 // Test block keywords. 631 verifyFormat("if (x) begin\n" 632 " x = x;\n" 633 "end"); 634 verifyFormat("if (x) begin : x\n" 635 " x = x;\n" 636 "end : x"); 637 verifyFormat("if (x) begin\n" 638 " x = x;\n" 639 " x = x;\n" 640 "end"); 641 verifyFormat("if (x) fork\n" 642 " x = x;\n" 643 "join"); 644 verifyFormat("if (x) fork\n" 645 " x = x;\n" 646 "join_any"); 647 verifyFormat("if (x) fork\n" 648 " x = x;\n" 649 "join_none"); 650 verifyFormat("if (x) generate\n" 651 " x = x;\n" 652 "endgenerate"); 653 verifyFormat("if (x) generate : x\n" 654 " x = x;\n" 655 "endgenerate : x"); 656 657 // Test that concatenation braces don't get regarded as blocks. 658 verifyFormat("if (x)\n" 659 " {x} = x;"); 660 verifyFormat("if (x)\n" 661 " x = {x};"); 662 verifyFormat("if (x)\n" 663 " x = {x};\n" 664 "else\n" 665 " {x} = {x};"); 666 667 // With attributes. 668 verifyFormat("(* x *) if (x)\n" 669 " x = x;"); 670 verifyFormat("(* x = \"x\" *) if (x)\n" 671 " x = x;"); 672 verifyFormat("(* x, x = \"x\" *) if (x)\n" 673 " x = x;"); 674 } 675 676 TEST_F(FormatTestVerilog, Instantiation) { 677 // Without ports. 678 verifyFormat("ffnand ff1;"); 679 // With named ports. 680 verifyFormat("ffnand ff1(.qbar(out1),\n" 681 " .clear(in1),\n" 682 " .preset(in2));"); 683 // With wildcard. 684 verifyFormat("ffnand ff1(.qbar(out1),\n" 685 " .clear(in1),\n" 686 " .preset(in2),\n" 687 " .*);"); 688 verifyFormat("ffnand ff1(.*,\n" 689 " .qbar(out1),\n" 690 " .clear(in1),\n" 691 " .preset(in2));"); 692 // With unconnected ports. 693 verifyFormat("ffnand ff1(.q(),\n" 694 " .qbar(out1),\n" 695 " .clear(in1),\n" 696 " .preset(in2));"); 697 verifyFormat("ffnand ff1(.q(),\n" 698 " .qbar(),\n" 699 " .clear(),\n" 700 " .preset());"); 701 verifyFormat("ffnand ff1(,\n" 702 " .qbar(out1),\n" 703 " .clear(in1),\n" 704 " .preset(in2));"); 705 // With positional ports. 706 verifyFormat("ffnand ff1(out1,\n" 707 " in1,\n" 708 " in2);"); 709 verifyFormat("ffnand ff1(,\n" 710 " out1,\n" 711 " in1,\n" 712 " in2);"); 713 // Multiple instantiations. 714 verifyFormat("ffnand ff1(.q(),\n" 715 " .qbar(out1),\n" 716 " .clear(in1),\n" 717 " .preset(in2)),\n" 718 " ff1(.q(),\n" 719 " .qbar(out1),\n" 720 " .clear(in1),\n" 721 " .preset(in2));"); 722 verifyFormat("ffnand //\n" 723 " ff1(.q(),\n" 724 " .qbar(out1),\n" 725 " .clear(in1),\n" 726 " .preset(in2)),\n" 727 " ff1(.q(),\n" 728 " .qbar(out1),\n" 729 " .clear(in1),\n" 730 " .preset(in2));"); 731 // With breaking between instance ports disabled. 732 auto Style = getDefaultStyle(); 733 Style.VerilogBreakBetweenInstancePorts = false; 734 verifyFormat("ffnand ff1;", Style); 735 verifyFormat("ffnand ff1(.qbar(out1), .clear(in1), .preset(in2), .*);", 736 Style); 737 verifyFormat("ffnand ff1(out1, in1, in2);", Style); 738 verifyFormat("ffnand ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n" 739 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));", 740 Style); 741 verifyFormat("ffnand //\n" 742 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2)),\n" 743 " ff1(.q(), .qbar(out1), .clear(in1), .preset(in2));", 744 Style); 745 } 746 747 TEST_F(FormatTestVerilog, Operators) { 748 // Test that unary operators are not followed by space. 749 verifyFormat("x = +x;"); 750 verifyFormat("x = -x;"); 751 verifyFormat("x = !x;"); 752 verifyFormat("x = ~x;"); 753 verifyFormat("x = &x;"); 754 verifyFormat("x = ~&x;"); 755 verifyFormat("x = |x;"); 756 verifyFormat("x = ~|x;"); 757 verifyFormat("x = ^x;"); 758 verifyFormat("x = ~^x;"); 759 verifyFormat("x = ^~x;"); 760 verifyFormat("x = ++x;"); 761 verifyFormat("x = --x;"); 762 763 // Test that `*` and `*>` are binary. 764 verifyFormat("x = x * x;"); 765 verifyFormat("x = (x * x);"); 766 verifyFormat("(opcode *> o1) = 6.1;"); 767 verifyFormat("(C, D *> Q) = 18;"); 768 // The wildcard import is not a binary operator. 769 verifyFormat("import p::*;"); 770 771 // Test that operators don't get split. 772 verifyFormat("x = x++;"); 773 verifyFormat("x = x--;"); 774 verifyFormat("x = x ** x;"); 775 verifyFormat("x = x << x;"); 776 verifyFormat("x = x >> x;"); 777 verifyFormat("x = x <<< x;"); 778 verifyFormat("x = x >>> x;"); 779 verifyFormat("x = x <= x;"); 780 verifyFormat("x = x >= x;"); 781 verifyFormat("x = x == x;"); 782 verifyFormat("x = x != x;"); 783 verifyFormat("x = x === x;"); 784 verifyFormat("x = x !== x;"); 785 verifyFormat("x = x ==? x;"); 786 verifyFormat("x = x !=? x;"); 787 verifyFormat("x = x ~^ x;"); 788 verifyFormat("x = x ^~ x;"); 789 verifyFormat("x = x && x;"); 790 verifyFormat("x = x || x;"); 791 verifyFormat("x = x->x;"); 792 verifyFormat("x = x <-> x;"); 793 verifyFormat("x += x;"); 794 verifyFormat("x -= x;"); 795 verifyFormat("x *= x;"); 796 verifyFormat("x /= x;"); 797 verifyFormat("x %= x;"); 798 verifyFormat("x &= x;"); 799 verifyFormat("x ^= x;"); 800 verifyFormat("x |= x;"); 801 verifyFormat("x <<= x;"); 802 verifyFormat("x >>= x;"); 803 verifyFormat("x <<<= x;"); 804 verifyFormat("x >>>= x;"); 805 verifyFormat("x <= x;"); 806 807 // Test that space is added between operators. 808 verifyFormat("x = x < -x;", "x=x<-x;"); 809 verifyFormat("x = x << -x;", "x=x<<-x;"); 810 verifyFormat("x = x <<< -x;", "x=x<<<-x;"); 811 812 // Test that operators that are C++ identifiers get treated as operators. 813 verifyFormat("solve s before d;"); // before 814 verifyFormat("binsof(i) intersect {0};"); // intersect 815 verifyFormat("req dist {1};"); // dist 816 verifyFormat("a inside {b, c};"); // inside 817 verifyFormat("bus.randomize() with { atype == low; };"); // with 818 } 819 820 TEST_F(FormatTestVerilog, Preprocessor) { 821 auto Style = getDefaultStyle(); 822 Style.ColumnLimit = 20; 823 824 // Macro definitions. 825 verifyFormat("`define X \\\n" 826 " if (x) \\\n" 827 " x = x;", 828 "`define X if(x)x=x;", Style); 829 verifyFormat("`define X(x) \\\n" 830 " if (x) \\\n" 831 " x = x;", 832 "`define X(x) if(x)x=x;", Style); 833 verifyFormat("`define X \\\n" 834 " x = x; \\\n" 835 " x = x;", 836 "`define X x=x;x=x;", Style); 837 // Macro definitions with invocations inside. 838 verifyFormat("`define LIST \\\n" 839 " `ENTRY \\\n" 840 " `ENTRY", 841 "`define LIST \\\n" 842 "`ENTRY \\\n" 843 "`ENTRY", 844 Style); 845 verifyFormat("`define LIST \\\n" 846 " `x = `x; \\\n" 847 " `x = `x;", 848 "`define LIST \\\n" 849 "`x = `x; \\\n" 850 "`x = `x;", 851 Style); 852 verifyFormat("`define LIST \\\n" 853 " `x = `x; \\\n" 854 " `x = `x;", 855 "`define LIST `x=`x;`x=`x;", Style); 856 // Macro invocations. 857 verifyFormat("`x = (`x1 + `x2 + x);"); 858 // Lines starting with a preprocessor directive should not be indented. 859 std::string Directives[] = { 860 "begin_keywords", 861 "celldefine", 862 "default_nettype", 863 "define", 864 "else", 865 "elsif", 866 "end_keywords", 867 "endcelldefine", 868 "endif", 869 "ifdef", 870 "ifndef", 871 "include", 872 "line", 873 "nounconnected_drive", 874 "pragma", 875 "resetall", 876 "timescale", 877 "unconnected_drive", 878 "undef", 879 "undefineall", 880 }; 881 for (auto &Name : Directives) { 882 verifyFormat("if (x)\n" 883 "`" + 884 Name + 885 "\n" 886 " ;", 887 "if (x)\n" 888 "`" + 889 Name + 890 "\n" 891 ";", 892 Style); 893 } 894 // Lines starting with a regular macro invocation should be indented as a 895 // normal line. 896 verifyFormat("if (x)\n" 897 " `x = `x;\n" 898 "`timescale 1ns / 1ps", 899 "if (x)\n" 900 "`x = `x;\n" 901 "`timescale 1ns / 1ps", 902 Style); 903 verifyFormat("if (x)\n" 904 "`timescale 1ns / 1ps\n" 905 " `x = `x;", 906 "if (x)\n" 907 "`timescale 1ns / 1ps\n" 908 "`x = `x;", 909 Style); 910 std::string NonDirectives[] = { 911 // For `__FILE__` and `__LINE__`, although the standard classifies them as 912 // preprocessor directives, they are used like regular macros. 913 "__FILE__", "__LINE__", "elif", "foo", "x", 914 }; 915 for (auto &Name : NonDirectives) { 916 verifyFormat("if (x)\n" 917 " `" + 918 Name + ";", 919 "if (x)\n" 920 "`" + 921 Name + 922 "\n" 923 ";", 924 Style); 925 } 926 } 927 928 TEST_F(FormatTestVerilog, Primitive) { 929 verifyFormat("primitive multiplexer\n" 930 " (mux, control, dataA, dataB);\n" 931 " output mux;\n" 932 " input control, dataA, dataB;\n" 933 " table\n" 934 " 0 1 ? : 1;\n" 935 " 0 0 ? : 0;\n" 936 " 1 ? 1 : 1;\n" 937 " 1 ? 0 : 0;\n" 938 " x 0 0 : 0;\n" 939 " x 1 1 : 1;\n" 940 " endtable\n" 941 "endprimitive"); 942 verifyFormat("primitive latch\n" 943 " (q, ena_, data);\n" 944 " output q;\n" 945 " reg q;\n" 946 " input ena_, data;\n" 947 " table\n" 948 " 0 1 : ? : 1;\n" 949 " 0 0 : ? : 0;\n" 950 " 1 ? : ? : -;\n" 951 " ? * : ? : -;\n" 952 " endtable\n" 953 "endprimitive"); 954 verifyFormat("primitive d\n" 955 " (q, clock, data);\n" 956 " output q;\n" 957 " reg q;\n" 958 " input clock, data;\n" 959 " table\n" 960 " (01) 0 : ? : 0;\n" 961 " (01) 1 : ? : 1;\n" 962 " (0?) 1 : 1 : 1;\n" 963 " (0?) 0 : 0 : 0;\n" 964 " (?0) ? : ? : -;\n" 965 " (?\?) ? : ? : -;\n" 966 " endtable\n" 967 "endprimitive"); 968 } 969 970 TEST_F(FormatTestVerilog, Streaming) { 971 verifyFormat("x = {>>{j}};"); 972 verifyFormat("x = {>>byte{j}};"); 973 verifyFormat("x = {<<{j}};"); 974 verifyFormat("x = {<<byte{j}};"); 975 verifyFormat("x = {<<16{j}};"); 976 verifyFormat("x = {<<{8'b0011_0101}};"); 977 verifyFormat("x = {<<4{6'b11_0101}};"); 978 verifyFormat("x = {>>4{6'b11_0101}};"); 979 verifyFormat("x = {<<2{{<<{4'b1101}}}};"); 980 verifyFormat("bit [96 : 1] y = {>>{a, b, c}};"); 981 verifyFormat("int j = {>>{a, b, c}};"); 982 verifyFormat("{>>{a, b, c}} = 23'b1;"); 983 verifyFormat("{>>{a, b, c}} = x;"); 984 verifyFormat("{>>{j}} = x;"); 985 verifyFormat("{>>byte{j}} = x;"); 986 verifyFormat("{<<{j}} = x;"); 987 verifyFormat("{<<byte{j}} = x;"); 988 } 989 990 TEST_F(FormatTestVerilog, StructLiteral) { 991 verifyFormat("c = '{0, 0.0};"); 992 verifyFormat("c = '{'{1, 1.0}, '{2, 2.0}};"); 993 verifyFormat("c = '{a: 0, b: 0.0};"); 994 verifyFormat("c = '{a: 0, b: 0.0, default: 0};"); 995 verifyFormat("c = ab'{a: 0, b: 0.0};"); 996 verifyFormat("c = ab'{cd: cd'{1, 1.0}, ef: ef'{2, 2.0}};"); 997 verifyFormat("c = ab'{cd'{1, 1.0}, ef'{2, 2.0}};"); 998 verifyFormat("d = {int: 1, shortreal: 1.0};"); 999 verifyFormat("d = ab'{int: 1, shortreal: 1.0};"); 1000 verifyFormat("c = '{default: 0};"); 1001 auto Style = getDefaultStyle(); 1002 Style.SpacesInContainerLiterals = true; 1003 verifyFormat("c = '{a : 0, b : 0.0};", Style); 1004 verifyFormat("c = '{a : 0, b : 0.0, default : 0};", Style); 1005 verifyFormat("c = ab'{a : 0, b : 0.0};", Style); 1006 verifyFormat("c = ab'{cd : cd'{1, 1.0}, ef : ef'{2, 2.0}};", Style); 1007 } 1008 1009 TEST_F(FormatTestVerilog, StructuredProcedure) { 1010 // Blocks should be indented correctly. 1011 verifyFormat("initial begin\n" 1012 "end"); 1013 verifyFormat("initial begin\n" 1014 " x <= x;\n" 1015 " x <= x;\n" 1016 "end"); 1017 verifyFormat("initial\n" 1018 " x <= x;\n" 1019 "x <= x;"); 1020 verifyFormat("always @(x) begin\n" 1021 "end"); 1022 verifyFormat("always @(x) begin\n" 1023 " x <= x;\n" 1024 " x <= x;\n" 1025 "end"); 1026 verifyFormat("always @(x)\n" 1027 " x <= x;\n" 1028 "x <= x;"); 1029 // Various keywords. 1030 verifyFormat("always @(x)\n" 1031 " x <= x;"); 1032 verifyFormat("always @(posedge x)\n" 1033 " x <= x;"); 1034 verifyFormat("always\n" 1035 " x <= x;"); 1036 verifyFormat("always @*\n" 1037 " x <= x;"); 1038 verifyFormat("always @(*)\n" 1039 " x <= x;"); 1040 verifyFormat("always_comb\n" 1041 " x <= x;"); 1042 verifyFormat("always_latch @(x)\n" 1043 " x <= x;"); 1044 verifyFormat("always_ff @(posedge x)\n" 1045 " x <= x;"); 1046 verifyFormat("initial\n" 1047 " x <= x;"); 1048 verifyFormat("final\n" 1049 " x <= x;"); 1050 verifyFormat("forever\n" 1051 " x <= x;"); 1052 } 1053 } // namespace 1054 } // namespace test 1055 } // namespace format 1056 } // namespace clang 1057