clang-tools  16.0.0git
SemanticHighlighting.h
Go to the documentation of this file.
1 //==-- SemanticHighlighting.h - Generating highlights from the AST-- 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 // This file supports semantic highlighting: categorizing tokens in the file so
10 // that the editor can color/style them differently.
11 // This is particularly valuable for C++: its complex and context-dependent
12 // grammar is a challenge for simple syntax-highlighting techniques.
13 //
14 // Semantic highlightings are calculated for an AST by visiting every AST node
15 // and classifying nodes that are interesting to highlight (variables/function
16 // calls etc.).
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
21 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SEMANTICHIGHLIGHTING_H
22 
23 #include "Protocol.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 namespace clang {
28 namespace clangd {
29 class ParsedAST;
30 
31 enum class HighlightingKind {
32  Variable = 0,
34  Parameter,
35  Function,
36  Method,
38  Field,
40  Class,
41  Interface,
42  Enum,
44  Typedef,
45  Type,
46  Unknown,
47  Namespace,
49  Concept,
50  Primitive,
51  Macro,
52  Modifier,
53 
54  // This one is different from the other kinds as it's a line style
55  // rather than a token style.
57 
59 };
60 
61 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K);
62 
65  Definition,
66  Deprecated,
67  Deduced,
68  Readonly,
69  Static,
70  Abstract,
71  Virtual,
77 
79  ClassScope,
80  FileScope,
82 
84 };
85 static_assert(static_cast<unsigned>(HighlightingModifier::LastModifier) < 32,
86  "Increase width of modifiers bitfield!");
87 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingModifier K);
88 
89 // Contains all information needed for the highlighting a token.
92  uint32_t Modifiers = 0;
94 
96  Modifiers |= 1 << static_cast<unsigned>(M);
97  return *this;
98  }
99 };
100 
101 bool operator==(const HighlightingToken &L, const HighlightingToken &R);
102 bool operator<(const HighlightingToken &L, const HighlightingToken &R);
103 
104 // Returns all HighlightingTokens from an AST. Only generates highlights for the
105 // main AST.
106 std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST);
107 
108 std::vector<SemanticToken> toSemanticTokens(llvm::ArrayRef<HighlightingToken>,
109  llvm::StringRef Code);
110 llvm::StringRef toSemanticTokenType(HighlightingKind Kind);
111 llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier);
112 std::vector<SemanticTokensEdit> diffTokens(llvm::ArrayRef<SemanticToken> Before,
113  llvm::ArrayRef<SemanticToken> After);
114 
115 } // namespace clangd
116 } // namespace clang
117 
118 #endif
clang::clangd::HighlightingModifier::FunctionScope
@ FunctionScope
clang::clangd::HighlightingModifier::FileScope
@ FileScope
clang::clangd::HighlightingKind::Modifier
@ Modifier
clang::clangd::HighlightingModifier::UsedAsMutablePointer
@ UsedAsMutablePointer
clang::clangd::HighlightingModifier
HighlightingModifier
Definition: SemanticHighlighting.h:63
clang::clangd::HighlightingToken::Modifiers
uint32_t Modifiers
Definition: SemanticHighlighting.h:92
clang::clangd::HighlightingModifier::LastModifier
@ LastModifier
clang::clangd::toSemanticTokens
std::vector< SemanticToken > toSemanticTokens(llvm::ArrayRef< HighlightingToken > Tokens, llvm::StringRef Code)
Definition: SemanticHighlighting.cpp:1030
clang::clangd::HighlightingKind::Primitive
@ Primitive
clang::clangd::toSemanticTokenModifier
llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier)
Definition: SemanticHighlighting.cpp:1144
clang::clangd::HighlightingModifier::Deduced
@ Deduced
clang::clangd::HighlightingToken::R
Range R
Definition: SemanticHighlighting.h:93
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:59
clang::clangd::HighlightingKind
HighlightingKind
Definition: SemanticHighlighting.h:31
clang::clangd::HighlightingKind::Typedef
@ Typedef
clang::clangd::HighlightingToken::Kind
HighlightingKind Kind
Definition: SemanticHighlighting.h:91
clang::clangd::HighlightingKind::TemplateParameter
@ TemplateParameter
clang::clangd::HighlightingKind::LastKind
@ LastKind
clang::clangd::HighlightingModifier::Virtual
@ Virtual
clang::clangd::HighlightingKind::Macro
@ Macro
clang::clangd::HighlightingKind::Variable
@ Variable
clang::clangd::HighlightingKind::InactiveCode
@ InactiveCode
Protocol.h
M
const google::protobuf::Message & M
Definition: Server.cpp:309
clang::clangd::HighlightingToken::addModifier
HighlightingToken & addModifier(HighlightingModifier M)
Definition: SemanticHighlighting.h:95
Code
std::string Code
Definition: FindTargetTests.cpp:67
clang::clangd::HighlightingKind::Function
@ Function
clang::clangd::diffTokens
std::vector< SemanticTokensEdit > diffTokens(llvm::ArrayRef< SemanticToken > Old, llvm::ArrayRef< SemanticToken > New)
Definition: SemanticHighlighting.cpp:1185
clang::clangd::HighlightingKind::Unknown
@ Unknown
clang::clangd::HighlightingKind::Namespace
@ Namespace
clang::clangd::HighlightingKind::Type
@ Type
clang::clangd::HighlightingKind::Concept
@ Concept
clang::clangd::HighlightingKind::LocalVariable
@ LocalVariable
clang::clangd::HighlightingModifier::Deprecated
@ Deprecated
clang::clangd::HighlightingModifier::Abstract
@ Abstract
clang::clangd::HighlightingModifier::GlobalScope
@ GlobalScope
clang::clangd::HighlightingKind::Enum
@ Enum
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2217
clang::clangd::getSemanticHighlightings
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST)
Definition: SemanticHighlighting.cpp:889
clang::clangd::HighlightingKind::Method
@ Method
clang::clangd::HighlightingModifier::Static
@ Static
clang::clangd::HighlightingKind::Parameter
@ Parameter
clang::clangd::HighlightingModifier::ConstructorOrDestructor
@ ConstructorOrDestructor
clang::clangd::HighlightingToken
Definition: SemanticHighlighting.h:90
clang::clangd::HighlightingKind::StaticMethod
@ StaticMethod
clang::clangd::HighlightingModifier::Definition
@ Definition
clang::clangd::Range
Definition: Protocol.h:185
clang::clangd::toSemanticTokenType
llvm::StringRef toSemanticTokenType(HighlightingKind Kind)
Definition: SemanticHighlighting.cpp:1096
clang::clangd::HighlightingModifier::DefaultLibrary
@ DefaultLibrary
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
clang::clangd::HighlightingKind::Interface
@ Interface
clang::clangd::HighlightingKind::StaticField
@ StaticField
clang::clangd::operator<
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:95
clang::clangd::operator==
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition: Headers.cpp:362
clang::clangd::HighlightingKind::EnumConstant
@ EnumConstant
clang::clangd::HighlightingKind::Field
@ Field
clang::clangd::HighlightingKind::Class
@ Class
clang::clangd::HighlightingModifier::Readonly
@ Readonly
K
Kind K
Definition: Rename.cpp:454
clang::clangd::HighlightingModifier::DependentName
@ DependentName
clang::clangd::HighlightingModifier::UsedAsMutableReference
@ UsedAsMutableReference
clang::clangd::HighlightingModifier::ClassScope
@ ClassScope
clang::clangd::HighlightingModifier::Declaration
@ Declaration