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, If) { 143 verifyFormat("if (x)\n" 144 " x = x;"); 145 verifyFormat("if (x)\n" 146 " x = x;\n" 147 "x = x;"); 148 149 // Test else 150 verifyFormat("if (x)\n" 151 " x = x;\n" 152 "else if (x)\n" 153 " x = x;\n" 154 "else\n" 155 " x = x;"); 156 verifyFormat("if (x) begin\n" 157 " x = x;\n" 158 "end else if (x) begin\n" 159 " x = x;\n" 160 "end else begin\n" 161 " x = x;\n" 162 "end"); 163 verifyFormat("if (x) begin : x\n" 164 " x = x;\n" 165 "end : x else if (x) begin : x\n" 166 " x = x;\n" 167 "end : x else begin : x\n" 168 " x = x;\n" 169 "end : x"); 170 171 // Test block keywords. 172 verifyFormat("if (x) begin\n" 173 " x = x;\n" 174 "end"); 175 verifyFormat("if (x) begin : x\n" 176 " x = x;\n" 177 "end : x"); 178 verifyFormat("if (x) begin\n" 179 " x = x;\n" 180 " x = x;\n" 181 "end"); 182 verifyFormat("if (x) fork\n" 183 " x = x;\n" 184 "join"); 185 verifyFormat("if (x) fork\n" 186 " x = x;\n" 187 "join_any"); 188 verifyFormat("if (x) fork\n" 189 " x = x;\n" 190 "join_none"); 191 verifyFormat("if (x) generate\n" 192 " x = x;\n" 193 "endgenerate"); 194 verifyFormat("if (x) generate : x\n" 195 " x = x;\n" 196 "endgenerate : x"); 197 198 // Test that concatenation braces don't get regarded as blocks. 199 verifyFormat("if (x)\n" 200 " {x} = x;"); 201 verifyFormat("if (x)\n" 202 " x = {x};"); 203 verifyFormat("if (x)\n" 204 " x = {x};\n" 205 "else\n" 206 " {x} = {x};"); 207 } 208 209 TEST_F(FormatTestVerilog, Operators) { 210 // Test that unary operators are not followed by space. 211 verifyFormat("x = +x;"); 212 verifyFormat("x = -x;"); 213 verifyFormat("x = !x;"); 214 verifyFormat("x = ~x;"); 215 verifyFormat("x = &x;"); 216 verifyFormat("x = ~&x;"); 217 verifyFormat("x = |x;"); 218 verifyFormat("x = ~|x;"); 219 verifyFormat("x = ^x;"); 220 verifyFormat("x = ~^x;"); 221 verifyFormat("x = ^~x;"); 222 verifyFormat("x = ++x;"); 223 verifyFormat("x = --x;"); 224 225 // Test that operators don't get split. 226 verifyFormat("x = x++;"); 227 verifyFormat("x = x--;"); 228 verifyFormat("x = x ** x;"); 229 verifyFormat("x = x << x;"); 230 verifyFormat("x = x >> x;"); 231 verifyFormat("x = x <<< x;"); 232 verifyFormat("x = x >>> x;"); 233 verifyFormat("x = x <= x;"); 234 verifyFormat("x = x >= x;"); 235 verifyFormat("x = x == x;"); 236 verifyFormat("x = x != x;"); 237 verifyFormat("x = x === x;"); 238 verifyFormat("x = x !== x;"); 239 verifyFormat("x = x ==? x;"); 240 verifyFormat("x = x !=? x;"); 241 verifyFormat("x = x ~^ x;"); 242 verifyFormat("x = x ^~ x;"); 243 verifyFormat("x = x && x;"); 244 verifyFormat("x = x || x;"); 245 verifyFormat("x = x->x;"); 246 verifyFormat("x = x <-> x;"); 247 verifyFormat("x += x;"); 248 verifyFormat("x -= x;"); 249 verifyFormat("x *= x;"); 250 verifyFormat("x /= x;"); 251 verifyFormat("x %= x;"); 252 verifyFormat("x &= x;"); 253 verifyFormat("x ^= x;"); 254 verifyFormat("x |= x;"); 255 verifyFormat("x <<= x;"); 256 verifyFormat("x >>= x;"); 257 verifyFormat("x <<<= x;"); 258 verifyFormat("x >>>= x;"); 259 verifyFormat("x <= x;"); 260 261 // Test that space is added between operators. 262 EXPECT_EQ("x = x < -x;", format("x=x<-x;")); 263 EXPECT_EQ("x = x << -x;", format("x=x<<-x;")); 264 EXPECT_EQ("x = x <<< -x;", format("x=x<<<-x;")); 265 } 266 267 TEST_F(FormatTestVerilog, Preprocessor) { 268 auto Style = getLLVMStyle(FormatStyle::LK_Verilog); 269 Style.ColumnLimit = 20; 270 271 // Macro definitions. 272 EXPECT_EQ("`define X \\\n" 273 " if (x) \\\n" 274 " x = x;", 275 format("`define X if(x)x=x;", Style)); 276 EXPECT_EQ("`define X(x) \\\n" 277 " if (x) \\\n" 278 " x = x;", 279 format("`define X(x) if(x)x=x;", Style)); 280 EXPECT_EQ("`define X \\\n" 281 " x = x; \\\n" 282 " x = x;", 283 format("`define X x=x;x=x;", Style)); 284 // Macro definitions with invocations inside. 285 EXPECT_EQ("`define LIST \\\n" 286 " `ENTRY \\\n" 287 " `ENTRY", 288 format("`define LIST \\\n" 289 "`ENTRY \\\n" 290 "`ENTRY", 291 Style)); 292 EXPECT_EQ("`define LIST \\\n" 293 " `x = `x; \\\n" 294 " `x = `x;", 295 format("`define LIST \\\n" 296 "`x = `x; \\\n" 297 "`x = `x;", 298 Style)); 299 EXPECT_EQ("`define LIST \\\n" 300 " `x = `x; \\\n" 301 " `x = `x;", 302 format("`define LIST `x=`x;`x=`x;", Style)); 303 // Macro invocations. 304 verifyFormat("`x = (`x1 + `x2 + x);"); 305 // Lines starting with a preprocessor directive should not be indented. 306 std::string Directives[] = { 307 "begin_keywords", 308 "celldefine", 309 "default_nettype", 310 "define", 311 "else", 312 "elsif", 313 "end_keywords", 314 "endcelldefine", 315 "endif", 316 "ifdef", 317 "ifndef", 318 "include", 319 "line", 320 "nounconnected_drive", 321 "pragma", 322 "resetall", 323 "timescale", 324 "unconnected_drive", 325 "undef", 326 "undefineall", 327 }; 328 for (auto &Name : Directives) { 329 EXPECT_EQ("if (x)\n" 330 "`" + 331 Name + 332 "\n" 333 " ;", 334 format("if (x)\n" 335 "`" + 336 Name + 337 "\n" 338 ";", 339 Style)); 340 } 341 // Lines starting with a regular macro invocation should be indented as a 342 // normal line. 343 EXPECT_EQ("if (x)\n" 344 " `x = `x;\n" 345 "`timescale 1ns / 1ps", 346 format("if (x)\n" 347 "`x = `x;\n" 348 "`timescale 1ns / 1ps", 349 Style)); 350 EXPECT_EQ("if (x)\n" 351 "`timescale 1ns / 1ps\n" 352 " `x = `x;", 353 format("if (x)\n" 354 "`timescale 1ns / 1ps\n" 355 "`x = `x;", 356 Style)); 357 std::string NonDirectives[] = { 358 // For `__FILE__` and `__LINE__`, although the standard classifies them as 359 // preprocessor directives, they are used like regular macros. 360 "__FILE__", "__LINE__", "elif", "foo", "x", 361 }; 362 for (auto &Name : NonDirectives) { 363 EXPECT_EQ("if (x)\n" 364 " `" + 365 Name + ";", 366 format("if (x)\n" 367 "`" + 368 Name + 369 "\n" 370 ";", 371 Style)); 372 } 373 } 374 375 } // namespace format 376 } // end namespace clang 377