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 "FormatTestUtils.h" 10 #include "clang/Format/Format.h" 11 #include "llvm/Support/Debug.h" 12 #include "gtest/gtest.h" 13 14 #define DEBUG_TYPE "format-test" 15 16 namespace clang { 17 namespace format { 18 19 class FormatTestVerilog : public ::testing::Test { 20 protected: 21 static std::string format(llvm::StringRef Code, unsigned Offset, 22 unsigned Length, const FormatStyle &Style) { 23 LLVM_DEBUG(llvm::errs() << "---\n"); 24 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 25 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 26 tooling::Replacements Replaces = reformat(Style, Code, Ranges); 27 auto Result = applyAllReplacements(Code, Replaces); 28 EXPECT_TRUE(static_cast<bool>(Result)); 29 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 30 return *Result; 31 } 32 33 static std::string 34 format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(FormatStyle::LK_Verilog)) { 36 return format(Code, 0, Code.size(), Style); 37 } 38 39 static void verifyFormat( 40 llvm::StringRef Code, 41 const FormatStyle &Style = getLLVMStyle(FormatStyle::LK_Verilog)) { 42 EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable"; 43 EXPECT_EQ(Code.str(), 44 format(test::messUp(Code, /*HandleHash=*/false), Style)); 45 } 46 }; 47 48 TEST_F(FormatTestVerilog, BasedLiteral) { 49 verifyFormat("x = '0;"); 50 verifyFormat("x = '1;"); 51 verifyFormat("x = 'X;"); 52 verifyFormat("x = 'x;"); 53 verifyFormat("x = 'Z;"); 54 verifyFormat("x = 'z;"); 55 verifyFormat("x = 659;"); 56 verifyFormat("x = 'h837ff;"); 57 verifyFormat("x = 'o7460;"); 58 verifyFormat("x = 4'b1001;"); 59 verifyFormat("x = 5'D3;"); 60 verifyFormat("x = 3'b01x;"); 61 verifyFormat("x = 12'hx;"); 62 verifyFormat("x = 16'hz;"); 63 verifyFormat("x = -8'd6;"); 64 verifyFormat("x = 4'shf;"); 65 verifyFormat("x = -4'sd15;"); 66 verifyFormat("x = 16'sd?;"); 67 } 68 69 TEST_F(FormatTestVerilog, Block) { 70 verifyFormat("begin\n" 71 " x = x;\n" 72 "end"); 73 verifyFormat("begin : x\n" 74 " x = x;\n" 75 "end : x"); 76 verifyFormat("begin\n" 77 " x = x;\n" 78 " x = x;\n" 79 "end"); 80 verifyFormat("fork\n" 81 " x = x;\n" 82 "join"); 83 verifyFormat("fork\n" 84 " x = x;\n" 85 "join_any"); 86 verifyFormat("fork\n" 87 " x = x;\n" 88 "join_none"); 89 verifyFormat("generate\n" 90 " x = x;\n" 91 "endgenerate"); 92 verifyFormat("generate : x\n" 93 " x = x;\n" 94 "endgenerate : x"); 95 // Nested blocks. 96 verifyFormat("begin\n" 97 " begin\n" 98 " end\n" 99 "end"); 100 verifyFormat("begin : x\n" 101 " begin\n" 102 " end\n" 103 "end : x"); 104 verifyFormat("begin : x\n" 105 " begin : x\n" 106 " end : x\n" 107 "end : x"); 108 verifyFormat("begin\n" 109 " begin : x\n" 110 " end : x\n" 111 "end"); 112 // Test that 'disable fork' and 'rand join' don't get mistaken as blocks. 113 verifyFormat("disable fork;\n" 114 "x = x;"); 115 verifyFormat("rand join x x;\n" 116 "x = x;"); 117 } 118 119 TEST_F(FormatTestVerilog, Delay) { 120 // Delay by the default unit. 121 verifyFormat("#0;"); 122 verifyFormat("#1;"); 123 verifyFormat("#10;"); 124 verifyFormat("#1.5;"); 125 // Explicit unit. 126 verifyFormat("#1fs;"); 127 verifyFormat("#1.5fs;"); 128 verifyFormat("#1ns;"); 129 verifyFormat("#1.5ns;"); 130 verifyFormat("#1us;"); 131 verifyFormat("#1.5us;"); 132 verifyFormat("#1ms;"); 133 verifyFormat("#1.5ms;"); 134 verifyFormat("#1s;"); 135 verifyFormat("#1.5s;"); 136 // The following expression should be on the same line. 137 verifyFormat("#1 x = x;"); 138 EXPECT_EQ("#1 x = x;", format("#1\n" 139 "x = x;")); 140 } 141 142 TEST_F(FormatTestVerilog, Hierarchy) { 143 verifyFormat("module x;\n" 144 "endmodule"); 145 // Test that the end label is on the same line as the end keyword. 146 verifyFormat("module x;\n" 147 "endmodule : x"); 148 // Test that things inside are indented. 149 verifyFormat("module x;\n" 150 " generate\n" 151 " endgenerate\n" 152 "endmodule"); 153 verifyFormat("program x;\n" 154 " generate\n" 155 " endgenerate\n" 156 "endprogram"); 157 verifyFormat("interface x;\n" 158 " generate\n" 159 " endgenerate\n" 160 "endinterface"); 161 verifyFormat("task x;\n" 162 " generate\n" 163 " endgenerate\n" 164 "endtask"); 165 verifyFormat("function x;\n" 166 " generate\n" 167 " endgenerate\n" 168 "endfunction"); 169 verifyFormat("class x;\n" 170 " generate\n" 171 " endgenerate\n" 172 "endclass"); 173 // Test that they nest. 174 verifyFormat("module x;\n" 175 " program x;\n" 176 " program x;\n" 177 " endprogram\n" 178 " endprogram\n" 179 "endmodule"); 180 // Test that an extern declaration doesn't change the indentation. 181 verifyFormat("extern module x;\n" 182 "x = x;"); 183 // Test complex headers 184 verifyFormat("extern module x\n" 185 " import x.x::x::*;\n" 186 " import x;\n" 187 " #(parameter x)\n" 188 " (output x);"); 189 verifyFormat("module x\n" 190 " import x.x::x::*;\n" 191 " import x;\n" 192 " #(parameter x)\n" 193 " (output x);\n" 194 " generate\n" 195 " endgenerate\n" 196 "endmodule : x"); 197 verifyFormat("virtual class x\n" 198 " (x)\n" 199 " extends x(x)\n" 200 " implements x, x, x;\n" 201 " generate\n" 202 " endgenerate\n" 203 "endclass : x\n"); 204 verifyFormat("function automatic logic [1 : 0] x\n" 205 " (input x);\n" 206 " generate\n" 207 " endgenerate\n" 208 "endfunction : x"); 209 } 210 211 TEST_F(FormatTestVerilog, If) { 212 verifyFormat("if (x)\n" 213 " x = x;"); 214 verifyFormat("if (x)\n" 215 " x = x;\n" 216 "x = x;"); 217 218 // Test else 219 verifyFormat("if (x)\n" 220 " x = x;\n" 221 "else if (x)\n" 222 " x = x;\n" 223 "else\n" 224 " x = x;"); 225 verifyFormat("if (x) begin\n" 226 " x = x;\n" 227 "end else if (x) begin\n" 228 " x = x;\n" 229 "end else begin\n" 230 " x = x;\n" 231 "end"); 232 verifyFormat("if (x) begin : x\n" 233 " x = x;\n" 234 "end : x else if (x) begin : x\n" 235 " x = x;\n" 236 "end : x else begin : x\n" 237 " x = x;\n" 238 "end : x"); 239 240 // Test block keywords. 241 verifyFormat("if (x) begin\n" 242 " x = x;\n" 243 "end"); 244 verifyFormat("if (x) begin : x\n" 245 " x = x;\n" 246 "end : x"); 247 verifyFormat("if (x) begin\n" 248 " x = x;\n" 249 " x = x;\n" 250 "end"); 251 verifyFormat("if (x) fork\n" 252 " x = x;\n" 253 "join"); 254 verifyFormat("if (x) fork\n" 255 " x = x;\n" 256 "join_any"); 257 verifyFormat("if (x) fork\n" 258 " x = x;\n" 259 "join_none"); 260 verifyFormat("if (x) generate\n" 261 " x = x;\n" 262 "endgenerate"); 263 verifyFormat("if (x) generate : x\n" 264 " x = x;\n" 265 "endgenerate : x"); 266 267 // Test that concatenation braces don't get regarded as blocks. 268 verifyFormat("if (x)\n" 269 " {x} = x;"); 270 verifyFormat("if (x)\n" 271 " x = {x};"); 272 verifyFormat("if (x)\n" 273 " x = {x};\n" 274 "else\n" 275 " {x} = {x};"); 276 } 277 278 TEST_F(FormatTestVerilog, Operators) { 279 // Test that unary operators are not followed by space. 280 verifyFormat("x = +x;"); 281 verifyFormat("x = -x;"); 282 verifyFormat("x = !x;"); 283 verifyFormat("x = ~x;"); 284 verifyFormat("x = &x;"); 285 verifyFormat("x = ~&x;"); 286 verifyFormat("x = |x;"); 287 verifyFormat("x = ~|x;"); 288 verifyFormat("x = ^x;"); 289 verifyFormat("x = ~^x;"); 290 verifyFormat("x = ^~x;"); 291 verifyFormat("x = ++x;"); 292 verifyFormat("x = --x;"); 293 294 // Test that operators don't get split. 295 verifyFormat("x = x++;"); 296 verifyFormat("x = x--;"); 297 verifyFormat("x = x ** x;"); 298 verifyFormat("x = x << x;"); 299 verifyFormat("x = x >> x;"); 300 verifyFormat("x = x <<< x;"); 301 verifyFormat("x = x >>> x;"); 302 verifyFormat("x = x <= x;"); 303 verifyFormat("x = x >= x;"); 304 verifyFormat("x = x == x;"); 305 verifyFormat("x = x != x;"); 306 verifyFormat("x = x === x;"); 307 verifyFormat("x = x !== x;"); 308 verifyFormat("x = x ==? x;"); 309 verifyFormat("x = x !=? x;"); 310 verifyFormat("x = x ~^ x;"); 311 verifyFormat("x = x ^~ x;"); 312 verifyFormat("x = x && x;"); 313 verifyFormat("x = x || x;"); 314 verifyFormat("x = x->x;"); 315 verifyFormat("x = x <-> x;"); 316 verifyFormat("x += x;"); 317 verifyFormat("x -= x;"); 318 verifyFormat("x *= x;"); 319 verifyFormat("x /= x;"); 320 verifyFormat("x %= x;"); 321 verifyFormat("x &= x;"); 322 verifyFormat("x ^= x;"); 323 verifyFormat("x |= x;"); 324 verifyFormat("x <<= x;"); 325 verifyFormat("x >>= x;"); 326 verifyFormat("x <<<= x;"); 327 verifyFormat("x >>>= x;"); 328 verifyFormat("x <= x;"); 329 330 // Test that space is added between operators. 331 EXPECT_EQ("x = x < -x;", format("x=x<-x;")); 332 EXPECT_EQ("x = x << -x;", format("x=x<<-x;")); 333 EXPECT_EQ("x = x <<< -x;", format("x=x<<<-x;")); 334 } 335 336 TEST_F(FormatTestVerilog, Preprocessor) { 337 auto Style = getLLVMStyle(FormatStyle::LK_Verilog); 338 Style.ColumnLimit = 20; 339 340 // Macro definitions. 341 EXPECT_EQ("`define X \\\n" 342 " if (x) \\\n" 343 " x = x;", 344 format("`define X if(x)x=x;", Style)); 345 EXPECT_EQ("`define X(x) \\\n" 346 " if (x) \\\n" 347 " x = x;", 348 format("`define X(x) if(x)x=x;", Style)); 349 EXPECT_EQ("`define X \\\n" 350 " x = x; \\\n" 351 " x = x;", 352 format("`define X x=x;x=x;", Style)); 353 // Macro definitions with invocations inside. 354 EXPECT_EQ("`define LIST \\\n" 355 " `ENTRY \\\n" 356 " `ENTRY", 357 format("`define LIST \\\n" 358 "`ENTRY \\\n" 359 "`ENTRY", 360 Style)); 361 EXPECT_EQ("`define LIST \\\n" 362 " `x = `x; \\\n" 363 " `x = `x;", 364 format("`define LIST \\\n" 365 "`x = `x; \\\n" 366 "`x = `x;", 367 Style)); 368 EXPECT_EQ("`define LIST \\\n" 369 " `x = `x; \\\n" 370 " `x = `x;", 371 format("`define LIST `x=`x;`x=`x;", Style)); 372 // Macro invocations. 373 verifyFormat("`x = (`x1 + `x2 + x);"); 374 // Lines starting with a preprocessor directive should not be indented. 375 std::string Directives[] = { 376 "begin_keywords", 377 "celldefine", 378 "default_nettype", 379 "define", 380 "else", 381 "elsif", 382 "end_keywords", 383 "endcelldefine", 384 "endif", 385 "ifdef", 386 "ifndef", 387 "include", 388 "line", 389 "nounconnected_drive", 390 "pragma", 391 "resetall", 392 "timescale", 393 "unconnected_drive", 394 "undef", 395 "undefineall", 396 }; 397 for (auto &Name : Directives) { 398 EXPECT_EQ("if (x)\n" 399 "`" + 400 Name + 401 "\n" 402 " ;", 403 format("if (x)\n" 404 "`" + 405 Name + 406 "\n" 407 ";", 408 Style)); 409 } 410 // Lines starting with a regular macro invocation should be indented as a 411 // normal line. 412 EXPECT_EQ("if (x)\n" 413 " `x = `x;\n" 414 "`timescale 1ns / 1ps", 415 format("if (x)\n" 416 "`x = `x;\n" 417 "`timescale 1ns / 1ps", 418 Style)); 419 EXPECT_EQ("if (x)\n" 420 "`timescale 1ns / 1ps\n" 421 " `x = `x;", 422 format("if (x)\n" 423 "`timescale 1ns / 1ps\n" 424 "`x = `x;", 425 Style)); 426 std::string NonDirectives[] = { 427 // For `__FILE__` and `__LINE__`, although the standard classifies them as 428 // preprocessor directives, they are used like regular macros. 429 "__FILE__", "__LINE__", "elif", "foo", "x", 430 }; 431 for (auto &Name : NonDirectives) { 432 EXPECT_EQ("if (x)\n" 433 " `" + 434 Name + ";", 435 format("if (x)\n" 436 "`" + 437 Name + 438 "\n" 439 ";", 440 Style)); 441 } 442 } 443 444 TEST_F(FormatTestVerilog, Primitive) { 445 verifyFormat("primitive multiplexer\n" 446 " (mux, control, dataA, dataB);\n" 447 " output mux;\n" 448 " input control, dataA, dataB;\n" 449 " table\n" 450 " 0 1 ? : 1;\n" 451 " 0 0 ? : 0;\n" 452 " 1 ? 1 : 1;\n" 453 " 1 ? 0 : 0;\n" 454 " x 0 0 : 0;\n" 455 " x 1 1 : 1;\n" 456 " endtable\n" 457 "endprimitive"); 458 verifyFormat("primitive latch\n" 459 " (q, ena_, data);\n" 460 " output q;\n" 461 " reg q;\n" 462 " input ena_, data;\n" 463 " table\n" 464 " 0 1 : ? : 1;\n" 465 " 0 0 : ? : 0;\n" 466 " 1 ? : ? : -;\n" 467 " ? * : ? : -;\n" 468 " endtable\n" 469 "endprimitive"); 470 verifyFormat("primitive d\n" 471 " (q, clock, data);\n" 472 " output q;\n" 473 " reg q;\n" 474 " input clock, data;\n" 475 " table\n" 476 " (01) 0 : ? : 0;\n" 477 " (01) 1 : ? : 1;\n" 478 " (0?) 1 : 1 : 1;\n" 479 " (0?) 0 : 0 : 0;\n" 480 " (?0) ? : ? : -;\n" 481 " (?\?) ? : ? : -;\n" 482 " endtable\n" 483 "endprimitive"); 484 } 485 } // namespace format 486 } // end namespace clang 487