Skip to content

Commit 47c37d0

Browse files
authored
Refactor validator API to use enums (#1209)
* refactor validator API to use enums
1 parent 8283229 commit 47c37d0

File tree

7 files changed

+27
-13
lines changed

7 files changed

+27
-13
lines changed

scripts/test/support.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ def run_command(cmd, expected_status=0, stderr=None,
156156
print 'executing: ', ' '.join(cmd)
157157
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=stderr)
158158
out, err = proc.communicate()
159-
if proc.returncode != expected_status:
160-
raise Exception(('run_command failed', err))
159+
code = proc.returncode
160+
if code != expected_status:
161+
raise Exception(('run_command failed (%s)' % code, out + str(err or '')))
161162
err_correct = expected_err is None or \
162163
(expected_err in err if err_contains else expected_err == err)
163164
if not err_correct:

src/passes/pass.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ void PassRunner::run() {
194194
// for debug logging purposes, run each pass in full before running the other
195195
auto totalTime = std::chrono::duration<double>(0);
196196
size_t padding = 0;
197+
WasmValidator::Flags validationFlags = WasmValidator::Minimal;
198+
if (options.validateGlobally) {
199+
validationFlags = validationFlags | WasmValidator::Globally;
200+
}
197201
std::cerr << "[PassRunner] running passes..." << std::endl;
198202
for (auto pass : passes) {
199203
padding = std::max(padding, pass->name.size());
@@ -227,7 +231,7 @@ void PassRunner::run() {
227231
totalTime += diff;
228232
// validate, ignoring the time
229233
std::cerr << "[PassRunner] (validating)\n";
230-
if (!WasmValidator().validate(*wasm, false, options.validateGlobally)) {
234+
if (!WasmValidator().validate(*wasm, validationFlags)) {
231235
if (passDebug >= 2) {
232236
std::cerr << "Last pass (" << pass->name << ") broke validation. Here is the module before: \n" << moduleBefore.str() << "\n";
233237
} else {
@@ -242,7 +246,7 @@ void PassRunner::run() {
242246
std::cerr << "[PassRunner] passes took " << totalTime.count() << " seconds." << std::endl;
243247
// validate
244248
std::cerr << "[PassRunner] (final validation)\n";
245-
if (!WasmValidator().validate(*wasm, false, options.validateGlobally)) {
249+
if (!WasmValidator().validate(*wasm, validationFlags)) {
246250
std::cerr << "final module does not validate\n";
247251
abort();
248252
}

src/tools/s2wasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ int main(int argc, const char *argv[]) {
195195
if (options.extra["validate"] != "none") {
196196
if (options.debug) std::cerr << "Validating..." << std::endl;
197197
if (!wasm::WasmValidator().validate(linker.getOutput().wasm,
198-
options.extra["validate"] == "web")) {
198+
WasmValidator::Globally | (options.extra["validate"] == "web" ? WasmValidator::Web : 0))) {
199199
Fatal() << "Error: linked module is not valid.\n";
200200
}
201201
}

src/tools/wasm-as.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int main(int argc, const char *argv[]) {
9393
if (options.extra["validate"] != "none") {
9494
if (options.debug) std::cerr << "Validating..." << std::endl;
9595
if (!wasm::WasmValidator().validate(wasm,
96-
options.extra["validate"] == "web")) {
96+
WasmValidator::Globally | (options.extra["validate"] == "web" ? WasmValidator::Web : 0))) {
9797
Fatal() << "Error: input module is not valid.\n";
9898
}
9999
}

src/tools/wasm-reduce.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
414414
}
415415
for (auto& func : functions) {
416416
curr->removeFunction(func.name);
417-
if (WasmValidator().validate(*curr, false, true, true /* override quiet */) && writeAndTestReduction()) {
417+
if (WasmValidator().validate(*curr, WasmValidator::Globally | WasmValidator::Quiet) &&
418+
writeAndTestReduction()) {
418419
std::cerr << "| removed function " << func.name << '\n';
419420
noteReduction();
420421
} else {

src/wasm-validator.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@
4949
namespace wasm {
5050

5151
struct WasmValidator {
52-
bool validate(Module& module, bool validateWeb = false, bool validateGlobally = true, bool quiet = false);
52+
enum FlagValues {
53+
Minimal = 0,
54+
Web = 1 << 0,
55+
Globally = 1 << 1,
56+
Quiet = 1 << 2
57+
};
58+
typedef uint32_t Flags;
59+
60+
bool validate(Module& module, Flags flags = Globally);
5361
};
5462

5563
} // namespace wasm

src/wasm/wasm-validator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -997,18 +997,18 @@ static void validateModule(Module& module, ValidationInfo& info) {
997997
// TODO: If we want the validator to be part of libwasm rather than libpasses, then
998998
// Using PassRunner::getPassDebug causes a circular dependence. We should fix that,
999999
// perhaps by moving some of the pass infrastructure into libsupport.
1000-
bool WasmValidator::validate(Module& module, bool validateWeb, bool validateGlobally, bool quiet) {
1000+
bool WasmValidator::validate(Module& module, Flags flags) {
10011001
ValidationInfo info;
1002-
info.validateWeb = validateWeb;
1003-
info.validateGlobally = validateGlobally;
1004-
info.quiet = quiet;
1002+
info.validateWeb = flags & Web;
1003+
info.validateGlobally = flags & Globally;
1004+
info.quiet = flags & Quiet;
10051005
// parallel wasm logic validation
10061006
PassRunner runner(&module);
10071007
runner.add<FunctionValidator>(&info);
10081008
runner.setIsNested(true);
10091009
runner.run();
10101010
// validate globally
1011-
if (validateGlobally) {
1011+
if (info.validateGlobally) {
10121012
validateImports(module, info);
10131013
validateExports(module, info);
10141014
validateGlobals(module, info);

0 commit comments

Comments
 (0)