Skip to content

Commit 5623dd9

Browse files
committed
[NFC] address review comments via source code comments
1 parent aa7d0bd commit 5623dd9

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class SwiftInterfaceGenContext::Implementation {
6868
std::string Text;
6969
std::vector<TextReference> References;
7070
std::vector<TextDecl> Decls;
71+
/// Do not clear the map or erase elements, without keeping the Decls vector in sync
7172
llvm::StringMap<TextDecl> USRMap;
7273
};
7374

@@ -160,6 +161,9 @@ class AnnotatingPrinter : public StreamPrinter {
160161
DeclUSRs.emplace_back(VD, USR);
161162
auto iterator = Info.USRMap.insert_or_assign(USR, Entry).first;
162163
// Set the USR in the declarations to the key in the USRMap, because the lifetime of that matches/exceeds the lifetime of Decls
164+
// String keys in the StringMap are heap allocated and only get destroyed on explicit erase() or clear() calls, or on destructor calls
165+
// (the ProgrammersManual description itself also states that StringMap "only ever copies a string if a value is inserted").
166+
// Thus this never results in a dangling reference, as the USRMap is never cleared and no elements are erased in its lifetime.
163167
Info.Decls.back().USR = iterator->getKey();
164168
}
165169
}

0 commit comments

Comments
 (0)