diff options
author | Johan Vikstrom <jvikstrom@google.com> | 2019-07-05 13:06:03 +0000 |
---|---|---|
committer | Johan Vikstrom <jvikstrom@google.com> | 2019-07-05 13:06:03 +0000 |
commit | 8ca1c65cedb0b86c9706b5ec1a962d80f48af434 (patch) | |
tree | 05871da6c5d9b08b046142b105fb463f293515fe | |
parent | ee08036df8db0b14c7b52bfc8a810d2354c55f19 (diff) | |
download | bcm5719-llvm-8ca1c65cedb0b86c9706b5ec1a962d80f48af434.tar.gz bcm5719-llvm-8ca1c65cedb0b86c9706b5ec1a962d80f48af434.zip |
[clangd] Added highlighting for variable references (declrefs)
Summary: Added highlighting for variable references using VisitDeclRefExpr.
Reviewers: hokein, sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D64199
llvm-svn: 365205
-rw-r--r-- | clang-tools-extra/clangd/SemanticHighlighting.cpp | 40 | ||||
-rw-r--r-- | clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp | 43 |
2 files changed, 59 insertions, 24 deletions
diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index e3c2c7fa8bb..3171feb306a 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -34,26 +34,46 @@ public: return Tokens; } - bool VisitVarDecl(VarDecl *Var) { - addToken(Var, HighlightingKind::Variable); + bool VisitNamedDecl(NamedDecl *ND) { + // FIXME: (De)Constructors/operator need to be highlighted some other way. + if (ND->getDeclName().getNameKind() != DeclarationName::Identifier) + return true; + + if (ND->getDeclName().isEmpty()) + // Don't add symbols that don't have any length. + return true; + addToken(ND->getLocation(), ND); return true; } - bool VisitFunctionDecl(FunctionDecl *Func) { - addToken(Func, HighlightingKind::Function); + + bool VisitDeclRefExpr(DeclRefExpr *Ref) { + if (Ref->getNameInfo().getName().getNameKind() != + DeclarationName::Identifier) + // Only want to highlight identifiers. + return true; + + addToken(Ref->getLocation(), Ref->getDecl()); return true; } private: - void addToken(const NamedDecl *D, HighlightingKind Kind) { - if (D->getLocation().isMacroID()) - // FIXME: skip tokens inside macros for now. + void addToken(SourceLocation Loc, const Decl *D) { + if (isa<VarDecl>(D)) { + addToken(Loc, HighlightingKind::Variable); return; + } + if (isa<FunctionDecl>(D)) { + addToken(Loc, HighlightingKind::Function); + return; + } + } - if (D->getDeclName().isEmpty()) - // Don't add symbols that don't have any length. + void addToken(SourceLocation Loc, HighlightingKind Kind) { + if (Loc.isMacroID()) + // FIXME: skip tokens inside macros for now. return; - auto R = getTokenRange(SM, Ctx.getLangOpts(), D->getLocation()); + auto R = getTokenRange(SM, Ctx.getLangOpts(), Loc); if (!R) { // R should always have a value, if it doesn't something is very wrong. elog("Tried to add semantic token with an invalid range"); diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index a5c793251a4..599a3430da9 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -48,20 +48,35 @@ void checkHighlightings(llvm::StringRef Code) { TEST(SemanticHighlighting, GetsCorrectTokens) { const char *TestCases[] = { - R"cpp( - struct A { - double SomeMember; - }; - struct { - } $Variable[[HStruct]]; - void $Function[[foo]](int $Variable[[a]]) { - auto $Variable[[VeryLongVariableName]] = 12312; - A $Variable[[aa]]; - } - )cpp", - R"cpp( - void $Function[[foo]](int); - )cpp"}; + R"cpp( + struct AS { + double SomeMember; + }; + struct { + } $Variable[[S]]; + void $Function[[foo]](int $Variable[[A]]) { + auto $Variable[[VeryLongVariableName]] = 12312; + AS $Variable[[AA]]; + auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]]; + auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {}; + $Variable[[FN]](12312); + } + )cpp", + R"cpp( + void $Function[[foo]](int); + void $Function[[Gah]](); + void $Function[[foo]]() { + auto $Variable[[Bou]] = $Function[[Gah]]; + } + )cpp", + R"cpp( + struct A { + A(); + ~A(); + void $Function[[abc]](); + void operator<<(int); + }; + )cpp"}; for (const auto &TestCase : TestCases) { checkHighlightings(TestCase); } |