|
17 | 17 | #include "clang/AST/RecursiveASTVisitor.h"
|
18 | 18 | #include "clang/AST/Type.h"
|
19 | 19 | #include "clang/AST/TypeLoc.h"
|
| 20 | +#include "clang/Basic/SourceLocation.h" |
20 | 21 | #include <algorithm>
|
21 | 22 |
|
22 | 23 | namespace clang {
|
@@ -125,13 +126,12 @@ class HighlightingTokenCollector
|
125 | 126 | }
|
126 | 127 |
|
127 | 128 | bool VisitTypedefNameDecl(TypedefNameDecl *TD) {
|
128 |
| - if (const auto *TSI = TD->getTypeSourceInfo()) |
129 |
| - addType(TD->getLocation(), TSI->getTypeLoc().getTypePtr()); |
| 129 | + addTokenForTypedef(TD->getLocation(), TD); |
130 | 130 | return true;
|
131 | 131 | }
|
132 | 132 |
|
133 | 133 | bool VisitTypedefTypeLoc(TypedefTypeLoc TL) {
|
134 |
| - addType(TL.getBeginLoc(), TL.getTypePtr()); |
| 134 | + addTokenForTypedef(TL.getBeginLoc(), TL.getTypedefNameDecl()); |
135 | 135 | return true;
|
136 | 136 | }
|
137 | 137 |
|
@@ -182,17 +182,35 @@ class HighlightingTokenCollector
|
182 | 182 | }
|
183 | 183 |
|
184 | 184 | private:
|
185 |
| - void addType(SourceLocation Loc, const Type *TP) { |
186 |
| - if (!TP) |
| 185 | + void addTokenForTypedef(SourceLocation Loc, const TypedefNameDecl *TD) { |
| 186 | + auto *TSI = TD->getTypeSourceInfo(); |
| 187 | + if (!TSI) |
| 188 | + return; |
| 189 | + // Try to highlight as underlying type. |
| 190 | + if (addType(Loc, TSI->getType().getTypePtrOrNull())) |
187 | 191 | return;
|
188 |
| - if (TP->isBuiltinType()) |
| 192 | + // Fallback to the typedef highlighting kind. |
| 193 | + addToken(Loc, HighlightingKind::Typedef); |
| 194 | + } |
| 195 | + |
| 196 | + bool addType(SourceLocation Loc, const Type *TP) { |
| 197 | + if (!TP) |
| 198 | + return false; |
| 199 | + if (TP->isBuiltinType()) { |
189 | 200 | // Builtins must be special cased as they do not have a TagDecl.
|
190 | 201 | addToken(Loc, HighlightingKind::Primitive);
|
191 |
| - if (auto *TD = dyn_cast<TemplateTypeParmType>(TP)) |
| 202 | + return true; |
| 203 | + } |
| 204 | + if (auto *TD = dyn_cast<TemplateTypeParmType>(TP)) { |
192 | 205 | // TemplateTypeParmType also do not have a TagDecl.
|
193 | 206 | addToken(Loc, TD->getDecl());
|
194 |
| - if (auto *TD = TP->getAsTagDecl()) |
| 207 | + return true; |
| 208 | + } |
| 209 | + if (auto *TD = TP->getAsTagDecl()) { |
195 | 210 | addToken(Loc, TD);
|
| 211 | + return true; |
| 212 | + } |
| 213 | + return false; |
196 | 214 | }
|
197 | 215 |
|
198 | 216 | void addToken(SourceLocation Loc, const NamedDecl *D) {
|
@@ -379,6 +397,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K) {
|
379 | 397 | return OS << "Enum";
|
380 | 398 | case HighlightingKind::EnumConstant:
|
381 | 399 | return OS << "EnumConstant";
|
| 400 | + case HighlightingKind::Typedef: |
| 401 | + return OS << "Typedef"; |
382 | 402 | case HighlightingKind::Namespace:
|
383 | 403 | return OS << "Namespace";
|
384 | 404 | case HighlightingKind::TemplateParameter:
|
@@ -506,6 +526,8 @@ llvm::StringRef toTextMateScope(HighlightingKind Kind) {
|
506 | 526 | return "entity.name.type.enum.cpp";
|
507 | 527 | case HighlightingKind::EnumConstant:
|
508 | 528 | return "variable.other.enummember.cpp";
|
| 529 | + case HighlightingKind::Typedef: |
| 530 | + return "entity.name.type.typedef.cpp"; |
509 | 531 | case HighlightingKind::Namespace:
|
510 | 532 | return "entity.name.namespace.cpp";
|
511 | 533 | case HighlightingKind::TemplateParameter:
|
|
0 commit comments