Skip to content

Commit aae7553

Browse files
committed
[clangd] Added highlighting for the targets in typedefs and using.
Summary: In `typedef int A` the `A` was not highlighted previously. This patch gives `A` the same kind of highlighting that the underlying type has (class/enum) (which in this example is no special highlighting because builtins are not handled yet) Will add highlightings for built ins in another patch. Reviewers: hokein, sammccall, ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64754 llvm-svn: 366207
1 parent 3a6aaa4 commit aae7553

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

clang-tools-extra/clangd/SemanticHighlighting.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class HighlightingTokenCollector
9393
return true;
9494
}
9595

96+
bool VisitTypedefNameDecl(TypedefNameDecl *TD) {
97+
if(const auto *TSI = TD->getTypeSourceInfo())
98+
addTypeLoc(TD->getLocation(), TSI->getTypeLoc());
99+
return true;
100+
}
101+
96102
bool VisitTypeLoc(TypeLoc &TL) {
97103
// This check is for not getting two entries when there are anonymous
98104
// structs. It also makes us not highlight certain namespace qualifiers
@@ -101,9 +107,7 @@ class HighlightingTokenCollector
101107
if (TL.getTypeLocClass() == TypeLoc::TypeLocClass::Elaborated)
102108
return true;
103109

104-
if (const Type *TP = TL.getTypePtr())
105-
if (const TagDecl *TD = TP->getAsTagDecl())
106-
addToken(TL.getBeginLoc(), TD);
110+
addTypeLoc(TL.getBeginLoc(), TL);
107111
return true;
108112
}
109113

@@ -118,6 +122,12 @@ class HighlightingTokenCollector
118122
}
119123

120124
private:
125+
void addTypeLoc(SourceLocation Loc, const TypeLoc &TL) {
126+
if (const Type *TP = TL.getTypePtr())
127+
if (const TagDecl *TD = TP->getAsTagDecl())
128+
addToken(Loc, TD);
129+
}
130+
121131
void addToken(SourceLocation Loc, const NamedDecl *D) {
122132
if (D->getDeclName().isIdentifier() && D->getName().empty())
123133
// Don't add symbols that don't have any length.

clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
9090
typename T::A* $Field[[D]];
9191
};
9292
$Namespace[[abc]]::$Class[[A]]<int> $Variable[[AA]];
93-
typedef $Namespace[[abc]]::$Class[[A]]<int> AAA;
93+
typedef $Namespace[[abc]]::$Class[[A]]<int> $Class[[AAA]];
9494
struct $Class[[B]] {
9595
$Class[[B]]();
9696
~$Class[[B]]();
@@ -173,6 +173,19 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
173173
}
174174
int $Variable[[B]];
175175
$Class[[AA]] $Variable[[A]]{$Variable[[B]]};
176+
)cpp",
177+
R"cpp(
178+
namespace $Namespace[[a]] {
179+
struct $Class[[A]] {};
180+
}
181+
typedef $Namespace[[a]]::$Class[[A]] $Class[[B]];
182+
using $Class[[BB]] = $Namespace[[a]]::$Class[[A]];
183+
enum class $Enum[[E]] {};
184+
typedef $Enum[[E]] $Enum[[C]];
185+
typedef $Enum[[C]] $Enum[[CC]];
186+
using $Enum[[CD]] = $Enum[[CC]];
187+
$Enum[[CC]] $Function[[f]]($Class[[B]]);
188+
$Enum[[CD]] $Function[[f]]($Class[[BB]]);
176189
)cpp"};
177190
for (const auto &TestCase : TestCases) {
178191
checkHighlightings(TestCase);

0 commit comments

Comments
 (0)