Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatter: lowRISC style guide testsuite #841

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/util/iterator_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ iterator_range<Iter> make_range(std::pair<Iter, Iter> p) {
return iterator_range<Iter>(std::move(p.first), std::move(p.second));
}

// Test if range is empty
template <typename Iter>
bool empty_range(Iter begin, Iter end) {
return begin == end;
}

} // namespace verible

#endif // VERIBLE_COMMON_UTIL_ITERATOR_RANGE_H_
63 changes: 63 additions & 0 deletions verilog/formatting/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ licenses(["notice"])
package(
default_visibility = [
"//verilog/tools/formatter:__pkg__",
"//verilog/tools/style_tester:__pkg__",
],
)

Expand Down Expand Up @@ -237,6 +238,68 @@ cc_library(
],
)

cc_library(
name = "lowrisc_format_style",
hdrs = ["lowrisc_format_style.h"],
deps = [
":format_style"
],
)

cc_library(
name = "formatter_lowrisc_style_test_cases",
srcs = ["formatter_lowrisc_style_test_cases.cc"],
hdrs = ["formatter_lowrisc_style_test_cases.h"],
deps = [
":lowrisc_format_style",
],
)

cc_test(
name = "formatter_lowrisc_style_test",
srcs = ["formatter_lowrisc_style_test.cc"],
deps = [
":format_style",
":lowrisc_format_style",
":formatter",
":style_compliance_report",
":formatter_lowrisc_style_test_cases",
"//common/formatting:align",
"//common/strings:position",
"//common/text:text_structure",
"//common/util:logging",
"//verilog/analysis:verilog_analyzer",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "style_compliance_report",
srcs = ["style_compliance_report.cc"],
hdrs = ["style_compliance_report.h"],
deps = [
"//common/util:logging",
"//common/formatting:basic_format_style",
"//common/util:spacer",
"//verilog/formatting:formatter",
"//verilog/formatting:lowrisc_format_style",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
)

cc_test(
name = "style_compliance_report_test",
srcs = ["style_compliance_report_test.cc"],
deps = [
":style_compliance_report",
"//common/util:logging",
"@com_google_googletest//:gtest_main",
],
)

cc_library(
name = "token_annotator",
srcs = ["token_annotator.cc"],
Expand Down
7 changes: 7 additions & 0 deletions verilog/formatting/format_style.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ struct FormatStyle : public verible::BasicFormatStyle {
enum_assignment_statement_alignment = policy;
distribution_items_alignment = policy;
}

absl::string_view StyleName() const {
return style_name_;
}

protected:
std::string style_name_ = "default";
};

} // namespace formatter
Expand Down
74 changes: 74 additions & 0 deletions verilog/formatting/formatter_lowrisc_style_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2017-2021 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "verilog/formatting/formatter.h"

#include <memory>
#include <sstream>
#include <string>
#include <vector>

#include <fstream>

#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "common/formatting/align.h"
#include "common/strings/position.h"
#include "common/text/text_structure.h"
#include "common/util/logging.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "verilog/analysis/verilog_analyzer.h"
#include "verilog/formatting/format_style.h"
#include "verilog/formatting/formatter_lowrisc_style_test_cases.h"

#undef EXPECT_OK
#define EXPECT_OK(value) EXPECT_TRUE((value).ok())

#undef ASSERT_OK
#define ASSERT_OK(value) ASSERT_TRUE((value).ok())

namespace verilog {
namespace formatter {

namespace {

using absl::StatusCode;
using verible::AlignmentPolicy;
using verible::IndentationStyle;
using verible::LineNumberSet;

TEST(FormatterLowRISCStyleTest, ComplianceTest) {
std::pair<const verilog::formatter::tests::ComplianceTestCase*,
size_t> kComplianceTestCases =
verilog::formatter::tests::GetLowRISCComplianceTestCases();

const auto* test_cases = ABSL_DIE_IF_NULL(std::get<0>(kComplianceTestCases));
size_t n = std::get<1>(kComplianceTestCases);

for (size_t i = 0 ; i < n ; ++i) {
std::ostringstream stream;

VLOG(1) << "code-to-format:\n" << test_cases[i].input << "<EOF>";
const auto status =
FormatVerilog(test_cases[i].input, "<filename>", test_cases[i].style, stream);
EXPECT_OK(status) << status.message();
EXPECT_EQ(stream.str(), test_cases[i].expected) << "code:\n" << test_cases[i].input;
}
}

} // namespace
} // namespace formatter
} // namespace verilog
Loading