clang-tools 23.0.0git
SemanticHighlighting.cpp
Go to the documentation of this file.
1//===--- SemanticHighlighting.cpp - ------------------------- ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "AST.h"
11#include "Config.h"
12#include "FindSymbols.h"
13#include "FindTarget.h"
14#include "ParsedAST.h"
15#include "Protocol.h"
16#include "SourceCode.h"
17#include "support/Logger.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/DeclTemplate.h"
23#include "clang/AST/DeclarationName.h"
24#include "clang/AST/ExprCXX.h"
25#include "clang/AST/RecursiveASTVisitor.h"
26#include "clang/AST/TypeLoc.h"
27#include "clang/Basic/SourceLocation.h"
28#include "clang/Basic/SourceManager.h"
29#include "clang/Sema/HeuristicResolver.h"
30#include "clang/Tooling/Syntax/Tokens.h"
31#include "llvm/ADT/STLExtras.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/Support/Casting.h"
34#include "llvm/Support/Error.h"
35
36#include <algorithm>
37#include <optional>
38
39namespace clang {
40namespace clangd {
41namespace {
42
43/// Get the last Position on a given line.
44llvm::Expected<Position> endOfLine(llvm::StringRef Code, int Line) {
45 auto StartOfLine = positionToOffset(Code, Position{Line, 0});
46 if (!StartOfLine)
47 return StartOfLine.takeError();
48 StringRef LineText = Code.drop_front(*StartOfLine).take_until([](char C) {
49 return C == '\n';
50 });
51 return Position{Line, static_cast<int>(lspLength(LineText))};
52}
53
54/// Some names are not written in the source code and cannot be highlighted,
55/// e.g. anonymous classes. This function detects those cases.
56bool canHighlightName(DeclarationName Name) {
57 switch (Name.getNameKind()) {
58 case DeclarationName::Identifier: {
59 auto *II = Name.getAsIdentifierInfo();
60 return II && !II->getName().empty();
61 }
62 case DeclarationName::CXXConstructorName:
63 case DeclarationName::CXXDestructorName:
64 return true;
65 case DeclarationName::ObjCZeroArgSelector:
66 case DeclarationName::ObjCOneArgSelector:
67 case DeclarationName::ObjCMultiArgSelector:
68 // Multi-arg selectors need special handling, and we handle 0/1 arg
69 // selectors there too.
70 return false;
71 case DeclarationName::CXXConversionFunctionName:
72 case DeclarationName::CXXOperatorName:
73 case DeclarationName::CXXDeductionGuideName:
74 case DeclarationName::CXXLiteralOperatorName:
75 case DeclarationName::CXXUsingDirective:
76 return false;
77 }
78 llvm_unreachable("invalid name kind");
79}
80
81std::optional<HighlightingKind> kindForType(const Type *TP,
82 const HeuristicResolver *Resolver);
83std::optional<HighlightingKind> kindForDecl(const NamedDecl *D,
84 const HeuristicResolver *Resolver) {
85 if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
86 if (auto *Target = USD->getTargetDecl())
87 D = Target;
88 }
89 if (auto *TD = dyn_cast<TemplateDecl>(D)) {
90 if (auto *Templated = TD->getTemplatedDecl())
91 D = Templated;
92 }
93 if (auto *TD = dyn_cast<TypedefNameDecl>(D)) {
94 // We try to highlight typedefs as their underlying type.
95 if (auto K =
96 kindForType(TD->getUnderlyingType().getTypePtrOrNull(), Resolver))
97 return K;
98 // And fallback to a generic kind if this fails.
100 }
101 // We highlight class decls, constructor decls and destructor decls as
102 // `Class` type. The destructor decls are handled in `VisitTagTypeLoc` (we
103 // will visit a TypeLoc where the underlying Type is a CXXRecordDecl).
104 if (auto *RD = llvm::dyn_cast<RecordDecl>(D)) {
105 // We don't want to highlight lambdas like classes.
106 if (RD->isLambda())
107 return std::nullopt;
109 }
110 if (isa<ClassTemplateDecl, RecordDecl, CXXConstructorDecl, ObjCInterfaceDecl,
111 ObjCImplementationDecl>(D))
113 if (isa<ObjCProtocolDecl>(D))
115 if (isa<ObjCCategoryDecl, ObjCCategoryImplDecl>(D))
117 if (auto *MD = dyn_cast<CXXMethodDecl>(D))
118 return MD->isStatic() ? HighlightingKind::StaticMethod
120 if (auto *OMD = dyn_cast<ObjCMethodDecl>(D))
121 return OMD->isClassMethod() ? HighlightingKind::StaticMethod
123 if (isa<FieldDecl, IndirectFieldDecl, ObjCPropertyDecl>(D))
125 if (isa<EnumDecl>(D))
127 if (isa<EnumConstantDecl>(D))
129 if (isa<ParmVarDecl>(D))
131 if (auto *VD = dyn_cast<VarDecl>(D)) {
132 if (isa<ImplicitParamDecl>(VD)) // e.g. ObjC Self
133 return std::nullopt;
134 return VD->isStaticDataMember() ? HighlightingKind::StaticField
135 : VD->isLocalVarDecl() ? HighlightingKind::LocalVariable
137 }
138 if (const auto *BD = dyn_cast<BindingDecl>(D))
139 return BD->getDeclContext()->isFunctionOrMethod()
142 if (isa<FunctionDecl>(D))
144 if (isa<NamespaceDecl>(D) || isa<NamespaceAliasDecl>(D) ||
145 isa<UsingDirectiveDecl>(D))
147 if (isa<TemplateTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
148 isa<NonTypeTemplateParmDecl>(D))
150 if (isa<ConceptDecl>(D))
152 if (isa<LabelDecl>(D))
154 if (const auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(D)) {
155 auto Targets = Resolver->resolveUsingValueDecl(UUVD);
156 if (!Targets.empty() && Targets[0] != UUVD) {
157 return kindForDecl(Targets[0], Resolver);
158 }
160 }
161 return std::nullopt;
162}
163std::optional<HighlightingKind> kindForType(const Type *TP,
164 const HeuristicResolver *Resolver) {
165 if (!TP)
166 return std::nullopt;
167 if (TP->isBuiltinType()) // Builtins are special, they do not have decls.
169 if (auto *TD = dyn_cast<TemplateTypeParmType>(TP))
170 return kindForDecl(TD->getDecl(), Resolver);
171 if (isa<ObjCObjectPointerType>(TP))
173 if (auto *TD = TP->getAsTagDecl())
174 return kindForDecl(TD, Resolver);
175 return std::nullopt;
176}
177
178bool isDependent(const Decl *D) {
179 if (isa<UnresolvedUsingValueDecl>(D))
180 return true;
181 return false;
182}
183
184/// Returns true if `Decl` is considered to be from a default/system library.
185/// This currently checks the systemness of the file by include type, although
186/// different heuristics may be used in the future (e.g. sysroot paths).
187bool isDefaultLibrary(const Decl *D) {
188 SourceLocation Loc = D->getLocation();
189 if (!Loc.isValid())
190 return false;
191 return D->getASTContext().getSourceManager().isInSystemHeader(Loc);
192}
193
194bool isDefaultLibrary(const Type *T) {
195 if (!T)
196 return false;
197 const Type *Underlying = T->getPointeeOrArrayElementType();
198 if (Underlying->isBuiltinType())
199 return true;
200 if (auto *TD = dyn_cast<TemplateTypeParmType>(Underlying))
201 return isDefaultLibrary(TD->getDecl());
202 if (auto *TD = Underlying->getAsTagDecl())
203 return isDefaultLibrary(TD);
204 return false;
205}
206
207// For a macro usage `DUMP(foo)`, we want:
208// - DUMP --> "macro"
209// - foo --> "variable".
210SourceLocation getHighlightableSpellingToken(SourceLocation L,
211 const SourceManager &SM) {
212 if (L.isFileID())
213 return SM.isWrittenInMainFile(L) ? L : SourceLocation{};
214 // Tokens expanded from the macro body contribute no highlightings.
215 if (!SM.isMacroArgExpansion(L))
216 return {};
217 // Tokens expanded from macro args are potentially highlightable.
218 return getHighlightableSpellingToken(SM.getImmediateSpellingLoc(L), SM);
219}
220
221unsigned evaluateHighlightPriority(const HighlightingToken &Tok) {
222 enum HighlightPriority { Dependent = 0, Resolved = 1 };
223 return (Tok.Modifiers & (1 << uint32_t(HighlightingModifier::DependentName)))
224 ? Dependent
225 : Resolved;
226}
227
228// Sometimes we get multiple tokens at the same location:
229//
230// - findExplicitReferences() returns a heuristic result for a dependent name
231// (e.g. Method) and CollectExtraHighlighting returning a fallback dependent
232// highlighting (e.g. Unknown+Dependent).
233// - macro arguments are expanded multiple times and have different roles
234// - broken code recovery produces several AST nodes at the same location
235//
236// We should either resolve these to a single token, or drop them all.
237// Our heuristics are:
238//
239// - token kinds that come with "dependent-name" modifiers are less reliable
240// (these tend to be vague, like Type or Unknown)
241// - if we have multiple equally reliable kinds, drop token rather than guess
242// - take the union of modifiers from all tokens
243//
244// In particular, heuristically resolved dependent names get their heuristic
245// kind, plus the dependent modifier.
246std::optional<HighlightingToken> resolveConflict(const HighlightingToken &A,
247 const HighlightingToken &B) {
248 unsigned Priority1 = evaluateHighlightPriority(A);
249 unsigned Priority2 = evaluateHighlightPriority(B);
250 if (Priority1 == Priority2 && A.Kind != B.Kind)
251 return std::nullopt;
252 auto Result = Priority1 > Priority2 ? A : B;
253 Result.Modifiers = A.Modifiers | B.Modifiers;
254 return Result;
255}
256std::optional<HighlightingToken>
257resolveConflict(ArrayRef<HighlightingToken> Tokens) {
258 if (Tokens.size() == 1)
259 return Tokens[0];
260
261 assert(Tokens.size() >= 2);
262 std::optional<HighlightingToken> Winner =
263 resolveConflict(Tokens[0], Tokens[1]);
264 for (size_t I = 2; Winner && I < Tokens.size(); ++I)
265 Winner = resolveConflict(*Winner, Tokens[I]);
266 return Winner;
267}
268
269/// Filter to remove particular kinds of highlighting tokens and modifiers from
270/// the output.
271class HighlightingFilter {
272public:
273 HighlightingFilter() {
274 for (auto &Active : ActiveKindLookup)
275 Active = true;
276
277 ActiveModifiersMask = ~0;
278 }
279
280 void disableKind(HighlightingKind Kind) {
281 ActiveKindLookup[static_cast<size_t>(Kind)] = false;
282 }
283
284 void disableModifier(HighlightingModifier Modifier) {
285 ActiveModifiersMask &= ~(1 << static_cast<uint32_t>(Modifier));
286 }
287
288 bool isHighlightKindActive(HighlightingKind Kind) const {
289 return ActiveKindLookup[static_cast<size_t>(Kind)];
290 }
291
292 uint32_t maskModifiers(uint32_t Modifiers) const {
293 return Modifiers & ActiveModifiersMask;
294 }
295
296 static HighlightingFilter fromCurrentConfig() {
297 const Config &C = Config::current();
298 HighlightingFilter Filter;
299 for (const auto &Kind : C.SemanticTokens.DisabledKinds)
300 if (auto K = highlightingKindFromString(Kind))
301 Filter.disableKind(*K);
302 for (const auto &Modifier : C.SemanticTokens.DisabledModifiers)
304 Filter.disableModifier(*M);
305
306 return Filter;
307 }
308
309private:
310 bool ActiveKindLookup[static_cast<size_t>(HighlightingKind::LastKind) + 1];
311 uint32_t ActiveModifiersMask;
312};
313
314/// Consumes source locations and maps them to text ranges for highlightings.
315class HighlightingsBuilder {
316public:
317 HighlightingsBuilder(const ParsedAST &AST, const HighlightingFilter &Filter)
318 : TB(AST.getTokens()), SourceMgr(AST.getSourceManager()),
319 LangOpts(AST.getLangOpts()), Filter(Filter),
320 Resolver(AST.getHeuristicResolver()) {}
321
322 HighlightingToken &addToken(SourceLocation Loc, HighlightingKind Kind) {
323 auto Range = getRangeForSourceLocation(Loc);
324 if (!Range)
325 return InvalidHighlightingToken;
326
327 return addToken(*Range, Kind);
328 }
329
330 // Most of this function works around
331 // https://github.com/clangd/clangd/issues/871.
332 void addAngleBracketTokens(SourceLocation LLoc, SourceLocation RLoc) {
333 if (!LLoc.isValid() || !RLoc.isValid())
334 return;
335
336 auto LRange = getRangeForSourceLocation(LLoc);
337 if (!LRange)
338 return;
339
340 // RLoc might be pointing at a virtual buffer when it's part of a `>>`
341 // token.
342 RLoc = SourceMgr.getFileLoc(RLoc);
343 // Make sure token is part of the main file.
344 RLoc = getHighlightableSpellingToken(RLoc, SourceMgr);
345 if (!RLoc.isValid())
346 return;
347
348 const auto *RTok = TB.spelledTokenContaining(RLoc);
349 // Handle `>>`. RLoc is either part of `>>` or a spelled token on its own
350 // `>`. If it's the former, slice to have length of 1, if latter use the
351 // token as-is.
352 if (!RTok || RTok->kind() == tok::greatergreater) {
353 Position Begin = sourceLocToPosition(SourceMgr, RLoc);
354 Position End = sourceLocToPosition(SourceMgr, RLoc.getLocWithOffset(1));
355 addToken(*LRange, HighlightingKind::Bracket);
356 addToken({Begin, End}, HighlightingKind::Bracket);
357 return;
358 }
359
360 // Easy case, we have the `>` token directly available.
361 if (RTok->kind() == tok::greater) {
362 if (auto RRange = getRangeForSourceLocation(RLoc)) {
363 addToken(*LRange, HighlightingKind::Bracket);
364 addToken(*RRange, HighlightingKind::Bracket);
365 }
366 return;
367 }
368 }
369
370 HighlightingToken &addToken(Range R, HighlightingKind Kind) {
371 if (!Filter.isHighlightKindActive(Kind))
372 return InvalidHighlightingToken;
373
374 HighlightingToken HT;
375 HT.R = std::move(R);
376 HT.Kind = Kind;
377 Tokens.push_back(std::move(HT));
378 return Tokens.back();
379 }
380
381 void addExtraModifier(SourceLocation Loc, HighlightingModifier Modifier) {
382 if (auto Range = getRangeForSourceLocation(Loc))
383 ExtraModifiers[*Range].push_back(Modifier);
384 }
385
386 std::vector<HighlightingToken> collect(ParsedAST &AST) && {
387 // Initializer lists can give duplicates of tokens, therefore all tokens
388 // must be deduplicated.
389 llvm::sort(Tokens);
390 auto Last = llvm::unique(Tokens);
391 Tokens.erase(Last, Tokens.end());
392
393 // Macros can give tokens that have the same source range but conflicting
394 // kinds. In this case all tokens sharing this source range should be
395 // removed.
396 std::vector<HighlightingToken> NonConflicting;
397 NonConflicting.reserve(Tokens.size());
398 for (ArrayRef<HighlightingToken> TokRef = Tokens; !TokRef.empty();) {
399 ArrayRef<HighlightingToken> Conflicting =
400 TokRef.take_while([&](const HighlightingToken &T) {
401 // TokRef is guaranteed at least one element here because otherwise
402 // this predicate would never fire.
403 return T.R == TokRef.front().R;
404 });
405 if (auto Resolved = resolveConflict(Conflicting)) {
406 // Apply extra collected highlighting modifiers
407 auto Modifiers = ExtraModifiers.find(Resolved->R);
408 if (Modifiers != ExtraModifiers.end()) {
409 for (HighlightingModifier Mod : Modifiers->second) {
410 Resolved->addModifier(Mod);
411 }
412 }
413
414 Resolved->Modifiers = Filter.maskModifiers(Resolved->Modifiers);
415 NonConflicting.push_back(*Resolved);
416 }
417 // TokRef[Conflicting.size()] is the next token with a different range (or
418 // the end of the Tokens).
419 TokRef = TokRef.drop_front(Conflicting.size());
420 }
421
422 if (!Filter.isHighlightKindActive(HighlightingKind::InactiveCode))
423 return NonConflicting;
424
425 const auto &SM = AST.getSourceManager();
426 StringRef MainCode = SM.getBufferOrFake(SM.getMainFileID()).getBuffer();
427
428 // Merge token stream with "inactive line" markers.
429 std::vector<HighlightingToken> WithInactiveLines;
430 auto SortedInactiveRegions = getInactiveRegions(AST);
431 llvm::sort(SortedInactiveRegions);
432 auto It = NonConflicting.begin();
433 for (const Range &R : SortedInactiveRegions) {
434 // Create one token for each line in the inactive range, so it works
435 // with line-based diffing.
436 assert(R.start.line <= R.end.line);
437 for (int Line = R.start.line; Line <= R.end.line; ++Line) {
438 // Copy tokens before the inactive line
439 for (; It != NonConflicting.end() && It->R.start.line < Line; ++It)
440 WithInactiveLines.push_back(std::move(*It));
441 // Add a token for the inactive line itself.
442 auto EndOfLine = endOfLine(MainCode, Line);
443 if (EndOfLine) {
444 HighlightingToken HT;
445 WithInactiveLines.emplace_back();
446 WithInactiveLines.back().Kind = HighlightingKind::InactiveCode;
447 WithInactiveLines.back().R.start.line = Line;
448 WithInactiveLines.back().R.end = *EndOfLine;
449 } else {
450 elog("Failed to determine end of line: {0}", EndOfLine.takeError());
451 }
452
453 // Skip any other tokens on the inactive line. e.g.
454 // `#ifndef Foo` is considered as part of an inactive region when Foo is
455 // defined, and there is a Foo macro token.
456 // FIXME: we should reduce the scope of the inactive region to not
457 // include the directive itself.
458 while (It != NonConflicting.end() && It->R.start.line == Line)
459 ++It;
460 }
461 }
462 // Copy tokens after the last inactive line
463 for (; It != NonConflicting.end(); ++It)
464 WithInactiveLines.push_back(std::move(*It));
465 return WithInactiveLines;
466 }
467
468 const HeuristicResolver *getResolver() const { return Resolver; }
469
470private:
471 std::optional<Range> getRangeForSourceLocation(SourceLocation Loc) {
472 Loc = getHighlightableSpellingToken(Loc, SourceMgr);
473 if (Loc.isInvalid())
474 return std::nullopt;
475 // We might have offsets in the main file that don't correspond to any
476 // spelled tokens.
477 const auto *Tok = TB.spelledTokenContaining(Loc);
478 if (!Tok)
479 return std::nullopt;
480 return halfOpenToRange(SourceMgr,
481 Tok->range(SourceMgr).toCharRange(SourceMgr));
482 }
483
484 const syntax::TokenBuffer &TB;
485 const SourceManager &SourceMgr;
486 const LangOptions &LangOpts;
487 HighlightingFilter Filter;
488 std::vector<HighlightingToken> Tokens;
489 std::map<Range, llvm::SmallVector<HighlightingModifier, 1>> ExtraModifiers;
490 const HeuristicResolver *Resolver;
491 // returned from addToken(InvalidLoc)
492 HighlightingToken InvalidHighlightingToken;
493};
494
495std::optional<HighlightingModifier> scopeModifier(const NamedDecl *D) {
496 const DeclContext *DC = D->getDeclContext();
497 // Injected "Foo" within the class "Foo" has file scope, not class scope.
498 if (auto *R = dyn_cast_or_null<CXXRecordDecl>(D))
499 if (R->isInjectedClassName())
500 DC = DC->getParent();
501 // Lambda captures are considered function scope, not class scope.
502 if (llvm::isa<FieldDecl>(D))
503 if (const auto *RD = llvm::dyn_cast<RecordDecl>(DC))
504 if (RD->isLambda())
506 // Walk up the DeclContext hierarchy until we find something interesting.
507 for (; !DC->isFileContext(); DC = DC->getParent()) {
508 if (DC->isFunctionOrMethod())
510 if (DC->isRecord())
512 }
513 // Some template parameters (e.g. those for variable templates) don't have
514 // meaningful DeclContexts. That doesn't mean they're global!
515 if (DC->isTranslationUnit() && D->isTemplateParameter())
516 return std::nullopt;
517 // ExternalLinkage threshold could be tweaked, e.g. module-visible as global.
518 if (llvm::to_underlying(D->getLinkageInternal()) <
519 llvm::to_underlying(Linkage::External))
522}
523
524std::optional<HighlightingModifier> scopeModifier(const Type *T) {
525 if (!T)
526 return std::nullopt;
527 if (T->isBuiltinType())
529 if (auto *TD = dyn_cast<TemplateTypeParmType>(T))
530 return scopeModifier(TD->getDecl());
531 if (auto *TD = T->getAsTagDecl())
532 return scopeModifier(TD);
533 return std::nullopt;
534}
535
536/// Produces highlightings, which are not captured by findExplicitReferences,
537/// e.g. highlights dependent names and 'auto' as the underlying type.
538class CollectExtraHighlightings
539 : public RecursiveASTVisitor<CollectExtraHighlightings> {
540 using Base = RecursiveASTVisitor<CollectExtraHighlightings>;
541
542public:
543 CollectExtraHighlightings(HighlightingsBuilder &H) : H(H) {}
544
545 bool VisitCXXConstructExpr(CXXConstructExpr *E) {
546 highlightMutableReferenceArguments(E->getConstructor(),
547 {E->getArgs(), E->getNumArgs()});
548
549 return true;
550 }
551
552 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
553 if (Init->isMemberInitializer())
554 if (auto *Member = Init->getMember())
555 highlightMutableReferenceArgument(Member->getType(), Init->getInit());
556 return Base::TraverseConstructorInitializer(Init);
557 }
558
559 bool TraverseTypeConstraint(const TypeConstraint *C) {
560 if (auto *Args = C->getTemplateArgsAsWritten())
561 H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc());
562 return Base::TraverseTypeConstraint(C);
563 }
564
565 bool VisitPredefinedExpr(PredefinedExpr *E) {
566 H.addToken(E->getLocation(), HighlightingKind::LocalVariable)
567 .addModifier(HighlightingModifier::Static)
570 return true;
571 }
572
573 bool VisitConceptSpecializationExpr(ConceptSpecializationExpr *E) {
574 if (auto *Args = E->getTemplateArgsAsWritten())
575 H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc());
576 return true;
577 }
578
579 bool VisitTemplateDecl(TemplateDecl *D) {
580 if (auto *TPL = D->getTemplateParameters())
581 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
582 return true;
583 }
584
585 bool VisitTagDecl(TagDecl *D) {
586 for (unsigned i = 0; i < D->getNumTemplateParameterLists(); ++i) {
587 if (auto *TPL = D->getTemplateParameterList(i))
588 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
589 }
590 return true;
591 }
592
593 bool
594 VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D) {
595 if (auto *Args = D->getTemplateArgsAsWritten())
596 H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc());
597 return true;
598 }
599
600 bool VisitClassTemplatePartialSpecializationDecl(
601 ClassTemplatePartialSpecializationDecl *D) {
602 if (auto *TPL = D->getTemplateParameters())
603 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
604 return true;
605 }
606
607 bool VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
608 if (auto *Args = D->getTemplateArgsAsWritten())
609 H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc());
610 return true;
611 }
612
613 bool VisitVarTemplatePartialSpecializationDecl(
614 VarTemplatePartialSpecializationDecl *D) {
615 if (auto *TPL = D->getTemplateParameters())
616 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
617 return true;
618 }
619
620 bool VisitDeclRefExpr(DeclRefExpr *E) {
621 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
622 return true;
623 }
624 bool VisitMemberExpr(MemberExpr *E) {
625 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
626 return true;
627 }
628
629 bool VisitFunctionDecl(FunctionDecl *D) {
630 if (D->isOverloadedOperator()) {
631 const auto AddOpDeclToken = [&](SourceLocation Loc) {
632 auto &Token = H.addToken(Loc, HighlightingKind::Operator)
634 if (D->isThisDeclarationADefinition())
635 Token.addModifier(HighlightingModifier::Definition);
636 };
637 const auto Range = D->getNameInfo().getCXXOperatorNameRange();
638 AddOpDeclToken(Range.getBegin());
639 const auto Kind = D->getOverloadedOperator();
640 if (Kind == OO_Call || Kind == OO_Subscript)
641 AddOpDeclToken(Range.getEnd());
642 }
643 if (auto *Args = D->getTemplateSpecializationArgsAsWritten())
644 H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc());
645 return true;
646 }
647
648 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
649 const auto AddOpToken = [&](SourceLocation Loc) {
650 H.addToken(Loc, HighlightingKind::Operator)
652 };
653 AddOpToken(E->getOperatorLoc());
654 const auto Kind = E->getOperator();
655 if (Kind == OO_Call || Kind == OO_Subscript) {
656 if (auto *Callee = E->getCallee())
657 AddOpToken(Callee->getBeginLoc());
658 }
659 return true;
660 }
661
662 bool VisitUnaryOperator(UnaryOperator *Op) {
663 auto &Token = H.addToken(Op->getOperatorLoc(), HighlightingKind::Operator);
664 if (Op->getSubExpr()->isTypeDependent())
665 Token.addModifier(HighlightingModifier::UserDefined);
666 return true;
667 }
668
669 bool VisitBinaryOperator(BinaryOperator *Op) {
670 auto &Token = H.addToken(Op->getOperatorLoc(), HighlightingKind::Operator);
671 if (Op->getLHS()->isTypeDependent() || Op->getRHS()->isTypeDependent())
672 Token.addModifier(HighlightingModifier::UserDefined);
673 return true;
674 }
675
676 bool VisitConditionalOperator(ConditionalOperator *Op) {
677 H.addToken(Op->getQuestionLoc(), HighlightingKind::Operator);
678 H.addToken(Op->getColonLoc(), HighlightingKind::Operator);
679 return true;
680 }
681
682 bool VisitCXXNewExpr(CXXNewExpr *E) {
683 auto &Token = H.addToken(E->getBeginLoc(), HighlightingKind::Operator);
684 if (isa_and_present<CXXMethodDecl>(E->getOperatorNew()))
685 Token.addModifier(HighlightingModifier::UserDefined);
686 return true;
687 }
688
689 bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
690 auto &Token = H.addToken(E->getBeginLoc(), HighlightingKind::Operator);
691 if (isa_and_present<CXXMethodDecl>(E->getOperatorDelete()))
692 Token.addModifier(HighlightingModifier::UserDefined);
693 return true;
694 }
695
696 bool VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
697 const auto &B = E->getAngleBrackets();
698 H.addAngleBracketTokens(B.getBegin(), B.getEnd());
699 return true;
700 }
701
702 bool VisitCallExpr(CallExpr *E) {
703 // Highlighting parameters passed by non-const reference does not really
704 // make sense for literals...
705 if (isa<UserDefinedLiteral>(E))
706 return true;
707
708 // FIXME: consider highlighting parameters of some other overloaded
709 // operators as well
710 llvm::ArrayRef<const Expr *> Args = {E->getArgs(), E->getNumArgs()};
711 if (auto *CallOp = dyn_cast<CXXOperatorCallExpr>(E)) {
712 switch (CallOp->getOperator()) {
713 case OO_Call:
714 case OO_Subscript:
715 Args = Args.drop_front(); // Drop object parameter
716 break;
717 default:
718 return true;
719 }
720 }
721
722 highlightMutableReferenceArguments(
723 dyn_cast_or_null<FunctionDecl>(E->getCalleeDecl()), Args);
724
725 return true;
726 }
727
728 void highlightMutableReferenceArgument(QualType T, const Expr *Arg) {
729 if (!Arg)
730 return;
731
732 // Is this parameter passed by non-const pointer or reference?
733 // FIXME The condition T->idDependentType() could be relaxed a bit,
734 // e.g. std::vector<T>& is dependent but we would want to highlight it
735 bool IsRef = T->isLValueReferenceType();
736 bool IsPtr = T->isPointerType();
737 if ((!IsRef && !IsPtr) || T->getPointeeType().isConstQualified() ||
738 T->isDependentType()) {
739 return;
740 }
741
742 std::optional<SourceLocation> Location;
743
744 // FIXME Add "unwrapping" for ArraySubscriptExpr,
745 // e.g. highlight `a` in `a[i]`
746 // FIXME Handle dependent expression types
747 if (auto *IC = dyn_cast<ImplicitCastExpr>(Arg))
748 Arg = IC->getSubExprAsWritten();
749 if (auto *UO = dyn_cast<UnaryOperator>(Arg)) {
750 if (UO->getOpcode() == UO_AddrOf)
751 Arg = UO->getSubExpr();
752 }
753 if (auto *DR = dyn_cast<DeclRefExpr>(Arg))
754 Location = DR->getLocation();
755 else if (auto *M = dyn_cast<MemberExpr>(Arg))
756 Location = M->getMemberLoc();
757
758 if (Location)
759 H.addExtraModifier(*Location,
762 }
763
764 void
765 highlightMutableReferenceArguments(const FunctionDecl *FD,
766 llvm::ArrayRef<const Expr *const> Args) {
767 if (!FD)
768 return;
769
770 if (auto *ProtoType = FD->getType()->getAs<FunctionProtoType>()) {
771 // Iterate over the types of the function parameters.
772 // If any of them are non-const reference paramteres, add it as a
773 // highlighting modifier to the corresponding expression
774 for (size_t I = 0;
775 I < std::min(size_t(ProtoType->getNumParams()), Args.size()); ++I) {
776 highlightMutableReferenceArgument(ProtoType->getParamType(I), Args[I]);
777 }
778 }
779 }
780
781 bool VisitDecltypeTypeLoc(DecltypeTypeLoc L) {
782 if (auto K = kindForType(L.getTypePtr(), H.getResolver())) {
783 auto &Tok = H.addToken(L.getBeginLoc(), *K)
784 .addModifier(HighlightingModifier::Deduced);
785 if (auto Mod = scopeModifier(L.getTypePtr()))
786 Tok.addModifier(*Mod);
787 if (isDefaultLibrary(L.getTypePtr()))
789 }
790 return true;
791 }
792
793 bool VisitCXXDestructorDecl(CXXDestructorDecl *D) {
794 if (auto *TI = D->getNameInfo().getNamedTypeInfo()) {
795 SourceLocation Loc = TI->getTypeLoc().getBeginLoc();
796 H.addExtraModifier(Loc, HighlightingModifier::ConstructorOrDestructor);
797 H.addExtraModifier(Loc, HighlightingModifier::Declaration);
798 if (D->isThisDeclarationADefinition())
799 H.addExtraModifier(Loc, HighlightingModifier::Definition);
800 }
801 return true;
802 }
803
804 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *CE) {
805 // getMethodDecl can return nullptr with member pointers, e.g.
806 // `(foo.*pointer_to_member_fun)(arg);`
807 if (auto *D = CE->getMethodDecl()) {
808 if (isa<CXXDestructorDecl>(D)) {
809 if (auto *ME = dyn_cast<MemberExpr>(CE->getCallee())) {
810 if (auto *TI = ME->getMemberNameInfo().getNamedTypeInfo()) {
811 H.addExtraModifier(TI->getTypeLoc().getBeginLoc(),
813 }
814 }
815 } else if (D->isOverloadedOperator()) {
816 if (auto *ME = dyn_cast<MemberExpr>(CE->getCallee()))
817 H.addToken(
818 ME->getMemberNameInfo().getCXXOperatorNameRange().getBegin(),
821 }
822 }
823 return true;
824 }
825
826 bool VisitDeclaratorDecl(DeclaratorDecl *D) {
827 for (unsigned i = 0; i < D->getNumTemplateParameterLists(); ++i) {
828 if (auto *TPL = D->getTemplateParameterList(i))
829 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
830 }
831 auto *AT = D->getType()->getContainedAutoType();
832 if (!AT)
833 return true;
834 auto K =
835 kindForType(AT->getDeducedType().getTypePtrOrNull(), H.getResolver());
836 if (!K)
837 return true;
838 auto *TSI = D->getTypeSourceInfo();
839 if (!TSI)
840 return true;
841 SourceLocation StartLoc =
842 TSI->getTypeLoc().getContainedAutoTypeLoc().getNameLoc();
843 // The AutoType may not have a corresponding token, e.g. in the case of
844 // init-captures. In this case, StartLoc overlaps with the location
845 // of the decl itself, and producing a token for the type here would result
846 // in both it and the token for the decl being dropped due to conflict.
847 if (StartLoc == D->getLocation())
848 return true;
849
850 auto &Tok =
851 H.addToken(StartLoc, *K).addModifier(HighlightingModifier::Deduced);
852 const Type *Deduced = AT->getDeducedType().getTypePtrOrNull();
853 if (auto Mod = scopeModifier(Deduced))
854 Tok.addModifier(*Mod);
855 if (isDefaultLibrary(Deduced))
857 return true;
858 }
859
860 // We handle objective-C selectors specially, because one reference can
861 // cover several non-contiguous tokens.
862 void highlightObjCSelector(const ArrayRef<SourceLocation> &Locs, bool Decl,
863 bool Def, bool Class, bool DefaultLibrary) {
864 HighlightingKind Kind =
866 for (SourceLocation Part : Locs) {
867 auto &Tok =
868 H.addToken(Part, Kind).addModifier(HighlightingModifier::ClassScope);
869 if (Decl)
870 Tok.addModifier(HighlightingModifier::Declaration);
871 if (Def)
872 Tok.addModifier(HighlightingModifier::Definition);
873 if (Class)
874 Tok.addModifier(HighlightingModifier::Static);
875 if (DefaultLibrary)
877 }
878 }
879
880 bool VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
881 llvm::SmallVector<SourceLocation> Locs;
882 OMD->getSelectorLocs(Locs);
883 highlightObjCSelector(Locs, /*Decl=*/true,
884 OMD->isThisDeclarationADefinition(),
885 OMD->isClassMethod(), isDefaultLibrary(OMD));
886 return true;
887 }
888
889 bool VisitObjCMessageExpr(ObjCMessageExpr *OME) {
890 llvm::SmallVector<SourceLocation> Locs;
891 OME->getSelectorLocs(Locs);
892 bool DefaultLibrary = false;
893 if (ObjCMethodDecl *OMD = OME->getMethodDecl())
894 DefaultLibrary = isDefaultLibrary(OMD);
895 highlightObjCSelector(Locs, /*Decl=*/false, /*Def=*/false,
896 OME->isClassMessage(), DefaultLibrary);
897 return true;
898 }
899
900 // Objective-C allows you to use property syntax `self.prop` as sugar for
901 // `[self prop]` and `[self setProp:]` when there's no explicit `@property`
902 // for `prop` as well as for class properties. We treat this like a property
903 // even though semantically it's equivalent to a method expression.
904 void highlightObjCImplicitPropertyRef(const ObjCMethodDecl *OMD,
905 SourceLocation Loc) {
906 auto &Tok = H.addToken(Loc, HighlightingKind::Field)
908 if (OMD->isClassMethod())
909 Tok.addModifier(HighlightingModifier::Static);
910 if (isDefaultLibrary(OMD))
912 }
913
914 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *OPRE) {
915 // We need to handle implicit properties here since they will appear to
916 // reference `ObjCMethodDecl` via an implicit `ObjCMessageExpr`, so normal
917 // highlighting will not work.
918 if (!OPRE->isImplicitProperty())
919 return true;
920 // A single property expr can reference both a getter and setter, but we can
921 // only provide a single semantic token, so prefer the getter. In most cases
922 // the end result should be the same, although it's technically possible
923 // that the user defines a setter for a system SDK.
924 if (OPRE->isMessagingGetter()) {
925 highlightObjCImplicitPropertyRef(OPRE->getImplicitPropertyGetter(),
926 OPRE->getLocation());
927 return true;
928 }
929 if (OPRE->isMessagingSetter()) {
930 highlightObjCImplicitPropertyRef(OPRE->getImplicitPropertySetter(),
931 OPRE->getLocation());
932 }
933 return true;
934 }
935
936 bool VisitOverloadExpr(OverloadExpr *E) {
937 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
938 if (!E->decls().empty())
939 return true; // handled by findExplicitReferences.
940 auto &Tok = H.addToken(E->getNameLoc(), HighlightingKind::Unknown)
942 if (llvm::isa<UnresolvedMemberExpr>(E))
943 Tok.addModifier(HighlightingModifier::ClassScope);
944 // other case is UnresolvedLookupExpr, scope is unknown.
945 return true;
946 }
947
948 bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
949 H.addToken(E->getMemberNameInfo().getLoc(), HighlightingKind::Unknown)
952 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
953 return true;
954 }
955
956 bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
957 H.addToken(E->getNameInfo().getLoc(), HighlightingKind::Unknown)
960 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
961 return true;
962 }
963
964 bool VisitAttr(Attr *A) {
965 switch (A->getKind()) {
966 case attr::Override:
967 case attr::Final:
968 H.addToken(A->getLocation(), HighlightingKind::Modifier);
969 break;
970 default:
971 break;
972 }
973 return true;
974 }
975
976 bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
977 H.addToken(L.getNameLoc(), HighlightingKind::Type)
980 return true;
981 }
982
983 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
984 if (!L.getTypePtr()->getTemplateName().getAsTemplateDecl(
985 /*IgnoreDeduced=*/true))
986 H.addToken(L.getTemplateNameLoc(), HighlightingKind::Type)
989 H.addAngleBracketTokens(L.getLAngleLoc(), L.getRAngleLoc());
990 return true;
991 }
992
993 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc L) {
994 // Handle template template arguments only (other arguments are handled by
995 // their Expr, TypeLoc etc values).
996 if (L.getArgument().getKind() != TemplateArgument::Template &&
997 L.getArgument().getKind() != TemplateArgument::TemplateExpansion)
998 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(L);
999
1000 TemplateName N = L.getArgument().getAsTemplateOrTemplatePattern();
1001 switch (N.getKind()) {
1002 case TemplateName::OverloadedTemplate:
1003 // Template template params must always be class templates.
1004 // Don't bother to try to work out the scope here.
1005 H.addToken(L.getTemplateNameLoc(), HighlightingKind::Class);
1006 break;
1007 case TemplateName::DependentTemplate:
1008 case TemplateName::AssumedTemplate:
1009 H.addToken(L.getTemplateNameLoc(), HighlightingKind::Class)
1011 break;
1012 case TemplateName::Template:
1013 case TemplateName::QualifiedTemplate:
1014 case TemplateName::SubstTemplateTemplateParm:
1015 case TemplateName::SubstTemplateTemplateParmPack:
1016 case TemplateName::UsingTemplate:
1017 case TemplateName::DeducedTemplate:
1018 // Names that could be resolved to a TemplateDecl are handled elsewhere.
1019 break;
1020 }
1021 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(L);
1022 }
1023
1024private:
1025 HighlightingsBuilder &H;
1026};
1027} // namespace
1028
1029std::vector<HighlightingToken>
1030getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens) {
1031 auto &C = AST.getASTContext();
1032 HighlightingFilter Filter = HighlightingFilter::fromCurrentConfig();
1033 if (!IncludeInactiveRegionTokens)
1034 Filter.disableKind(HighlightingKind::InactiveCode);
1035 // Add highlightings for AST nodes.
1036 HighlightingsBuilder Builder(AST, Filter);
1037 // Highlight 'decltype' and 'auto' as their underlying types.
1038 CollectExtraHighlightings(Builder).TraverseAST(C);
1039 // Highlight all decls and references coming from the AST.
1041 C,
1042 [&](ReferenceLoc R) {
1043 for (const NamedDecl *Decl : R.Targets) {
1044 if (!canHighlightName(Decl->getDeclName()))
1045 continue;
1046 auto Kind = kindForDecl(Decl, AST.getHeuristicResolver());
1047 if (!Kind)
1048 continue;
1049 auto &Tok = Builder.addToken(R.NameLoc, *Kind);
1050
1051 // The attribute tests don't want to look at the template.
1052 if (auto *TD = dyn_cast<TemplateDecl>(Decl)) {
1053 if (auto *Templated = TD->getTemplatedDecl())
1054 Decl = Templated;
1055 }
1056 if (auto Mod = scopeModifier(Decl))
1057 Tok.addModifier(*Mod);
1058
1059 const auto SymbolTags = computeSymbolTags(*Decl);
1060
1061 static const thread_local llvm::DenseMap<SymbolTag,
1063 TagModifierMap = {
1069 // Declaration and Definition are handled separately below.
1070 };
1071
1072 for (const auto &[Tag, Modifier] : TagModifierMap) {
1073 if (SymbolTags & toSymbolTagBitmask(Tag))
1074 Tok.addModifier(Modifier);
1075 }
1076
1077 if (R.IsDecl &&
1079 Tok.addModifier(HighlightingModifier::Declaration);
1080
1082 Tok.addModifier(HighlightingModifier::Definition);
1083 }
1084
1085 if (isDependent(Decl))
1086 Tok.addModifier(HighlightingModifier::DependentName);
1087
1088 if (isDefaultLibrary(Decl))
1089 Tok.addModifier(HighlightingModifier::DefaultLibrary);
1090
1091 if (isa<CXXConstructorDecl>(Decl))
1093 }
1094 },
1095 AST.getHeuristicResolver());
1096 // Add highlightings for macro references.
1097 auto AddMacro = [&](const MacroOccurrence &M) {
1098 auto &T = Builder.addToken(M.toRange(C.getSourceManager()),
1100 T.addModifier(HighlightingModifier::GlobalScope);
1101 if (M.IsDefinition)
1102 T.addModifier(HighlightingModifier::Declaration);
1103 };
1104 for (const auto &SIDToRefs : AST.getMacros().MacroRefs)
1105 for (const auto &M : SIDToRefs.second)
1106 AddMacro(M);
1107 for (const auto &M : AST.getMacros().UnknownMacros)
1108 AddMacro(M);
1109
1110 return std::move(Builder).collect(AST);
1111}
1112
1113llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K) {
1114 switch (K) {
1116 return OS << "Variable";
1118 return OS << "LocalVariable";
1120 return OS << "Parameter";
1122 return OS << "Function";
1124 return OS << "Method";
1126 return OS << "StaticMethod";
1128 return OS << "Field";
1130 return OS << "StaticField";
1132 return OS << "Class";
1134 return OS << "Interface";
1136 return OS << "Enum";
1138 return OS << "EnumConstant";
1140 return OS << "Typedef";
1142 return OS << "Type";
1144 return OS << "Unknown";
1146 return OS << "Namespace";
1148 return OS << "TemplateParameter";
1150 return OS << "Concept";
1152 return OS << "Primitive";
1154 return OS << "Macro";
1156 return OS << "Modifier";
1158 return OS << "Operator";
1160 return OS << "Bracket";
1162 return OS << "Label";
1164 return OS << "InactiveCode";
1165 }
1166 llvm_unreachable("invalid HighlightingKind");
1167}
1168std::optional<HighlightingKind>
1169highlightingKindFromString(llvm::StringRef Name) {
1170 static llvm::StringMap<HighlightingKind> Lookup = {
1171 {"Variable", HighlightingKind::Variable},
1172 {"LocalVariable", HighlightingKind::LocalVariable},
1173 {"Parameter", HighlightingKind::Parameter},
1174 {"Function", HighlightingKind::Function},
1175 {"Method", HighlightingKind::Method},
1176 {"StaticMethod", HighlightingKind::StaticMethod},
1177 {"Field", HighlightingKind::Field},
1178 {"StaticField", HighlightingKind::StaticField},
1179 {"Class", HighlightingKind::Class},
1180 {"Interface", HighlightingKind::Interface},
1181 {"Enum", HighlightingKind::Enum},
1182 {"EnumConstant", HighlightingKind::EnumConstant},
1183 {"Typedef", HighlightingKind::Typedef},
1184 {"Type", HighlightingKind::Type},
1185 {"Unknown", HighlightingKind::Unknown},
1186 {"Namespace", HighlightingKind::Namespace},
1187 {"TemplateParameter", HighlightingKind::TemplateParameter},
1188 {"Concept", HighlightingKind::Concept},
1189 {"Primitive", HighlightingKind::Primitive},
1190 {"Macro", HighlightingKind::Macro},
1191 {"Modifier", HighlightingKind::Modifier},
1192 {"Operator", HighlightingKind::Operator},
1193 {"Bracket", HighlightingKind::Bracket},
1194 {"InactiveCode", HighlightingKind::InactiveCode},
1195 };
1196
1197 auto It = Lookup.find(Name);
1198 return It != Lookup.end() ? std::make_optional(It->getValue()) : std::nullopt;
1199}
1200llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingModifier K) {
1201 switch (K) {
1203 return OS << "decl"; // abbreviation for common case
1205 return OS << "def"; // abbrevation for common case
1207 return OS << "constrDestr";
1208 default:
1209 return OS << toSemanticTokenModifier(K);
1210 }
1211}
1212std::optional<HighlightingModifier>
1213highlightingModifierFromString(llvm::StringRef Name) {
1214 static llvm::StringMap<HighlightingModifier> Lookup = {
1215 {"Declaration", HighlightingModifier::Declaration},
1216 {"Definition", HighlightingModifier::Definition},
1217 {"Deprecated", HighlightingModifier::Deprecated},
1218 {"Deduced", HighlightingModifier::Deduced},
1219 {"Readonly", HighlightingModifier::Readonly},
1220 {"Static", HighlightingModifier::Static},
1221 {"Abstract", HighlightingModifier::Abstract},
1222 {"Virtual", HighlightingModifier::Virtual},
1223 {"DependentName", HighlightingModifier::DependentName},
1224 {"DefaultLibrary", HighlightingModifier::DefaultLibrary},
1225 {"UsedAsMutableReference", HighlightingModifier::UsedAsMutableReference},
1226 {"UsedAsMutablePointer", HighlightingModifier::UsedAsMutablePointer},
1227 {"ConstructorOrDestructor",
1229 {"UserDefined", HighlightingModifier::UserDefined},
1230 {"FunctionScope", HighlightingModifier::FunctionScope},
1231 {"ClassScope", HighlightingModifier::ClassScope},
1232 {"FileScope", HighlightingModifier::FileScope},
1233 {"GlobalScope", HighlightingModifier::GlobalScope},
1234 };
1235
1236 auto It = Lookup.find(Name);
1237 return It != Lookup.end() ? std::make_optional(It->getValue()) : std::nullopt;
1238}
1239
1241 return std::tie(L.R, L.Kind, L.Modifiers) ==
1242 std::tie(R.R, R.Kind, R.Modifiers);
1243}
1245 return std::tie(L.R, L.Kind, L.Modifiers) <
1246 std::tie(R.R, R.Kind, R.Modifiers);
1247}
1248
1249std::vector<SemanticToken>
1250toSemanticTokens(llvm::ArrayRef<HighlightingToken> Tokens,
1251 llvm::StringRef Code) {
1252 assert(llvm::is_sorted(Tokens));
1253 std::vector<SemanticToken> Result;
1254 // In case we split a HighlightingToken into multiple tokens (e.g. because it
1255 // was spanning multiple lines), this tracks the last one. This prevents
1256 // having a copy all the time.
1257 HighlightingToken Scratch;
1258 const HighlightingToken *Last = nullptr;
1259 for (const HighlightingToken &Tok : Tokens) {
1260 Result.emplace_back();
1261 SemanticToken *Out = &Result.back();
1262 // deltaStart/deltaLine are relative if possible.
1263 if (Last) {
1264 assert(Tok.R.start.line >= Last->R.end.line);
1265 Out->deltaLine = Tok.R.start.line - Last->R.end.line;
1266 if (Out->deltaLine == 0) {
1267 assert(Tok.R.start.character >= Last->R.start.character);
1268 Out->deltaStart = Tok.R.start.character - Last->R.start.character;
1269 } else {
1270 Out->deltaStart = Tok.R.start.character;
1271 }
1272 } else {
1273 Out->deltaLine = Tok.R.start.line;
1274 Out->deltaStart = Tok.R.start.character;
1275 }
1276 Out->tokenType = static_cast<unsigned>(Tok.Kind);
1277 Out->tokenModifiers = Tok.Modifiers;
1278 Last = &Tok;
1279
1280 if (Tok.R.end.line == Tok.R.start.line) {
1281 Out->length = Tok.R.end.character - Tok.R.start.character;
1282 } else {
1283 // If the token spans a line break, split it into multiple pieces for each
1284 // line.
1285 // This is slow, but multiline tokens are rare.
1286 // FIXME: There's a client capability for supporting multiline tokens,
1287 // respect that.
1288 auto TokStartOffset = llvm::cantFail(positionToOffset(Code, Tok.R.start));
1289 // Note that the loop doesn't cover the last line, which has a special
1290 // length.
1291 for (int I = Tok.R.start.line; I < Tok.R.end.line; ++I) {
1292 auto LineEnd = Code.find('\n', TokStartOffset);
1293 assert(LineEnd != Code.npos);
1294 Out->length = LineEnd - TokStartOffset;
1295 // Token continues on next line, right after the line break.
1296 TokStartOffset = LineEnd + 1;
1297 Result.emplace_back();
1298 Out = &Result.back();
1299 *Out = Result[Result.size() - 2];
1300 // New token starts at the first column of the next line.
1301 Out->deltaLine = 1;
1302 Out->deltaStart = 0;
1303 }
1304 // This is the token on last line.
1305 Out->length = Tok.R.end.character;
1306 // Update the start location for last token, as that's used in the
1307 // relative delta calculation for following tokens.
1308 Scratch = *Last;
1309 Scratch.R.start.line = Tok.R.end.line;
1310 Scratch.R.start.character = 0;
1311 Last = &Scratch;
1312 }
1313 }
1314 return Result;
1315}
1317 switch (Kind) {
1321 return "variable";
1323 return "parameter";
1325 return "function";
1327 return "method";
1329 // FIXME: better method with static modifier?
1330 return "function";
1332 return "property";
1334 return "class";
1336 return "interface";
1338 return "enum";
1340 return "enumMember";
1343 return "type";
1345 return "unknown"; // nonstandard
1347 return "namespace";
1349 return "typeParameter";
1351 return "concept"; // nonstandard
1353 return "type";
1355 return "macro";
1357 return "modifier";
1359 return "operator";
1361 return "bracket";
1363 return "label";
1365 return "comment";
1366 }
1367 llvm_unreachable("unhandled HighlightingKind");
1368}
1369
1371 switch (Modifier) {
1373 return "declaration";
1375 return "definition";
1377 return "deprecated";
1379 return "readonly";
1381 return "static";
1383 return "deduced"; // nonstandard
1385 return "abstract";
1387 return "virtual";
1389 return "dependentName"; // nonstandard
1391 return "defaultLibrary";
1393 return "usedAsMutableReference"; // nonstandard
1395 return "usedAsMutablePointer"; // nonstandard
1397 return "constructorOrDestructor"; // nonstandard
1399 return "userDefined"; // nonstandard
1401 return "functionScope"; // nonstandard
1403 return "classScope"; // nonstandard
1405 return "fileScope"; // nonstandard
1407 return "globalScope"; // nonstandard
1408 }
1409 llvm_unreachable("unhandled HighlightingModifier");
1410}
1411
1412std::vector<SemanticTokensEdit> diffTokens(llvm::ArrayRef<SemanticToken> Old,
1413 llvm::ArrayRef<SemanticToken> New) {
1414 // For now, just replace everything from the first-last modification.
1415 // FIXME: use a real diff instead, this is bad with include-insertion.
1416
1417 unsigned Offset = 0;
1418 while (!Old.empty() && !New.empty() && Old.front() == New.front()) {
1419 ++Offset;
1420 Old = Old.drop_front();
1421 New = New.drop_front();
1422 }
1423 while (!Old.empty() && !New.empty() && Old.back() == New.back()) {
1424 Old = Old.drop_back();
1425 New = New.drop_back();
1426 }
1427
1428 if (Old.empty() && New.empty())
1429 return {};
1431 Edit.startToken = Offset;
1432 Edit.deleteTokens = Old.size();
1433 Edit.tokens = New;
1434 return {std::move(Edit)};
1435}
1436
1437std::vector<Range> getInactiveRegions(ParsedAST &AST) {
1438 std::vector<Range> SkippedRanges(std::move(AST.getMacros().SkippedRanges));
1439 const auto &SM = AST.getSourceManager();
1440 StringRef MainCode = SM.getBufferOrFake(SM.getMainFileID()).getBuffer();
1441 std::vector<Range> InactiveRegions;
1442 for (const Range &Skipped : SkippedRanges) {
1443 Range Inactive = Skipped;
1444 // Sometimes, SkippedRanges contains a range ending at position 0
1445 // of a line. Clients that apply whole-line styles will treat that
1446 // line as inactive which is not desirable, so adjust the ending
1447 // position to be the end of the previous line.
1448 if (Inactive.end.character == 0 && Inactive.end.line > 0) {
1449 --Inactive.end.line;
1450 }
1451 // Exclude the directive lines themselves from the range.
1452 if (Inactive.end.line >= Inactive.start.line + 2) {
1453 ++Inactive.start.line;
1454 --Inactive.end.line;
1455 } else {
1456 // range would be empty, e.g. #endif on next line after #ifdef
1457 continue;
1458 }
1459 // Since we've adjusted the ending line, we need to recompute the
1460 // column to reflect the end of that line.
1461 if (auto EndOfLine = endOfLine(MainCode, Inactive.end.line)) {
1462 Inactive.end = *EndOfLine;
1463 } else {
1464 elog("Failed to determine end of line: {0}", EndOfLine.takeError());
1465 continue;
1466 }
1467 InactiveRegions.push_back(Inactive);
1468 }
1469 return InactiveRegions;
1470}
1471
1472} // namespace clangd
1473} // namespace clang
static cl::opt< std::string > Config("config", desc(R"( Specifies a configuration in YAML/JSON format: -config="{Checks:' *', CheckOptions:{x:y}}" When the value is empty, clang-tidy will attempt to find a file named .clang-tidy for each source file in its parent directories. )"), cl::init(""), cl::cat(ClangTidyCategory))
Stores and provides access to parsed AST.
Definition ParsedAST.h:46
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
std::optional< HighlightingModifier > highlightingModifierFromString(llvm::StringRef Name)
llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier)
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens)
std::optional< HighlightingKind > highlightingKindFromString(llvm::StringRef Name)
uint32_t SymbolTags
A bitmask type representing symbol tags supported by LSP.
Definition FindSymbols.h:28
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
llvm::StringRef toSemanticTokenType(HighlightingKind Kind)
size_t lspLength(llvm::StringRef Code)
SymbolTag
Symbol tags are extra annotations that can be attached to a symbol.
Definition Protocol.h:1109
std::vector< SemanticToken > toSemanticTokens(llvm::ArrayRef< HighlightingToken > Tokens, llvm::StringRef Code)
std::vector< SemanticTokensEdit > diffTokens(llvm::ArrayRef< SemanticToken > Old, llvm::ArrayRef< SemanticToken > New)
void findExplicitReferences(const Stmt *S, llvm::function_ref< void(ReferenceLoc)> Out, const HeuristicResolver *Resolver)
Recursively traverse S and report all references explicitly written in the code.
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition Headers.cpp:356
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
SymbolTags toSymbolTagBitmask(const SymbolTag ST)
Converts a single SymbolTag to a bitmask.
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
bool operator<(const Ref &L, const Ref &R)
Definition Ref.h:98
@ Type
An inlay hint that for a type annotation.
Definition Protocol.h:1710
std::vector< Range > getInactiveRegions(ParsedAST &AST)
void elog(const char *Fmt, Ts &&... Vals)
Definition Logger.h:61
@ Underlying
This is the underlying declaration for a renaming-alias, decltype etc.
Definition FindTarget.h:121
SymbolTags computeSymbolTags(const NamedDecl &ND)
Computes symbol tags for a given NamedDecl.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static const Config & current()
Returns the Config of the current Context, or an empty configuration.
Definition Config.cpp:17
A set of edits generated for a single file.
Definition SourceCode.h:189
int line
Line position in a document (zero-based).
Definition Protocol.h:158
int character
Character offset on a line in a document (zero-based).
Definition Protocol.h:163
Position start
The range's start position.
Definition Protocol.h:187
Position end
The range's end position.
Definition Protocol.h:190
Information about a reference written in the source code, independent of the actual AST node that thi...
Definition FindTarget.h:128
bool IsDecl
True if the reference is a declaration or definition;.
Definition FindTarget.h:134
SourceLocation NameLoc
Start location of the last name part, i.e. 'foo' in 'ns::foo<int>'.
Definition FindTarget.h:132
llvm::SmallVector< const NamedDecl *, 1 > Targets
A list of targets referenced by this name.
Definition FindTarget.h:140
Specifies a single semantic token in the document.
Definition Protocol.h:1864
unsigned length
the length of the token. A token cannot be multiline
Definition Protocol.h:1871
unsigned deltaStart
token start character, relative to the previous token (relative to 0 or the previous token's start if...
Definition Protocol.h:1869
unsigned deltaLine
token line number, relative to the previous token
Definition Protocol.h:1866
unsigned tokenType
will be looked up in SemanticTokensLegend.tokenTypes
Definition Protocol.h:1873
unsigned tokenModifiers
each set bit will be looked up in SemanticTokensLegend.tokenModifiers
Definition Protocol.h:1875
Describes a replacement of a contiguous range of semanticTokens.
Definition Protocol.h:1912