RFC: Upstream HeuristicResolver from clangd to libSema

HeuristicResolver is a code component in clangd used to aid code navigation features in templated code by heuristically associating dependent names with the declarations they’re likely to target.

It’s a standalone component with no dependencies on anything else in clangd. As of PR121313, it also has a unit test suite with no dependencies on anything else in clangd.

I would like to propose upstreaming HeuristicResolver to clang/lib/Sema, with the public interface exposed in clang/include/Sema.

The main advantage of doing so is that code outside of clangd would be able to use HeuristicResolver. A notable example of in-tree code that would benefit from doing so is SemaCodeComplete.cpp, where being able to use HeuristicResolver would allow making various improvements to code completion behaviour in templated code, such as Code completion not working with smart pointers in template class header files in VS Code · Issue #810 · clangd/clangd · GitHub.

A secondary advantage would be better integration between HeuristicResolver and other code in the clang libraries that performs similar tasks. For example, CXXRecordDecl::lookupDependentName() implements a subset of the same heuristics, and the two could share code after upstreaming.

I have a patch implementing the proposed upstreaming, and a draft patch implementing an example improvement to SemaCodeComplete.cpp that uses HeuristicResolver.

This was previously discussed in the clangd issue tracker.

Any feedback on the proposed upstreaming is welcome.

6 Likes

Thanks! This will allow out-of-tree language servers and complete engines to use the feature.

1 Like

Thank you! I think it’s a reasonable thing to do so long as it’s actually being used somewhere in Clang itself (it would be at risk of being removed as dead code if it to lived in Clang but was not be used anywhere).

2 Likes

Makes sense. That will be covered by PR121315 which adds a usage to SemaCodeComplete (and likely additional usages in SemaCodeComplete coming after that as I fix other “completion in templated code” issues in clangd).

The upstreaming PR has now merged. Thank you all for your feedback and encouragement!