17#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclObjC.h"
21#include "clang/AST/DeclTemplate.h"
22#include "clang/AST/DeclarationName.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/AST/RecursiveASTVisitor.h"
25#include "clang/AST/Type.h"
26#include "clang/AST/TypeLoc.h"
27#include "clang/Basic/LangOptions.h"
28#include "clang/Basic/SourceLocation.h"
29#include "clang/Basic/SourceManager.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"
43llvm::Expected<Position> endOfLine(llvm::StringRef
Code,
int Line) {
46 return StartOfLine.takeError();
47 StringRef LineText =
Code.drop_front(*StartOfLine).take_until([](
char C) {
55bool canHighlightName(DeclarationName
Name) {
56 switch (
Name.getNameKind()) {
57 case DeclarationName::Identifier: {
58 auto *II =
Name.getAsIdentifierInfo();
59 return II && !II->getName().empty();
61 case DeclarationName::CXXConstructorName:
62 case DeclarationName::CXXDestructorName:
64 case DeclarationName::ObjCZeroArgSelector:
65 case DeclarationName::ObjCOneArgSelector:
66 case DeclarationName::ObjCMultiArgSelector:
70 case DeclarationName::CXXConversionFunctionName:
71 case DeclarationName::CXXOperatorName:
72 case DeclarationName::CXXDeductionGuideName:
73 case DeclarationName::CXXLiteralOperatorName:
74 case DeclarationName::CXXUsingDirective:
77 llvm_unreachable(
"invalid name kind");
80bool isUniqueDefinition(
const NamedDecl *
Decl) {
81 if (
auto *Func = dyn_cast<FunctionDecl>(
Decl))
82 return Func->isThisDeclarationADefinition();
83 if (
auto *Klass = dyn_cast<CXXRecordDecl>(
Decl))
84 return Klass->isThisDeclarationADefinition();
85 if (
auto *Iface = dyn_cast<ObjCInterfaceDecl>(
Decl))
86 return Iface->isThisDeclarationADefinition();
87 if (
auto *Proto = dyn_cast<ObjCProtocolDecl>(
Decl))
88 return Proto->isThisDeclarationADefinition();
89 if (
auto *Var = dyn_cast<VarDecl>(
Decl))
90 return Var->isThisDeclarationADefinition();
91 return isa<TemplateTypeParmDecl>(
Decl) ||
92 isa<NonTypeTemplateParmDecl>(
Decl) ||
93 isa<TemplateTemplateParmDecl>(
Decl) || isa<ObjCCategoryDecl>(
Decl) ||
94 isa<ObjCImplDecl>(
Decl);
97std::optional<HighlightingKind> kindForType(
const Type *TP,
98 const HeuristicResolver *Resolver);
99std::optional<HighlightingKind> kindForDecl(
const NamedDecl *D,
100 const HeuristicResolver *Resolver) {
101 if (
auto *USD = dyn_cast<UsingShadowDecl>(D)) {
102 if (
auto *Target = USD->getTargetDecl())
105 if (
auto *TD = dyn_cast<TemplateDecl>(D)) {
106 if (
auto *Templated = TD->getTemplatedDecl())
109 if (
auto *TD = dyn_cast<TypedefNameDecl>(D)) {
112 kindForType(TD->getUnderlyingType().getTypePtrOrNull(), Resolver))
120 if (
auto *RD = llvm::dyn_cast<RecordDecl>(D)) {
126 if (isa<ClassTemplateDecl, RecordDecl, CXXConstructorDecl, ObjCInterfaceDecl,
127 ObjCImplementationDecl>(D))
129 if (isa<ObjCProtocolDecl>(D))
131 if (isa<ObjCCategoryDecl, ObjCCategoryImplDecl>(D))
133 if (
auto *MD = dyn_cast<CXXMethodDecl>(D))
136 if (
auto *OMD = dyn_cast<ObjCMethodDecl>(D))
139 if (isa<FieldDecl, IndirectFieldDecl, ObjCPropertyDecl>(D))
141 if (isa<EnumDecl>(D))
143 if (isa<EnumConstantDecl>(D))
145 if (isa<ParmVarDecl>(D))
147 if (
auto *VD = dyn_cast<VarDecl>(D)) {
148 if (isa<ImplicitParamDecl>(VD))
150 return VD->isStaticDataMember()
155 if (
const auto *BD = dyn_cast<BindingDecl>(D))
156 return BD->getDeclContext()->isFunctionOrMethod()
159 if (isa<FunctionDecl>(D))
161 if (isa<NamespaceDecl>(D) || isa<NamespaceAliasDecl>(D) ||
162 isa<UsingDirectiveDecl>(D))
164 if (isa<TemplateTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
165 isa<NonTypeTemplateParmDecl>(D))
167 if (isa<ConceptDecl>(D))
169 if (isa<LabelDecl>(D))
171 if (
const auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(D)) {
173 if (!Targets.empty() && Targets[0] != UUVD) {
174 return kindForDecl(Targets[0], Resolver);
180std::optional<HighlightingKind> kindForType(
const Type *TP,
181 const HeuristicResolver *Resolver) {
184 if (TP->isBuiltinType())
186 if (
auto *TD = dyn_cast<TemplateTypeParmType>(TP))
187 return kindForDecl(TD->getDecl(), Resolver);
188 if (isa<ObjCObjectPointerType>(TP))
190 if (
auto *TD = TP->getAsTagDecl())
191 return kindForDecl(TD, Resolver);
196bool isConst(QualType T) {
199 T = T.getNonReferenceType();
200 if (T.isConstQualified())
202 if (
const auto *AT = T->getAsArrayTypeUnsafe())
203 return isConst(AT->getElementType());
204 if (isConst(T->getPointeeType()))
212bool isConst(
const Decl *D) {
213 if (llvm::isa<EnumConstantDecl>(D) || llvm::isa<NonTypeTemplateParmDecl>(D))
215 if (llvm::isa<FieldDecl>(D) || llvm::isa<VarDecl>(D) ||
216 llvm::isa<MSPropertyDecl>(D) || llvm::isa<BindingDecl>(D)) {
217 if (isConst(llvm::cast<ValueDecl>(D)->getType()))
220 if (
const auto *OCPD = llvm::dyn_cast<ObjCPropertyDecl>(D)) {
221 if (OCPD->isReadOnly())
224 if (
const auto *MPD = llvm::dyn_cast<MSPropertyDecl>(D)) {
225 if (!MPD->hasSetter())
228 if (
const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) {
254bool isStatic(
const Decl *D) {
255 if (
const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D))
256 return CMD->isStatic();
257 if (
const VarDecl *VD = llvm::dyn_cast<VarDecl>(D))
258 return VD->isStaticDataMember() || VD->isStaticLocal();
259 if (
const auto *OPD = llvm::dyn_cast<ObjCPropertyDecl>(D))
260 return OPD->isClassProperty();
261 if (
const auto *OMD = llvm::dyn_cast<ObjCMethodDecl>(D))
262 return OMD->isClassMethod();
266bool isAbstract(
const Decl *D) {
267 if (
const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D))
268 return CMD->isPureVirtual();
269 if (
const auto *CRD = llvm::dyn_cast<CXXRecordDecl>(D))
270 return CRD->hasDefinition() && CRD->isAbstract();
274bool isVirtual(
const Decl *D) {
275 if (
const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D))
276 return CMD->isVirtual();
280bool isDependent(
const Decl *D) {
281 if (isa<UnresolvedUsingValueDecl>(D))
289bool isDefaultLibrary(
const Decl *D) {
290 SourceLocation
Loc = D->getLocation();
293 return D->getASTContext().getSourceManager().isInSystemHeader(
Loc);
296bool isDefaultLibrary(
const Type *T) {
302 if (
auto *TD = dyn_cast<TemplateTypeParmType>(
Underlying))
303 return isDefaultLibrary(TD->getDecl());
305 return isDefaultLibrary(TD);
312SourceLocation getHighlightableSpellingToken(SourceLocation L,
313 const SourceManager &SM) {
315 return SM.isWrittenInMainFile(L) ? L : SourceLocation{};
317 if (!SM.isMacroArgExpansion(L))
320 return getHighlightableSpellingToken(SM.getImmediateSpellingLoc(L), SM);
323unsigned evaluateHighlightPriority(
const HighlightingToken &Tok) {
324 enum HighlightPriority { Dependent = 0, Resolved = 1 };
348std::optional<HighlightingToken> resolveConflict(
const HighlightingToken &A,
349 const HighlightingToken &B) {
350 unsigned Priority1 = evaluateHighlightPriority(A);
351 unsigned Priority2 = evaluateHighlightPriority(B);
352 if (Priority1 == Priority2 &&
A.Kind !=
B.Kind)
354 auto Result = Priority1 > Priority2 ?
A :
B;
355 Result.Modifiers =
A.Modifiers |
B.Modifiers;
358std::optional<HighlightingToken>
359resolveConflict(ArrayRef<HighlightingToken> Tokens) {
360 if (Tokens.size() == 1)
363 assert(Tokens.size() >= 2);
364 std::optional<HighlightingToken> Winner =
365 resolveConflict(Tokens[0], Tokens[1]);
366 for (
size_t I = 2; Winner && I < Tokens.size(); ++I)
367 Winner = resolveConflict(*Winner, Tokens[I]);
373class HighlightingFilter {
375 HighlightingFilter() {
376 for (
auto &Active : ActiveKindLookup)
379 ActiveModifiersMask = ~0;
383 ActiveKindLookup[
static_cast<size_t>(
Kind)] =
false;
387 ActiveModifiersMask &= ~(1 <<
static_cast<uint32_t
>(
Modifier));
391 return ActiveKindLookup[
static_cast<size_t>(
Kind)];
394 uint32_t maskModifiers(uint32_t Modifiers)
const {
395 return Modifiers & ActiveModifiersMask;
398 static HighlightingFilter fromCurrentConfig() {
400 HighlightingFilter Filter;
401 for (
const auto &
Kind :
C.SemanticTokens.DisabledKinds)
403 Filter.disableKind(*
K);
404 for (
const auto &
Modifier :
C.SemanticTokens.DisabledModifiers)
406 Filter.disableModifier(*
M);
413 uint32_t ActiveModifiersMask;
417class HighlightingsBuilder {
419 HighlightingsBuilder(
const ParsedAST &
AST,
const HighlightingFilter &Filter)
420 : TB(
AST.getTokens()), SourceMgr(
AST.getSourceManager()),
421 LangOpts(
AST.getLangOpts()), Filter(Filter),
422 Resolver(
AST.getHeuristicResolver()) {}
425 auto Range = getRangeForSourceLocation(
Loc);
427 return InvalidHighlightingToken;
429 return addToken(*Range,
Kind);
434 void addAngleBracketTokens(SourceLocation LLoc, SourceLocation RLoc) {
435 if (!LLoc.isValid() || !RLoc.isValid())
438 auto LRange = getRangeForSourceLocation(LLoc);
444 RLoc = SourceMgr.getFileLoc(RLoc);
446 RLoc = getHighlightableSpellingToken(RLoc, SourceMgr);
450 const auto *RTok = TB.spelledTokenAt(RLoc);
455 if (!RTok || RTok->kind() == tok::greatergreater) {
464 if (RTok->kind() == tok::greater) {
465 if (
auto RRange = getRangeForSourceLocation(RLoc)) {
474 if (!Filter.isHighlightKindActive(
Kind))
475 return InvalidHighlightingToken;
477 HighlightingToken HT;
480 Tokens.push_back(std::move(HT));
481 return Tokens.back();
485 if (
auto Range = getRangeForSourceLocation(
Loc))
489 std::vector<HighlightingToken> collect(ParsedAST &
AST) && {
493 auto Last = std::unique(Tokens.begin(), Tokens.end());
494 Tokens.erase(Last, Tokens.end());
499 std::vector<HighlightingToken> NonConflicting;
500 NonConflicting.reserve(Tokens.size());
501 for (ArrayRef<HighlightingToken> TokRef = Tokens; !TokRef.empty();) {
502 ArrayRef<HighlightingToken> Conflicting =
503 TokRef.take_while([&](
const HighlightingToken &T) {
506 return T.R == TokRef.front().R;
508 if (
auto Resolved = resolveConflict(Conflicting)) {
510 auto Modifiers = ExtraModifiers.find(Resolved->R);
511 if (Modifiers != ExtraModifiers.end()) {
513 Resolved->addModifier(Mod);
517 Resolved->Modifiers = Filter.maskModifiers(Resolved->Modifiers);
518 NonConflicting.push_back(*Resolved);
522 TokRef = TokRef.drop_front(Conflicting.size());
526 return NonConflicting;
528 const auto &SM =
AST.getSourceManager();
529 StringRef MainCode = SM.getBufferOrFake(SM.getMainFileID()).getBuffer();
532 std::vector<HighlightingToken> WithInactiveLines;
534 llvm::sort(SortedInactiveRegions);
535 auto It = NonConflicting.begin();
536 for (
const Range &R : SortedInactiveRegions) {
539 assert(R.start.line <= R.end.line);
540 for (
int Line = R.start.line; Line <= R.end.line; ++Line) {
542 for (; It != NonConflicting.end() && It->R.start.line < Line; ++It)
543 WithInactiveLines.push_back(std::move(*It));
545 auto EndOfLine = endOfLine(MainCode, Line);
547 HighlightingToken HT;
548 WithInactiveLines.emplace_back();
550 WithInactiveLines.back().R.start.line =
Line;
551 WithInactiveLines.back().R.end = *EndOfLine;
553 elog(
"Failed to determine end of line: {0}", EndOfLine.takeError());
561 while (It != NonConflicting.end() && It->R.start.line == Line)
566 for (; It != NonConflicting.end(); ++It)
567 WithInactiveLines.push_back(std::move(*It));
568 return WithInactiveLines;
571 const HeuristicResolver *getResolver()
const {
return Resolver; }
574 std::optional<Range> getRangeForSourceLocation(SourceLocation
Loc) {
575 Loc = getHighlightableSpellingToken(
Loc, SourceMgr);
580 const auto *Tok = TB.spelledTokenAt(
Loc);
584 Tok->range(SourceMgr).toCharRange(SourceMgr));
587 const syntax::TokenBuffer &TB;
588 const SourceManager &SourceMgr;
589 const LangOptions &LangOpts;
590 HighlightingFilter Filter;
591 std::vector<HighlightingToken> Tokens;
592 std::map<Range, llvm::SmallVector<HighlightingModifier, 1>> ExtraModifiers;
593 const HeuristicResolver *Resolver;
595 HighlightingToken InvalidHighlightingToken;
598std::optional<HighlightingModifier> scopeModifier(
const NamedDecl *D) {
599 const DeclContext *DC = D->getDeclContext();
601 if (
auto *R = dyn_cast_or_null<RecordDecl>(D))
602 if (R->isInjectedClassName())
603 DC = DC->getParent();
605 if (llvm::isa<FieldDecl>(D))
606 if (
const auto *RD = llvm::dyn_cast<RecordDecl>(DC))
610 for (; !DC->isFileContext(); DC = DC->getParent()) {
611 if (DC->isFunctionOrMethod())
618 if (DC->isTranslationUnit() && D->isTemplateParameter())
621 if (llvm::to_underlying(D->getLinkageInternal()) <
622 llvm::to_underlying(Linkage::External))
627std::optional<HighlightingModifier> scopeModifier(
const Type *T) {
630 if (T->isBuiltinType())
632 if (
auto *TD = dyn_cast<TemplateTypeParmType>(T))
633 return scopeModifier(TD->getDecl());
634 if (
auto *TD = T->getAsTagDecl())
635 return scopeModifier(TD);
641class CollectExtraHighlightings
646 CollectExtraHighlightings(HighlightingsBuilder &H) : H(H) {}
648 bool VisitCXXConstructExpr(CXXConstructExpr *
E) {
649 highlightMutableReferenceArguments(
E->getConstructor(),
650 {E->getArgs(), E->getNumArgs()});
655 bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
656 if (Init->isMemberInitializer())
657 if (
auto *Member = Init->getMember())
658 highlightMutableReferenceArgument(Member->getType(), Init->getInit());
659 return Base::TraverseConstructorInitializer(Init);
662 bool TraverseTypeConstraint(
const TypeConstraint *
C) {
663 if (
auto *
Args =
C->getTemplateArgsAsWritten())
664 H.addAngleBracketTokens(
Args->getLAngleLoc(),
Args->getRAngleLoc());
665 return Base::TraverseTypeConstraint(
C);
668 bool VisitPredefinedExpr(PredefinedExpr *
E) {
676 bool VisitConceptSpecializationExpr(ConceptSpecializationExpr *
E) {
677 if (
auto *
Args =
E->getTemplateArgsAsWritten())
678 H.addAngleBracketTokens(
Args->getLAngleLoc(),
Args->getRAngleLoc());
682 bool VisitTemplateDecl(TemplateDecl *D) {
683 if (
auto *TPL = D->getTemplateParameters())
684 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
688 bool VisitTagDecl(TagDecl *D) {
689 for (
unsigned i = 0; i < D->getNumTemplateParameterLists(); ++i) {
690 if (
auto *TPL = D->getTemplateParameterList(i))
691 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
696 bool VisitClassTemplatePartialSpecializationDecl(
697 ClassTemplatePartialSpecializationDecl *D) {
698 if (
auto *TPL = D->getTemplateParameters())
699 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
700 if (
auto *
Args = D->getTemplateArgsAsWritten())
701 H.addAngleBracketTokens(
Args->getLAngleLoc(),
Args->getRAngleLoc());
705 bool VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D) {
706 if (
auto *
Args = D->getTemplateArgsInfo())
707 H.addAngleBracketTokens(
Args->getLAngleLoc(),
Args->getRAngleLoc());
711 bool VisitVarTemplatePartialSpecializationDecl(
712 VarTemplatePartialSpecializationDecl *D) {
713 if (
auto *TPL = D->getTemplateParameters())
714 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
715 if (
auto *
Args = D->getTemplateArgsAsWritten())
716 H.addAngleBracketTokens(
Args->getLAngleLoc(),
Args->getRAngleLoc());
720 bool VisitDeclRefExpr(DeclRefExpr *
E) {
721 H.addAngleBracketTokens(
E->getLAngleLoc(),
E->getRAngleLoc());
724 bool VisitMemberExpr(MemberExpr *
E) {
725 H.addAngleBracketTokens(
E->getLAngleLoc(),
E->getRAngleLoc());
729 bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc L) {
730 H.addAngleBracketTokens(L.getLAngleLoc(), L.getRAngleLoc());
734 bool VisitFunctionDecl(FunctionDecl *D) {
735 if (D->isOverloadedOperator()) {
736 const auto AddOpDeclToken = [&](SourceLocation
Loc) {
739 if (D->isThisDeclarationADefinition())
742 const auto Range = D->getNameInfo().getCXXOperatorNameRange();
743 AddOpDeclToken(
Range.getBegin());
744 const auto Kind = D->getOverloadedOperator();
745 if (
Kind == OO_Call ||
Kind == OO_Subscript)
746 AddOpDeclToken(
Range.getEnd());
748 if (
auto *
Args = D->getTemplateSpecializationArgsAsWritten())
749 H.addAngleBracketTokens(
Args->getLAngleLoc(),
Args->getRAngleLoc());
753 bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *
E) {
754 const auto AddOpToken = [&](SourceLocation
Loc) {
758 AddOpToken(
E->getOperatorLoc());
759 const auto Kind =
E->getOperator();
760 if (
Kind == OO_Call ||
Kind == OO_Subscript) {
761 if (
auto *Callee =
E->getCallee())
762 AddOpToken(Callee->getBeginLoc());
767 bool VisitUnaryOperator(UnaryOperator *Op) {
769 if (Op->getSubExpr()->isTypeDependent())
774 bool VisitBinaryOperator(BinaryOperator *Op) {
776 if (Op->getLHS()->isTypeDependent() || Op->getRHS()->isTypeDependent())
781 bool VisitConditionalOperator(ConditionalOperator *Op) {
787 bool VisitCXXNewExpr(CXXNewExpr *
E) {
789 if (isa_and_present<CXXMethodDecl>(
E->getOperatorNew()))
794 bool VisitCXXDeleteExpr(CXXDeleteExpr *
E) {
796 if (isa_and_present<CXXMethodDecl>(
E->getOperatorDelete()))
801 bool VisitCXXNamedCastExpr(CXXNamedCastExpr *
E) {
802 const auto &
B =
E->getAngleBrackets();
803 H.addAngleBracketTokens(
B.getBegin(),
B.getEnd());
807 bool VisitCallExpr(CallExpr *
E) {
810 if (isa<UserDefinedLiteral>(
E))
815 llvm::ArrayRef<const Expr *>
Args = {
E->getArgs(),
E->getNumArgs()};
816 if (
auto *CallOp = dyn_cast<CXXOperatorCallExpr>(
E)) {
817 switch (CallOp->getOperator()) {
827 highlightMutableReferenceArguments(
828 dyn_cast_or_null<FunctionDecl>(
E->getCalleeDecl()),
Args);
833 void highlightMutableReferenceArgument(QualType T,
const Expr *Arg) {
840 bool IsRef = T->isLValueReferenceType();
841 bool IsPtr = T->isPointerType();
842 if ((!IsRef && !IsPtr) || T->getPointeeType().isConstQualified() ||
843 T->isDependentType()) {
847 std::optional<SourceLocation>
Location;
852 if (
auto *IC = dyn_cast<ImplicitCastExpr>(Arg))
853 Arg = IC->getSubExprAsWritten();
854 if (
auto *UO = dyn_cast<UnaryOperator>(Arg)) {
855 if (UO->getOpcode() == UO_AddrOf)
856 Arg = UO->getSubExpr();
858 if (
auto *DR = dyn_cast<DeclRefExpr>(Arg))
860 else if (
auto *
M = dyn_cast<MemberExpr>(Arg))
870 highlightMutableReferenceArguments(
const FunctionDecl *FD,
871 llvm::ArrayRef<const Expr *const>
Args) {
875 if (
auto *ProtoType = FD->getType()->getAs<FunctionProtoType>()) {
880 I < std::min(
size_t(ProtoType->getNumParams()),
Args.size()); ++I) {
881 highlightMutableReferenceArgument(ProtoType->getParamType(I),
Args[I]);
886 bool VisitDecltypeTypeLoc(DecltypeTypeLoc L) {
887 if (
auto K = kindForType(L.getTypePtr(), H.getResolver())) {
888 auto &Tok = H.addToken(L.getBeginLoc(), *
K)
890 if (
auto Mod = scopeModifier(L.getTypePtr()))
891 Tok.addModifier(*Mod);
892 if (isDefaultLibrary(L.getTypePtr()))
898 bool VisitCXXDestructorDecl(CXXDestructorDecl *D) {
899 if (
auto *TI = D->getNameInfo().getNamedTypeInfo()) {
900 SourceLocation
Loc = TI->getTypeLoc().getBeginLoc();
903 if (D->isThisDeclarationADefinition())
909 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *
CE) {
912 if (
auto *D =
CE->getMethodDecl()) {
913 if (isa<CXXDestructorDecl>(D)) {
914 if (
auto *ME = dyn_cast<MemberExpr>(
CE->getCallee())) {
915 if (
auto *TI = ME->getMemberNameInfo().getNamedTypeInfo()) {
916 H.addExtraModifier(TI->getTypeLoc().getBeginLoc(),
920 }
else if (D->isOverloadedOperator()) {
921 if (
auto *ME = dyn_cast<MemberExpr>(
CE->getCallee()))
923 ME->getMemberNameInfo().getCXXOperatorNameRange().getBegin(),
931 bool VisitDeclaratorDecl(DeclaratorDecl *D) {
932 for (
unsigned i = 0; i < D->getNumTemplateParameterLists(); ++i) {
933 if (
auto *TPL = D->getTemplateParameterList(i))
934 H.addAngleBracketTokens(TPL->getLAngleLoc(), TPL->getRAngleLoc());
936 auto *AT = D->getType()->getContainedAutoType();
940 kindForType(AT->getDeducedType().getTypePtrOrNull(), H.getResolver());
943 auto *TSI = D->getTypeSourceInfo();
946 SourceLocation StartLoc =
947 TSI->getTypeLoc().getContainedAutoTypeLoc().getNameLoc();
952 if (StartLoc == D->getLocation())
957 const Type *
Deduced = AT->getDeducedType().getTypePtrOrNull();
958 if (
auto Mod = scopeModifier(
Deduced))
959 Tok.addModifier(*Mod);
967 void highlightObjCSelector(
const ArrayRef<SourceLocation> &Locs,
bool Decl,
971 for (SourceLocation Part : Locs) {
985 bool VisitObjCMethodDecl(ObjCMethodDecl *OMD) {
986 llvm::SmallVector<SourceLocation> Locs;
987 OMD->getSelectorLocs(Locs);
988 highlightObjCSelector(Locs,
true,
989 OMD->isThisDeclarationADefinition(),
990 OMD->isClassMethod(), isDefaultLibrary(OMD));
994 bool VisitObjCMessageExpr(ObjCMessageExpr *OME) {
995 llvm::SmallVector<SourceLocation> Locs;
996 OME->getSelectorLocs(Locs);
998 if (ObjCMethodDecl *OMD = OME->getMethodDecl())
1000 highlightObjCSelector(Locs,
false,
false,
1009 void highlightObjCImplicitPropertyRef(
const ObjCMethodDecl *OMD,
1010 SourceLocation
Loc) {
1013 if (OMD->isClassMethod())
1015 if (isDefaultLibrary(OMD))
1019 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *OPRE) {
1023 if (!OPRE->isImplicitProperty())
1029 if (OPRE->isMessagingGetter()) {
1030 highlightObjCImplicitPropertyRef(OPRE->getImplicitPropertyGetter(),
1031 OPRE->getLocation());
1034 if (OPRE->isMessagingSetter()) {
1035 highlightObjCImplicitPropertyRef(OPRE->getImplicitPropertySetter(),
1036 OPRE->getLocation());
1041 bool VisitOverloadExpr(OverloadExpr *
E) {
1042 H.addAngleBracketTokens(
E->getLAngleLoc(),
E->getRAngleLoc());
1043 if (!
E->decls().empty())
1047 if (llvm::isa<UnresolvedMemberExpr>(
E))
1053 bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *
E) {
1057 H.addAngleBracketTokens(
E->getLAngleLoc(),
E->getRAngleLoc());
1061 bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *
E) {
1065 H.addAngleBracketTokens(
E->getLAngleLoc(),
E->getRAngleLoc());
1069 bool VisitAttr(Attr *A) {
1070 switch (
A->getKind()) {
1071 case attr::Override:
1081 bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
1088 bool VisitDependentTemplateSpecializationTypeLoc(
1089 DependentTemplateSpecializationTypeLoc L) {
1093 H.addAngleBracketTokens(L.getLAngleLoc(), L.getRAngleLoc());
1097 bool TraverseTemplateArgumentLoc(TemplateArgumentLoc L) {
1100 if (L.getArgument().getKind() != TemplateArgument::Template &&
1101 L.getArgument().getKind() != TemplateArgument::TemplateExpansion)
1102 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(L);
1104 TemplateName N = L.getArgument().getAsTemplateOrTemplatePattern();
1105 switch (N.getKind()) {
1106 case TemplateName::OverloadedTemplate:
1111 case TemplateName::DependentTemplate:
1112 case TemplateName::AssumedTemplate:
1116 case TemplateName::Template:
1117 case TemplateName::QualifiedTemplate:
1118 case TemplateName::SubstTemplateTemplateParm:
1119 case TemplateName::SubstTemplateTemplateParmPack:
1120 case TemplateName::UsingTemplate:
1124 return RecursiveASTVisitor::TraverseTemplateArgumentLoc(L);
1132 bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc Q) {
1133 if (NestedNameSpecifier *NNS = Q.getNestedNameSpecifier()) {
1134 if (NNS->getKind() == NestedNameSpecifier::Identifier)
1139 return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(Q);
1143 HighlightingsBuilder &H;
1147std::vector<HighlightingToken>
1149 auto &
C =
AST.getASTContext();
1150 HighlightingFilter Filter = HighlightingFilter::fromCurrentConfig();
1151 if (!IncludeInactiveRegionTokens)
1156 CollectExtraHighlightings(
Builder).TraverseAST(
C);
1162 if (!canHighlightName(Decl->getDeclName()))
1164 auto Kind = kindForDecl(Decl, AST.getHeuristicResolver());
1167 auto &Tok = Builder.addToken(R.NameLoc, *Kind);
1170 if (auto *TD = dyn_cast<TemplateDecl>(Decl)) {
1171 if (auto *Templated = TD->getTemplatedDecl())
1174 if (
auto Mod = scopeModifier(
Decl))
1175 Tok.addModifier(*Mod);
1180 if (isAbstract(
Decl))
1182 if (isVirtual(
Decl))
1184 if (isDependent(
Decl))
1186 if (isDefaultLibrary(
Decl))
1188 if (
Decl->isDeprecated())
1190 if (isa<CXXConstructorDecl>(
Decl))
1196 if (!isa<UnresolvedUsingValueDecl>(Decl))
1197 Tok.addModifier(HighlightingModifier::Declaration);
1198 if (isUniqueDefinition(Decl))
1199 Tok.addModifier(HighlightingModifier::Definition);
1203 AST.getHeuristicResolver());
1205 auto AddMacro = [&](
const MacroOccurrence &
M) {
1206 auto &T =
Builder.addToken(
M.toRange(
C.getSourceManager()),
1212 for (
const auto &SIDToRefs :
AST.getMacros().MacroRefs)
1213 for (
const auto &
M : SIDToRefs.second)
1215 for (
const auto &
M :
AST.getMacros().UnknownMacros)
1223 case HighlightingKind::Variable:
1224 return OS <<
"Variable";
1225 case HighlightingKind::LocalVariable:
1226 return OS <<
"LocalVariable";
1227 case HighlightingKind::Parameter:
1228 return OS <<
"Parameter";
1229 case HighlightingKind::Function:
1230 return OS <<
"Function";
1231 case HighlightingKind::Method:
1232 return OS <<
"Method";
1233 case HighlightingKind::StaticMethod:
1234 return OS <<
"StaticMethod";
1235 case HighlightingKind::Field:
1236 return OS <<
"Field";
1237 case HighlightingKind::StaticField:
1238 return OS <<
"StaticField";
1239 case HighlightingKind::Class:
1240 return OS <<
"Class";
1241 case HighlightingKind::Interface:
1242 return OS <<
"Interface";
1243 case HighlightingKind::Enum:
1244 return OS <<
"Enum";
1245 case HighlightingKind::EnumConstant:
1246 return OS <<
"EnumConstant";
1247 case HighlightingKind::Typedef:
1248 return OS <<
"Typedef";
1249 case HighlightingKind::Type:
1250 return OS <<
"Type";
1251 case HighlightingKind::Unknown:
1252 return OS <<
"Unknown";
1253 case HighlightingKind::Namespace:
1254 return OS <<
"Namespace";
1255 case HighlightingKind::TemplateParameter:
1256 return OS <<
"TemplateParameter";
1257 case HighlightingKind::Concept:
1258 return OS <<
"Concept";
1259 case HighlightingKind::Primitive:
1260 return OS <<
"Primitive";
1261 case HighlightingKind::Macro:
1262 return OS <<
"Macro";
1263 case HighlightingKind::Modifier:
1264 return OS <<
"Modifier";
1265 case HighlightingKind::Operator:
1266 return OS <<
"Operator";
1267 case HighlightingKind::Bracket:
1268 return OS <<
"Bracket";
1269 case HighlightingKind::Label:
1270 return OS <<
"Label";
1271 case HighlightingKind::InactiveCode:
1272 return OS <<
"InactiveCode";
1274 llvm_unreachable(
"invalid HighlightingKind");
1276std::optional<HighlightingKind>
1278 static llvm::StringMap<HighlightingKind> Lookup = {
1279 {
"Variable", HighlightingKind::Variable},
1280 {
"LocalVariable", HighlightingKind::LocalVariable},
1281 {
"Parameter", HighlightingKind::Parameter},
1282 {
"Function", HighlightingKind::Function},
1283 {
"Method", HighlightingKind::Method},
1284 {
"StaticMethod", HighlightingKind::StaticMethod},
1285 {
"Field", HighlightingKind::Field},
1286 {
"StaticField", HighlightingKind::StaticField},
1287 {
"Class", HighlightingKind::Class},
1288 {
"Interface", HighlightingKind::Interface},
1289 {
"Enum", HighlightingKind::Enum},
1290 {
"EnumConstant", HighlightingKind::EnumConstant},
1291 {
"Typedef", HighlightingKind::Typedef},
1292 {
"Type", HighlightingKind::Type},
1293 {
"Unknown", HighlightingKind::Unknown},
1294 {
"Namespace", HighlightingKind::Namespace},
1295 {
"TemplateParameter", HighlightingKind::TemplateParameter},
1296 {
"Concept", HighlightingKind::Concept},
1297 {
"Primitive", HighlightingKind::Primitive},
1298 {
"Macro", HighlightingKind::Macro},
1299 {
"Modifier", HighlightingKind::Modifier},
1300 {
"Operator", HighlightingKind::Operator},
1301 {
"Bracket", HighlightingKind::Bracket},
1302 {
"InactiveCode", HighlightingKind::InactiveCode},
1305 auto It = Lookup.find(
Name);
1306 return It != Lookup.end() ? std::make_optional(It->getValue()) : std::nullopt;
1310 case HighlightingModifier::Declaration:
1311 return OS <<
"decl";
1312 case HighlightingModifier::Definition:
1314 case HighlightingModifier::ConstructorOrDestructor:
1315 return OS <<
"constrDestr";
1320std::optional<HighlightingModifier>
1322 static llvm::StringMap<HighlightingModifier> Lookup = {
1323 {
"Declaration", HighlightingModifier::Declaration},
1324 {
"Definition", HighlightingModifier::Definition},
1325 {
"Deprecated", HighlightingModifier::Deprecated},
1326 {
"Deduced", HighlightingModifier::Deduced},
1327 {
"Readonly", HighlightingModifier::Readonly},
1328 {
"Static", HighlightingModifier::Static},
1329 {
"Abstract", HighlightingModifier::Abstract},
1330 {
"Virtual", HighlightingModifier::Virtual},
1331 {
"DependentName", HighlightingModifier::DependentName},
1332 {
"DefaultLibrary", HighlightingModifier::DefaultLibrary},
1333 {
"UsedAsMutableReference", HighlightingModifier::UsedAsMutableReference},
1334 {
"UsedAsMutablePointer", HighlightingModifier::UsedAsMutablePointer},
1335 {
"ConstructorOrDestructor",
1336 HighlightingModifier::ConstructorOrDestructor},
1337 {
"UserDefined", HighlightingModifier::UserDefined},
1338 {
"FunctionScope", HighlightingModifier::FunctionScope},
1339 {
"ClassScope", HighlightingModifier::ClassScope},
1340 {
"FileScope", HighlightingModifier::FileScope},
1341 {
"GlobalScope", HighlightingModifier::GlobalScope},
1344 auto It = Lookup.find(
Name);
1345 return It != Lookup.end() ? std::make_optional(It->getValue()) : std::nullopt;
1349 return std::tie(L.R, L.Kind, L.Modifiers) ==
1353 return std::tie(L.R, L.Kind, L.Modifiers) <
1357std::vector<SemanticToken>
1359 llvm::StringRef
Code) {
1360 assert(llvm::is_sorted(Tokens));
1361 std::vector<SemanticToken> Result;
1368 Result.emplace_back();
1372 assert(Tok.R.start.line >= Last->
R.
end.
line);
1373 Out->deltaLine = Tok.R.start.line - Last->
R.
end.
line;
1374 if (
Out->deltaLine == 0) {
1378 Out->deltaStart = Tok.R.start.character;
1381 Out->deltaLine = Tok.R.start.line;
1382 Out->deltaStart = Tok.R.start.character;
1384 Out->tokenType =
static_cast<unsigned>(Tok.Kind);
1385 Out->tokenModifiers = Tok.Modifiers;
1388 if (Tok.R.end.line == Tok.R.start.line) {
1389 Out->length = Tok.R.end.character - Tok.R.start.character;
1399 for (
int I = Tok.R.start.line; I < Tok.R.end.line; ++I) {
1400 auto LineEnd =
Code.find(
'\n', TokStartOffset);
1401 assert(LineEnd !=
Code.npos);
1402 Out->length = LineEnd - TokStartOffset;
1404 TokStartOffset = LineEnd + 1;
1405 Result.emplace_back();
1406 Out = &Result.back();
1407 *
Out = Result[Result.size() - 2];
1410 Out->deltaStart = 0;
1413 Out->length = Tok.R.end.character;
1426 case HighlightingKind::Variable:
1427 case HighlightingKind::LocalVariable:
1428 case HighlightingKind::StaticField:
1430 case HighlightingKind::Parameter:
1432 case HighlightingKind::Function:
1434 case HighlightingKind::Method:
1436 case HighlightingKind::StaticMethod:
1439 case HighlightingKind::Field:
1441 case HighlightingKind::Class:
1443 case HighlightingKind::Interface:
1445 case HighlightingKind::Enum:
1447 case HighlightingKind::EnumConstant:
1448 return "enumMember";
1449 case HighlightingKind::Typedef:
1450 case HighlightingKind::Type:
1452 case HighlightingKind::Unknown:
1454 case HighlightingKind::Namespace:
1456 case HighlightingKind::TemplateParameter:
1457 return "typeParameter";
1458 case HighlightingKind::Concept:
1460 case HighlightingKind::Primitive:
1462 case HighlightingKind::Macro:
1464 case HighlightingKind::Modifier:
1466 case HighlightingKind::Operator:
1468 case HighlightingKind::Bracket:
1470 case HighlightingKind::Label:
1472 case HighlightingKind::InactiveCode:
1475 llvm_unreachable(
"unhandled HighlightingKind");
1480 case HighlightingModifier::Declaration:
1481 return "declaration";
1482 case HighlightingModifier::Definition:
1483 return "definition";
1484 case HighlightingModifier::Deprecated:
1485 return "deprecated";
1486 case HighlightingModifier::Readonly:
1488 case HighlightingModifier::Static:
1490 case HighlightingModifier::Deduced:
1492 case HighlightingModifier::Abstract:
1494 case HighlightingModifier::Virtual:
1496 case HighlightingModifier::DependentName:
1497 return "dependentName";
1498 case HighlightingModifier::DefaultLibrary:
1499 return "defaultLibrary";
1500 case HighlightingModifier::UsedAsMutableReference:
1501 return "usedAsMutableReference";
1502 case HighlightingModifier::UsedAsMutablePointer:
1503 return "usedAsMutablePointer";
1504 case HighlightingModifier::ConstructorOrDestructor:
1505 return "constructorOrDestructor";
1506 case HighlightingModifier::UserDefined:
1507 return "userDefined";
1508 case HighlightingModifier::FunctionScope:
1509 return "functionScope";
1510 case HighlightingModifier::ClassScope:
1511 return "classScope";
1512 case HighlightingModifier::FileScope:
1514 case HighlightingModifier::GlobalScope:
1515 return "globalScope";
1517 llvm_unreachable(
"unhandled HighlightingModifier");
1520std::vector<SemanticTokensEdit>
1522 llvm::ArrayRef<SemanticToken> New) {
1527 while (!Old.empty() && !New.empty() && Old.front() == New.front()) {
1529 Old = Old.drop_front();
1530 New = New.drop_front();
1532 while (!Old.empty() && !New.empty() && Old.back() == New.back()) {
1533 Old = Old.drop_back();
1534 New = New.drop_back();
1537 if (Old.empty() && New.empty())
1541 Edit.deleteTokens = Old.size();
1543 return {std::move(
Edit)};
1547 std::vector<Range> SkippedRanges(std::move(AST.getMacros().SkippedRanges));
1548 const auto &SM = AST.getSourceManager();
1549 StringRef MainCode = SM.getBufferOrFake(SM.getMainFileID()).getBuffer();
1550 std::vector<Range> InactiveRegions;
1551 for (
const Range &Skipped : SkippedRanges) {
1552 Range Inactive = Skipped;
1570 if (
auto EndOfLine = endOfLine(MainCode, Inactive.
end.
line)) {
1571 Inactive.
end = *EndOfLine;
1573 elog(
"Failed to determine end of line: {0}", EndOfLine.takeError());
1576 InactiveRegions.push_back(Inactive);
1578 return InactiveRegions;
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
CompiledFragmentImpl & Out
CharSourceRange Range
SourceRange for the file name.
const google::protobuf::Message & M
llvm::raw_string_ostream OS
std::vector< const NamedDecl * > resolveUsingValueDecl(const UnresolvedUsingValueDecl *UUVD) const
Stores and provides access to parsed AST.
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)
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
llvm::StringRef toSemanticTokenType(HighlightingKind Kind)
size_t lspLength(llvm::StringRef Code)
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)
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.
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)
@ Type
An inlay hint that for a type annotation.
std::vector< Range > getInactiveRegions(ParsedAST &AST)
void elog(const char *Fmt, Ts &&... Vals)
@ Underlying
This is the underlying declaration for a renaming-alias, decltype etc.
@ ConstructorOrDestructor
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static const Config & current()
Returns the Config of the current Context, or an empty configuration.
A set of edits generated for a single file.
int line
Line position in a document (zero-based).
int character
Character offset on a line in a document (zero-based).
Position start
The range's start position.
Position end
The range's end position.
Information about a reference written in the source code, independent of the actual AST node that thi...
bool IsDecl
True if the reference is a declaration or definition;.
llvm::SmallVector< const NamedDecl *, 1 > Targets
A list of targets referenced by this name.
Specifies a single semantic token in the document.
Describes a replacement of a contiguous range of semanticTokens.