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 (TemplateParameterList *TPL : D->getTemplateParameterLists())
587 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
588 return true;
589 }
590
591 bool
592 VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *D) {
593 if (auto *Args = D->getTemplateArgsAsWritten())
594 H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc());
595 return true;
596 }
597
598 bool VisitClassTemplatePartialSpecializationDecl(
599 ClassTemplatePartialSpecializationDecl *D) {
600 if (auto *TPL = D->getTemplateParameters())
601 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
602 return true;
603 }
604
605 bool VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
606 if (auto *Args = D->getTemplateArgsAsWritten())
607 H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc());
608 return true;
609 }
610
611 bool VisitVarTemplatePartialSpecializationDecl(
612 VarTemplatePartialSpecializationDecl *D) {
613 if (auto *TPL = D->getTemplateParameters())
614 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
615 return true;
616 }
617
618 bool VisitDeclRefExpr(DeclRefExpr *E) {
619 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
620 return true;
621 }
622 bool VisitMemberExpr(MemberExpr *E) {
623 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
624 return true;
625 }
626
627 bool VisitFunctionDecl(FunctionDecl *D) {
628 if (D->isOverloadedOperator()) {
629 const auto AddOpDeclToken = [&](SourceLocation Loc) {
630 auto &Token = H.addToken(Loc, HighlightingKind::Operator)
632 if (D->isThisDeclarationADefinition())
633 Token.addModifier(HighlightingModifier::Definition);
634 };
635 const auto Range = D->getNameInfo().getCXXOperatorNameRange();
636 AddOpDeclToken(Range.getBegin());
637 const auto Kind = D->getOverloadedOperator();
638 if (Kind == OO_Call || Kind == OO_Subscript)
639 AddOpDeclToken(Range.getEnd());
640 }
641 if (auto *Args = D->getTemplateSpecializationArgsAsWritten())
642 H.addAngleBracketTokens(Args->getLAngleLoc(), Args->getRAngleLoc());
643 return true;
644 }
645
646 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
647 const auto AddOpToken = [&](SourceLocation Loc) {
648 H.addToken(Loc, HighlightingKind::Operator)
650 };
651 AddOpToken(E->getOperatorLoc());
652 const auto Kind = E->getOperator();
653 if (Kind == OO_Call || Kind == OO_Subscript) {
654 if (auto *Callee = E->getCallee())
655 AddOpToken(Callee->getBeginLoc());
656 }
657 return true;
658 }
659
660 bool VisitUnaryOperator(UnaryOperator *Op) {
661 auto &Token = H.addToken(Op->getOperatorLoc(), HighlightingKind::Operator);
662 if (Op->getSubExpr()->isTypeDependent())
663 Token.addModifier(HighlightingModifier::UserDefined);
664 return true;
665 }
666
667 bool VisitBinaryOperator(BinaryOperator *Op) {
668 auto &Token = H.addToken(Op->getOperatorLoc(), HighlightingKind::Operator);
669 if (Op->getLHS()->isTypeDependent() || Op->getRHS()->isTypeDependent())
670 Token.addModifier(HighlightingModifier::UserDefined);
671 return true;
672 }
673
674 bool VisitConditionalOperator(ConditionalOperator *Op) {
675 H.addToken(Op->getQuestionLoc(), HighlightingKind::Operator);
676 H.addToken(Op->getColonLoc(), HighlightingKind::Operator);
677 return true;
678 }
679
680 bool VisitCXXNewExpr(CXXNewExpr *E) {
681 auto &Token = H.addToken(E->getBeginLoc(), HighlightingKind::Operator);
682 if (isa_and_present<CXXMethodDecl>(E->getOperatorNew()))
683 Token.addModifier(HighlightingModifier::UserDefined);
684 return true;
685 }
686
687 bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
688 auto &Token = H.addToken(E->getBeginLoc(), HighlightingKind::Operator);
689 if (isa_and_present<CXXMethodDecl>(E->getOperatorDelete()))
690 Token.addModifier(HighlightingModifier::UserDefined);
691 return true;
692 }
693
694 bool VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
695 const auto &B = E->getAngleBrackets();
696 H.addAngleBracketTokens(B.getBegin(), B.getEnd());
697 return true;
698 }
699
700 bool VisitCallExpr(CallExpr *E) {
701 // Highlighting parameters passed by non-const reference does not really
702 // make sense for literals...
703 if (isa<UserDefinedLiteral>(E))
704 return true;
705
706 // FIXME: consider highlighting parameters of some other overloaded
707 // operators as well
708 llvm::ArrayRef<const Expr *> Args = {E->getArgs(), E->getNumArgs()};
709 if (auto *CallOp = dyn_cast<CXXOperatorCallExpr>(E)) {
710 switch (CallOp->getOperator()) {
711 case OO_Call:
712 case OO_Subscript:
713 Args = Args.drop_front(); // Drop object parameter
714 break;
715 default:
716 return true;
717 }
718 }
719
720 highlightMutableReferenceArguments(
721 dyn_cast_or_null<FunctionDecl>(E->getCalleeDecl()), Args);
722
723 return true;
724 }
725
726 void highlightMutableReferenceArgument(QualType T, const Expr *Arg) {
727 if (!Arg)
728 return;
729
730 // Is this parameter passed by non-const pointer or reference?
731 // FIXME The condition T->idDependentType() could be relaxed a bit,
732 // e.g. std::vector<T>& is dependent but we would want to highlight it
733 bool IsRef = T->isLValueReferenceType();
734 bool IsPtr = T->isPointerType();
735 if ((!IsRef && !IsPtr) || T->getPointeeType().isConstQualified() ||
736 T->isDependentType()) {
737 return;
738 }
739
740 std::optional<SourceLocation> Location;
741
742 // FIXME Add "unwrapping" for ArraySubscriptExpr,
743 // e.g. highlight `a` in `a[i]`
744 // FIXME Handle dependent expression types
745 if (auto *IC = dyn_cast<ImplicitCastExpr>(Arg))
746 Arg = IC->getSubExprAsWritten();
747 if (auto *UO = dyn_cast<UnaryOperator>(Arg)) {
748 if (UO->getOpcode() == UO_AddrOf)
749 Arg = UO->getSubExpr();
750 }
751 if (auto *DR = dyn_cast<DeclRefExpr>(Arg))
752 Location = DR->getLocation();
753 else if (auto *M = dyn_cast<MemberExpr>(Arg))
754 Location = M->getMemberLoc();
755
756 if (Location)
757 H.addExtraModifier(*Location,
760 }
761
762 void
763 highlightMutableReferenceArguments(const FunctionDecl *FD,
764 llvm::ArrayRef<const Expr *const> Args) {
765 if (!FD)
766 return;
767
768 if (auto *ProtoType = FD->getType()->getAs<FunctionProtoType>()) {
769 // Iterate over the types of the function parameters.
770 // If any of them are non-const reference paramteres, add it as a
771 // highlighting modifier to the corresponding expression
772 for (size_t I = 0;
773 I < std::min(size_t(ProtoType->getNumParams()), Args.size()); ++I) {
774 highlightMutableReferenceArgument(ProtoType->getParamType(I), Args[I]);
775 }
776 }
777 }
778
779 bool VisitDecltypeTypeLoc(DecltypeTypeLoc L) {
780 if (auto K = kindForType(L.getTypePtr(), H.getResolver())) {
781 auto &Tok = H.addToken(L.getBeginLoc(), *K)
782 .addModifier(HighlightingModifier::Deduced);
783 if (auto Mod = scopeModifier(L.getTypePtr()))
784 Tok.addModifier(*Mod);
785 if (isDefaultLibrary(L.getTypePtr()))
787 }
788 return true;
789 }
790
791 bool VisitCXXDestructorDecl(CXXDestructorDecl *D) {
792 if (auto *TI = D->getNameInfo().getNamedTypeInfo()) {
793 SourceLocation Loc = TI->getTypeLoc().getBeginLoc();
794 H.addExtraModifier(Loc, HighlightingModifier::ConstructorOrDestructor);
795 H.addExtraModifier(Loc, HighlightingModifier::Declaration);
796 if (D->isThisDeclarationADefinition())
797 H.addExtraModifier(Loc, HighlightingModifier::Definition);
798 }
799 return true;
800 }
801
802 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *CE) {
803 // getMethodDecl can return nullptr with member pointers, e.g.
804 // `(foo.*pointer_to_member_fun)(arg);`
805 if (auto *D = CE->getMethodDecl()) {
806 if (isa<CXXDestructorDecl>(D)) {
807 if (auto *ME = dyn_cast<MemberExpr>(CE->getCallee())) {
808 if (auto *TI = ME->getMemberNameInfo().getNamedTypeInfo()) {
809 H.addExtraModifier(TI->getTypeLoc().getBeginLoc(),
811 }
812 }
813 } else if (D->isOverloadedOperator()) {
814 if (auto *ME = dyn_cast<MemberExpr>(CE->getCallee()))
815 H.addToken(
816 ME->getMemberNameInfo().getCXXOperatorNameRange().getBegin(),
819 }
820 }
821 return true;
822 }
823
824 bool VisitDeclaratorDecl(DeclaratorDecl *D) {
825 for (TemplateParameterList *TPL : D->getTemplateParameterLists())
826 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
827 auto *AT = D->getType()->getContainedAutoType();
828 if (!AT)
829 return true;
830 auto K =
831 kindForType(AT->getDeducedType().getTypePtrOrNull(), H.getResolver());
832 if (!K)
833 return true;
834 auto *TSI = D->getTypeSourceInfo();
835 if (!TSI)
836 return true;
837 SourceLocation StartLoc =
838 TSI->getTypeLoc().getContainedAutoTypeLoc().getNameLoc();
839 // The AutoType may not have a corresponding token, e.g. in the case of
840 // init-captures. In this case, StartLoc overlaps with the location
841 // of the decl itself, and producing a token for the type here would result
842 // in both it and the token for the decl being dropped due to conflict.
843 if (StartLoc == D->getLocation())
844 return true;
845
846 auto &Tok =
847 H.addToken(StartLoc, *K).addModifier(HighlightingModifier::Deduced);
848 const Type *Deduced = AT->getDeducedType().getTypePtrOrNull();
849 if (auto Mod = scopeModifier(Deduced))
850 Tok.addModifier(*Mod);
851 if (isDefaultLibrary(Deduced))
853 return true;
854 }
855
856 // We handle objective-C selectors specially, because one reference can
857 // cover several non-contiguous tokens.
858 void highlightObjCSelector(const ArrayRef<SourceLocation> &Locs, bool Decl,
859 bool Def, bool Class, bool DefaultLibrary) {
860 HighlightingKind Kind =
862 for (SourceLocation Part : Locs) {
863 auto &Tok =
864 H.addToken(Part, Kind).addModifier(HighlightingModifier::ClassScope);
865 if (Decl)
866 Tok.addModifier(HighlightingModifier::Declaration);
867 if (Def)
868 Tok.addModifier(HighlightingModifier::Definition);
869 if (Class)
870 Tok.addModifier(HighlightingModifier::Static);
871 if (DefaultLibrary)
873 }
874 }
875
876 bool VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
877 llvm::SmallVector<SourceLocation> Locs;
878 OMD->getSelectorLocs(Locs);
879 highlightObjCSelector(Locs, /*Decl=*/true,
880 OMD->isThisDeclarationADefinition(),
881 OMD->isClassMethod(), isDefaultLibrary(OMD));
882 return true;
883 }
884
885 bool VisitObjCMessageExpr(ObjCMessageExpr *OME) {
886 llvm::SmallVector<SourceLocation> Locs;
887 OME->getSelectorLocs(Locs);
888 bool DefaultLibrary = false;
889 if (ObjCMethodDecl *OMD = OME->getMethodDecl())
890 DefaultLibrary = isDefaultLibrary(OMD);
891 highlightObjCSelector(Locs, /*Decl=*/false, /*Def=*/false,
892 OME->isClassMessage(), DefaultLibrary);
893 return true;
894 }
895
896 // Objective-C allows you to use property syntax `self.prop` as sugar for
897 // `[self prop]` and `[self setProp:]` when there's no explicit `@property`
898 // for `prop` as well as for class properties. We treat this like a property
899 // even though semantically it's equivalent to a method expression.
900 void highlightObjCImplicitPropertyRef(const ObjCMethodDecl *OMD,
901 SourceLocation Loc) {
902 auto &Tok = H.addToken(Loc, HighlightingKind::Field)
904 if (OMD->isClassMethod())
905 Tok.addModifier(HighlightingModifier::Static);
906 if (isDefaultLibrary(OMD))
908 }
909
910 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *OPRE) {
911 // We need to handle implicit properties here since they will appear to
912 // reference `ObjCMethodDecl` via an implicit `ObjCMessageExpr`, so normal
913 // highlighting will not work.
914 if (!OPRE->isImplicitProperty())
915 return true;
916 // A single property expr can reference both a getter and setter, but we can
917 // only provide a single semantic token, so prefer the getter. In most cases
918 // the end result should be the same, although it's technically possible
919 // that the user defines a setter for a system SDK.
920 if (OPRE->isMessagingGetter()) {
921 highlightObjCImplicitPropertyRef(OPRE->getImplicitPropertyGetter(),
922 OPRE->getLocation());
923 return true;
924 }
925 if (OPRE->isMessagingSetter()) {
926 highlightObjCImplicitPropertyRef(OPRE->getImplicitPropertySetter(),
927 OPRE->getLocation());
928 }
929 return true;
930 }
931
932 bool VisitOverloadExpr(OverloadExpr *E) {
933 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
934 if (!E->decls().empty())
935 return true; // handled by findExplicitReferences.
936 auto &Tok = H.addToken(E->getNameLoc(), HighlightingKind::Unknown)
938 if (llvm::isa<UnresolvedMemberExpr>(E))
939 Tok.addModifier(HighlightingModifier::ClassScope);
940 // other case is UnresolvedLookupExpr, scope is unknown.
941 return true;
942 }
943
944 bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
945 H.addToken(E->getMemberNameInfo().getLoc(), HighlightingKind::Unknown)
948 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
949 return true;
950 }
951
952 bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
953 H.addToken(E->getNameInfo().getLoc(), HighlightingKind::Unknown)
956 H.addAngleBracketTokens(E->getLAngleLoc(), E->getRAngleLoc());
957 return true;
958 }
959
960 bool VisitAttr(Attr *A) {
961 switch (A->getKind()) {
962 case attr::Override:
963 case attr::Final:
964 H.addToken(A->getLocation(), HighlightingKind::Modifier);
965 break;
966 default:
967 break;
968 }
969 return true;
970 }
971
972 bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
973 H.addToken(L.getNameLoc(), HighlightingKind::Type)
976 return true;
977 }
978
979 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
980 if (!L.getTypePtr()->getTemplateName().getAsTemplateDecl(
981 /*IgnoreDeduced=*/true))
982 H.addToken(L.getTemplateNameLoc(), HighlightingKind::Type)
985 H.addAngleBracketTokens(L.getLAngleLoc(), L.getRAngleLoc());
986 return true;
987 }
988
989 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc L) {
990 // Handle template template arguments only (other arguments are handled by
991 // their Expr, TypeLoc etc values).
992 if (L.getArgument().getKind() != TemplateArgument::Template &&
993 L.getArgument().getKind() != TemplateArgument::TemplateExpansion)
994 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(L);
995
996 TemplateName N = L.getArgument().getAsTemplateOrTemplatePattern();
997 switch (N.getKind()) {
998 case TemplateName::OverloadedTemplate:
999 // Template template params must always be class templates.
1000 // Don't bother to try to work out the scope here.
1001 H.addToken(L.getTemplateNameLoc(), HighlightingKind::Class);
1002 break;
1003 case TemplateName::DependentTemplate:
1004 case TemplateName::AssumedTemplate:
1005 H.addToken(L.getTemplateNameLoc(), HighlightingKind::Class)
1007 break;
1008 case TemplateName::Template:
1009 case TemplateName::QualifiedTemplate:
1010 case TemplateName::SubstTemplateTemplateParm:
1011 case TemplateName::SubstTemplateTemplateParmPack:
1012 case TemplateName::UsingTemplate:
1013 case TemplateName::DeducedTemplate:
1014 // Names that could be resolved to a TemplateDecl are handled elsewhere.
1015 break;
1016 }
1017 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(L);
1018 }
1019
1020private:
1021 HighlightingsBuilder &H;
1022};
1023} // namespace
1024
1025std::vector<HighlightingToken>
1026getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens) {
1027 auto &C = AST.getASTContext();
1028 HighlightingFilter Filter = HighlightingFilter::fromCurrentConfig();
1029 if (!IncludeInactiveRegionTokens)
1030 Filter.disableKind(HighlightingKind::InactiveCode);
1031 // Add highlightings for AST nodes.
1032 HighlightingsBuilder Builder(AST, Filter);
1033 // Highlight 'decltype' and 'auto' as their underlying types.
1034 CollectExtraHighlightings(Builder).TraverseAST(C);
1035 // Highlight all decls and references coming from the AST.
1037 C,
1038 [&](ReferenceLoc R) {
1039 for (const NamedDecl *Decl : R.Targets) {
1040 if (!canHighlightName(Decl->getDeclName()))
1041 continue;
1042 auto Kind = kindForDecl(Decl, AST.getHeuristicResolver());
1043 if (!Kind)
1044 continue;
1045 auto &Tok = Builder.addToken(R.NameLoc, *Kind);
1046
1047 // The attribute tests don't want to look at the template.
1048 if (auto *TD = dyn_cast<TemplateDecl>(Decl)) {
1049 if (auto *Templated = TD->getTemplatedDecl())
1050 Decl = Templated;
1051 }
1052 if (auto Mod = scopeModifier(Decl))
1053 Tok.addModifier(*Mod);
1054
1055 const auto SymbolTags = computeSymbolTags(*Decl);
1056
1057 static const thread_local llvm::DenseMap<SymbolTag,
1059 TagModifierMap = {
1065 // Declaration and Definition are handled separately below.
1066 };
1067
1068 for (const auto &[Tag, Modifier] : TagModifierMap) {
1069 if (SymbolTags & toSymbolTagBitmask(Tag))
1070 Tok.addModifier(Modifier);
1071 }
1072
1073 if (R.IsDecl &&
1075 Tok.addModifier(HighlightingModifier::Declaration);
1076
1078 Tok.addModifier(HighlightingModifier::Definition);
1079 }
1080
1081 if (isDependent(Decl))
1082 Tok.addModifier(HighlightingModifier::DependentName);
1083
1084 if (isDefaultLibrary(Decl))
1085 Tok.addModifier(HighlightingModifier::DefaultLibrary);
1086
1087 if (isa<CXXConstructorDecl>(Decl))
1089 }
1090 },
1091 AST.getHeuristicResolver());
1092 // Add highlightings for macro references.
1093 auto AddMacro = [&](const MacroOccurrence &M) {
1094 auto &T = Builder.addToken(M.toRange(C.getSourceManager()),
1096 T.addModifier(HighlightingModifier::GlobalScope);
1097 if (M.IsDefinition)
1098 T.addModifier(HighlightingModifier::Declaration);
1099 };
1100 for (const auto &SIDToRefs : AST.getMacros().MacroRefs)
1101 for (const auto &M : SIDToRefs.second)
1102 AddMacro(M);
1103 for (const auto &M : AST.getMacros().UnknownMacros)
1104 AddMacro(M);
1105
1106 return std::move(Builder).collect(AST);
1107}
1108
1109llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K) {
1110 switch (K) {
1112 return OS << "Variable";
1114 return OS << "LocalVariable";
1116 return OS << "Parameter";
1118 return OS << "Function";
1120 return OS << "Method";
1122 return OS << "StaticMethod";
1124 return OS << "Field";
1126 return OS << "StaticField";
1128 return OS << "Class";
1130 return OS << "Interface";
1132 return OS << "Enum";
1134 return OS << "EnumConstant";
1136 return OS << "Typedef";
1138 return OS << "Type";
1140 return OS << "Unknown";
1142 return OS << "Namespace";
1144 return OS << "TemplateParameter";
1146 return OS << "Concept";
1148 return OS << "Primitive";
1150 return OS << "Macro";
1152 return OS << "Modifier";
1154 return OS << "Operator";
1156 return OS << "Bracket";
1158 return OS << "Label";
1160 return OS << "InactiveCode";
1161 }
1162 llvm_unreachable("invalid HighlightingKind");
1163}
1164std::optional<HighlightingKind>
1165highlightingKindFromString(llvm::StringRef Name) {
1166 static llvm::StringMap<HighlightingKind> Lookup = {
1167 {"Variable", HighlightingKind::Variable},
1168 {"LocalVariable", HighlightingKind::LocalVariable},
1169 {"Parameter", HighlightingKind::Parameter},
1170 {"Function", HighlightingKind::Function},
1171 {"Method", HighlightingKind::Method},
1172 {"StaticMethod", HighlightingKind::StaticMethod},
1173 {"Field", HighlightingKind::Field},
1174 {"StaticField", HighlightingKind::StaticField},
1175 {"Class", HighlightingKind::Class},
1176 {"Interface", HighlightingKind::Interface},
1177 {"Enum", HighlightingKind::Enum},
1178 {"EnumConstant", HighlightingKind::EnumConstant},
1179 {"Typedef", HighlightingKind::Typedef},
1180 {"Type", HighlightingKind::Type},
1181 {"Unknown", HighlightingKind::Unknown},
1182 {"Namespace", HighlightingKind::Namespace},
1183 {"TemplateParameter", HighlightingKind::TemplateParameter},
1184 {"Concept", HighlightingKind::Concept},
1185 {"Primitive", HighlightingKind::Primitive},
1186 {"Macro", HighlightingKind::Macro},
1187 {"Modifier", HighlightingKind::Modifier},
1188 {"Operator", HighlightingKind::Operator},
1189 {"Bracket", HighlightingKind::Bracket},
1190 {"InactiveCode", HighlightingKind::InactiveCode},
1191 };
1192
1193 auto It = Lookup.find(Name);
1194 return It != Lookup.end() ? std::make_optional(It->getValue()) : std::nullopt;
1195}
1196llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingModifier K) {
1197 switch (K) {
1199 return OS << "decl"; // abbreviation for common case
1201 return OS << "def"; // abbrevation for common case
1203 return OS << "constrDestr";
1204 default:
1205 return OS << toSemanticTokenModifier(K);
1206 }
1207}
1208std::optional<HighlightingModifier>
1209highlightingModifierFromString(llvm::StringRef Name) {
1210 static llvm::StringMap<HighlightingModifier> Lookup = {
1211 {"Declaration", HighlightingModifier::Declaration},
1212 {"Definition", HighlightingModifier::Definition},
1213 {"Deprecated", HighlightingModifier::Deprecated},
1214 {"Deduced", HighlightingModifier::Deduced},
1215 {"Readonly", HighlightingModifier::Readonly},
1216 {"Static", HighlightingModifier::Static},
1217 {"Abstract", HighlightingModifier::Abstract},
1218 {"Virtual", HighlightingModifier::Virtual},
1219 {"DependentName", HighlightingModifier::DependentName},
1220 {"DefaultLibrary", HighlightingModifier::DefaultLibrary},
1221 {"UsedAsMutableReference", HighlightingModifier::UsedAsMutableReference},
1222 {"UsedAsMutablePointer", HighlightingModifier::UsedAsMutablePointer},
1223 {"ConstructorOrDestructor",
1225 {"UserDefined", HighlightingModifier::UserDefined},
1226 {"FunctionScope", HighlightingModifier::FunctionScope},
1227 {"ClassScope", HighlightingModifier::ClassScope},
1228 {"FileScope", HighlightingModifier::FileScope},
1229 {"GlobalScope", HighlightingModifier::GlobalScope},
1230 };
1231
1232 auto It = Lookup.find(Name);
1233 return It != Lookup.end() ? std::make_optional(It->getValue()) : std::nullopt;
1234}
1235
1237 return std::tie(L.R, L.Kind, L.Modifiers) ==
1238 std::tie(R.R, R.Kind, R.Modifiers);
1239}
1241 return std::tie(L.R, L.Kind, L.Modifiers) <
1242 std::tie(R.R, R.Kind, R.Modifiers);
1243}
1244
1245std::vector<SemanticToken>
1246toSemanticTokens(llvm::ArrayRef<HighlightingToken> Tokens,
1247 llvm::StringRef Code) {
1248 assert(llvm::is_sorted(Tokens));
1249 std::vector<SemanticToken> Result;
1250 // In case we split a HighlightingToken into multiple tokens (e.g. because it
1251 // was spanning multiple lines), this tracks the last one. This prevents
1252 // having a copy all the time.
1253 HighlightingToken Scratch;
1254 const HighlightingToken *Last = nullptr;
1255 for (const HighlightingToken &Tok : Tokens) {
1256 Result.emplace_back();
1257 SemanticToken *Out = &Result.back();
1258 // deltaStart/deltaLine are relative if possible.
1259 if (Last) {
1260 assert(Tok.R.start.line >= Last->R.end.line);
1261 Out->deltaLine = Tok.R.start.line - Last->R.end.line;
1262 if (Out->deltaLine == 0) {
1263 assert(Tok.R.start.character >= Last->R.start.character);
1264 Out->deltaStart = Tok.R.start.character - Last->R.start.character;
1265 } else {
1266 Out->deltaStart = Tok.R.start.character;
1267 }
1268 } else {
1269 Out->deltaLine = Tok.R.start.line;
1270 Out->deltaStart = Tok.R.start.character;
1271 }
1272 Out->tokenType = static_cast<unsigned>(Tok.Kind);
1273 Out->tokenModifiers = Tok.Modifiers;
1274 Last = &Tok;
1275
1276 if (Tok.R.end.line == Tok.R.start.line) {
1277 Out->length = Tok.R.end.character - Tok.R.start.character;
1278 } else {
1279 // If the token spans a line break, split it into multiple pieces for each
1280 // line.
1281 // This is slow, but multiline tokens are rare.
1282 // FIXME: There's a client capability for supporting multiline tokens,
1283 // respect that.
1284 auto TokStartOffset = llvm::cantFail(positionToOffset(Code, Tok.R.start));
1285 // Note that the loop doesn't cover the last line, which has a special
1286 // length.
1287 for (int I = Tok.R.start.line; I < Tok.R.end.line; ++I) {
1288 auto LineEnd = Code.find('\n', TokStartOffset);
1289 assert(LineEnd != Code.npos);
1290 Out->length = LineEnd - TokStartOffset;
1291 // Token continues on next line, right after the line break.
1292 TokStartOffset = LineEnd + 1;
1293 Result.emplace_back();
1294 Out = &Result.back();
1295 *Out = Result[Result.size() - 2];
1296 // New token starts at the first column of the next line.
1297 Out->deltaLine = 1;
1298 Out->deltaStart = 0;
1299 }
1300 // This is the token on last line.
1301 Out->length = Tok.R.end.character;
1302 // Update the start location for last token, as that's used in the
1303 // relative delta calculation for following tokens.
1304 Scratch = *Last;
1305 Scratch.R.start.line = Tok.R.end.line;
1306 Scratch.R.start.character = 0;
1307 Last = &Scratch;
1308 }
1309 }
1310 return Result;
1311}
1313 switch (Kind) {
1317 return "variable";
1319 return "parameter";
1321 return "function";
1323 return "method";
1325 // FIXME: better method with static modifier?
1326 return "function";
1328 return "property";
1330 return "class";
1332 return "interface";
1334 return "enum";
1336 return "enumMember";
1339 return "type";
1341 return "unknown"; // nonstandard
1343 return "namespace";
1345 return "typeParameter";
1347 return "concept"; // nonstandard
1349 return "type";
1351 return "macro";
1353 return "modifier";
1355 return "operator";
1357 return "bracket";
1359 return "label";
1361 return "comment";
1362 }
1363 llvm_unreachable("unhandled HighlightingKind");
1364}
1365
1367 switch (Modifier) {
1369 return "declaration";
1371 return "definition";
1373 return "deprecated";
1375 return "readonly";
1377 return "static";
1379 return "deduced"; // nonstandard
1381 return "abstract";
1383 return "virtual";
1385 return "dependentName"; // nonstandard
1387 return "defaultLibrary";
1389 return "usedAsMutableReference"; // nonstandard
1391 return "usedAsMutablePointer"; // nonstandard
1393 return "constructorOrDestructor"; // nonstandard
1395 return "userDefined"; // nonstandard
1397 return "functionScope"; // nonstandard
1399 return "classScope"; // nonstandard
1401 return "fileScope"; // nonstandard
1403 return "globalScope"; // nonstandard
1404 }
1405 llvm_unreachable("unhandled HighlightingModifier");
1406}
1407
1408std::vector<SemanticTokensEdit> diffTokens(llvm::ArrayRef<SemanticToken> Old,
1409 llvm::ArrayRef<SemanticToken> New) {
1410 // For now, just replace everything from the first-last modification.
1411 // FIXME: use a real diff instead, this is bad with include-insertion.
1412
1413 unsigned Offset = 0;
1414 while (!Old.empty() && !New.empty() && Old.front() == New.front()) {
1415 ++Offset;
1416 Old = Old.drop_front();
1417 New = New.drop_front();
1418 }
1419 while (!Old.empty() && !New.empty() && Old.back() == New.back()) {
1420 Old = Old.drop_back();
1421 New = New.drop_back();
1422 }
1423
1424 if (Old.empty() && New.empty())
1425 return {};
1427 Edit.startToken = Offset;
1428 Edit.deleteTokens = Old.size();
1429 Edit.tokens = New;
1430 return {std::move(Edit)};
1431}
1432
1433std::vector<Range> getInactiveRegions(ParsedAST &AST) {
1434 std::vector<Range> SkippedRanges(std::move(AST.getMacros().SkippedRanges));
1435 const auto &SM = AST.getSourceManager();
1436 StringRef MainCode = SM.getBufferOrFake(SM.getMainFileID()).getBuffer();
1437 std::vector<Range> InactiveRegions;
1438 for (const Range &Skipped : SkippedRanges) {
1439 Range Inactive = Skipped;
1440 // Sometimes, SkippedRanges contains a range ending at position 0
1441 // of a line. Clients that apply whole-line styles will treat that
1442 // line as inactive which is not desirable, so adjust the ending
1443 // position to be the end of the previous line.
1444 if (Inactive.end.character == 0 && Inactive.end.line > 0) {
1445 --Inactive.end.line;
1446 }
1447 // Exclude the directive lines themselves from the range.
1448 if (Inactive.end.line >= Inactive.start.line + 2) {
1449 ++Inactive.start.line;
1450 --Inactive.end.line;
1451 } else {
1452 // range would be empty, e.g. #endif on next line after #ifdef
1453 continue;
1454 }
1455 // Since we've adjusted the ending line, we need to recompute the
1456 // column to reflect the end of that line.
1457 if (auto EndOfLine = endOfLine(MainCode, Inactive.end.line)) {
1458 Inactive.end = *EndOfLine;
1459 } else {
1460 elog("Failed to determine end of line: {0}", EndOfLine.takeError());
1461 continue;
1462 }
1463 InactiveRegions.push_back(Inactive);
1464 }
1465 return InactiveRegions;
1466}
1467
1468} // namespace clangd
1469} // 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:1126
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:1731
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:159
int character
Character offset on a line in a document (zero-based).
Definition Protocol.h:164
Position start
The range's start position.
Definition Protocol.h:188
Position end
The range's end position.
Definition Protocol.h:191
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:1885
unsigned length
the length of the token. A token cannot be multiline
Definition Protocol.h:1892
unsigned deltaStart
token start character, relative to the previous token (relative to 0 or the previous token's start if...
Definition Protocol.h:1890
unsigned deltaLine
token line number, relative to the previous token
Definition Protocol.h:1887
unsigned tokenType
will be looked up in SemanticTokensLegend.tokenTypes
Definition Protocol.h:1894
unsigned tokenModifiers
each set bit will be looked up in SemanticTokensLegend.tokenModifiers
Definition Protocol.h:1896
Describes a replacement of a contiguous range of semanticTokens.
Definition Protocol.h:1933