diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index c4f123375d9..df8b6a28df5 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1245,14 +1245,20 @@ void WasmBinaryWriter::writeSourceMapProlog() { std::map oldToNewIndex; // Collect all used symbol name indexes. - for (auto& func : wasm->functions) { - for (auto& [_, location] : func->debugLocations) { + auto prepareIndexMap = + [&](const std::optional& location) { if (location && location->symbolNameIndex) { uint32_t oldIndex = *location->symbolNameIndex; assert(oldIndex < wasm->debugInfoSymbolNames.size()); oldToNewIndex[oldIndex] = 0; // placeholder } + }; + for (auto& func : wasm->functions) { + for (auto& [_, location] : func->debugLocations) { + prepareIndexMap(location); } + prepareIndexMap(func->prologLocation); + prepareIndexMap(func->epilogLocation); } // Create the new list of names and the mapping from old to new indices. @@ -1263,13 +1269,18 @@ void WasmBinaryWriter::writeSourceMapProlog() { } // Update all debug locations to point to the new indices. + auto updateIndex = [&](std::optional& location) { + if (location && location->symbolNameIndex) { + uint32_t oldIndex = *location->symbolNameIndex; + location->symbolNameIndex = oldToNewIndex[oldIndex]; + } + }; for (auto& func : wasm->functions) { for (auto& [_, location] : func->debugLocations) { - if (location && location->symbolNameIndex) { - uint32_t oldIndex = *location->symbolNameIndex; - location->symbolNameIndex = oldToNewIndex[oldIndex]; - } + updateIndex(location); } + updateIndex(func->prologLocation); + updateIndex(func->epilogLocation); } // Replace the old symbol names with the new, pruned list. diff --git a/test/lit/source-map-names.wast b/test/lit/source-map-names.wast index b0a184bb588..1b49b14e269 100644 --- a/test/lit/source-map-names.wast +++ b/test/lit/source-map-names.wast @@ -5,11 +5,12 @@ ;; After --remove-unused-module-elements, the output source map's 'names' field ;; should NOT contain 'unused' -;; OUT-MAP: "names":["used","used2"] +;; OUT-MAP: "names":["used","used2","usedProlog"] (module (export "used" (func $used)) (export "used2" (func $used2)) + (export "usedProlog" (func $usedProlog)) (func $unused ;;@ src.cpp:1:1:unused @@ -29,4 +30,11 @@ ;;@ src.cpp:3:1:used2 (nop) ) + + ;; OUT-WAST: ;;@ src.cpp:4:1:usedProlog + ;; OUT-WAST-NEXT: (func + ;;@ src.cpp:4:1:usedProlog + (func $usedProlog + (nop) + ) )