Skip to content

Commit f346257

Browse files
authored
WAT parser: include file name in error messages (#7318)
This is especially useful with `wasm-merge` which takes several file as inputs.
1 parent 83c82e5 commit f346257

12 files changed

+33
-14
lines changed

src/parser/lexer.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ struct Lexer {
6161
private:
6262
size_t pos = 0;
6363
std::vector<Annotation> annotations;
64+
std::optional<std::string> file;
6465

6566
public:
6667
std::string_view buffer;
6768

68-
Lexer(std::string_view buffer) : buffer(buffer) { setPos(0); }
69+
Lexer(std::string_view buffer, std::optional<std::string> file = std::nullopt)
70+
: file(file), buffer(buffer) {
71+
setPos(0);
72+
}
6973

7074
size_t getPos() const { return pos; }
7175

@@ -169,6 +173,9 @@ struct Lexer {
169173

170174
[[nodiscard]] Err err(size_t pos, std::string reason) {
171175
std::stringstream msg;
176+
if (file) {
177+
msg << *file << ":";
178+
}
172179
msg << position(pos) << ": error: " << reason;
173180
return Err{msg.str()};
174181
}

src/parser/wat-parser.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ Result<> doParseModule(Module& wasm, Lexer& input, bool allowExtra) {
121121

122122
} // anonymous namespace
123123

124+
Result<> parseModule(Module& wasm,
125+
std::string_view in,
126+
std::optional<std::string> filename) {
127+
Lexer lexer(in, filename);
128+
return doParseModule(wasm, lexer, false);
129+
}
130+
124131
Result<> parseModule(Module& wasm, std::string_view in) {
125132
Lexer lexer(in);
126133
return doParseModule(wasm, lexer, false);

src/parser/wat-parser.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
namespace wasm::WATParser {
2727

2828
// Parse a single WAT module.
29-
Result<> parseModule(Module& wasm, std::string_view in);
29+
Result<> parseModule(Module& wasm,
30+
std::string_view in,
31+
std::optional<std::string> filename = std::nullopt);
3032

3133
// Parse a single WAT module that may have other things after it, as in a wast
3234
// file.

src/wasm/wasm-io.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ namespace wasm {
3434

3535
#define DEBUG_TYPE "writer"
3636

37-
static void readTextData(std::string& input, Module& wasm, IRProfile profile) {
38-
if (auto parsed = WATParser::parseModule(wasm, input);
37+
static void readTextData(std::optional<std::string> filename,
38+
std::string& input,
39+
Module& wasm,
40+
IRProfile profile) {
41+
if (auto parsed = WATParser::parseModule(wasm, input, filename);
3942
auto err = parsed.getErr()) {
4043
Fatal() << err->msg;
4144
}
@@ -44,7 +47,7 @@ static void readTextData(std::string& input, Module& wasm, IRProfile profile) {
4447
void ModuleReader::readText(std::string filename, Module& wasm) {
4548
BYN_TRACE("reading text from " << filename << "\n");
4649
auto input(read_file<std::string>(filename, Flags::Text));
47-
readTextData(input, wasm, profile);
50+
readTextData(filename, input, wasm, profile);
4851
}
4952

5053
void ModuleReader::readBinaryData(std::vector<char>& input,
@@ -114,7 +117,7 @@ void ModuleReader::readStdin(Module& wasm, std::string sourceMapFilename) {
114117
std::ostringstream s;
115118
s.write(input.data(), input.size());
116119
std::string input_str = s.str();
117-
readTextData(input_str, wasm, profile);
120+
readTextData(std::nullopt, input_str, wasm, profile);
118121
}
119122
}
120123

test/lit/parse-bad-optional-memidx.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
;; RUN: not wasm-opt -all %s 2>&1 | filecheck %s
55

6-
;; CHECK: Fatal: 12:22: error: expected end of instruction
6+
;; CHECK: Fatal: {{.*}}:12:22: error: expected end of instruction
77

88
(module
99
(memory 1 1)

test/lit/parse-bad-supertype-8616.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
;; RUN: not wasm-opt %s 2>&1 | filecheck %s
22

3-
;; CHECK: Fatal: 9:2: error: invalid type: Heap type has an undeclared supertype
3+
;; CHECK: Fatal: {{.*}}:9:2: error: invalid type: Heap type has an undeclared supertype
44

55
;; Regression test for a parser bug that caused an assertion failure in this case.
66
(module

test/lit/parse-bad-supertype.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
;; RUN: not wasm-opt %s -all 2>&1 | filecheck %s
44

5-
;; CHECK: Fatal: 8:2: error: invalid type: Heap type has an invalid supertype
5+
;; CHECK: Fatal: {{.*}}:8:2: error: invalid type: Heap type has an invalid supertype
66
(module
77
(type $super (sub (struct i32)))
88
(type $sub (sub $super (struct i64)))

test/lit/parse-bad-tuple-extract-index.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
;; RUN: not wasm-opt %s 2>&1 | filecheck %s
44

5-
;; CHECK: Fatal: 9:3: error: tuple index out of bounds
5+
;; CHECK: Fatal: {{.*}}:9:3: error: tuple index out of bounds
66

77
(module
88
(func

test/lit/parse-bad-tuple-in-sig.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
;; RUN: not wasm-opt %s 2>&1 | filecheck %s
44

5-
;; CHECK: Fatal: 9:1: error: tuple types not allowed in signature
5+
;; CHECK: Fatal: {{.*}}:9:1: error: tuple types not allowed in signature
66

77
(module
88
(func $tuple-in-sig (param (tuple i32 i32))

test/lit/parse-error-func-param-type.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
;; This function's type does not match the param we define for it.
22

33
;; RUN: not wasm-opt %s 2>&1 | filecheck %s
4-
;; CHECK: Fatal: 9:10: error: type does not match provided signature
4+
;; CHECK: Fatal: {{.*}}:9:10: error: type does not match provided signature
55

66
(module
77
(type $0 (func))

0 commit comments

Comments
 (0)