[clangd] Avoid round-trip from SourceLocation to clangd::Range and back in SymbolCollector::handleMacros() (#127757)

This commit is contained in:
Nathan Ridge
2025-02-19 12:57:38 -05:00
committed by GitHub
parent d1889cf935
commit ccd3defd8f
3 changed files with 10 additions and 7 deletions

View File

@@ -18,10 +18,13 @@
namespace clang {
namespace clangd {
Range MacroOccurrence::toRange(const SourceManager &SM) const {
CharSourceRange MacroOccurrence::toSourceRange(const SourceManager &SM) const {
auto MainFile = SM.getMainFileID();
return halfOpenToRange(
SM, syntax::FileRange(MainFile, StartOffset, EndOffset).toCharRange(SM));
return syntax::FileRange(MainFile, StartOffset, EndOffset).toCharRange(SM);
}
Range MacroOccurrence::toRange(const SourceManager &SM) const {
return halfOpenToRange(SM, toSourceRange(SM));
}
void CollectMainFileMacros::add(const Token &MacroNameTok, const MacroInfo *MI,

View File

@@ -31,6 +31,7 @@ struct MacroOccurrence {
// True if the occurence is used in a conditional directive, e.g. #ifdef MACRO
bool InConditionalDirective;
CharSourceRange toSourceRange(const SourceManager &SM) const;
Range toRange(const SourceManager &SM) const;
};

View File

@@ -713,7 +713,8 @@ void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) {
// Add macro references.
for (const auto &IDToRefs : MacroRefsToIndex.MacroRefs) {
for (const auto &MacroRef : IDToRefs.second) {
const auto &Range = MacroRef.toRange(SM);
const auto &SR = MacroRef.toSourceRange(SM);
auto Range = halfOpenToRange(SM, SR);
bool IsDefinition = MacroRef.IsDefinition;
Ref R;
R.Location.Start.setLine(Range.start.line);
@@ -726,9 +727,7 @@ void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) {
if (IsDefinition) {
Symbol S;
S.ID = IDToRefs.first;
auto StartLoc = cantFail(sourceLocationInMainFile(SM, Range.start));
auto EndLoc = cantFail(sourceLocationInMainFile(SM, Range.end));
S.Name = toSourceCode(SM, SourceRange(StartLoc, EndLoc));
S.Name = toSourceCode(SM, SR.getAsRange());
S.SymInfo.Kind = index::SymbolKind::Macro;
S.SymInfo.SubKind = index::SymbolSubKind::None;
S.SymInfo.Properties = index::SymbolPropertySet();