[clang-tools-extra] ccd3def - [clangd] Avoid round-trip from SourceLocation to clangd::Range and back in SymbolCollector::handleMacros() (#127757)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 19 09:57:41 PST 2025
Author: Nathan Ridge
Date: 2025-02-19T12:57:38-05:00
New Revision: ccd3defd8f7da2e825f167e488827efa0df6b62c
URL: https://github.com/llvm/llvm-project/commit/ccd3defd8f7da2e825f167e488827efa0df6b62c
DIFF: https://github.com/llvm/llvm-project/commit/ccd3defd8f7da2e825f167e488827efa0df6b62c.diff
LOG: [clangd] Avoid round-trip from SourceLocation to clangd::Range and back in SymbolCollector::handleMacros() (#127757)
Added:
Modified:
clang-tools-extra/clangd/CollectMacros.cpp
clang-tools-extra/clangd/CollectMacros.h
clang-tools-extra/clangd/index/SymbolCollector.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/CollectMacros.cpp b/clang-tools-extra/clangd/CollectMacros.cpp
index 96298ee3ea50a..1e7d765f0b6f1 100644
--- a/clang-tools-extra/clangd/CollectMacros.cpp
+++ b/clang-tools-extra/clangd/CollectMacros.cpp
@@ -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,
diff --git a/clang-tools-extra/clangd/CollectMacros.h b/clang-tools-extra/clangd/CollectMacros.h
index e7198641d8d53..20a3fc24d759c 100644
--- a/clang-tools-extra/clangd/CollectMacros.h
+++ b/clang-tools-extra/clangd/CollectMacros.h
@@ -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;
};
diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 1de7faf81746e..3f5633357073d 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -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();
More information about the cfe-commits
mailing list