Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clang-tools-extra/clangd/CollectMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/CollectMacros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
7 changes: 3 additions & 4 deletions clang-tools-extra/clangd/index/SymbolCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down