Skip to content

Commit 7dcc532

Browse files
authored
Support one-line-one-function file format for asyncify lists (#6051)
If there are newlines in the list, then we split using them in a simple manner (that does not take into account nesting of any other delimiters). Fixes #6047 Fixes #5271
1 parent 57e0b2f commit 7dcc532

File tree

5 files changed

+47
-7
lines changed

5 files changed

+47
-7
lines changed

src/passes/Asyncify.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,8 @@ struct Asyncify : public Pass {
16261626
options.getArgumentOrDefault("asyncify-ignore-imports", "");
16271627
bool allImportsCanChangeState =
16281628
stateChangingImports == "" && ignoreImports == "";
1629-
String::Split listedImports(stateChangingImports, ",");
1629+
String::Split listedImports(stateChangingImports,
1630+
String::Split::NewLineOr(","));
16301631
// canIndirectChangeState is the default. asyncify-ignore-indirect sets it
16311632
// to false.
16321633
auto canIndirectChangeState =
@@ -1638,19 +1639,21 @@ struct Asyncify : public Pass {
16381639
removeListInput = options.getArgumentOrDefault("asyncify-blacklist", "");
16391640
}
16401641
String::Split removeList(
1641-
String::trim(read_possible_response_file(removeListInput)), ",");
1642+
String::trim(read_possible_response_file(removeListInput)),
1643+
String::Split::NewLineOr(","));
16421644
String::Split addList(
16431645
String::trim(read_possible_response_file(
16441646
options.getArgumentOrDefault("asyncify-addlist", ""))),
1645-
",");
1647+
String::Split::NewLineOr(","));
16461648
std::string onlyListInput =
16471649
options.getArgumentOrDefault("asyncify-onlylist", "");
16481650
if (onlyListInput.empty()) {
16491651
// Support old name for now to avoid immediate breakage TODO remove
16501652
onlyListInput = options.getArgumentOrDefault("asyncify-whitelist", "");
16511653
}
16521654
String::Split onlyList(
1653-
String::trim(read_possible_response_file(onlyListInput)), ",");
1655+
String::trim(read_possible_response_file(onlyListInput)),
1656+
String::Split::NewLineOr(","));
16541657
auto asserts = options.hasArgument("asyncify-asserts");
16551658
auto verbose = options.hasArgument("asyncify-verbose");
16561659
auto relocatable = options.hasArgument("asyncify-relocatable");

src/support/string.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ namespace wasm::String {
3131

3232
// Creates a vector of the split parts of a string, by a delimiter.
3333
class Split : public std::vector<std::string> {
34-
public:
35-
Split() = default;
34+
private:
35+
// If we split on newlines then we do not need to handle bracketing at all.
36+
// Otherwise, splitting on say "," does require us to understanding the
37+
// scoping of brackets, e.g., "foo(x, y),bar" should be split as "foo(x, y)",
38+
// "bar".
39+
bool needToHandleBracketingOperations = true;
3640

37-
Split(const std::string& input, const std::string& delim) {
41+
void split(const std::string& input, const std::string& delim) {
3842
size_t lastEnd = 0;
3943
while (lastEnd < input.size()) {
4044
auto nextDelim = input.find(delim, lastEnd);
@@ -44,6 +48,31 @@ class Split : public std::vector<std::string> {
4448
(*this).push_back(input.substr(lastEnd, nextDelim - lastEnd));
4549
lastEnd = nextDelim + delim.size();
4650
}
51+
needToHandleBracketingOperations = delim != "\n";
52+
}
53+
friend String::Split handleBracketingOperators(String::Split split);
54+
55+
public:
56+
// This can be used when we want to split on newlines if there are any, and if
57+
// there are not, then using the given delimiter.
58+
struct NewLineOr {
59+
const std::string delim;
60+
explicit NewLineOr(const std::string& delim) : delim(delim) {}
61+
};
62+
63+
Split() = default;
64+
65+
Split(const std::string& input, const NewLineOr& newLineOrDelim) {
66+
auto first = input.find("\n", 0);
67+
if (first != std::string::npos && first != input.length() - 1) {
68+
split(input, "\n");
69+
} else {
70+
split(input, newLineOrDelim.delim);
71+
}
72+
}
73+
74+
Split(const std::string& input, const std::string& delim) {
75+
split(input, delim);
4776
}
4877
};
4978

@@ -53,6 +82,10 @@ class Split : public std::vector<std::string> {
5382
// must be kept together because of the "(". Likewise, "{", "<", "[" are
5483
// handled.
5584
inline String::Split handleBracketingOperators(String::Split split) {
85+
if (!split.needToHandleBracketingOperations) {
86+
return split;
87+
}
88+
5689
String::Split ret;
5790
std::string last;
5891
int nesting = 0;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo
2+
bar

test/lit/passes/asyncify_pass-arg=asyncify-blacklist@foo,bar.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up.
33

44
;; RUN: foreach %s %t wasm-opt --asyncify --pass-arg=asyncify-blacklist@foo,bar -S -o - | filecheck %s
5+
;; RUN: foreach %s %t wasm-opt --asyncify --pass-arg=asyncify-blacklist@@%S/asyncify-foo,bar-nl.txt -S -o - | filecheck %s
56

67
(module
78
(memory 1 2)

test/lit/passes/asyncify_pass-arg=asyncify-onlylist@foo,bar.wast

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up.
33

44
;; RUN: foreach %s %t wasm-opt --asyncify --pass-arg=asyncify-onlylist@foo,bar -S -o - | filecheck %s
5+
;; RUN: foreach %s %t wasm-opt --asyncify --pass-arg=asyncify-onlylist@@%S/asyncify-foo,bar-nl.txt -S -o - | filecheck %s
56

67
(module
78
(memory 1 2)

0 commit comments

Comments
 (0)