@@ -45,18 +45,14 @@ class SwiftDispatcher {
4545 // the SwiftDispatcher
4646 SwiftDispatcher (const swift::SourceManager& sourceManager,
4747 TrapDomain& trap,
48+ SwiftLocationExtractor& locationExtractor,
4849 swift::ModuleDecl& currentModule,
4950 swift::SourceFile* currentPrimarySourceFile = nullptr )
5051 : sourceManager{sourceManager},
5152 trap{trap},
53+ locationExtractor{locationExtractor},
5254 currentModule{currentModule},
53- currentPrimarySourceFile{currentPrimarySourceFile},
54- locationExtractor (trap) {
55- if (currentPrimarySourceFile) {
56- // we make sure the file is in the trap output even if the source is empty
57- locationExtractor.emitFile (currentPrimarySourceFile->getFilename ());
58- }
59- }
55+ currentPrimarySourceFile{currentPrimarySourceFile} {}
6056
6157 const std::unordered_set<swift::ModuleDecl*> getEncounteredModules () && {
6258 return std::move (encounteredModules);
@@ -157,7 +153,7 @@ class SwiftDispatcher {
157153 // TODO when everything is moved to structured C++ classes, this should be moved to createEntry
158154 if (auto l = store.get (e)) {
159155 if constexpr (IsLocatable<E>) {
160- attachLocation (e, *l);
156+ locationExtractor. attachLocation (sourceManager, e, *l);
161157 }
162158 return *l;
163159 }
@@ -206,50 +202,10 @@ class SwiftDispatcher {
206202 template <typename E, typename ... Args>
207203 auto createUncachedEntry (const E& e, Args&&... args) {
208204 auto label = trap.createLabel <TrapTagOf<E>>(std::forward<Args>(args)...);
209- attachLocation (&e, label);
205+ locationExtractor. attachLocation (sourceManager, &e, label);
210206 return TrapClassOf<E>{label};
211207 }
212208
213- template <typename Locatable>
214- void attachLocation (Locatable locatable, TrapLabel<LocatableTag> locatableLabel) {
215- attachLocation (&locatable, locatableLabel);
216- }
217-
218- // Emits a Location TRAP entry and attaches it to a `Locatable` trap label
219- template <typename Locatable>
220- void attachLocation (Locatable* locatable, TrapLabel<LocatableTag> locatableLabel) {
221- attachLocation (locatable->getStartLoc (), locatable->getEndLoc (), locatableLabel);
222- }
223-
224- void attachLocation (const swift::CapturedValue* capture, TrapLabel<LocatableTag> locatableLabel) {
225- attachLocation (capture->getLoc (), locatableLabel);
226- }
227-
228- void attachLocation (const swift::IfConfigClause* clause, TrapLabel<LocatableTag> locatableLabel) {
229- attachLocation (clause->Loc , clause->Loc , locatableLabel);
230- }
231-
232- void attachLocation (swift::AvailabilitySpec* spec, TrapLabel<LocatableTag> locatableLabel) {
233- attachLocation (spec->getSourceRange ().Start , spec->getSourceRange ().End , locatableLabel);
234- }
235-
236- // Emits a Location TRAP entry and attaches it to a `Locatable` trap label for a given `SourceLoc`
237- void attachLocation (swift::SourceLoc loc, TrapLabel<LocatableTag> locatableLabel) {
238- attachLocation (loc, loc, locatableLabel);
239- }
240-
241- // Emits a Location TRAP entry for a list of swift entities and attaches it to a `Locatable` trap
242- // label
243- template <typename Locatable>
244- void attachLocation (llvm::MutableArrayRef<Locatable>* locatables,
245- TrapLabel<LocatableTag> locatableLabel) {
246- if (locatables->empty ()) {
247- return ;
248- }
249- attachLocation (locatables->front ().getStartLoc (), locatables->back ().getEndLoc (),
250- locatableLabel);
251- }
252-
253209 // return `std::optional(fetchLabel(arg))` if arg converts to true, otherwise std::nullopt
254210 // universal reference `Arg&&` is used to catch both temporary and non-const references, not
255211 // for perfect forwarding
@@ -317,7 +273,7 @@ class SwiftDispatcher {
317273 void emitComment (swift::Token& comment) {
318274 CommentsTrap entry{trap.createLabel <CommentTag>(), comment.getRawText ().str ()};
319275 trap.emit (entry);
320- attachLocation (comment. getRange (). getStart () , comment. getRange (). getEnd () , entry.id );
276+ locationExtractor. attachLocation (sourceManager , comment, entry.id );
321277 }
322278
323279 private:
@@ -333,12 +289,6 @@ class SwiftDispatcher {
333289 template <typename T>
334290 struct HasId <T, decltype (std::declval<T>().id, void ())> : std::true_type {};
335291
336- void attachLocation (swift::SourceLoc start,
337- swift::SourceLoc end,
338- TrapLabel<LocatableTag> locatableLabel) {
339- locationExtractor.attachLocation (sourceManager, start, end, locatableLabel);
340- }
341-
342292 template <typename Tag, typename ... Ts>
343293 TrapLabel<Tag> fetchLabelFromUnion (const llvm::PointerUnion<Ts...> u) {
344294 TrapLabel<Tag> ret{};
@@ -382,11 +332,11 @@ class SwiftDispatcher {
382332 const swift::SourceManager& sourceManager;
383333 TrapDomain& trap;
384334 Store store;
335+ SwiftLocationExtractor& locationExtractor;
385336 Store::Handle waitingForNewLabel{std::monostate{}};
386337 swift::ModuleDecl& currentModule;
387338 swift::SourceFile* currentPrimarySourceFile;
388339 std::unordered_set<swift::ModuleDecl*> encounteredModules;
389- SwiftLocationExtractor locationExtractor;
390340};
391341
392342} // namespace codeql
0 commit comments