@@ -398,8 +398,10 @@ void WasmBinaryWriter::writeFunctions() {
398398 bool DWARF = Debug::hasDWARFSections (*getModule ());
399399 ModuleUtils::iterDefinedFunctions (*wasm, [&](Function* func) {
400400 assert (binaryLocationTrackedExpressionsForFunc.empty ());
401- size_t sourceMapLocationsSizeAtFunctionStart = sourceMapLocations.size ();
402401 BYN_TRACE (" write one at" << o.size () << std::endl);
402+ // Do not smear any debug location from the previous function.
403+ writeNoDebugLocation ();
404+ size_t sourceMapLocationsSizeAtFunctionStart = sourceMapLocations.size ();
403405 size_t sizePos = writeU32LEBPlaceholder ();
404406 size_t start = o.size ();
405407 BYN_TRACE (" writing" << func->name << std::endl);
@@ -1373,6 +1375,28 @@ void WasmBinaryWriter::writeDebugLocation(const Function::DebugLocation& loc) {
13731375 lastDebugLocation = loc;
13741376}
13751377
1378+ void WasmBinaryWriter::writeNoDebugLocation () {
1379+ // Emit an indication that there is no debug location there (so that
1380+ // we do not get "smeared" with debug info from anything before or
1381+ // after us).
1382+ //
1383+ // We don't need to write repeated "no debug info" indications, as a
1384+ // single one is enough to make it clear that the debug information
1385+ // before us is valid no longer. We also don't need to write one if
1386+ // there is nothing before us.
1387+ if (!sourceMapLocations.empty () &&
1388+ sourceMapLocations.back ().second != nullptr ) {
1389+ sourceMapLocations.emplace_back (o.size (), nullptr );
1390+
1391+ // Initialize the state of debug info to indicate there is no current
1392+ // debug info relevant. This sets |lastDebugLocation| to a dummy value,
1393+ // so that later places with debug info can see that they differ from
1394+ // it (without this, if we had some debug info, then a nullptr for none,
1395+ // and then the same debug info, we could get confused).
1396+ initializeDebugInfo ();
1397+ }
1398+ }
1399+
13761400void WasmBinaryWriter::writeDebugLocation (Expression* curr, Function* func) {
13771401 if (sourceMap) {
13781402 auto & debugLocations = func->debugLocations ;
@@ -1381,25 +1405,8 @@ void WasmBinaryWriter::writeDebugLocation(Expression* curr, Function* func) {
13811405 // There is debug information here, write it out.
13821406 writeDebugLocation (iter->second );
13831407 } else {
1384- // This expression has no debug location. We need to emit an indication
1385- // of that (so that we do not get "smeared" with debug info from anything
1386- // before or after us).
1387- //
1388- // We don't need to write repeated "no debug info" indications, as a
1389- // single one is enough to make it clear that the debug information before
1390- // us is valid no longer. We also don't need to write one if there is
1391- // nothing before us.
1392- if (!sourceMapLocations.empty () &&
1393- sourceMapLocations.back ().second != nullptr ) {
1394- sourceMapLocations.emplace_back (o.size (), nullptr );
1395-
1396- // Initialize the state of debug info to indicate there is no current
1397- // debug info relevant. This sets |lastDebugLocation| to a dummy value,
1398- // so that later places with debug info can see that they differ from
1399- // it (without this, if we had some debug info, then a nullptr for none,
1400- // and then the same debug info, we could get confused).
1401- initializeDebugInfo ();
1402- }
1408+ // This expression has no debug location.
1409+ writeNoDebugLocation ();
14031410 }
14041411 }
14051412 // If this is an instruction in a function, and if the original wasm had
@@ -2687,12 +2694,11 @@ void WasmBinaryReader::readFunctions() {
26872694
26882695 readVars ();
26892696
2690- std::swap ( func->prologLocation , debugLocation) ;
2697+ func->prologLocation = debugLocation;
26912698 {
26922699 // process the function body
26932700 BYN_TRACE (" processing function: " << i << std::endl);
26942701 nextLabel = 0 ;
2695- debugLocation.clear ();
26962702 willBeIgnored = false ;
26972703 // process body
26982704 assert (breakStack.empty ());
@@ -2931,7 +2937,6 @@ void WasmBinaryReader::readNextDebugLocation() {
29312937
29322938 if (nextDebugPos == 0 ) {
29332939 // We reached the end of the source map; nothing left to read.
2934- debugLocation.clear ();
29352940 return ;
29362941 }
29372942
0 commit comments