|
16 | 16 |
|
17 | 17 | #include <algorithm> |
18 | 18 | #include <fstream> |
19 | | -#include <iomanip> |
20 | 19 |
|
21 | | -#include "ir/eh-utils.h" |
22 | 20 | #include "ir/module-utils.h" |
23 | 21 | #include "ir/names.h" |
24 | 22 | #include "ir/table-utils.h" |
25 | 23 | #include "ir/type-updating.h" |
26 | 24 | #include "pass.h" |
27 | 25 | #include "support/bits.h" |
28 | | -#include "support/debug.h" |
29 | 26 | #include "support/stdckdint.h" |
30 | 27 | #include "support/string.h" |
31 | 28 | #include "wasm-annotations.h" |
@@ -1242,6 +1239,43 @@ void WasmBinaryWriter::writeSourceMapProlog() { |
1242 | 1239 | } |
1243 | 1240 | } |
1244 | 1241 |
|
| 1242 | + // Remove unused function names from 'names' field. |
| 1243 | + if (!wasm->debugInfoSymbolNames.empty()) { |
| 1244 | + std::vector<std::string> newSymbolNames; |
| 1245 | + std::map<Index, Index> oldToNewIndex; |
| 1246 | + |
| 1247 | + // Collect all used symbol name indexes. |
| 1248 | + for (auto& func : wasm->functions) { |
| 1249 | + for (auto& [_, location] : func->debugLocations) { |
| 1250 | + if (location && location->symbolNameIndex) { |
| 1251 | + uint32_t oldIndex = *location->symbolNameIndex; |
| 1252 | + assert(oldIndex < wasm->debugInfoSymbolNames.size()); |
| 1253 | + oldToNewIndex[oldIndex] = 0; // placeholder |
| 1254 | + } |
| 1255 | + } |
| 1256 | + } |
| 1257 | + |
| 1258 | + // Create the new list of names and the mapping from old to new indices. |
| 1259 | + uint32_t index = 0; |
| 1260 | + for (auto& [oldIndex, newIndex] : oldToNewIndex) { |
| 1261 | + newSymbolNames.push_back(wasm->debugInfoSymbolNames[oldIndex]); |
| 1262 | + newIndex = index++; |
| 1263 | + } |
| 1264 | + |
| 1265 | + // Update all debug locations to point to the new indices. |
| 1266 | + for (auto& func : wasm->functions) { |
| 1267 | + for (auto& [_, location] : func->debugLocations) { |
| 1268 | + if (location && location->symbolNameIndex) { |
| 1269 | + uint32_t oldIndex = *location->symbolNameIndex; |
| 1270 | + location->symbolNameIndex = oldToNewIndex[oldIndex]; |
| 1271 | + } |
| 1272 | + } |
| 1273 | + } |
| 1274 | + |
| 1275 | + // Replace the old symbol names with the new, pruned list. |
| 1276 | + wasm->debugInfoSymbolNames = std::move(newSymbolNames); |
| 1277 | + } |
| 1278 | + |
1245 | 1279 | auto writeOptionalString = [&](const char* name, const std::string& str) { |
1246 | 1280 | if (!str.empty()) { |
1247 | 1281 | *sourceMap << "\"" << name << "\":\"" << str << "\","; |
@@ -1269,10 +1303,6 @@ void WasmBinaryWriter::writeSourceMapProlog() { |
1269 | 1303 | writeStringVector("sourcesContent", wasm->debugInfoSourcesContent); |
1270 | 1304 | } |
1271 | 1305 |
|
1272 | | - // TODO: This field is optional; maybe we should omit if it's empty. |
1273 | | - // TODO: Binaryen actually does not correctly preserve symbol names when it |
1274 | | - // rewrites the mappings. We should maybe just drop them, or else handle |
1275 | | - // them correctly. |
1276 | 1306 | writeStringVector("names", wasm->debugInfoSymbolNames); |
1277 | 1307 |
|
1278 | 1308 | *sourceMap << "\"mappings\":\""; |
|
0 commit comments