clang-tools  16.0.0git
Hover.h
Go to the documentation of this file.
1 //===--- Hover.h - Information about code at the cursor location -*- 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_HOVER_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_HOVER_H
11 
12 #include "ParsedAST.h"
13 #include "Protocol.h"
14 #include "support/Markup.h"
15 #include "clang/Index/IndexSymbol.h"
16 
17 namespace clang {
18 namespace clangd {
19 
20 /// Contains detailed information about a Symbol. Especially useful when
21 /// generating hover responses. It can be rendered as a hover panel, or
22 /// embedding clients can use the structured information to provide their own
23 /// UI.
24 struct HoverInfo {
25  /// Contains pretty-printed type and desugared type
26  struct PrintedType {
27  PrintedType() = default;
28  PrintedType(const char *Type) : Type(Type) {}
29  PrintedType(const char *Type, const char *AKAType)
30  : Type(Type), AKA(AKAType) {}
31 
32  /// Pretty-printed type
33  std::string Type;
34  /// Desugared type
35  llvm::Optional<std::string> AKA;
36  };
37 
38  /// Represents parameters of a function, a template or a macro.
39  /// For example:
40  /// - void foo(ParamType Name = DefaultValue)
41  /// - #define FOO(Name)
42  /// - template <ParamType Name = DefaultType> class Foo {};
43  struct Param {
44  /// The printable parameter type, e.g. "int", or "typename" (in
45  /// TemplateParameters), might be None for macro parameters.
46  llvm::Optional<PrintedType> Type;
47  /// None for unnamed parameters.
48  llvm::Optional<std::string> Name;
49  /// None if no default is provided.
50  llvm::Optional<std::string> Default;
51  };
52 
53  /// For a variable named Bar, declared in clang::clangd::Foo::getFoo the
54  /// following fields will hold:
55  /// - NamespaceScope: clang::clangd::
56  /// - LocalScope: Foo::getFoo::
57  /// - Name: Bar
58 
59  /// Scopes might be None in cases where they don't make sense, e.g. macros and
60  /// auto/decltype.
61  /// Contains all of the enclosing namespaces, empty string means global
62  /// namespace.
63  llvm::Optional<std::string> NamespaceScope;
64  /// Remaining named contexts in symbol's qualified name, empty string means
65  /// symbol is not local.
66  std::string LocalScope;
67  /// Name of the symbol, does not contain any "::".
68  std::string Name;
69  llvm::Optional<Range> SymRange;
71  std::string Documentation;
72  /// Source code containing the definition of the symbol.
73  std::string Definition;
74  const char *DefinitionLanguage = "cpp";
75  /// Access specifier for declarations inside class/struct/unions, empty for
76  /// others.
77  std::string AccessSpecifier;
78  /// Printable variable type.
79  /// Set only for variables.
80  llvm::Optional<PrintedType> Type;
81  /// Set for functions and lambdas.
82  llvm::Optional<PrintedType> ReturnType;
83  /// Set for functions, lambdas and macros with parameters.
84  llvm::Optional<std::vector<Param>> Parameters;
85  /// Set for all templates(function, class, variable).
86  llvm::Optional<std::vector<Param>> TemplateParameters;
87  /// Contains the evaluated value of the symbol if available.
88  llvm::Optional<std::string> Value;
89  /// Contains the byte-size of fields and types where it's interesting.
90  llvm::Optional<uint64_t> Size;
91  /// Contains the offset of fields within the enclosing class.
92  llvm::Optional<uint64_t> Offset;
93  /// Contains the padding following a field within the enclosing class.
94  llvm::Optional<uint64_t> Padding;
95  // Set when symbol is inside function call. Contains information extracted
96  // from the callee definition about the argument this is passed as.
97  llvm::Optional<Param> CalleeArgInfo;
98  struct PassType {
99  // How the variable is passed to callee.
102  // True if type conversion happened. This includes calls to implicit
103  // constructor, as well as built-in type conversions. Casting to base class
104  // is not considered conversion.
105  bool Converted = false;
106  };
107  // Set only if CalleeArgInfo is set.
108  llvm::Optional<PassType> CallPassType;
109 
110  /// Produce a user-readable information.
111  markup::Document present() const;
112 };
113 
114 inline bool operator==(const HoverInfo::PrintedType &LHS,
115  const HoverInfo::PrintedType &RHS) {
116  return std::tie(LHS.Type, LHS.AKA) == std::tie(RHS.Type, RHS.AKA);
117 }
118 
119 inline bool operator==(const HoverInfo::PassType &LHS,
120  const HoverInfo::PassType &RHS) {
121  return std::tie(LHS.PassBy, LHS.Converted) ==
122  std::tie(RHS.PassBy, RHS.Converted);
123 }
124 
125 // Try to infer structure of a documentation comment (e.g. line breaks).
126 // FIXME: move to another file so CodeComplete doesn't depend on Hover.
127 void parseDocumentation(llvm::StringRef Input, markup::Document &Output);
128 
129 llvm::raw_ostream &operator<<(llvm::raw_ostream &,
130  const HoverInfo::PrintedType &);
131 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const HoverInfo::Param &);
132 inline bool operator==(const HoverInfo::Param &LHS,
133  const HoverInfo::Param &RHS) {
134  return std::tie(LHS.Type, LHS.Name, LHS.Default) ==
135  std::tie(RHS.Type, RHS.Name, RHS.Default);
136 }
137 
138 /// Get the hover information when hovering at \p Pos.
139 llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
140  const format::FormatStyle &Style,
141  const SymbolIndex *Index);
142 
143 } // namespace clangd
144 } // namespace clang
145 
146 #endif
clang::clangd::HoverInfo::PrintedType::PrintedType
PrintedType(const char *Type, const char *AKAType)
Definition: Hover.h:29
clang::clangd::HoverInfo::Type
llvm::Optional< PrintedType > Type
Printable variable type.
Definition: Hover.h:80
clang::clangd::HoverInfo::Offset
llvm::Optional< uint64_t > Offset
Contains the offset of fields within the enclosing class.
Definition: Hover.h:92
FormatStyle
static cl::opt< std::string > FormatStyle("format-style", cl::desc(R"( Style for formatting code around applied fixes: - 'none' (default) turns off formatting - 'file' (literally 'file', not a placeholder) uses .clang-format file in the closest parent directory - '{ <json> }' specifies options inline, e.g. -format-style='{BasedOnStyle: llvm, IndentWidth: 8}' - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. This option overrides the 'FormatStyle` option in .clang-tidy file, if any. )"), cl::init("none"), cl::cat(ClangTidyCategory))
clang::clangd::HoverInfo::Kind
index::SymbolKind Kind
Definition: Hover.h:70
clang::clangd::HoverInfo::PrintedType::PrintedType
PrintedType()=default
clang::clangd::HoverInfo::CallPassType
llvm::Optional< PassType > CallPassType
Definition: Hover.h:108
clang::clangd::markup::Document
A format-agnostic representation for structured text.
Definition: Markup.h:97
clang::clangd::HoverInfo::ReturnType
llvm::Optional< PrintedType > ReturnType
Set for functions and lambdas.
Definition: Hover.h:82
clang::clangd::HoverInfo::PrintedType::PrintedType
PrintedType(const char *Type)
Definition: Hover.h:28
clang::clangd::HoverInfo::PassType::Converted
bool Converted
Definition: Hover.h:105
clang::clangd::HoverInfo::PrintedType::Type
std::string Type
Pretty-printed type.
Definition: Hover.h:33
Pos
size_t Pos
Definition: NoLintDirectiveHandler.cpp:97
clang::clangd::HoverInfo::Parameters
llvm::Optional< std::vector< Param > > Parameters
Set for functions, lambdas and macros with parameters.
Definition: Hover.h:84
Protocol.h
clang::clangd::HoverInfo::Size
llvm::Optional< uint64_t > Size
Contains the byte-size of fields and types where it's interesting.
Definition: Hover.h:90
clang::clangd::HoverInfo::Name
std::string Name
Name of the symbol, does not contain any "::".
Definition: Hover.h:68
clang::clangd::HoverInfo::Definition
std::string Definition
Source code containing the definition of the symbol.
Definition: Hover.h:73
clang::clangd::Unknown
@ Unknown
Definition: FuzzyMatch.h:56
clang::clangd::HoverInfo::Padding
llvm::Optional< uint64_t > Padding
Contains the padding following a field within the enclosing class.
Definition: Hover.h:94
Markup.h
clang::clangd::HoverInfo
Contains detailed information about a Symbol.
Definition: Hover.h:24
clang::clangd::HoverInfo::AccessSpecifier
std::string AccessSpecifier
Access specifier for declarations inside class/struct/unions, empty for others.
Definition: Hover.h:77
clang::clangd::HoverInfo::PassType::Ref
@ Ref
Definition: Hover.h:100
clang::clangd::HoverInfo::Param::Name
llvm::Optional< std::string > Name
None for unnamed parameters.
Definition: Hover.h:48
clang::clangd::HoverInfo::SymRange
llvm::Optional< Range > SymRange
Definition: Hover.h:69
clang::clangd::getHover
llvm::Optional< HoverInfo > getHover(ParsedAST &AST, Position Pos, const format::FormatStyle &Style, const SymbolIndex *Index)
Get the hover information when hovering at Pos.
Definition: Hover.cpp:1056
clang::clangd::HoverInfo::PrintedType::AKA
llvm::Optional< std::string > AKA
Desugared type.
Definition: Hover.h:35
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2217
clang::clangd::HoverInfo::NamespaceScope
llvm::Optional< std::string > NamespaceScope
For a variable named Bar, declared in clang::clangd::Foo::getFoo the following fields will hold:
Definition: Hover.h:63
clang::clangd::HoverInfo::Param::Default
llvm::Optional< std::string > Default
None if no default is provided.
Definition: Hover.h:50
Output
std::string Output
Definition: TraceTests.cpp:159
SymbolKind
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
Definition: SymbolInfo.cpp:19
clang::clangd::HoverInfo::TemplateParameters
llvm::Optional< std::vector< Param > > TemplateParameters
Set for all templates(function, class, variable).
Definition: Hover.h:86
Index
const SymbolIndex * Index
Definition: Dexp.cpp:98
clang::clangd::HoverInfo::PassType::PassBy
PassMode PassBy
Definition: Hover.h:101
clang::clangd::HoverInfo::LocalScope
std::string LocalScope
Remaining named contexts in symbol's qualified name, empty string means symbol is not local.
Definition: Hover.h:66
clang::clangd::HoverInfo::DefinitionLanguage
const char * DefinitionLanguage
Definition: Hover.h:74
clang::clangd::HoverInfo::present
markup::Document present() const
Produce a user-readable information.
Definition: Hover.cpp:1166
clang::clangd::HoverInfo::PrintedType
Contains pretty-printed type and desugared type.
Definition: Hover.h:26
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::HoverInfo::CalleeArgInfo
llvm::Optional< Param > CalleeArgInfo
Definition: Hover.h:97
clang::clangd::HoverInfo::Value
llvm::Optional< std::string > Value
Contains the evaluated value of the symbol if available.
Definition: Hover.h:88
clang::clangd::HoverInfo::PassType::PassMode
PassMode
Definition: Hover.h:100
clang::clangd::parseDocumentation
void parseDocumentation(llvm::StringRef Input, markup::Document &Output)
Definition: Hover.cpp:1330
clang::clangd::operator==
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition: Headers.cpp:362
clang::clangd::HoverInfo::PassType::Value
@ Value
Definition: Hover.h:100
clang::clangd::HoverInfo::Param
Represents parameters of a function, a template or a macro.
Definition: Hover.h:43
clang::clangd::HoverInfo::PassType::ConstRef
@ ConstRef
Definition: Hover.h:100
clang::clangd::HoverInfo::Param::Type
llvm::Optional< PrintedType > Type
The printable parameter type, e.g.
Definition: Hover.h:46
clang::clangd::HoverInfo::Documentation
std::string Documentation
Definition: Hover.h:71
clang::clangd::HoverInfo::PassType
Definition: Hover.h:98
ParsedAST.h