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