Skip to content

Commit d139d54

Browse files
committed
Use std::optional to to signify the absence of a debug location
1 parent 43ec3e3 commit d139d54

File tree

9 files changed

+58
-40
lines changed

9 files changed

+58
-40
lines changed

src/ir/module-utils.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ Function* copyFunction(Function* func,
6060
// Update file indices if needed
6161
if (fileIndexMap) {
6262
for (auto& iter : ret->debugLocations) {
63-
iter.second.fileIndex = (*fileIndexMap)[iter.second.fileIndex];
63+
if (iter.second) {
64+
iter.second->fileIndex = (*fileIndexMap)[iter.second->fileIndex];
65+
}
6466
}
6567
updateLocationSet(ret->prologLocation, *fileIndexMap);
6668
updateLocationSet(ret->epilogLocation, *fileIndexMap);

src/parser/contexts.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
17231723
}
17241724
Lexer lexer(annotation->contents);
17251725
if (lexer.empty()) {
1726-
irBuilder.setDebugLocation({0, 0, 0});
1726+
irBuilder.setDebugLocation(std::nullopt);
17271727
return;
17281728
}
17291729

@@ -1762,7 +1762,8 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
17621762
assert(wasm.debugInfoFileNames.size() == it->second);
17631763
wasm.debugInfoFileNames.push_back(std::string(file));
17641764
}
1765-
irBuilder.setDebugLocation({it->second, *line, *col});
1765+
irBuilder.setDebugLocation(
1766+
Function::DebugLocation({it->second, *line, *col}));
17661767
}
17671768

17681769
Result<> makeBlock(Index pos,

src/passes/Print.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
117117

118118
Module* currModule = nullptr;
119119
Function* currFunction = nullptr;
120-
Function::DebugLocation lastPrintedLocation;
120+
std::optional<Function::DebugLocation> lastPrintedLocation;
121121
bool debugInfo;
122122

123123
// Used to print delegate's depth argument when it throws to the caller
@@ -247,7 +247,8 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
247247
return o;
248248
}
249249

250-
void printDebugLocation(const Function::DebugLocation& location);
250+
void
251+
printDebugLocation(const std::optional<Function::DebugLocation>& location);
251252
void printDebugLocation(Expression* curr);
252253

253254
// Prints debug info for a delimiter in an expression.
@@ -2473,21 +2474,20 @@ std::ostream& PrintSExpression::printPrefixedTypes(const char* prefix,
24732474
}
24742475

