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 format(llvm::StringRef Code, const FormatStyle &Style) { 34 return format(Code, 0, Code.size(), Style); 35 } 36 37 static void verifyFormat( 38 llvm::StringRef Code, 39 const FormatStyle &Style = getLLVMStyle(FormatStyle::LK_Verilog)) { 40 EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable"; 41 EXPECT_EQ(Code.str(), 42 format(test::messUp(Code, /*HandleHash=*/false), Style)); 43 } 44 }; 45 46 TEST_F(FormatTestVerilog, If) { 47 verifyFormat("if (x)\n" 48 " x = x;"); 49 verifyFormat("if (x)\n" 50 " x = x;\n" 51 "x = x;"); 52 53 // Test else 54 verifyFormat("if (x)\n" 55 " x = x;\n" 56 "else if (x)\n" 57 " x = x;\n" 58 "else\n" 59 " x = x;"); 60 verifyFormat("if (x) begin\n" 61 " x = x;\n" 62 "end else if (x) begin\n" 63 " x = x;\n" 64 "end else begin\n" 65 " x = x;\n" 66 "end"); 67 verifyFormat("if (x) begin : x\n" 68 " x = x;\n" 69 "end : x else if (x) begin : x\n" 70 " x = x;\n" 71 "end : x else begin : x\n" 72 " x = x;\n" 73 "end : x"); 74 75 // Test block keywords. 76 verifyFormat("if (x) begin\n" 77 " x = x;\n" 78 "end"); 79 verifyFormat("if (x) begin : x\n" 80 " x = x;\n" 81 "end : x"); 82 verifyFormat("if (x) begin\n" 83 " x = x;\n" 84 " x = x;\n" 85 "end"); 86 verifyFormat("disable fork;\n" 87 "x = x;"); 88 verifyFormat("rand join x x;\n" 89 "x = x;"); 90 verifyFormat("if (x) fork\n" 91 " x = x;\n" 92 "join"); 93 verifyFormat("if (x) fork\n" 94 " x = x;\n" 95 "join_any"); 96 verifyFormat("if (x) fork\n" 97 " x = x;\n" 98 "join_none"); 99 verifyFormat("if (x) generate\n" 100 " x = x;\n" 101 "endgenerate"); 102 verifyFormat("if (x) generate : x\n" 103 " x = x;\n" 104 "endgenerate : x"); 105 106 // Test that concatenation braces don't get regarded as blocks. 107 verifyFormat("if (x)\n" 108 " {x} = x;"); 109 verifyFormat("if (x)\n" 110 " x = {x};"); 111 verifyFormat("if (x)\n" 112 " x = {x};\n" 113 "else\n" 114 " {x} = {x};"); 115 } 116 117 } // namespace format 118 } // end namespace clang 119