clang-tools  14.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/Support/raw_ostream.h"
25 
26 namespace clang {
27 namespace clangd {
28 class ParsedAST;
29 
30 enum class HighlightingKind {
31  Variable = 0,
33  Parameter,
34  Function,
35  Method,
37  Field,
39  Class,
40  Interface,
41  Enum,
43  Typedef,
44  Type,
45  Unknown,
46  Namespace,
48  Concept,
49  Primitive,
50  Macro,
51 
52  // This one is different from the other kinds as it's a line style
53  // rather than a token style.
55 
57 };
58 
59 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingKind K);
60 
63  // FIXME: Definition (needs findExplicitReferences support)
64  Deprecated,
65  Deduced,
66  Readonly,
67  Static,
68  Abstract,
69  Virtual,
73 
75  ClassScope,
76  FileScope,
78 
80 };
81 static_assert(static_cast<unsigned>(HighlightingModifier::LastModifier) < 32,
82  "Increase width of modifiers bitfield!");
83 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, HighlightingModifier K);
84 
85 // Contains all information needed for the highlighting a token.
88  uint32_t Modifiers = 0;
90 
92  Modifiers |= 1 << static_cast<unsigned>(M);
93  return *this;
94  }
95 };
96 
97 bool operator==(const HighlightingToken &L, const HighlightingToken &R);
98 bool operator<(const HighlightingToken &L, const HighlightingToken &R);
99 
100 // Returns all HighlightingTokens from an AST. Only generates highlights for the
101 // main AST.
102 std::vector<HighlightingToken> getSemanticHighlightings(ParsedAST &AST);
103 
104 std::vector<SemanticToken> toSemanticTokens(llvm::ArrayRef<HighlightingToken>);
105 llvm::StringRef toSemanticTokenType(HighlightingKind Kind);
106 llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier);
107 std::vector<SemanticTokensEdit> diffTokens(llvm::ArrayRef<SemanticToken> Before,
108  llvm::ArrayRef<SemanticToken> After);
109 
110 } // namespace clangd
111 } // namespace clang
112 
113 #endif
clang::clangd::HighlightingModifier::FunctionScope
@ FunctionScope
clang::clangd::HighlightingModifier::FileScope
@ FileScope
clang::clangd::HighlightingModifier
HighlightingModifier
Definition: SemanticHighlighting.h:61
clang::clangd::HighlightingToken::Modifiers
uint32_t Modifiers
Definition: SemanticHighlighting.h:88
clang::clangd::HighlightingModifier::LastModifier
@ LastModifier
clang::clangd::HighlightingKind::Primitive
@ Primitive
clang::clangd::toSemanticTokenModifier
llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier)
Definition: SemanticHighlighting.cpp:984
clang::clangd::HighlightingModifier::Deduced
@ Deduced
clang::clangd::HighlightingToken::R
Range R
Definition: SemanticHighlighting.h:89
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:59
clang::clangd::HighlightingKind
HighlightingKind
Definition: SemanticHighlighting.h:30
clang::clangd::HighlightingKind::Typedef
@ Typedef
clang::clangd::HighlightingToken::Kind
HighlightingKind Kind
Definition: SemanticHighlighting.h:87
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:91
clang::clangd::HighlightingKind::Function
@ Function
clang::clangd::diffTokens
std::vector< SemanticTokensEdit > diffTokens(llvm::ArrayRef< SemanticToken > Old, llvm::ArrayRef< SemanticToken > New)
Definition: SemanticHighlighting.cpp:1019
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:2013
clang::clangd::getSemanticHighlightings
std::vector< HighlightingToken > getSemanticHighlightings(ParsedAST &AST)
Definition: SemanticHighlighting.cpp:779
clang::clangd::HighlightingKind::Method
@ Method
clang::clangd::HighlightingModifier::Static
@ Static
clang::clangd::HighlightingKind::Parameter
@ Parameter
clang::clangd::HighlightingToken
Definition: SemanticHighlighting.h:86
clang::clangd::toSemanticTokens
std::vector< SemanticToken > toSemanticTokens(llvm::ArrayRef< HighlightingToken > Tokens)
Definition: SemanticHighlighting.cpp:908
clang::clangd::HighlightingKind::StaticMethod
@ StaticMethod
clang::clangd::Range
Definition: Protocol.h:177
clang::clangd::toSemanticTokenType
llvm::StringRef toSemanticTokenType(HighlightingKind Kind)
Definition: SemanticHighlighting.cpp:938
clang::clangd::HighlightingModifier::DefaultLibrary
@ DefaultLibrary
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
clang::clangd::HighlightingKind::Interface
@ Interface
clang::clangd::HighlightingKind::StaticField
@ StaticField
clang::clangd::operator<
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:97
clang::clangd::operator==
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition: Headers.cpp:273
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:442
clang::clangd::HighlightingModifier::DependentName
@ DependentName
clang::clangd::HighlightingModifier::UsedAsMutableReference
@ UsedAsMutableReference
clang::clangd::HighlightingModifier::ClassScope
@ ClassScope
clang::clangd::HighlightingModifier::Declaration
@ Declaration