Skip to content

Commit

Permalink
[clangd] Add missing highlights for using decls.
Browse files Browse the repository at this point in the history
Reviewers: ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D69506
  • Loading branch information
hokein committed Oct 29, 2019
1 parent 3fe7f1d commit 94cd2f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
19 changes: 14 additions & 5 deletions clang-tools-extra/clangd/SemanticHighlighting.cpp
Expand Up @@ -37,6 +37,10 @@ bool canHighlightName(DeclarationName Name) {

llvm::Optional<HighlightingKind> kindForType(const Type *TP);
llvm::Optional<HighlightingKind> kindForDecl(const NamedDecl *D) {
if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
if (auto *Target = USD->getTargetDecl())
D = Target;
}
if (auto *TD = dyn_cast<TemplateDecl>(D)) {
if (auto *Templated = TD->getTemplatedDecl())
D = Templated;
Expand Down Expand Up @@ -99,11 +103,10 @@ llvm::Optional<HighlightingKind> kindForType(const Type *TP) {
return kindForDecl(TD);
return llvm::None;
}
// Given a set of candidate declarations for an unresolved name,
// if the declarations all have the same highlighting kind, return
// that highlighting kind, otherwise return None.
llvm::Optional<HighlightingKind>
kindForCandidateDecls(llvm::iterator_range<UnresolvedSetIterator> Decls) {
// Given a set of candidate declarations, if the declarations all have the same
// highlighting kind, return that highlighting kind, otherwise return None.
template <typename IteratorRange>
llvm::Optional<HighlightingKind> kindForCandidateDecls(IteratorRange Decls) {
llvm::Optional<HighlightingKind> Result;
for (NamedDecl *Decl : Decls) {
auto Kind = kindForDecl(Decl);
Expand Down Expand Up @@ -196,6 +199,12 @@ class HighlightingTokenCollector
return true;
}

bool VisitUsingDecl(UsingDecl *UD) {
if (auto K = kindForCandidateDecls(UD->shadows()))
addToken(UD->getLocation(), *K);
return true;
}

bool VisitDeclRefExpr(DeclRefExpr *Ref) {
if (canHighlightName(Ref->getNameInfo().getName()))
addToken(Ref->getLocation(), Ref->getDecl());
Expand Down
Expand Up @@ -584,6 +584,11 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
return $TemplateParameter[[T]]::$DependentName[[Field]];
}
};
)cpp",
// Highlighting the using decl as the underlying using shadow decl.
R"cpp(
void $Function[[foo]]();
using ::$Function[[foo]];
)cpp"};
for (const auto &TestCase : TestCases) {
checkHighlightings(TestCase);
Expand Down

0 comments on commit 94cd2f0

Please sign in to comment.