[ASTGen] Support macro expanded buffer

* Make ExportedSourceFile hold any Syntax as the root node
* Move `ExportedSourceFileRequest::evaluate()` to `ParseRequests.cpp`
* Pass  the decl context and `GeneatedSourceFileInfo::Kind` to
  `swift_ASTGen_parseSourceFile()` to customize the parsing
* Make `ExportedSourceFile` to hold an arbitrary Syntax node
* Move round-trip checking into `ExportedSourceFileRequest::evaluate()`
* Split `parseSourceFileViaASTGen` completely from C++ parsing logic
  (in `ParseSourceFileRequest::evaluate()`)
* Remove 'ParserDiagnostics' experimental feature: Now that we have
  ParserASTGen mode which includes the swift-syntax parser diagnostics.
This commit is contained in:
Rintaro Ishizaki
2024-10-28 16:45:15 -07:00
parent 919ec930f9
commit e566a746c8
21 changed files with 408 additions and 297 deletions

View File

@@ -1073,6 +1073,10 @@ void SourceFile::lookupClassMembers(ImportPath::Access accessPath,
cache.lookupClassMembers(accessPath, consumer);
}
const GeneratedSourceInfo *SourceFile::getGeneratedSourceFileInfo() const {
return getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
}
ASTNode SourceFile::getMacroExpansion() const {
if (Kind != SourceFileKind::MacroExpansion)
return nullptr;
@@ -1084,9 +1088,7 @@ SourceRange SourceFile::getMacroInsertionRange() const {
if (Kind != SourceFileKind::MacroExpansion)
return SourceRange();
auto generatedInfo =
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
auto origRange = generatedInfo.originalSourceRange;
auto origRange = getGeneratedSourceFileInfo()->originalSourceRange;
return {origRange.getStart(), origRange.getEnd()};
}
@@ -1094,18 +1096,14 @@ CustomAttr *SourceFile::getAttachedMacroAttribute() const {
if (Kind != SourceFileKind::MacroExpansion)
return nullptr;
auto genInfo =
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
return genInfo.attachedMacroCustomAttr;
return getGeneratedSourceFileInfo()->attachedMacroCustomAttr;
}
std::optional<MacroRole> SourceFile::getFulfilledMacroRole() const {
if (Kind != SourceFileKind::MacroExpansion)
return std::nullopt;
auto genInfo =
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
switch (genInfo.kind) {
switch (getGeneratedSourceFileInfo()->kind) {
#define MACRO_ROLE(Name, Description) \
case GeneratedSourceInfo::Name##MacroExpansion: \
return MacroRole::Name;
@@ -1123,9 +1121,7 @@ SourceFile *SourceFile::getEnclosingSourceFile() const {
Kind != SourceFileKind::DefaultArgument)
return nullptr;
auto genInfo =
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
auto sourceLoc = genInfo.originalSourceRange.getStart();
auto sourceLoc = getGeneratedSourceFileInfo()->originalSourceRange.getStart();
return getParentModule()->getSourceFileContainingLocation(sourceLoc);
}
@@ -1134,9 +1130,7 @@ ASTNode SourceFile::getNodeInEnclosingSourceFile() const {
Kind != SourceFileKind::DefaultArgument)
return nullptr;
auto genInfo =
*getASTContext().SourceMgr.getGeneratedSourceInfo(getBufferID());
return ASTNode::getFromOpaqueValue(genInfo.astNode);
return ASTNode::getFromOpaqueValue(getGeneratedSourceFileInfo()->astNode);
}
void ModuleDecl::lookupClassMember(ImportPath::Access accessPath,
@@ -3573,6 +3567,11 @@ StringRef SourceFile::getFilename() const {
return SM.getIdentifierForBuffer(BufferID);
}
StringRef SourceFile::getBuffer() const {
SourceManager &SM = getASTContext().SourceMgr;
return SM.getEntireTextForBuffer(BufferID);
}
ASTScope &SourceFile::getScope() {
if (!Scope)
Scope = new (getASTContext()) ASTScope(this);