Merge pull request #37105 from bnbarham/serialize-offsets

[Serialization] Store offset of decls in .swiftsourceinfo
This commit is contained in:
Ben Barham
2021-04-29 16:37:15 +10:00
committed by GitHub
18 changed files with 309 additions and 225 deletions

View File

@@ -855,37 +855,50 @@ TypeDecl *SourceFile::lookupLocalType(llvm::StringRef mangledName) const {
return nullptr;
}
Optional<BasicDeclLocs>
SourceFile::getBasicLocsForDecl(const Decl *D) const {
Optional<ExternalSourceLocs::RawLocs>
SourceFile::getExternalRawLocsForDecl(const Decl *D) const {
auto *FileCtx = D->getDeclContext()->getModuleScopeContext();
assert(FileCtx == this && "D doesn't belong to this source file");
if (FileCtx != this) {
// D doesn't belong to this file. This shouldn't happen in practice.
return None;
}
if (D->getLoc().isInvalid())
SourceLoc Loc = D->getLoc(/*SerializedOK=*/false);
if (Loc.isInvalid())
return None;
SourceManager &SM = getASTContext().SourceMgr;
BasicDeclLocs Result;
Result.SourceFilePath = SM.getDisplayNameForLoc(D->getLoc());
auto BufferID = SM.findBufferContainingLoc(Loc);
for (const auto &SRC : D->getRawComment(/*SerializedOK*/false).Comments) {
auto LineAndCol = SM.getLineAndColumnInBuffer(SRC.Range.getStart());
Result.DocRanges.push_back(
std::make_pair(SourcePosition{LineAndCol.first, LineAndCol.second},
SRC.Range.getByteLength()));
}
ExternalSourceLocs::RawLocs Result;
auto setLoc = [&](ExternalSourceLocs::RawLoc &RawLoc, SourceLoc Loc) {
if (!Loc.isValid())
return;
auto setLineColumn = [&SM](SourcePosition &Home, SourceLoc Loc) {
if (Loc.isValid()) {
std::tie(Home.Line, Home.Column) = SM.getPresumedLineAndColumnForLoc(Loc);
}
RawLoc.Offset = SM.getLocOffsetInBuffer(Loc, BufferID);
std::tie(RawLoc.Line, RawLoc.Column) = SM.getLineAndColumnInBuffer(Loc);
auto *VF = SM.getVirtualFile(Loc);
if (!VF)
return;
RawLoc.Directive.Offset =
SM.getLocOffsetInBuffer(VF->Range.getStart(), BufferID);
RawLoc.Directive.LineOffset = VF->LineOffset;
RawLoc.Directive.Length = VF->Range.getByteLength();
RawLoc.Directive.Name = StringRef(VF->Name);
};
#define SET(X) setLineColumn(Result.X, D->get##X());
SET(Loc)
SET(StartLoc)
SET(EndLoc)
#undef SET
Result.SourceFilePath = SM.getIdentifierForBuffer(BufferID);
for (const auto &SRC : D->getRawComment(/*SerializedOK=*/false).Comments) {
Result.DocRanges.emplace_back(ExternalSourceLocs::RawLoc(),
SRC.Range.getByteLength());
setLoc(Result.DocRanges.back().first, SRC.Range.getStart());
}
setLoc(Result.Loc, D->getLoc(/*SerializedOK=*/false));
setLoc(Result.StartLoc, D->getStartLoc());
setLoc(Result.EndLoc, D->getEndLoc());
return Result;
}