[PATCH] D64199: [clangd] Added highlighting for variable references (declrefs)
Johan Vikström via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 4 02:53:33 PDT 2019
jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.
Added highlighting for variable references using VisitDeclRefExpr.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D64199
Files:
clang-tools-extra/clangd/SemanticHighlighting.cpp
clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
Index: clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp
@@ -57,10 +57,18 @@
void $Function[[foo]](int $Variable[[a]]) {
auto $Variable[[VeryLongVariableName]] = 12312;
A $Variable[[aa]];
+ auto $Variable[[l]] = $Variable[[aa]].SomeMember + $Variable[[a]];
}
)cpp",
R"cpp(
void $Function[[foo]](int);
+ )cpp",
+ R"cpp(
+ void $Function[[foo]]() {
+ int $Variable[[b]];
+ auto $Variable[[FN]] = [ $Variable[[b]]](int $Variable[[a]]) -> void {};
+ $Variable[[FN]](12312);
+ }
)cpp"};
for (const auto &TestCase : TestCases) {
checkHighlightings(TestCase);
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -35,25 +35,31 @@
}
bool VisitVarDecl(VarDecl *Var) {
- addToken(Var, HighlightingKind::Variable);
+ addNamedDecl(Var, HighlightingKind::Variable);
return true;
}
bool VisitFunctionDecl(FunctionDecl *Func) {
- addToken(Func, HighlightingKind::Function);
+ addNamedDecl(Func, HighlightingKind::Function);
+ return true;
+ }
+
+ bool VisitDeclRefExpr(DeclRefExpr *Ref) {
+ if (Ref->getNameInfo().getName().getNameKind() ==
+ DeclarationName::CXXOperatorName)
+ // Don't want to highlight operators.
+ return true;
+
+ addToken(Ref->getLocation(), HighlightingKind::Variable);
return true;
}
private:
- void addToken(const NamedDecl *D, HighlightingKind Kind) {
- if (D->getLocation().isMacroID())
+ void addToken(SourceLocation Loc, HighlightingKind Kind) {
+ if (Loc.isMacroID())
// FIXME: skip tokens inside macros for now.
return;
- if (D->getDeclName().isEmpty())
- // Don't add symbols that don't have any length.
- 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");
@@ -62,6 +68,13 @@
Tokens.push_back({Kind, R.getValue()});
}
+
+ void addNamedDecl(const NamedDecl *D, HighlightingKind Kind) {
+ if (D->getDeclName().isEmpty())
+ // Don't add symbols that don't have any length.
+ return;
+ addToken(D->getLocation(), Kind);
+ }
};
// Encode binary data into base64.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64199.208006.patch
Type: text/x-patch
Size: 2751 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190704/74c0bac8/attachment.bin>
More information about the cfe-commits
mailing list