diff --git a/src/passes/RemoveExports.cpp b/src/passes/RemoveExports.cpp index 99cd0ff641d..5207a6f2e1a 100644 --- a/src/passes/RemoveExports.cpp +++ b/src/passes/RemoveExports.cpp @@ -19,10 +19,14 @@ // // --remove-exports=__* // -// That will remove all exports with names like "__foo" and "__bar". +// In this case we will remove all exports with names like "__foo" and "__bar". +// +// Exports can also be specified as a comma-separated list, and can be a +// response file. // #include "pass.h" +#include "support/file.h" #include "support/string.h" #include "wasm.h" @@ -32,13 +36,21 @@ namespace { struct RemoveExports : public Pass { void run(Module* module) override { - std::string pattern = + std::string param = getArgument(name, "Usage usage: wasm-opt --" + name + "=WILDCARD"); + param = String::trim(read_possible_response_file(param)); + + String::Split patterns(param, String::Split::NewLineOr(",")); + patterns = handleBracketingOperators(patterns); + std::vector toRemove; for (auto& exp : module->exports) { - if (String::wildcardMatch(pattern, exp->name.toString())) { - toRemove.push_back(exp->name); + for (auto& pattern : patterns) { + if (String::wildcardMatch(pattern, exp->name.toString())) { + toRemove.push_back(exp->name); + break; + } } } diff --git a/test/lit/passes/remove-exports-file.txt b/test/lit/passes/remove-exports-file.txt new file mode 100644 index 00000000000..3bd1f0e2974 --- /dev/null +++ b/test/lit/passes/remove-exports-file.txt @@ -0,0 +1,2 @@ +foo +bar diff --git a/test/lit/passes/remove-exports-file.wast b/test/lit/passes/remove-exports-file.wast new file mode 100644 index 00000000000..b333ff20038 --- /dev/null +++ b/test/lit/passes/remove-exports-file.wast @@ -0,0 +1,37 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. + +;; RUN: foreach %s %t wasm-opt --remove-exports=@%S/remove-exports-file.txt -all -S -o - | filecheck %s + +;; Test a response file as the input to this pass. foo and bar will be removed. +(module + ;; CHECK: (type $0 (func)) + + ;; CHECK: (export "keep" (func $keep)) + + ;; CHECK: (func $foo (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $foo (export "foo") + (drop (i32.const 1)) + ) + + ;; CHECK: (func $bar (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 2) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $bar (export "bar") + (drop (i32.const 2)) + ) + + ;; CHECK: (func $keep (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 3) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $keep (export "keep") + (drop (i32.const 3)) + ) +) diff --git a/test/lit/passes/remove-exports-list.wast b/test/lit/passes/remove-exports-list.wast new file mode 100644 index 00000000000..7f850ee59fa --- /dev/null +++ b/test/lit/passes/remove-exports-list.wast @@ -0,0 +1,38 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. + +;; RUN: foreach %s %t wasm-opt "--remove-exports=foo,bar" -all -S -o - | filecheck %s + +;; The two exports mentioned will be removed (note the handling of comma +;; separation, taking into account bracketing). The other will remain. +(module + ;; CHECK: (type $0 (func)) + + ;; CHECK: (export "keep" (func $keep)) + + ;; CHECK: (func $"foo" (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $"foo" (export "foo") + (drop (i32.const 1)) + ) + + ;; CHECK: (func $bar (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 2) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $bar (export "bar") + (drop (i32.const 2)) + ) + + ;; CHECK: (func $keep (type $0) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 3) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $keep (export "keep") + (drop (i32.const 3)) + ) +) diff --git a/test/lit/passes/remove-exports.wast b/test/lit/passes/remove-exports.wast index 0a5af581346..7629afa38f7 100644 --- a/test/lit/passes/remove-exports.wast +++ b/test/lit/passes/remove-exports.wast @@ -1,5 +1,4 @@ ;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. -;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up. ;; RUN: foreach %s %t wasm-opt "--remove-exports=__*" -all -S -o - | filecheck %s