2424
2525namespace wasm ::ModuleUtils {
2626
27+ // Update the file name indices when moving a set of debug locations from one
28+ // module to another.
2729static void updateLocationSet (std::set<Function::DebugLocation>& locations,
28- std::vector<Index>* indexMap ) {
30+ std::vector<Index>& fileIndexMap ) {
2931 std::set<Function::DebugLocation> updatedLocations;
3032
3133 for (auto iter : locations) {
32- iter.fileIndex = (*indexMap) [iter.fileIndex ];
34+ iter.fileIndex = fileIndexMap [iter.fileIndex ];
3335 updatedLocations.insert (iter);
3436 }
3537 locations.clear ();
3638 std::swap (locations, updatedLocations);
3739}
3840
3941// Copies a function into a module. If newName is provided it is used as the
40- // name of the function (otherwise the original name is copied). If indexMap is
41- // specified, it is used to rename source map filename indices when copying the
42- // function from one module to another one.
42+ // name of the function (otherwise the original name is copied). If fileIndexMap
43+ // is specified, it is used to rename source map filename indices when copying
44+ // the function from one module to another one.
4345Function* copyFunction (Function* func,
4446 Module& out,
4547 Name newName,
46- std::vector<Index>* indexMap ) {
48+ std::optional<std:: vector<Index>> fileIndexMap ) {
4749 auto ret = std::make_unique<Function>();
4850 ret->name = newName.is () ? newName : func->name ;
4951 ret->type = func->type ;
@@ -57,12 +59,12 @@ Function* copyFunction(Function* func,
5759 ret->prologLocation = func->prologLocation ;
5860 ret->epilogLocation = func->epilogLocation ;
5961 // Update file indices if needed
60- if (indexMap ) {
62+ if (fileIndexMap ) {
6163 for (auto & iter : ret->debugLocations ) {
62- iter.second .fileIndex = (*indexMap )[iter.second .fileIndex ];
64+ iter.second .fileIndex = (*fileIndexMap )[iter.second .fileIndex ];
6365 }
64- updateLocationSet (ret->prologLocation , indexMap );
65- updateLocationSet (ret->epilogLocation , indexMap );
66+ updateLocationSet (ret->prologLocation , *fileIndexMap );
67+ updateLocationSet (ret->epilogLocation , *fileIndexMap );
6668 }
6769 ret->module = func->module ;
6870 ret->base = func->base ;
@@ -165,13 +167,27 @@ DataSegment* copyDataSegment(const DataSegment* segment, Module& out) {
165167
166168// Copies named toplevel module items (things of kind ModuleItemKind). See
167169// copyModule() for something that also copies exports, the start function, etc.
168- // The indexMap is used to update source map information when copying functions
169- // from one module to another.
170- void copyModuleItems (const Module& in,
171- Module& out,
172- std::vector<Index>* indexMap) {
170+ // The fileIndexMap is used to update source map information when copying
171+ // functions from one module to another.
172+ void copyModuleItems (const Module& in, Module& out) {
173+ std::vector<Index> fileIndexMap;
174+ std::unordered_map<std::string, Index> debugInfoFileIndices;
175+ for (Index i = 0 ; i < out.debugInfoFileNames .size (); i++) {
176+ debugInfoFileIndices[out.debugInfoFileNames [i]] = i;
177+ }
178+ for (Index i = 0 ; i < in.debugInfoFileNames .size (); i++) {
179+ std::string file = in.debugInfoFileNames [i];
180+ auto iter = debugInfoFileIndices.find (file);
181+ if (iter == debugInfoFileIndices.end ()) {
182+ Index index = out.debugInfoFileNames .size ();
183+ out.debugInfoFileNames .push_back (file);
184+ debugInfoFileIndices[file] = index;
185+ }
186+ fileIndexMap.push_back (debugInfoFileIndices[file]);
187+ }
188+
173189 for (auto & curr : in.functions ) {
174- copyFunction (curr.get (), out, Name (), indexMap );
190+ copyFunction (curr.get (), out, Name (), fileIndexMap );
175191 }
176192 for (auto & curr : in.globals ) {
177193 copyGlobal (curr.get (), out);
0 commit comments