Debian Codegraph @

llvm-project/clang-tools-extra/clangd/Hover.h

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