[Source manager] Start tracking generated-source info in the source manager

Establish the relationship for generated sources, whether for macro
expansions or (via a small stretch) replacing function bodies with
other bodies, in the source manager itself. This makes the information
available for diagnostic rendering, and unifies a little bit of the
representation, although it isn't used for much yet.
This commit is contained in:
Doug Gregor
2022-12-14 23:02:27 -08:00
parent cb3a24edda
commit f467ef4d37
9 changed files with 132 additions and 27 deletions

View File

@@ -877,10 +877,20 @@ void SourceFile::lookupClassMembers(ImportPath::Access accessPath,
cache.lookupClassMembers(accessPath, consumer);
}
SourceFile *SourceFile::getEnclosingSourceFile() const {
ASTNode SourceFile::getMacroExpansion() const {
if (Kind != SourceFileKind::MacroExpansion)
return nullptr;
auto genInfo =
*getASTContext().SourceMgr.getGeneratedSourceInfo(*getBufferID());
return ASTNode::getFromOpaqueValue(genInfo.astNode);
}
SourceFile *SourceFile::getEnclosingSourceFile() const {
auto macroExpansion = getMacroExpansion();
if (!macroExpansion)
return nullptr;
auto sourceLoc = macroExpansion.getStartLoc();
return getParentModule()->getSourceFileContainingLocation(sourceLoc);
}
@@ -3263,17 +3273,13 @@ ModuleDecl::computeFileIDMap(bool shouldDiagnose) const {
SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
Optional<unsigned> bufferID,
ParsingOptions parsingOpts, bool isPrimary,
ASTNode macroExpansion)
ParsingOptions parsingOpts, bool isPrimary)
: FileUnit(FileUnitKind::Source, M), BufferID(bufferID ? *bufferID : -1),
ParsingOpts(parsingOpts), IsPrimary(isPrimary),
macroExpansion(macroExpansion), Kind(K) {
ParsingOpts(parsingOpts), IsPrimary(isPrimary), Kind(K) {
M.getASTContext().addDestructorCleanup(*this);
assert(!IsPrimary || M.isMainModule() &&
"A primary cannot appear outside the main module");
assert(macroExpansion.isNull() == (K != SourceFileKind::MacroExpansion) &&
"Macro expansions always need an expansion node");
if (isScriptMode()) {
bool problem = M.registerEntryPointFile(this, SourceLoc(), None);