Skip to content

Commit 956e81c

Browse files
committed
Add API to get the "outermost" parent source file.
1 parent bf61578 commit 956e81c

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

include/swift/AST/DeclContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,12 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
519519
LLVM_READONLY
520520
SourceFile *getParentSourceFile() const;
521521

522+
/// Returns the "outermost" source file that contains this context,
523+
/// looking through any source files for generated code, such as
524+
/// macro expansions.
525+
LLVM_READONLY
526+
SourceFile *getOutermostParentSourceFile() const;
527+
522528
/// Determine whether this declaration context is generic, meaning that it or
523529
/// any of its parents have generic parameters.
524530
bool isGenericContext() const;

lib/AST/DeclContext.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,18 @@ SourceFile *DeclContext::getParentSourceFile() const {
323323
return fallbackSF;
324324
}
325325

326+
SourceFile *DeclContext::getOutermostParentSourceFile() const {
327+
auto sf = getParentSourceFile();
328+
if (!sf)
329+
return nullptr;
330+
331+
// Find the originating source file.
332+
while (auto enclosingSF = sf->getEnclosingSourceFile())
333+
sf = enclosingSF;
334+
335+
return sf;
336+
}
337+
326338
DeclContext *DeclContext::getModuleScopeContext() const {
327339
auto DC = const_cast<DeclContext*>(this);
328340

lib/IRGen/IRGenModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1919,10 +1919,11 @@ IRGenModule *IRGenerator::getGenModule(DeclContext *ctxt) {
19191919
if (GenModules.size() == 1 || !ctxt) {
19201920
return getPrimaryIGM();
19211921
}
1922-
SourceFile *SF = ctxt->getParentSourceFile();
1922+
SourceFile *SF = ctxt->getOutermostParentSourceFile();
19231923
if (!SF) {
19241924
return getPrimaryIGM();
19251925
}
1926+
19261927
IRGenModule *IGM = GenModules[SF];
19271928
assert(IGM);
19281929
return IGM;

0 commit comments

Comments
 (0)