24752476
void PrintSExpression::printDebugLocation(
2476-
const Function::DebugLocation& location) {
2477+
const std::optional<Function::DebugLocation>& location) {
24772478
// Do not skip repeated debug info in full mode, for less-confusing debugging:
24782479
// full mode prints out everything in the most verbose manner.
24792480
if (lastPrintedLocation == location && indent > lastPrintIndent && !full) {
24802481
return;
24812482
}
24822483
lastPrintedLocation = location;
24832484
lastPrintIndent = indent;
2484-
Function::DebugLocation noLocation = {0, 0, 0};
2485-
if (location == noLocation) {
2485+
if (!location) {
24862486
o << ";;@\n";
24872487
} else {
2488-
auto fileName = currModule->debugInfoFileNames[location.fileIndex];
2489-
o << ";;@ " << fileName << ":" << location.lineNumber << ":"
2490-
<< location.columnNumber << '\n';
2488+
auto fileName = currModule->debugInfoFileNames[location->fileIndex];
2489+
o << ";;@ " << fileName << ":" << location->lineNumber << ":"
2490+
<< location->columnNumber << '\n';
24912491
}
24922492
doIndent(o, indent);
24932493
}
@@ -2500,7 +2500,7 @@ void PrintSExpression::printDebugLocation(Expression* curr) {
25002500
if (iter != debugLocations.end()) {
25012501
printDebugLocation(iter->second);
25022502
} else {
2503-
printDebugLocation({0, 0, 0});
2503+
printDebugLocation(std::nullopt);
25042504
}
25052505
// show a binary position, if there is one
25062506
if (debugInfo) {
@@ -2963,7 +2963,7 @@ void PrintSExpression::visitFunction(Function* curr) {
29632963
void PrintSExpression::visitImportedFunction(Function* curr) {
29642964
doIndent(o, indent);
29652965
currFunction = curr;
2966-
lastPrintedLocation = {0, 0, 0};
2966+
lastPrintedLocation = std::nullopt;
29672967
o << '(';
29682968
emitImportHeader(curr);
29692969
handleSignature(curr->type, curr->name);
@@ -2974,7 +2974,7 @@ void PrintSExpression::visitImportedFunction(Function* curr) {
29742974
void PrintSExpression::visitDefinedFunction(Function* curr) {
29752975
doIndent(o, indent);
29762976
currFunction = curr;
2977-
lastPrintedLocation = {0, 0, 0};
2977+
lastPrintedLocation = std::nullopt;
29782978
lastPrintIndent = 0;
29792979
if (currFunction->prologLocation.size()) {
29802980
printDebugLocation(*currFunction->prologLocation.begin());

src/wasm-ir-builder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
5858

5959
// Set the debug location to be attached to the next visited, created, or
6060
// pushed instruction.
61-
void setDebugLocation(const Function::DebugLocation&);
61+
void setDebugLocation(const std::optional<Function::DebugLocation>&);
6262

6363
// Handle the boundaries of control flow structures. Users may choose to use
6464
// the corresponding `makeXYZ` function below instead of `visitXYZStart`, but
@@ -238,7 +238,7 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
238238
Module& wasm;
239239
Function* func;
240240
Builder builder;
241-
std::optional<Function::DebugLocation> debugLoc;
241+
std::optional<std::optional<Function::DebugLocation>> debugLoc;
242242

243243
struct ChildPopper;
244244

src/wasm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2185,7 +2185,9 @@ class Function : public Importable {
21852185
: columnNumber < other.columnNumber;
21862186
}
21872187
};
2188-
std::unordered_map<Expression*, DebugLocation> debugLocations;
2188+
// One can explicitly set the debug location of an expression to
2189+
// nullopt to stop the propagation of debug locations.
2190+
std::unordered_map<Expression*, std::optional<DebugLocation>> debugLocations;
21892191
std::set<DebugLocation> prologLocation;
21902192
std::set<DebugLocation> epilogLocation;
21912193

src/wasm/wasm-binary.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,9 +1401,9 @@ void WasmBinaryWriter::writeDebugLocation(Expression* curr, Function* func) {
14011401
if (sourceMap) {
14021402
auto& debugLocations = func->debugLocations;
14031403
auto iter = debugLocations.find(curr);
1404-
if (iter != debugLocations.end()) {
1404+
if (iter != debugLocations.end() && iter->second) {
14051405
// There is debug information here, write it out.
1406-
writeDebugLocation(iter->second);
1406+
writeDebugLocation(*(iter->second));
14071407
} else {
14081408
// This expression has no debug location.
14091409
writeNoDebugLocation();

src/wasm/wasm-ir-builder.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,28 @@ Result<Expression*> IRBuilder::build() {
168168
return expr;
169169
}
170170

171-
void IRBuilder::setDebugLocation(const Function::DebugLocation& loc) {
172-
DBG(std::cerr << "setting debugloc " << loc.fileIndex << ":" << loc.lineNumber
173-
<< ":" << loc.columnNumber << "\n";);
171+
void IRBuilder::setDebugLocation(
172+
const std::optional<Function::DebugLocation>& loc) {
173+
if (loc) {
174+
DBG(std::cerr << "setting debugloc " << loc->fileIndex << ":"
175+
<< loc->lineNumber << ":" << loc->columnNumber << "\n";);
176+
} else {
177+
DBG(std::cerr << "setting debugloc to none\n";);
178+
}
174179
debugLoc = loc;
175180
}
176181

177182
void IRBuilder::applyDebugLoc(Expression* expr) {
178183
if (debugLoc) {
179184
if (func) {
180-
DBG(std::cerr << "applying debugloc " << debugLoc->fileIndex << ":"
181-
<< debugLoc->lineNumber << ":" << debugLoc->columnNumber
182-
<< " to expression " << ShallowExpression{expr} << "\n");
185+
if (*debugLoc) {
186+
DBG(std::cerr << "applying debugloc " << *debugLoc->fileIndex << ":"
187+
<< *debugLoc->lineNumber << ":" << *debugLoc->columnNumber
188+
<< " to expression " << ShallowExpression{expr} << "\n");
189+
} else {
190+
DBG(std::cerr << "applying debugloc to expression "
191+
<< ShallowExpression{expr} << "\n");
192+
}
183193
func->debugLocations[expr] = *debugLoc;
184194
}
185195
debugLoc.reset();
@@ -677,10 +687,10 @@ Result<> IRBuilder::visitFunctionStart(Function* func) {
677687
if (!scopeStack.empty()) {
678688
return Err{"unexpected start of function"};
679689
}
680-
if (debugLoc) {
681-
func->prologLocation.insert(*debugLoc);
682-
debugLoc.reset();
690+
if (debugLoc && *debugLoc) {
691+
func->prologLocation.insert(**debugLoc);
683692
}
693+
debugLoc.reset();
684694
scopeStack.push_back(ScopeCtx::makeFunc(func));
685695
this->func = func;
686696
return Ok{};
@@ -718,9 +728,9 @@ Result<> IRBuilder::visitTryTableStart(TryTable* trytable, Name label) {
718728
}
719729

720730
Result<Expression*> IRBuilder::finishScope(Block* block) {
721-
if (debugLoc) {
722-
DBG(std::cerr << "discarding debugloc " << debugLoc->fileIndex << ":"
723-
<< debugLoc->lineNumber << ":" << debugLoc->columnNumber
731+
if (debugLoc && *debugLoc) {
732+
DBG(std::cerr << "discarding debugloc " << *debugLoc->fileIndex << ":"
733+
<< *debugLoc->lineNumber << ":" << *debugLoc->columnNumber
724734
<< "\n");
725735
}
726736
debugLoc.reset();
@@ -913,10 +923,10 @@ Result<> IRBuilder::visitEnd() {
913923
if (scope.isNone()) {
914924
return Err{"unexpected end"};
915925
}
916-
if (auto* func = scope.getFunction(); func && debugLoc) {
917-
func->epilogLocation.insert(*debugLoc);
918-
debugLoc.reset();
926+
if (auto* func = scope.getFunction(); func && debugLoc && *debugLoc) {
927+
func->epilogLocation.insert(**debugLoc);
919928
}
929+
debugLoc.reset();
920930
auto expr = finishScope(scope.getBlock());
921931
CHECK_ERR(expr);
922932

src/wasm/wasm-s-parser.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,8 +1990,11 @@ Expression* SExpressionWasmBuilder::makeConst(Element& s, Type type) {
19901990
return ret;
19911991
}
19921992

1993-
static size_t parseMemAttributes(
1994-
size_t i, Element& s, Address& offset, Address& align, bool memory64) {
1993+
static size_t parseMemAttributes(size_t i,
1994+
Element& s,
1995+
Address& offset,
1996+
Address& align,
1997+
bool memory64) {
19951998
// Parse "align=X" and "offset=X" arguments, bailing out on anything else.
19961999
while (!s[i]->isList()) {
19972000
const char* str = s[i]->str().str.data();

test/example/debug-location-propagation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ int main() {
3333

3434
auto& debugLocations = module->getFunction("adder")->debugLocations;
3535
assert(debugLocations.size() == 4);
36-
assert(debugLocations[x].columnNumber == 13);
37-
assert(debugLocations[y].columnNumber == 13);
38-
assert(debugLocations[add].columnNumber == 2);
39-
assert(debugLocations[drop].columnNumber == 2);
36+
assert(debugLocations[x]->columnNumber == 13);
37+
assert(debugLocations[y]->columnNumber == 13);
38+
assert(debugLocations[add]->columnNumber == 2);
39+
assert(debugLocations[drop]->columnNumber == 2);
4040

4141
BinaryenSetDebugInfo(false);
4242
BinaryenModuleDispose(module);

0 commit comments

Comments
 (0)