jvikstrom created this revision.
jvikstrom added reviewers: hokein, sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay.
Herald added a project: clang.
Added highlighting for non-builtin types using VisitTypeLoc. Ignoring namespace
qualifiers as for now.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D64257
Files:
clang-tools-extra/clangd/SemanticHighlighting.cpp
clang-tools-extra/clangd/SemanticHighlighting.h
clang-tools-extra/clangd/test/semantic-highlighting.test
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
@@ -34,11 +34,12 @@
auto AST = TestTU::withCode(Test.code()).build();
static const std::map<HighlightingKind, std::string> KindToString{
{HighlightingKind::Variable, "Variable"},
- {HighlightingKind::Function, "Function"}};
+ {HighlightingKind::Function, "Function"},
+ {HighlightingKind::Type, "Type"}};
std::vector<HighlightingToken> ExpectedTokens;
for (const auto &KindString : KindToString) {
- std::vector<HighlightingToken> Toks =
- makeHighlightingTokens(Test.ranges(KindString.second), KindString.first);
+ std::vector<HighlightingToken> Toks = makeHighlightingTokens(
+ Test.ranges(KindString.second), KindString.first);
ExpectedTokens.insert(ExpectedTokens.end(), Toks.begin(), Toks.end());
}
@@ -49,14 +50,14 @@
TEST(SemanticHighlighting, GetsCorrectTokens) {
const char *TestCases[] = {
R"cpp(
- struct AS {
+ struct $Type[[AS]] {
double SomeMember;
};
- struct {
+ $Type[[struct]] {
} $Variable[[S]];
- void $Function[[foo]](int $Variable[[A]]) {
+ void $Function[[foo]](int $Variable[[A]], $Type[[AS]] $Variable[[As]]) {
auto $Variable[[VeryLongVariableName]] = 12312;
- AS $Variable[[AA]];
+ $Type[[AS]] $Variable[[AA]];
auto $Variable[[L]] = $Variable[[AA]].SomeMember + $Variable[[A]];
auto $Variable[[FN]] = [ $Variable[[AA]]](int $Variable[[A]]) -> void {};
$Variable[[FN]](12312);
@@ -70,11 +71,18 @@
}
)cpp",
R"cpp(
- struct A {
+ namespace abc {
+ template<typename T>
+ struct $Type[[A]] {};
+ }
+ abc::$Type[[A]]<int> $Variable[[AA]];
+ )cpp",
+ R"cpp(
+ struct $Type[[A]] {
A();
~A();
void $Function[[abc]]();
- void operator<<(int);
+ void operator<<($Type[[A]]);
};
)cpp"};
for (const auto &TestCase : TestCases) {
Index: clang-tools-extra/clangd/test/semantic-highlighting.test
===================================================================
--- clang-tools-extra/clangd/test/semantic-highlighting.test
+++ clang-tools-extra/clangd/test/semantic-highlighting.test
@@ -10,6 +10,9 @@
# CHECK-NEXT: [
# CHECK-NEXT: "entity.name.function.cpp"
# CHECK-NEXT: ]
+# CHECK-NEXT: [
+# CHECK-NEXT: "entity.name.type.cpp"
+# CHECK-NEXT: ]
# CHECK-NEXT: ]
# CHECK-NEXT: },
---
Index: clang-tools-extra/clangd/SemanticHighlighting.h
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.h
+++ clang-tools-extra/clangd/SemanticHighlighting.h
@@ -26,6 +26,7 @@
enum class HighlightingKind {
Variable = 0,
Function,
+ Type,
NumKinds,
};
Index: clang-tools-extra/clangd/SemanticHighlighting.cpp
===================================================================
--- clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -56,6 +56,36 @@
return true;
}
+ bool VisitTypeLoc(TypeLoc &TL) {
+ // The check for DependentName is so namespace qualifiers are not
+ // highlighted. The check for elaborated type is for not getting two entries
+ // whenever there is an anonymous struct.
+ if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::DependentName ||
+ TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated) {
+ return true;
+ }
+
+ TagDecl *D = TL.getTypePtr()->getAsTagDecl();
+ if (!D)
+ return true;
+
+ if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
+ if (auto DC = RD->getDestructor()) {
+ auto Range = DC->getSourceRange();
+ // Destructors appear as a TagTypeLoc RecordDecl. To not highlight
+ // destructors incorrectly the TagTypeLoc is skipped if it is wrapped
+ // inside the actual destructor.
+ if (Range.getBegin() < TL.getBeginLoc() &&
+ TL.getBeginLoc() < Range.getEnd()) {
+ return true;
+ }
+ }
+ }
+
+ addToken(TL.getBeginLoc(), D);
+ return true;
+ }
+
private:
void addToken(SourceLocation Loc, const Decl *D) {
if (isa<VarDecl>(D)) {
@@ -66,6 +96,10 @@
addToken(Loc, HighlightingKind::Function);
return;
}
+ if(isa<CXXRecordDecl>(D)) {
+ addToken(Loc, HighlightingKind::Type);
+ return;
+ }
}
void addToken(SourceLocation Loc, HighlightingKind Kind) {
@@ -176,6 +210,8 @@
return "entity.name.function.cpp";
case HighlightingKind::Variable:
return "variable.cpp";
+ case HighlightingKind::Type:
+ return "entity.name.type.cpp";
case HighlightingKind::NumKinds:
llvm_unreachable("must not pass NumKinds to the function");
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits