[clangd] Remove calls to getFileLoc() in declToSym() (#83532)

toHalfOpenFileRange() already handles translating macro locations to
file locations, and it can provide a better result by knowing about both
ends of the range.

Fixes https://github.com/clangd/clangd/issues/1941
This commit is contained in:
Nathan Ridge 2024-03-05 23:12:43 -05:00 committed by GitHub
parent 5549b01736
commit d1aec79a2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -223,8 +223,8 @@ std::string getSymbolDetail(ASTContext &Ctx, const NamedDecl &ND) {
std::optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) {
auto &SM = Ctx.getSourceManager();
SourceLocation BeginLoc = SM.getFileLoc(ND.getBeginLoc());
SourceLocation EndLoc = SM.getFileLoc(ND.getEndLoc());
SourceLocation BeginLoc = ND.getBeginLoc();
SourceLocation EndLoc = ND.getEndLoc();
const auto SymbolRange =
toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
if (!SymbolRange)

View File

@ -750,6 +750,9 @@ TEST(DocumentSymbols, RangeFromMacro) {
$fullDef[[FF3() {
int var = 42;
}]]
#define FF4(name) int name = 0
$FooRange[[FF4($FooSelectionRange[[foo]])]];
)");
TU.Code = Main.code().str();
EXPECT_THAT(
@ -766,7 +769,11 @@ TEST(DocumentSymbols, RangeFromMacro) {
AllOf(withName("FF3"), withDetail("()"),
symRange(Main.range("fullDef")),
children(AllOf(withName("waldo"), withDetail("void ()"),
symRange(Main.range("fullDef")))))));
symRange(Main.range("fullDef"))))),
AllOf(
withName("FF4"), withDetail("(foo)"),
children(AllOf(withName("foo"), symRange(Main.range("FooRange")),
symNameRange(Main.range("FooSelectionRange")))))));
}
TEST(DocumentSymbols, FuncTemplates) {