Skip to content

Commit ccd3def

Browse files
[clangd] Avoid round-trip from SourceLocation to clangd::Range and back in SymbolCollector::handleMacros() (#127757)
1 parent d1889cf commit ccd3def

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

clang-tools-extra/clangd/CollectMacros.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
namespace clang {
1919
namespace clangd {
2020

21-
Range MacroOccurrence::toRange(const SourceManager &SM) const {
21+
CharSourceRange MacroOccurrence::toSourceRange(const SourceManager &SM) const {
2222
auto MainFile = SM.getMainFileID();
23-
return halfOpenToRange(
24-
SM, syntax::FileRange(MainFile, StartOffset, EndOffset).toCharRange(SM));
23+
return syntax::FileRange(MainFile, StartOffset, EndOffset).toCharRange(SM);
24+
}
25+
26+
Range MacroOccurrence::toRange(const SourceManager &SM) const {
27+
return halfOpenToRange(SM, toSourceRange(SM));
2528
}
2629

2730
void CollectMainFileMacros::add(const Token &MacroNameTok, const MacroInfo *MI,

clang-tools-extra/clangd/CollectMacros.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct MacroOccurrence {
3131
// True if the occurence is used in a conditional directive, e.g. #ifdef MACRO
3232
bool InConditionalDirective;
3333

34+
CharSourceRange toSourceRange(const SourceManager &SM) const;
3435
Range toRange(const SourceManager &SM) const;
3536
};
3637

clang-tools-extra/clangd/index/SymbolCollector.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) {
713713
// Add macro references.
714714
for (const auto &IDToRefs : MacroRefsToIndex.MacroRefs) {
715715
for (const auto &MacroRef : IDToRefs.second) {
716-
const auto &Range = MacroRef.toRange(SM);
716+
const auto &SR = MacroRef.toSourceRange(SM);
717+
auto Range = halfOpenToRange(SM, SR);
717718
bool IsDefinition = MacroRef.IsDefinition;
718719
Ref R;
719720
R.Location.Start.setLine(Range.start.line);
@@ -726,9 +727,7 @@ void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) {
726727
if (IsDefinition) {
727728
Symbol S;
728729
S.ID = IDToRefs.first;
729-
auto StartLoc = cantFail(sourceLocationInMainFile(SM, Range.start));
730-
auto EndLoc = cantFail(sourceLocationInMainFile(SM, Range.end));
731-
S.Name = toSourceCode(SM, SourceRange(StartLoc, EndLoc));
730+
S.Name = toSourceCode(SM, SR.getAsRange());
732731
S.SymInfo.Kind = index::SymbolKind::Macro;
733732
S.SymInfo.SubKind = index::SymbolSubKind::None;
734733
S.SymInfo.Properties = index::SymbolPropertySet();

0 commit comments

Comments
 (0)