This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Semantic highlighting for constrained-parameter
ClosedPublic

Authored by nridge on Jan 30 2023, 12:09 AM.

Diff Detail

Event Timeline

nridge created this revision.Jan 30 2023, 12:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 30 2023, 12:09 AM
Herald added a subscriber: arphaman. · View Herald Transcript
nridge requested review of this revision.Jan 30 2023, 12:09 AM
nridge added inline comments.Jan 30 2023, 12:10 AM
clang-tools-extra/clangd/FindTarget.cpp
719

Am I using the API correctly here? It's a bit weird that ConstDeclVisitor would work differently from RecursiveASTVisitor in that VisitXXX methods for super-classes are not automatically called

kadircet added inline comments.
clang-tools-extra/clangd/FindTarget.cpp
719

right, non-recursive ast visitors are mere helpers for visiting most specific type of an ast node which is interesting to the visitor (i.e. visit method is overridden), and only that. they also don't traverse children of an entitie's entries.

which usually hints towards this being the wrong place to handle such constructs. we actually have an outer recursiveastvisitor, that tries to visit each children. but concept references seem to be missing from there.
taking a look at the RecursiveASTVisitor pieces around this piece, we actually do visit the children appropriately:

We should probably figure out how to properly visit type constraints inside the RecursiveASTVisitor (i guess we should actually be visiting TypeConstraints). @usaxena95 who's looking at C++20 features and their interactions with source tooling.

In the meanwhile, i think we should have this specialization at the outer layer, in ExplicitReferenceCollector, with something like:

bool TraverseTemplateTypeParamDeclConstraints(TemplateTypeParmDecl *TTPD) {
  if (auto *TC = TTPD->getTypeConstraint()) {
    reportReference(ReferenceLoc{TC->getNestedNameSpecifierLoc(),
                                 TC->getConceptNameLoc(),
                                 /*IsDecl=*/false,
                                 {TC->getNamedConcept()}},
                    DynTypedNode::create(*TTPD));
    return RecursiveASTVisitor::TraverseTypeConstraint(TC);
  }
  return true;
}
sammccall added inline comments.
clang-tools-extra/clangd/FindTarget.cpp
719

https://github.com/llvm/llvm-project/blob/clang/include/clang/AST/RecursiveASTVisitor.h#L830 unfortunately we stop here, because all we store is an identifier.

This isn't unfortunate, this is where we would handle things internal to the name (e.g. if the name was operator vector<int>()) rather than what the name is used for.

The right level to handle this seems to be a missing TraverseConceptReference() which observes the lexical reference to a concept (we don't care here that it's a type constraint specifically).
Unfortunately this doesn't exist: we have TraverseConceptReferenceHelper() which is private and called nonpolymorphically.

Renaming this to TraverseConceptReference() and making it CRTP-overridable seems like the principled solution at the RAV level. Maybe there's some reason it wasn't done this way to start with though.

Failing that, overriding TraverseTypeConstraint() in our RAV subclass seems like it fits the pattern more neatly (there's no VisitTypeConstraint, so we have to override Traverse and call Base::Traverse). We're going to have to handle the other ways concepts can be referenced of course, but changing RAV vs working around its limitations is always a tradeoff of practicality.

nridge updated this revision to Diff 494172.Feb 1 2023, 11:25 PM

Address review comment

Also filed https://github.com/llvm/llvm-project/issues/60469 for the seemingly missing RecursiveASTVisitor methods

kadircet accepted this revision.Feb 6 2023, 12:34 AM
kadircet added inline comments.
clang-tools-extra/clangd/FindTarget.cpp
1047

can you also add a comment like // We actually want to handle all ConceptReferences but RAV doesn't traverse it polymorphically. So handle the ones inside TypeConstraints specially here.

This revision is now accepted and ready to land.Feb 6 2023, 12:34 AM
nridge updated this revision to Diff 495015.Feb 6 2023, 12:43 AM

address review comment

This revision was landed with ongoing or failed builds.Feb 6 2023, 12:44 AM
This revision was automatically updated to reflect the committed changes.