//===- unittest/Format/FormatTestVerilog.cpp ------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "FormatTestUtils.h" #include "clang/Format/Format.h" #include "llvm/Support/Debug.h" #include "gtest/gtest.h" #define DEBUG_TYPE "format-test" namespace clang { namespace format { class FormatTestVerilog : public ::testing::Test { protected: static std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, const FormatStyle &Style) { LLVM_DEBUG(llvm::errs() << "---\n"); LLVM_DEBUG(llvm::errs() << Code << "\n\n"); std::vector Ranges(1, tooling::Range(Offset, Length)); tooling::Replacements Replaces = reformat(Style, Code, Ranges); auto Result = applyAllReplacements(Code, Replaces); EXPECT_TRUE(static_cast(Result)); LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); return *Result; } static std::string format(llvm::StringRef Code, const FormatStyle &Style) { return format(Code, 0, Code.size(), Style); } static void verifyFormat( llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle(FormatStyle::LK_Verilog)) { EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable"; EXPECT_EQ(Code.str(), format(test::messUp(Code, /*HandleHash=*/false), Style)); } }; TEST_F(FormatTestVerilog, If) { verifyFormat("if (x)\n" " x = x;"); verifyFormat("if (x)\n" " x = x;\n" "x = x;"); // Test else verifyFormat("if (x)\n" " x = x;\n" "else if (x)\n" " x = x;\n" "else\n" " x = x;"); verifyFormat("if (x) begin\n" " x = x;\n" "end else if (x) begin\n" " x = x;\n" "end else begin\n" " x = x;\n" "end"); verifyFormat("if (x) begin : x\n" " x = x;\n" "end : x else if (x) begin : x\n" " x = x;\n" "end : x else begin : x\n" " x = x;\n" "end : x"); // Test block keywords. verifyFormat("if (x) begin\n" " x = x;\n" "end"); verifyFormat("if (x) begin : x\n" " x = x;\n" "end : x"); verifyFormat("if (x) begin\n" " x = x;\n" " x = x;\n" "end"); verifyFormat("disable fork;\n" "x = x;"); verifyFormat("rand join x x;\n" "x = x;"); verifyFormat("if (x) fork\n" " x = x;\n" "join"); verifyFormat("if (x) fork\n" " x = x;\n" "join_any"); verifyFormat("if (x) fork\n" " x = x;\n" "join_none"); verifyFormat("if (x) generate\n" " x = x;\n" "endgenerate"); verifyFormat("if (x) generate : x\n" " x = x;\n" "endgenerate : x"); // Test that concatenation braces don't get regarded as blocks. verifyFormat("if (x)\n" " {x} = x;"); verifyFormat("if (x)\n" " x = {x};"); verifyFormat("if (x)\n" " x = {x};\n" "else\n" " {x} = {x};"); } } // namespace format } // end namespace clang