clang-tools  11.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 
9 #include "SemanticHighlighting.h"
10 #include "FindTarget.h"
11 #include "ParsedAST.h"
12 #include "Protocol.h"
13 #include "SourceCode.h"
14 #include "support/Logger.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclarationName.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/RecursiveASTVisitor.h"
21 #include "clang/AST/Type.h"
22 #include "clang/AST/TypeLoc.h"
23 #include "clang/Basic/LangOptions.h"
24 #include "clang/Basic/SourceLocation.h"
25 #include "clang/Basic/SourceManager.h"
26 #include "clang/Tooling/Syntax/Tokens.h"
27 #include "llvm/ADT/None.h"
28 #include "llvm/ADT/Optional.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/Support/Base64.h"
31 #include "llvm/Support/Casting.h"
32 #include <algorithm>
33 
34 namespace clang {
35 namespace clangd {
36 namespace {
37 
38 /// Some names are not written in the source code and cannot be highlighted,
39 /// e.g. anonymous classes. This function detects those cases.
40 bool canHighlightName(DeclarationName Name) {
41  if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
42  Name.getNameKind() == DeclarationName::CXXUsingDirective)
43  return true;
44  auto *II = Name.getAsIdentifierInfo();
45  return II && !II->getName().empty();
46 }
47 
48 llvm::Optional<HighlightingKind> kindForType(const Type *TP);
49 llvm::Optional<HighlightingKind> kindForDecl(const NamedDecl *D) {
50  if (auto *USD = dyn_cast<UsingShadowDecl>(D)) {
51  if (auto *Target = USD->getTargetDecl())
52  D = Target;
53  }
54  if (auto *TD = dyn_cast<TemplateDecl>(D)) {
55  if (auto *Templated = TD->getTemplatedDecl())
56  D = Templated;
57  }
58  if (auto *TD = dyn_cast<TypedefNameDecl>(D)) {
59  // We try to highlight typedefs as their underlying type.
60  if (auto K = kindForType(TD->getUnderlyingType().getTypePtrOrNull()))
61  return K;
62  // And fallback to a generic kind if this fails.
64  }
65  // We highlight class decls, constructor decls and destructor decls as
66  // `Class` type. The destructor decls are handled in `VisitTagTypeLoc` (we
67  // will visit a TypeLoc where the underlying Type is a CXXRecordDecl).
68  if (auto *RD = llvm::dyn_cast<RecordDecl>(D)) {
69  // We don't want to highlight lambdas like classes.
70  if (RD->isLambda())
71  return llvm::None;
73  }
74  if (isa<ClassTemplateDecl>(D) || isa<RecordDecl>(D) ||
75  isa<CXXConstructorDecl>(D))
77  if (auto *MD = dyn_cast<CXXMethodDecl>(D))
78  return MD->isStatic() ? HighlightingKind::StaticMethod
80  if (isa<FieldDecl>(D))
82  if (isa<EnumDecl>(D))
84  if (isa<EnumConstantDecl>(D))
86  if (isa<ParmVarDecl>(D))
88  if (auto *VD = dyn_cast<VarDecl>(D))
89  return VD->isStaticDataMember()
91  : VD->isLocalVarDecl() ? HighlightingKind::LocalVariable
93  if (isa<BindingDecl>(D))
95  if (isa<FunctionDecl>(D))
97  if (isa<NamespaceDecl>(D) || isa<NamespaceAliasDecl>(D) ||
98  isa<UsingDirectiveDecl>(D))
100  if (isa<TemplateTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
101  isa<NonTypeTemplateParmDecl>(D))
103  if (isa<ConceptDecl>(D))
105  return llvm::None;
106 }
107 llvm::Optional<HighlightingKind> kindForType(const Type *TP) {
108  if (!TP)
109  return llvm::None;
110  if (TP->isBuiltinType()) // Builtins are special, they do not have decls.
112  if (auto *TD = dyn_cast<TemplateTypeParmType>(TP))
113  return kindForDecl(TD->getDecl());
114  if (auto *TD = TP->getAsTagDecl())
115  return kindForDecl(TD);
116  return llvm::None;
117 }
118 
119 llvm::Optional<HighlightingKind> kindForReference(const ReferenceLoc &R) {
120  llvm::Optional<HighlightingKind> Result;
121  for (const NamedDecl *Decl : R.Targets) {
122  if (!canHighlightName(Decl->getDeclName()))
123  return llvm::None;
124  auto Kind = kindForDecl(Decl);
125  if (!Kind || (Result && Kind != Result))
126  return llvm::None;
127  Result = Kind;
128  }
129  return Result;
130 }
131 
132 // For a macro usage `DUMP(foo)`, we want:
133 // - DUMP --> "macro"
134 // - foo --> "variable".
135 SourceLocation getHighlightableSpellingToken(SourceLocation L,
136  const SourceManager &SM) {
137  if (L.isFileID())
138  return SM.isWrittenInMainFile(L) ? L : SourceLocation{};
139  // Tokens expanded from the macro body contribute no highlightings.
140  if (!SM.isMacroArgExpansion(L))
141  return {};
142  // Tokens expanded from macro args are potentially highlightable.
143  return getHighlightableSpellingToken(SM.getImmediateSpellingLoc(L), SM);
144 }
145 
146 unsigned evaluateHighlightPriority(HighlightingKind Kind) {
147  enum HighlightPriority { Dependent = 0, Resolved = 1 };
148  return Kind == HighlightingKind::DependentType ||
150  ? Dependent
151  : Resolved;
152 }
153 
154 // Sometimes we get conflicts between findExplicitReferences() returning
155 // a heuristic result for a dependent name (e.g. Method) and
156 // CollectExtraHighlighting returning a fallback dependent highlighting (e.g.
157 // DependentName). In such cases, resolve the conflict in favour of the
158 // resolved (non-dependent) highlighting.
159 // With macros we can get other conflicts (if a spelled token has multiple
160 // expansions with different token types) which we can't usefully resolve.
161 llvm::Optional<HighlightingToken>
162 resolveConflict(ArrayRef<HighlightingToken> Tokens) {
163  if (Tokens.size() == 1)
164  return Tokens[0];
165 
166  if (Tokens.size() != 2)
167  return llvm::None;
168 
169  unsigned Priority1 = evaluateHighlightPriority(Tokens[0].Kind);
170  unsigned Priority2 = evaluateHighlightPriority(Tokens[1].Kind);
171  if (Priority1 == Priority2)
172  return llvm::None;
173  return Priority1 > Priority2 ? Tokens[0] : Tokens[1];
174 }
175 
176 /// Consumes source locations and maps them to text ranges for highlightings.
177 class HighlightingsBuilder {
178 public:
179  HighlightingsBuilder(const ParsedAST &AST)
180  : TB(AST.getTokens()), SourceMgr(AST.getSourceManager()),
181  LangOpts(AST.getLangOpts()) {}
182 
183  void addToken(HighlightingToken T) { Tokens.push_back(T); }
184 
185  void addToken(SourceLocation Loc, HighlightingKind Kind) {
186  Loc = getHighlightableSpellingToken(Loc, SourceMgr);
187  if (Loc.isInvalid())
188  return;
189  const auto *Tok = TB.spelledTokenAt(Loc);
190  assert(Tok);
191 
193  Tok->range(SourceMgr).toCharRange(SourceMgr));
194  Tokens.push_back(HighlightingToken{Kind, std::move(Range)});
195  }
196 
197  std::vector<HighlightingToken> collect(ParsedAST &AST) && {
198  // Initializer lists can give duplicates of tokens, therefore all tokens
199  // must be deduplicated.
200  llvm::sort(Tokens);
201  auto Last = std::unique(Tokens.begin(), Tokens.end());
202  Tokens.erase(Last, Tokens.end());
203 
204  // Macros can give tokens that have the same source range but conflicting
205  // kinds. In this case all tokens sharing this source range should be
206  // removed.
207  std::vector<HighlightingToken> NonConflicting;
208  NonConflicting.reserve(Tokens.size());
209  for (ArrayRef<HighlightingToken> TokRef = Tokens; !TokRef.empty();) {
210  ArrayRef<HighlightingToken> Conflicting =
211  TokRef.take_while([&](const HighlightingToken &T) {
212  // TokRef is guaranteed at least one element here because otherwise
213  // this predicate would never fire.
214  return T.R == TokRef.front().R;
215  });
216  if (auto Resolved = resolveConflict(Conflicting))
217  NonConflicting.push_back(*Resolved);
218  // TokRef[Conflicting.size()] is the next token with a different range (or
219  // the end of the Tokens).
220  TokRef = TokRef.drop_front(Conflicting.size());
221  }
222  // Add tokens indicating lines skipped by the preprocessor.
223  for (const Range &R : AST.getMacros().SkippedRanges) {
224  // Create one token for each line in the skipped range, so it works
225  // with line-based diffing.
226  assert(R.start.line <= R.end.line);
227  for (int Line = R.start.line; Line < R.end.line; ++Line) {
228  // Don't bother computing the offset for the end of the line, just use
229  // zero. The client will treat this highlighting kind specially, and
230  // highlight the entire line visually (i.e. not just to where the text
231  // on the line ends, but to the end of the screen).
232  NonConflicting.push_back({HighlightingKind::InactiveCode,
233  {Position{Line, 0}, Position{Line, 0}}});
234  }
235  }
236  // Re-sort the tokens because that's what the diffing expects.
237  llvm::sort(NonConflicting);
238  return NonConflicting;
239  }
240 
241 private:
242  const syntax::TokenBuffer &TB;
243  const SourceManager &SourceMgr;
244  const LangOptions &LangOpts;
245  std::vector<HighlightingToken> Tokens;
246 };
247 
248 /// Produces highlightings, which are not captured by findExplicitReferences,
249 /// e.g. highlights dependent names and 'auto' as the underlying type.
250 class CollectExtraHighlightings
251  : public RecursiveASTVisitor<CollectExtraHighlightings> {
252 public:
253  CollectExtraHighlightings(HighlightingsBuilder &H) : H(H) {}
254 
255  bool VisitDecltypeTypeLoc(DecltypeTypeLoc L) {
256  if (auto K = kindForType(L.getTypePtr()))
257  H.addToken(L.getBeginLoc(), *K);
258  return true;
259  }
260 
261  bool VisitDeclaratorDecl(DeclaratorDecl *D) {
262  auto *AT = D->getType()->getContainedAutoType();
263  if (!AT)
264  return true;
265  if (auto K = kindForType(AT->getDeducedType().getTypePtrOrNull()))
266  H.addToken(D->getTypeSpecStartLoc(), *K);
267  return true;
268  }
269 
270  bool VisitOverloadExpr(OverloadExpr *E) {
271  if (!E->decls().empty())
272  return true; // handled by findExplicitReferences.
273  H.addToken(E->getNameLoc(), HighlightingKind::DependentName);
274  return true;
275  }
276 
277  bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) {
278  H.addToken(E->getMemberNameInfo().getLoc(),
280  return true;
281  }
282 
283  bool VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
284  H.addToken(E->getNameInfo().getLoc(), HighlightingKind::DependentName);
285  return true;
286  }
287 
288  bool VisitDependentNameTypeLoc(DependentNameTypeLoc L) {
289  H.addToken(L.getNameLoc(), HighlightingKind::DependentType);
290  return true;
291  }
292 
293  bool VisitDependentTemplateSpecializationTypeLoc(
294  DependentTemplateSpecializationTypeLoc L) {
295  H.addToken(L.getTemplateNameLoc(), HighlightingKind::DependentType);
296  return true;
297  }
298 
299  // findExplicitReferences will walk nested-name-specifiers and
300  // find anything that can be resolved to a Decl. However, non-leaf
301  // components of nested-name-specifiers which are dependent names
302  // (kind "Identifier") cannot be resolved to a decl, so we visit
303  // them here.
304  bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc Q) {
305  if (NestedNameSpecifier *NNS = Q.getNestedNameSpecifier()) {
306  if (NNS->getKind() == NestedNameSpecifier::Identifier)
307  H.addToken(Q.getLocalBeginLoc(), HighlightingKind::DependentType);
308  }
309  return RecursiveASTVisitor::TraverseNestedNameSpecifierLoc(Q);
310  }
311 
312 private:
313  HighlightingsBuilder &H;
314 };
315 
316 void write32be(uint32_t I, llvm::raw_ostream &OS) {
317  std::array<char, 4> Buf;
318  llvm::support::endian::write32be(Buf.data(), I);
319  OS.write(Buf.data(), Buf.size());
320 }
321 
322 void write16be(uint16_t I, llvm::raw_ostream &OS) {
323  std::array<char, 2> Buf;
324  llvm::support::endian::write16be(Buf.data(), I);
325  OS.write(Buf.data(), Buf.size());
326 }
327 
328 // Get the highlightings on \c Line where the first entry of line is at \c
329 // StartLineIt. If it is not at \c StartLineIt an empty vector is returned.
330 ArrayRef<HighlightingToken>
331 takeLine(ArrayRef<HighlightingToken> AllTokens,
332  ArrayRef<HighlightingToken>::iterator StartLineIt, int Line) {
333  return ArrayRef<HighlightingToken>(StartLineIt, AllTokens.end())
334  .take_while([Line](const HighlightingToken &Token) {
335  return Token.R.start.line == Line;
336  });
337 }
338 } // namespace
339 
340 std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST) {
341  auto &C = AST.getASTContext();
342  // Add highlightings for AST nodes.
343  HighlightingsBuilder Builder(AST);
344  // Highlight 'decltype' and 'auto' as their underlying types.
345  CollectExtraHighlightings(Builder).TraverseAST(C);
346  // Highlight all decls and references coming from the AST.
348  if (auto Kind = kindForReference(R))
349  Builder.addToken(R.NameLoc, *Kind);
350  });
351  // Add highlightings for macro references.
352  for (const auto &SIDToRefs : AST.getMacros().MacroRefs) {
353  for (const auto &M : SIDToRefs.second)
354  Builder.addToken({HighlightingKind::Macro, M});
355  }
356  for (const auto &M : AST.getMacros().UnknownMacros)
357  Builder.addToken({HighlightingKind::Macro, M});
358 
359  return std::move(Builder).collect(AST);
360 }
361 
362 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K) {
363  switch (K) {
365  return OS << "Variable";
367  return OS << "LocalVariable";
369  return OS << "Parameter";
371  return OS << "Function";
373  return OS << "Method";
375  return OS << "StaticMethod";
377  return OS << "Field";
379  return OS << "StaticField";
381  return OS << "Class";
383  return OS << "Enum";
385  return OS << "EnumConstant";
387  return OS << "Typedef";
389  return OS << "DependentType";
391  return OS << "DependentName";
393  return OS << "Namespace";
395  return OS << "TemplateParameter";
397  return OS << "Concept";
399  return OS << "Primitive";
401  return OS << "Macro";
403  return OS << "InactiveCode";
404  }
405  llvm_unreachable("invalid HighlightingKind");
406 }
407 
408 std::vector<LineHighlightings>
409 diffHighlightings(ArrayRef<HighlightingToken> New,
410  ArrayRef<HighlightingToken> Old) {
411  assert(std::is_sorted(New.begin(), New.end()) &&
412  "New must be a sorted vector");
413  assert(std::is_sorted(Old.begin(), Old.end()) &&
414  "Old must be a sorted vector");
415 
416  // FIXME: There's an edge case when tokens span multiple lines. If the first
417  // token on the line started on a line above the current one and the rest of
418  // the line is the equal to the previous one than we will remove all
419  // highlights but the ones for the token spanning multiple lines. This means
420  // that when we get into the LSP layer the only highlights that will be
421  // visible are the ones for the token spanning multiple lines.
422  // Example:
423  // EndOfMultilineToken Token Token Token
424  // If "Token Token Token" don't differ from previously the line is
425  // incorrectly removed. Suggestion to fix is to separate any multiline tokens
426  // into one token for every line it covers. This requires reading from the
427  // file buffer to figure out the length of each line though.
428  std::vector<LineHighlightings> DiffedLines;
429  // ArrayRefs to the current line in the highlightings.
430  ArrayRef<HighlightingToken> NewLine(New.begin(),
431  /*length*/ static_cast<size_t>(0));
432  ArrayRef<HighlightingToken> OldLine(Old.begin(),
433  /*length*/ static_cast<size_t>(0));
434  auto NewEnd = New.end();
435  auto OldEnd = Old.end();
436  auto NextLineNumber = [&]() {
437  int NextNew = NewLine.end() != NewEnd ? NewLine.end()->R.start.line
438  : std::numeric_limits<int>::max();
439  int NextOld = OldLine.end() != OldEnd ? OldLine.end()->R.start.line
440  : std::numeric_limits<int>::max();
441  return std::min(NextNew, NextOld);
442  };
443 
444  for (int LineNumber = 0; NewLine.end() < NewEnd || OldLine.end() < OldEnd;
445  LineNumber = NextLineNumber()) {
446  NewLine = takeLine(New, NewLine.end(), LineNumber);
447  OldLine = takeLine(Old, OldLine.end(), LineNumber);
448  if (NewLine != OldLine) {
449  DiffedLines.push_back({LineNumber, NewLine, /*IsInactive=*/false});
450 
451  // Turn a HighlightingKind::InactiveCode token into the IsInactive flag.
452  auto &AddedLine = DiffedLines.back();
453  llvm::erase_if(AddedLine.Tokens, [&](const HighlightingToken &T) {
454  if (T.Kind == HighlightingKind::InactiveCode) {
455  AddedLine.IsInactive = true;
456  return true;
457  }
458  return false;
459  });
460  }
461  }
462 
463  return DiffedLines;
464 }
465 
467  return std::tie(L.R, L.Kind) == std::tie(R.R, R.Kind);
468 }
470  return std::tie(L.R, L.Kind) < std::tie(R.R, R.Kind);
471 }
473  return std::tie(L.Line, L.Tokens) == std::tie(R.Line, R.Tokens);
474 }
475 
476 std::vector<SemanticToken>
477 toSemanticTokens(llvm::ArrayRef<HighlightingToken> Tokens) {
478  assert(std::is_sorted(Tokens.begin(), Tokens.end()));
479  std::vector<SemanticToken> Result;
480  const HighlightingToken *Last = nullptr;
481  for (const HighlightingToken &Tok : Tokens) {
482  // FIXME: support inactive code - we need to provide the actual bounds.
483  if (Tok.Kind == HighlightingKind::InactiveCode)
484  continue;
485  Result.emplace_back();
486  SemanticToken &Out = Result.back();
487  // deltaStart/deltaLine are relative if possible.
488  if (Last) {
489  assert(Tok.R.start.line >= Last->R.start.line);
490  Out.deltaLine = Tok.R.start.line - Last->R.start.line;
491  if (Out.deltaLine == 0) {
492  assert(Tok.R.start.character >= Last->R.start.character);
493  Out.deltaStart = Tok.R.start.character - Last->R.start.character;
494  } else {
495  Out.deltaStart = Tok.R.start.character;
496  }
497  } else {
498  Out.deltaLine = Tok.R.start.line;
499  Out.deltaStart = Tok.R.start.character;
500  }
501  assert(Tok.R.end.line == Tok.R.start.line);
502  Out.length = Tok.R.end.character - Tok.R.start.character;
503  Out.tokenType = static_cast<unsigned>(Tok.Kind);
504 
505  Last = &Tok;
506  }
507  return Result;
508 }
509 llvm::StringRef toSemanticTokenType(HighlightingKind Kind) {
510  switch (Kind) {
514  return "variable";
516  return "parameter";
518  return "function";
520  return "member";
522  // FIXME: better function/member with static modifier?
523  return "function";
525  return "member";
527  return "class";
529  return "enum";
531  return "enumConstant"; // nonstandard
533  return "type";
535  return "dependent"; // nonstandard
537  return "dependent"; // nonstandard
539  return "namespace";
541  return "typeParameter";
543  return "concept"; // nonstandard
545  return "type";
547  return "macro";
549  return "comment";
550  }
551  llvm_unreachable("unhandled HighlightingKind");
552 }
553 
554 std::vector<TheiaSemanticHighlightingInformation>
556  llvm::ArrayRef<LineHighlightings> Tokens) {
557  if (Tokens.size() == 0)
558  return {};
559 
560  // FIXME: Tokens might be multiple lines long (block comments) in this case
561  // this needs to add multiple lines for those tokens.
562  std::vector<TheiaSemanticHighlightingInformation> Lines;
563  Lines.reserve(Tokens.size());
564  for (const auto &Line : Tokens) {
565  llvm::SmallVector<char, 128> LineByteTokens;
566  llvm::raw_svector_ostream OS(LineByteTokens);
567  for (const auto &Token : Line.Tokens) {
568  // Writes the token to LineByteTokens in the byte format specified by the
569  // LSP proposal. Described below.
570  // |<---- 4 bytes ---->|<-- 2 bytes -->|<--- 2 bytes -->|
571  // | character | length | index |
572 
573  write32be(Token.R.start.character, OS);
574  write16be(Token.R.end.character - Token.R.start.character, OS);
575  write16be(static_cast<int>(Token.Kind), OS);
576  }
577 
578  Lines.push_back({Line.Line, encodeBase64(LineByteTokens), Line.IsInactive});
579  }
580 
581  return Lines;
582 }
583 
584 llvm::StringRef toTextMateScope(HighlightingKind Kind) {
585  // FIXME: Add scopes for C and Objective C.
586  switch (Kind) {
588  return "entity.name.function.cpp";
590  return "entity.name.function.method.cpp";
592  return "entity.name.function.method.static.cpp";
594  return "variable.other.cpp";
596  return "variable.other.local.cpp";
598  return "variable.parameter.cpp";
600  return "variable.other.field.cpp";
602  return "variable.other.field.static.cpp";
604  return "entity.name.type.class.cpp";
606  return "entity.name.type.enum.cpp";
608  return "variable.other.enummember.cpp";
610  return "entity.name.type.typedef.cpp";
612  return "entity.name.type.dependent.cpp";
614  return "entity.name.other.dependent.cpp";
616  return "entity.name.namespace.cpp";
618  return "entity.name.type.template.cpp";
620  return "entity.name.type.concept.cpp";
622  return "storage.type.primitive.cpp";
624  return "entity.name.function.preprocessor.cpp";
626  return "meta.disabled";
627  }
628  llvm_unreachable("unhandled HighlightingKind");
629 }
630 
631 std::vector<SemanticTokensEdit>
632 diffTokens(llvm::ArrayRef<SemanticToken> Old,
633  llvm::ArrayRef<SemanticToken> New) {
634  // For now, just replace everything from the first-last modification.
635  // FIXME: use a real diff instead, this is bad with include-insertion.
636 
637  unsigned Offset = 0;
638  while (!Old.empty() && !New.empty() && Old.front() == New.front()) {
639  ++Offset;
640  Old = Old.drop_front();
641  New = New.drop_front();
642  }
643  while (!Old.empty() && !New.empty() && Old.back() == New.back()) {
644  Old = Old.drop_back();
645  New = New.drop_back();
646  }
647 
648  if (Old.empty() && New.empty())
649  return {};
651  Edit.startToken = Offset;
652  Edit.deleteTokens = Old.size();
653  Edit.tokens = New;
654  return {std::move(Edit)};
655 }
656 
657 } // namespace clangd
658 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
const FunctionDecl * Decl
std::vector< HighlightingToken > Tokens
Position start
The range&#39;s start position.
Definition: Protocol.h:175
Information about a reference written in the source code, independent of the actual AST node that thi...
Definition: FindTarget.h:119
CompiledFragmentImpl & Out
llvm::SourceMgr * SourceMgr
llvm::StringRef toTextMateScope(HighlightingKind Kind)
Converts a HighlightingKind to a corresponding TextMate scope (https://manual.macromates.com/en/language_grammars).
Documents should not be synced at all.
std::vector< TheiaSemanticHighlightingInformation > toTheiaSemanticHighlightingInformation(llvm::ArrayRef< LineHighlightings > Tokens)
Convert to LSP&#39;s semantic highlighting information.
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
Definition: ParsedAST.cpp:471
BindArgumentKind Kind
std::vector< Range > UnknownMacros
Definition: CollectMacros.h:32
llvm::DenseMap< SymbolID, std::vector< Range > > MacroRefs
Definition: CollectMacros.h:28
unsigned deltaStart
token start character, relative to the previous token (relative to 0 or the previous token&#39;s start if...
Definition: Protocol.h:1369
unsigned tokenType
will be looked up in SemanticTokensLegend.tokenTypes
Definition: Protocol.h:1373
SourceLocation NameLoc
Start location of the last name part, i.e. &#39;foo&#39; in &#39;ns::foo<int>&#39;.
Definition: FindTarget.h:123
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:93
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:162
std::vector< SemanticToken > tokens
Definition: Protocol.h:1416
std::vector< SemanticToken > toSemanticTokens(llvm::ArrayRef< HighlightingToken > Tokens)
unsigned deltaLine
token line number, relative to the previous token
Definition: Protocol.h:1366
static constexpr llvm::StringLiteral Name
std::vector< LineHighlightings > diffHighlightings(ArrayRef< HighlightingToken > New, ArrayRef< HighlightingToken > Old)
Return a line-by-line diff between two highlightings.
Specifies a single semantic token in the document.
Definition: Protocol.h:1364
Stores and provides access to parsed AST.
Definition: ParsedAST.h:48
CodeCompletionBuilder Builder
int line
Line position in a document (zero-based).
Definition: Protocol.h:146
size_t Offset
int character
Character offset on a line in a document (zero-based).
Definition: Protocol.h:151
const MainFileMacros & getMacros() const
Gets all macro references (definition, expansions) present in the main file, including those in the p...
Definition: ParsedAST.cpp:491
unsigned length
the length of the token. A token cannot be multiline
Definition: Protocol.h:1371
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Describes a a replacement of a contiguous range of semanticTokens.
Definition: Protocol.h:1410
Contains all information about highlightings on a single line.
void findExplicitReferences(const Stmt *S, llvm::function_ref< void(ReferenceLoc)> Out)
Recursively traverse S and report all references explicitly written in the code.
Definition: FindTarget.cpp:994
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
CharSourceRange Range
SourceRange for the file name.
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST)
const Expr * E
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition: Headers.cpp:273
std::vector< SemanticTokensEdit > diffTokens(llvm::ArrayRef< SemanticToken > Old, llvm::ArrayRef< SemanticToken > New)
A set of edits generated for a single file.
Definition: SourceCode.h:180
NodeType Type
llvm::StringRef toSemanticTokenType(HighlightingKind Kind)
unsigned Lines
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
Definition: SourceCode.cpp:471