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