[IDE] Pass a SourceRange instead of a CharSourceRange in SemaAnnotator::passReference

Most `SemaAnnotator`s don’t actually care about the char source range. Instead, they only care about the start location of the reference, which is also included in `SourceRange`. Computing a `CharSourceRange` from a `SourceRange` is kind of expensive because it needs to start a new lexer.

To avoid this overhead, pass `SourceRange` to `SemaAnnotator::passReference` and related functions and let the clients compute the `CharSourceRange` when needed.

This reduces the overhead of index-while-building by about 10%.
This commit is contained in:
Alex Hoppen
2025-06-04 12:34:07 +02:00
parent 9ae142c9d3
commit c6c40de73d
10 changed files with 95 additions and 88 deletions

View File

@@ -990,8 +990,8 @@ public:
}
}
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef, Type T,
bool visitDeclReference(ValueDecl *D, SourceRange Range, TypeDecl *CtorTyRef,
ExtensionDecl *ExtTyRef, Type T,
ReferenceMetaData Data) override {
if (Data.isImplicit)
return true;
@@ -1004,9 +1004,12 @@ public:
if (D->isUnavailable())
return true;
CharSourceRange CharRange = Lexer::getCharSourceRangeFromSourceRange(
D->getASTContext().SourceMgr, Range);
auto &SM = D->getASTContext().SourceMgr;
if (D == D->getASTContext().getOptionalNoneDecl() &&
SM.extractText(Range, BufferID) == "nil") {
SM.extractText(CharRange, BufferID) == "nil") {
// If a 'nil' literal occurs in a swift-case statement, it gets replaced
// by a reference to 'Optional.none' in the AST. We want to continue
// highlighting 'nil' as a keyword and not as an enum element.
@@ -1015,11 +1018,11 @@ public:
if (CtorTyRef)
D = CtorTyRef;
annotate(D, /*IsRef=*/true, Range);
annotate(D, /*IsRef=*/true, CharRange);
return true;
}
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
bool visitSubscriptReference(ValueDecl *D, SourceRange Range,
ReferenceMetaData Data,
bool IsOpenBracket) override {
// We should treat both open and close brackets equally