Skip to content

Commit b267a8a

Browse files
fix: Emit defs/refs for function-local types (#292)
At the moment, we're still not emitting defs and refs for dependent expressions, because it seems like they're unresolved in the AST. I need to look at what clangd is doing here, because it does support code navigation for such expressions.
1 parent 256f6e4 commit b267a8a

File tree

6 files changed

+110
-3
lines changed

6 files changed

+110
-3
lines changed

indexer/SymbolFormatter.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ std::optional<std::string_view> SymbolFormatter::getSymbolCached(
246246

247247
std::optional<std::string_view>
248248
SymbolFormatter::getContextSymbol(const clang::DeclContext &declContext) {
249-
if (auto namespaceDecl = llvm::dyn_cast<clang::NamespaceDecl>(&declContext)) {
249+
if (auto *namespaceDecl =
250+
llvm::dyn_cast<clang::NamespaceDecl>(&declContext)) {
250251
return this->getNamespaceSymbol(*namespaceDecl);
251252
}
252-
if (auto tagDecl = llvm::dyn_cast<clang::TagDecl>(&declContext)) {
253+
if (auto *tagDecl = llvm::dyn_cast<clang::TagDecl>(&declContext)) {
253254
return this->getTagSymbol(*tagDecl);
254255
}
255256
if (llvm::isa<clang::TranslationUnitDecl>(declContext)
@@ -264,16 +265,22 @@ SymbolFormatter::getContextSymbol(const clang::DeclContext &declContext) {
264265
return std::string(this->scratchBuffer);
265266
});
266267
}
268+
if (auto *functionDecl = llvm::dyn_cast<clang::FunctionDecl>(&declContext)) {
269+
// TODO: Strictly speaking, we should return some information marking
270+
// the symbol as local, but it shouldn't be possible to create spurious
271+
// references, so this is OK for now.
272+
return this->getFunctionSymbol(*functionDecl);
273+
}
267274
// TODO: Handle all cases of DeclContext here:
268275
// Done
269276
// - TranslationUnitDecl
270277
// - ExternCContext
271278
// - NamespaceDecl
272279
// - TagDecl
280+
// - FunctionDecl
273281
// Pending:
274282
// - OMPDeclareReductionDecl
275283
// - OMPDeclareMapperDecl
276-
// - FunctionDecl
277284
// - ObjCMethodDecl
278285
// - ObjCContainerDecl
279286
// - LinkageSpecDecl

test/index/functions/body.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
void f() {
2+
struct C {
3+
int plain_field;
4+
5+
void g() {};
6+
};
7+
8+
(void)C().plain_field;
9+
C().g();
10+
11+
int x = 0;
12+
(void)(2 * x);
13+
}

test/index/functions/body.snapshot.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
void f() {
2+
//^^^^ definition [..] `<file>/body.cc`/
3+
// ^ definition [..] f(49f6e7a06ebc5aa8).
4+
struct C {
5+
// ^ definition [..] f(49f6e7a06ebc5aa8).C#
6+
int plain_field;
7+
// ^^^^^^^^^^^ definition [..] f(49f6e7a06ebc5aa8).C#plain_field.
8+
9+
void g() {};
10+
// ^ definition [..] f(49f6e7a06ebc5aa8).C#g(49f6e7a06ebc5aa8).
11+
};
12+
13+
(void)C().plain_field;
14+
// ^ reference [..] f(49f6e7a06ebc5aa8).C#
15+
// ^^^^^^^^^^^ reference [..] f(49f6e7a06ebc5aa8).C#plain_field.
16+
C().g();
17+
// ^ reference [..] f(49f6e7a06ebc5aa8).C#
18+
// ^ reference [..] f(49f6e7a06ebc5aa8).C#g(49f6e7a06ebc5aa8).
19+
20+
int x = 0;
21+
// ^ definition local 0
22+
(void)(2 * x);
23+
// ^ reference local 0
24+
}

test/index/functions/template_body.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
template <typename T>
2+
void f() {
3+
struct C {
4+
int plain_field;
5+
T dependent_field;
6+
7+
void g() {};
8+
};
9+
10+
(void)C().plain_field;
11+
(void)C().dependent_field;
12+
C().g();
13+
14+
int x = 0;
15+
(void)(2 * x);
16+
17+
// The following are not allowed:
18+
// - Templated function-local classes
19+
// - Templates inside function-local classes
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
template <typename T>
2+
//^^^^^^^^ definition [..] `<file>/template_body.cc`/
3+
// ^ definition local 0
4+
void f() {
5+
// ^ definition [..] f(49f6e7a06ebc5aa8).
6+
struct C {
7+
// ^ definition [..] f(49f6e7a06ebc5aa8).C#
8+
int plain_field;
9+
// ^^^^^^^^^^^ definition [..] f(49f6e7a06ebc5aa8).C#plain_field.
10+
T dependent_field;
11+
// ^ reference local 0
12+
// ^^^^^^^^^^^^^^^ definition [..] f(49f6e7a06ebc5aa8).C#dependent_field.
13+
14+
void g() {};
15+
// ^ definition [..] f(49f6e7a06ebc5aa8).C#g(49f6e7a06ebc5aa8).
16+
};
17+
18+
(void)C().plain_field;
19+
// ^ reference [..] f(49f6e7a06ebc5aa8).C#
20+
(void)C().dependent_field;
21+
// ^ reference [..] f(49f6e7a06ebc5aa8).C#
22+
C().g();
23+
// ^ reference [..] f(49f6e7a06ebc5aa8).C#
24+
25+
int x = 0;
26+
// ^ definition local 1
27+
(void)(2 * x);
28+
// ^ reference local 1
29+
30+
// The following are not allowed:
31+
// - Templated function-local classes
32+
// - Templates inside function-local classes
33+
}

test/index/types/types.snapshot.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,17 @@
157157
// documentation
158158
// | No documentation available.
159159
class fC {
160+
// ^^ definition [..] f(49f6e7a06ebc5aa8).fC#
161+
// documentation
162+
// | No documentation available.
160163
void fCf() {
164+
// ^^^ definition [..] f(49f6e7a06ebc5aa8).fC#fCf(49f6e7a06ebc5aa8).
165+
// documentation
166+
// | No documentation available.
161167
class fCfC { };
168+
// ^^^^ definition [..] f(49f6e7a06ebc5aa8).fC#fCf(49f6e7a06ebc5aa8).fCfC#
169+
// documentation
170+
// | No documentation available.
162171
}
163172
};
164173
}
@@ -332,5 +341,6 @@
332341
};
333342
return ignore_first("", L{});
334343
// ^^^^^^^^^^^^ reference local 3
344+
// ^ reference [..] trailing_return_type(693bfa61ed1914d5).$anonymous_type_4#`operator()`(dc97d1a1ce4cdab3).
335345
// ^ reference [..] L#
336346
}

0 commit comments

Comments
 (0)