Index: clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp =================================================================== --- clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp +++ clang-tools-extra/trunk/clangd/SemanticHighlighting.cpp @@ -34,26 +34,46 @@ 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(D)) { + addToken(Loc, HighlightingKind::Variable); return; + } + if (isa(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"); Index: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp =================================================================== --- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp +++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp @@ -48,20 +48,35 @@ 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); }