[clang-tools-extra] [clangd] Resolve the dependent type from its single instantiation. Take 1 (PR #71279)

Nathan Ridge via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 27 00:50:31 PST 2023


HighCommander4 wrote:

Thinking some more about "Approach 2", I realized there is a challenge with it: given the node we are trying to resolve (for example, a dependent member expr) as a starting point, we need to find the enclosing template decl, so that we can query it for its "only instantiation".

The current patch does this by introspecting the starting node, and in a subset of cases (base expression is `MemberExpr` or `DeclRefExpr`) extracting a `Decl` from it, and then walking its `DeclContext` chain to find the template decl.

However, we would ideally like our "only instantiation" heuristic to work for any AST node inside a template (and so, for dependent member exprs, we would like it to work for any base expression type, not just specific ones).

In principle, it could be possible to extend the introspection approach to handle all expression types, since if an expression is dependent, it will ultimately reference a template parameter (or a Decl whose type references a template parameter, etc.) However, I feel like this may be a lot of work to implement and could be error-prone.

I wonder if we could do something easier: require that the caller pass in a `DeclContext*` which it knows to be enclosing the node we are trying to resolve (and then we can walk the chain from there to find the template).

Editor operations like go-to-def which involve taking an action at a cursor position use `SelectionTree` to "hit-test" the node under the cursor. `SelectionTree` exposes the chain of node that was hit (see e.g. `SelectionTree::print()` which prints the chain), so we can recover an enclosing `DeclContext*` from there; we'd just need to plumb the information through to `HeuristicResolver`.

In the future when we want to use `HeuristicResolver` for code completion, it can get the enclosing `DeclContext*` even more easily (`Sema::CurContext`).

https://github.com/llvm/llvm-project/pull/71279


More information about the cfe-commits mailing list