Skip to content

Commit a88253c

Browse files
kripkenradekdoulik
authored andcommitted
[NFC] Source maps: Simplify the code and add comments (WebAssembly#5912)
Almost entirely trivial, except for this part: - if (nextDebugLocation.availablePos == 0 && - nextDebugLocation.previousPos <= pos) { + if (nextDebugLocation.availablePos == 0) { return; I believe removing the extra check has no effect. Removing it does not change anything in the test suite, and logically, if we set availablePos to 0 then we definitely want to return here - we set it to 0 to indicate there is nothing left to read, which is what the code after it does. As a result, we can remove the previousPos field entirely.
1 parent fc01e67 commit a88253c

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

src/wasm-binary.h

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,34 +1526,48 @@ class WasmBinaryReader {
15261526
MixedArena& allocator;
15271527
const std::vector<char>& input;
15281528

1529+
// Source map debugging support.
1530+
15291531
std::istream* sourceMap;
15301532

1531-
struct NextDebugLocation {
1532-
uint32_t availablePos;
1533-
uint32_t previousPos;
1534-
Function::DebugLocation next;
1535-
} nextDebugLocation;
1536-
1537-
// Whether debug info is present next or not in the next debug location. A
1538-
// debug location can contain debug info (file:line:col) or it might not. We
1539-
// need to track this boolean alongside |nextDebugLocation| - that is, we
1540-
// can't just do something like std::optional<DebugLocation> or such - as we
1541-
// still need to track the values in |next|, as later positions are relative
1542-
// to them. That is, if we have line number 100, then no debug info, and then
1543-
// line number 500, then when we get to 500 we will see "+400" which is
1544-
// relative to the last existing line number (we "skip" over the place without
1545-
// debug info).
1533+
// The binary position that the next debug location refers to. That is, this
1534+
// is the first item in a source map entry that we have read (the "column", in
1535+
// source map terms, which for wasm means the offset in the binary). We have
1536+
// read this entry, but have not used it yet (we use it when we read the
1537+
// expression at this binary offset).
1538+
//
1539+
// This is set to 0 as an invalid value if we reach the end of the source map
1540+
// and there is nothing left to read.
1541+
size_t nextDebugPos;
1542+
1543+
// The debug location (file:line:col) corresponding to |nextDebugPos|. That
1544+
// is, this is the next 3 fields in a source map entry that we have read, but
1545+
// not used yet.
1546+
//
1547+
// If that location has no debug info (it lacks those 3 fields), then this
1548+
// contains the info from the previous one, because in a source map, these
1549+
// fields are relative to their last appearance, so we cannot forget them (we
1550+
// can't just do something like std::optional<DebugLocation> or such); for
1551+
// example, if we have line number 100, then no debug info, and then line
1552+
// number 500, then when we get to 500 we will see "+400" which is relative to
1553+
// the last existing line number (we "skip" over a place without debug info).
1554+
Function::DebugLocation nextDebugLocation;
1555+
1556+
// Whether debug info is present on |nextDebugPos| (see comment there).
15461557
bool nextDebugLocationHasDebugInfo;
15471558

1559+
// Settings.
1560+
15481561
bool debugInfo = true;
15491562
bool DWARF = false;
15501563
bool skipFunctionBodies = false;
15511564

1565+
// Internal state.
1566+
15521567
size_t pos = 0;
15531568
Index startIndex = -1;
15541569
std::set<Function::DebugLocation> debugLocation;
15551570
size_t codeSectionLocation;
1556-
15571571
std::set<BinaryConsts::Section> seenSections;
15581572

15591573
// All types defined in the type section

src/wasm/wasm-binary.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,8 +1638,8 @@ void WasmBinaryWriter::writeField(const Field& field) {
16381638
WasmBinaryReader::WasmBinaryReader(Module& wasm,
16391639
FeatureSet features,
16401640
const std::vector<char>& input)
1641-
: wasm(wasm), allocator(wasm.allocator), input(input),
1642-
sourceMap(nullptr), nextDebugLocation{0, 0, {0, 0, 0}},
1641+
: wasm(wasm), allocator(wasm.allocator), input(input), sourceMap(nullptr),
1642+
nextDebugPos(0), nextDebugLocation{0, 0, 0},
16431643
nextDebugLocationHasDebugInfo(false), debugLocation() {
16441644
wasm.features = features;
16451645
}
@@ -2796,7 +2796,7 @@ void WasmBinaryReader::readSourceMapHeader() {
27962796

27972797
mustReadChar('\"');
27982798
if (maybeReadChar('\"')) { // empty mappings
2799-
nextDebugLocation.availablePos = 0;
2799+
nextDebugPos = 0;
28002800
return;
28012801
}
28022802
// read first debug location
@@ -2809,8 +2809,8 @@ void WasmBinaryReader::readSourceMapHeader() {
28092809
uint32_t lineNumber =
28102810
readBase64VLQ(*sourceMap) + 1; // adjust zero-based line number
28112811
uint32_t columnNumber = readBase64VLQ(*sourceMap);
2812-
nextDebugLocation = {
2813-
position, position, {fileIndex, lineNumber, columnNumber}};
2812+
nextDebugPos = position;
2813+
nextDebugLocation = {fileIndex, lineNumber, columnNumber};
28142814
nextDebugLocationHasDebugInfo = true;
28152815
}
28162816

@@ -2819,21 +2819,18 @@ void WasmBinaryReader::readNextDebugLocation() {
28192819
return;
28202820
}
28212821

2822-
if (nextDebugLocation.availablePos == 0 &&
2823-
nextDebugLocation.previousPos <= pos) {
2824-
// if source map file had already reached the end and cache position also
2825-
// cannot cover the pos clear the debug location
2822+
if (nextDebugPos == 0) {
2823+
// We reached the end of the source map; nothing left to read.
28262824
debugLocation.clear();
28272825
return;
28282826
}
28292827

2830-
while (nextDebugLocation.availablePos &&
2831-
nextDebugLocation.availablePos <= pos) {
2828+
while (nextDebugPos && nextDebugPos <= pos) {
28322829
debugLocation.clear();
28332830
// use debugLocation only for function expressions
28342831
if (currFunction) {
28352832
if (nextDebugLocationHasDebugInfo) {
2836-
debugLocation.insert(nextDebugLocation.next);
2833+
debugLocation.insert(nextDebugLocation);
28372834
} else {
28382835
debugLocation.clear();
28392836
}
@@ -2842,18 +2839,17 @@ void WasmBinaryReader::readNextDebugLocation() {
28422839
char ch;
28432840
*sourceMap >> ch;
28442841
if (ch == '\"') { // end of records
2845-
nextDebugLocation.availablePos = 0;
2842+
nextDebugPos = 0;
28462843
break;
28472844
}
28482845
if (ch != ',') {
28492846
throw MapParseException("Unexpected delimiter");
28502847
}
28512848

28522849
int32_t positionDelta = readBase64VLQ(*sourceMap);
2853-
uint32_t position = nextDebugLocation.availablePos + positionDelta;
2850+
uint32_t position = nextDebugPos + positionDelta;
28542851

2855-
nextDebugLocation.previousPos = nextDebugLocation.availablePos;
2856-
nextDebugLocation.availablePos = position;
2852+
nextDebugPos = position;
28572853

28582854
auto peek = sourceMap->peek();
28592855
if (peek == ',' || peek == '\"') {
@@ -2863,14 +2859,13 @@ void WasmBinaryReader::readNextDebugLocation() {
28632859
}
28642860

28652861
int32_t fileIndexDelta = readBase64VLQ(*sourceMap);
2866-
uint32_t fileIndex = nextDebugLocation.next.fileIndex + fileIndexDelta;
2862+
uint32_t fileIndex = nextDebugLocation.fileIndex + fileIndexDelta;
28672863
int32_t lineNumberDelta = readBase64VLQ(*sourceMap);
2868-
uint32_t lineNumber = nextDebugLocation.next.lineNumber + lineNumberDelta;
2864+
uint32_t lineNumber = nextDebugLocation.lineNumber + lineNumberDelta;
28692865
int32_t columnNumberDelta = readBase64VLQ(*sourceMap);
2870-
uint32_t columnNumber =
2871-
nextDebugLocation.next.columnNumber + columnNumberDelta;
2866+
uint32_t columnNumber = nextDebugLocation.columnNumber + columnNumberDelta;
28722867

2873-
nextDebugLocation.next = {fileIndex, lineNumber, columnNumber};
2868+
nextDebugLocation = {fileIndex, lineNumber, columnNumber};
28742869
nextDebugLocationHasDebugInfo = true;
28752870
}
28762871
}

0 commit comments

Comments
 (0)