[SourceKit] Return the original location for decls within generated code

Pass back the original location (ie. where the macro was expanded, not
the location that the generated code would be inserted) for cursor info
and indexing. Also mark any declarations/references within generated
source as implicit.

Resolves rdar://107209132.
This commit is contained in:
Ben Barham
2023-03-17 15:34:36 -07:00
parent 1d7ef251d5
commit b9810fb0a3
10 changed files with 302 additions and 60 deletions

View File

@@ -899,12 +899,13 @@ static void setLocationInfo(const ValueDecl *VD,
NameLen = getCharLength(SM, Loc);
}
unsigned DeclBufID = SM.findBufferContainingLoc(Loc);
auto [DeclBufID, DeclLoc] =
VD->getModuleContext()->getOriginalLocation(Loc);
Location.Filename = SM.getIdentifierForBuffer(DeclBufID);
Location.Offset = SM.getLocOffsetInBuffer(Loc, DeclBufID);
Location.Offset = SM.getLocOffsetInBuffer(DeclLoc, DeclBufID);
Location.Length = NameLen;
std::tie(Location.Line, Location.Column) = SM.getLineAndColumnInBuffer(
Loc, DeclBufID);
std::tie(Location.Line, Location.Column) =
SM.getLineAndColumnInBuffer(DeclLoc, DeclBufID);
if (auto GeneratedSourceInfo = SM.getGeneratedSourceInfo(DeclBufID)) {
if (GeneratedSourceInfo->kind ==
GeneratedSourceInfo::ReplacedFunctionBody) {
@@ -918,9 +919,8 @@ static void setLocationInfo(const ValueDecl *VD,
auto GeneratedStartOffset = SM.getLocOffsetInBuffer(
GeneratedSourceInfo->generatedSourceRange.getStart(), DeclBufID);
Location.Offset += OriginalStartOffset - GeneratedStartOffset;
assert(SM.findBufferContainingLoc(Loc) == DeclBufID);
std::tie(Location.Line, Location.Column) =
SM.getPresumedLineAndColumnForLoc(Loc, DeclBufID);
SM.getPresumedLineAndColumnForLoc(DeclLoc, DeclBufID);
}
}
} else if (ClangNode) {