Repository navigation
Expand file tree
/
Copy pathprove_quickcheck_main.cc
More file actions
173 lines (152 loc) · 6.51 KB
/
Copy pathprove_quickcheck_main.cc
File metadata and controls
173 lines (152 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2024 The XLS 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 <cstdlib>
#include <filesystem>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "re2/re2.h"
#include "xls/common/exit_status.h"
#include "xls/common/file/filesystem.h"
#include "xls/common/init_xls.h"
#include "xls/common/status/status_macros.h"
#include "xls/dslx/command_line_utils.h"
#include "xls/dslx/default_dslx_stdlib_path.h"
#include "xls/dslx/parse_and_typecheck.h"
#include "xls/dslx/run_routines/run_routines.h"
#include "xls/dslx/run_routines/test_xml.h"
#include "xls/dslx/warning_kind.h"
ABSL_FLAG(std::string, dslx_path, "",
"Additional paths to search for modules (colon delimited).");
ABSL_FLAG(std::string, dslx_stdlib_path,
std::string(xls::kDefaultDslxStdlibPath),
"Path to DSLX standard library directory.");
ABSL_FLAG(std::string, test_filter, "",
"Regexp that must be a full match of test name(s) to run.");
ABSL_FLAG(std::string, enable_warnings, "",
"Comma-delimited list of warnings to enable -- not generally "
"recommended, but can be used in exceptional circumstances");
ABSL_FLAG(std::string, disable_warnings, "",
"Comma-delimited list of warnings to disable -- not generally "
"recommended, but can be used in exceptional circumstances");
ABSL_FLAG(bool, warnings_as_errors, true,
"Whether to fail early, as an error, if warnings are detected");
ABSL_FLAG(int, solver_num_threads, 1,
"Maximum number of threads available to each individual solver "
"invocation.");
static constexpr std::string_view kUsage = R"(
Attempts to proves a single quickcheck property in a given module to be
infallible, or provide a counterexample.
)";
namespace xls::dslx {
namespace {
absl::StatusOr<TestResultData> RealMain(
std::string_view entry_module_path, std::string_view test_filter,
const std::filesystem::path& dslx_stdlib_path,
absl::Span<const std::filesystem::path> dslx_paths, bool warnings_as_errors,
std::optional<std::string_view> xml_output_file, int solver_num_threads) {
XLS_ASSIGN_OR_RETURN(
WarningKindSet warnings,
GetWarningsSetFromFlags(absl::GetFlag(FLAGS_enable_warnings),
absl::GetFlag(FLAGS_disable_warnings)));
XLS_ASSIGN_OR_RETURN(std::string program, GetFileContents(entry_module_path));
XLS_ASSIGN_OR_RETURN(std::string module_name, PathToName(entry_module_path));
std::optional<RE2> test_filter_re;
const RE2* test_filter_re_ptr = nullptr;
if (!test_filter.empty()) {
test_filter_re.emplace(test_filter);
if (!test_filter_re->ok()) {
return absl::InvalidArgumentError(absl::StrFormat(
"Could not create regular expression from test "
"filter: `%s`; error: %s; error argument: `%s`",
test_filter, test_filter_re->error(), test_filter_re->error_arg()));
}
test_filter_re_ptr = &test_filter_re.value();
}
const ParseAndTypecheckOptions parse_and_typecheck_options = {
.dslx_stdlib_path = dslx_stdlib_path,
.dslx_paths = dslx_paths,
.warnings_as_errors = warnings_as_errors,
.warnings = warnings,
};
const ParseAndProveOptions options = {
.parse_and_typecheck_options = parse_and_typecheck_options,
.test_filter = test_filter_re_ptr,
.solver_num_threads = solver_num_threads,
};
XLS_ASSIGN_OR_RETURN(
ParseAndProveResult result,
ParseAndProve(program, module_name, entry_module_path, options));
if (xml_output_file.has_value()) {
test_xml::TestSuites suites =
result.test_result_data.ToXmlSuites(module_name);
absl::TimeZone local_tz = absl::LocalTimeZone();
std::unique_ptr<test_xml::XmlNode> root = ToXml(suites, local_tz);
std::string contents = test_xml::XmlRootToString(*root);
XLS_RETURN_IF_ERROR(SetFileContents(xml_output_file.value(), contents));
}
return result.test_result_data;
}
} // namespace
} // namespace xls::dslx
int main(int argc, char** argv) {
std::vector<std::string_view> positional_arguments =
xls::InitXls(kUsage, argc, argv);
if (positional_arguments.size() != 1) {
LOG(QFATAL) << "Wrong number of command-line arguments; got "
<< positional_arguments.size() << ": `"
<< absl::StrJoin(positional_arguments, " ") << "`; want "
<< argv[0] << " <input-file>";
}
std::string test_filter = absl::GetFlag(FLAGS_test_filter);
std::string dslx_path = absl::GetFlag(FLAGS_dslx_path);
std::vector<std::string> dslx_path_strs = absl::StrSplit(dslx_path, ':');
std::vector<std::filesystem::path> dslx_paths;
dslx_paths.reserve(dslx_path_strs.size());
for (const auto& path : dslx_path_strs) {
dslx_paths.push_back(std::filesystem::path(path));
}
// See https://bazel.build/reference/test-encyclopedia#initial-conditions
std::optional<std::string> xml_output_file;
if (const char* xml_output_file_env = getenv("XML_OUTPUT_FILE");
xml_output_file_env != nullptr &&
!std::string_view(xml_output_file_env).empty()) {
xml_output_file = xml_output_file_env;
}
bool warnings_as_errors = absl::GetFlag(FLAGS_warnings_as_errors);
int solver_num_threads = absl::GetFlag(FLAGS_solver_num_threads);
std::filesystem::path dslx_stdlib_path =
absl::GetFlag(FLAGS_dslx_stdlib_path);
absl::StatusOr<xls::dslx::TestResultData> test_result = xls::dslx::RealMain(
positional_arguments[0], test_filter, dslx_stdlib_path, dslx_paths,
warnings_as_errors, xml_output_file, solver_num_threads);
if (!test_result.ok()) {
return xls::ExitStatus(test_result.status());
}
if (test_result->result() != xls::dslx::TestResult::kAllPassed) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}