Skip to content

Commit

Permalink
fix(cxx): use correct definition anchor spans for lambdas (#5890)
Browse files Browse the repository at this point in the history
  • Loading branch information
justbuchanan committed Oct 12, 2023
1 parent 9953914 commit 3d0a2d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
17 changes: 14 additions & 3 deletions kythe/cxx/indexer/cxx/IndexerASTHooks.cc
Expand Up @@ -3469,10 +3469,21 @@ bool IndexerASTVisitor::VisitFunctionDecl(clang::FunctionDecl* Decl) {

if (IsFunctionDefinition && !Decl->isImplicit() &&
Decl->getBody() != nullptr) {
// For lambdas, Decl does not contain the correct SourceRange directly.
// Instead, we look at the "synthesized" lambda class and use it's beginning
// location as the start of the source range.
clang::SourceRange Range = Decl->getSourceRange();
if (clang::CXXMethodDecl* MethodDecl =
llvm::dyn_cast<clang::CXXMethodDecl>(Decl)) {
const clang::CXXRecordDecl* Parent = MethodDecl->getParent();
if (Parent->isLambda()) {
Range.setBegin(Parent->getSourceRange().getBegin());
}
}

SourceRange DefinitionRange = NormalizeRange(
{TemplateKeywordLoc.isValid() ? TemplateKeywordLoc
: Decl->getSourceRange().getBegin(),
Decl->getSourceRange().getEnd()});
{TemplateKeywordLoc.isValid() ? TemplateKeywordLoc : Range.getBegin(),
Range.getEnd()});
auto DefinitionRangeInContext =
RangeInCurrentContext(Decl->isImplicit(), DeclNode, DefinitionRange);
MaybeRecordFullDefinitionRange(DefinitionRangeInContext, DeclNode,
Expand Down
1 change: 1 addition & 0 deletions kythe/cxx/indexer/cxx/testdata/BUILD
Expand Up @@ -962,6 +962,7 @@ cc_indexer_test(
cc_indexer_test(
name = "lambda_test",
srcs = ["basic/lambda.cc"],
ignore_dups = True,
tags = ["basic"],
)

Expand Down
16 changes: 13 additions & 3 deletions kythe/cxx/indexer/cxx/testdata/basic/lambda.cc
@@ -1,4 +1,14 @@
// TODO(justinbuchanan): fix indexer to include leading '[](' in lambda anchor
//- @") {}" defines Lambda
//- Lambda.node/kind function
//- @"[]() {}" defines LambdaFn
//- LambdaFn.node/kind function
auto fn = []() {};

//- @"[]() -> int { return 0; }" defines LambdaFn2
//- LambdaFn2.node/kind function
auto fn2 = []() -> int { return 0; };

void outer() {
int i = 0;
//- @"[&i]() {}" defines LambdaFn3
//- LambdaFn3.node/kind function
auto fn3 = [&i]() {};
}

0 comments on commit 3d0a2d9

Please sign in to comment.