2018-08-15 00:03:32 +08:00
|
|
|
//===--- XRefs.h -------------------------------------------------*- C++-*-===//
|
2017-12-20 01:06:07 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-12-20 01:06:07 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-20 01:06:07 +08:00
|
|
|
//
|
|
|
|
// Features that traverse references between symbols.
|
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_XREFS_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_XREFS_H
|
|
|
|
|
|
|
|
#include "ClangdUnit.h"
|
2019-05-29 18:01:00 +08:00
|
|
|
#include "FormattedString.h"
|
2017-12-20 01:06:07 +08:00
|
|
|
#include "Protocol.h"
|
2018-04-30 23:24:17 +08:00
|
|
|
#include "index/Index.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "index/SymbolLocation.h"
|
|
|
|
#include "clang/Index/IndexSymbol.h"
|
2018-06-04 18:37:16 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-12-20 01:06:07 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
// Describes where a symbol is declared and defined (as far as clangd knows).
|
|
|
|
// There are three cases:
|
|
|
|
// - a declaration only, no definition is known (e.g. only header seen)
|
|
|
|
// - a declaration and a distinct definition (e.g. function declared in header)
|
|
|
|
// - a declaration and an equal definition (e.g. inline function, or class)
|
|
|
|
// For some types of symbol, e.g. macros, definition == declaration always.
|
|
|
|
struct LocatedSymbol {
|
|
|
|
// The (unqualified) name of the symbol.
|
|
|
|
std::string Name;
|
|
|
|
// The canonical or best declaration: where most users find its interface.
|
|
|
|
Location PreferredDeclaration;
|
|
|
|
// Where the symbol is defined, if known. May equal PreferredDeclaration.
|
|
|
|
llvm::Optional<Location> Definition;
|
|
|
|
};
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const LocatedSymbol &);
|
2017-12-20 01:06:07 +08:00
|
|
|
/// Get definition of symbol at a specified \p Pos.
|
[clangd] Implement textDocument/declaration from LSP 3.14
Summary:
LSP now reflects the declaration/definition distinction.
Language server changes:
- textDocument/definition now returns a definition if one is found, otherwise
the declaration. It no longer returns declaration + definition if they are
distinct.
- textDocument/declaration returns the best declaration we can find.
- For macros, the active macro definition is returned for both methods.
- For include directive, the top of the target file is returned for both.
There doesn't appear to be a discovery mechanism (we can't return everything to
clients that only know about definition), so this changes existing behavior.
In practice, it should greatly reduce the fraction of the time we need to show
the user a menu of options.
C++ API changes:
- findDefinitions is replaced by locateSymbolAt, which returns a
vector<LocatedSymbol> - one for each symbol under the cursor.
- this contains the preferred declaration, the definition (if found), and
the symbol name
This API enables some potentially-neat extensions, like swapping between decl
and def, and exposing the symbol name to the UI in the case of multiple symbols.
Reviewers: hokein
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D57388
llvm-svn: 352864
2019-02-01 19:26:13 +08:00
|
|
|
/// Multiple locations may be returned, corresponding to distinct symbols.
|
|
|
|
std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
|
|
|
|
const SymbolIndex *Index = nullptr);
|
2017-12-20 01:06:07 +08:00
|
|
|
|
|
|
|
/// Returns highlights for all usages of a symbol at \p Pos.
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
|
|
|
|
Position Pos);
|
2017-12-20 01:06:07 +08:00
|
|
|
|
2019-05-28 18:29:58 +08:00
|
|
|
/// Contains detailed information about a Symbol. Especially useful when
|
|
|
|
/// generating hover responses. It can be rendered as a hover panel, or
|
|
|
|
/// embedding clients can use the structured information to provide their own
|
|
|
|
/// UI.
|
|
|
|
struct HoverInfo {
|
|
|
|
/// Represents parameters of a function, a template or a macro.
|
|
|
|
/// For example:
|
|
|
|
/// - void foo(ParamType Name = DefaultValue)
|
|
|
|
/// - #define FOO(Name)
|
|
|
|
/// - template <ParamType Name = DefaultType> class Foo {};
|
|
|
|
struct Param {
|
|
|
|
/// The pretty-printed parameter type, e.g. "int", or "typename" (in
|
|
|
|
/// TemplateParameters)
|
|
|
|
llvm::Optional<std::string> Type;
|
|
|
|
/// None for unnamed parameters.
|
|
|
|
llvm::Optional<std::string> Name;
|
|
|
|
/// None if no default is provided.
|
|
|
|
llvm::Optional<std::string> Default;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// For a variable named Bar, declared in clang::clangd::Foo::getFoo the
|
|
|
|
/// following fields will hold:
|
|
|
|
/// - NamespaceScope: clang::clangd::
|
|
|
|
/// - LocalScope: Foo::getFoo::
|
|
|
|
/// - Name: Bar
|
|
|
|
|
|
|
|
/// Scopes might be None in cases where they don't make sense, e.g. macros and
|
|
|
|
/// auto/decltype.
|
|
|
|
/// Contains all of the enclosing namespaces, empty string means global
|
|
|
|
/// namespace.
|
|
|
|
llvm::Optional<std::string> NamespaceScope;
|
|
|
|
/// Remaining named contexts in symbol's qualified name, empty string means
|
|
|
|
/// symbol is not local.
|
|
|
|
std::string LocalScope;
|
|
|
|
/// Name of the symbol, does not contain any "::".
|
|
|
|
std::string Name;
|
|
|
|
llvm::Optional<Range> SymRange;
|
|
|
|
/// Scope containing the symbol. e.g, "global namespace", "function x::Y"
|
|
|
|
/// - None for deduced types, e.g "auto", "decltype" keywords.
|
|
|
|
SymbolKind Kind;
|
|
|
|
std::string Documentation;
|
|
|
|
/// Source code containing the definition of the symbol.
|
|
|
|
std::string Definition;
|
|
|
|
|
|
|
|
/// Pretty-printed variable type.
|
|
|
|
/// Set only for variables.
|
|
|
|
llvm::Optional<std::string> Type;
|
|
|
|
/// Set for functions and lambadas.
|
|
|
|
llvm::Optional<std::string> ReturnType;
|
|
|
|
/// Set for functions, lambdas and macros with parameters.
|
|
|
|
llvm::Optional<std::vector<Param>> Parameters;
|
|
|
|
/// Set for all templates(function, class, variable).
|
|
|
|
llvm::Optional<std::vector<Param>> TemplateParameters;
|
2019-06-26 16:00:24 +08:00
|
|
|
/// Contains the evaluated value of the symbol if available.
|
|
|
|
llvm::Optional<std::string> Value;
|
2019-05-28 18:29:58 +08:00
|
|
|
|
2019-05-29 18:01:00 +08:00
|
|
|
/// Produce a user-readable information.
|
|
|
|
FormattedString present() const;
|
2019-05-28 18:29:58 +08:00
|
|
|
};
|
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const HoverInfo::Param &);
|
|
|
|
inline bool operator==(const HoverInfo::Param &LHS,
|
|
|
|
const HoverInfo::Param &RHS) {
|
|
|
|
return std::tie(LHS.Type, LHS.Name, LHS.Default) ==
|
|
|
|
std::tie(RHS.Type, RHS.Name, RHS.Default);
|
|
|
|
}
|
|
|
|
|
[clangd] Implement textDocument/hover
Summary: Implemention of textDocument/hover as described in LSP definition.
This patch adds a basic Hover implementation. When hovering a variable,
function, method or namespace, clangd will return a text containing the
declaration's scope, as well as the declaration of the hovered entity.
For example, for a variable:
Declared in class Foo::Bar
int hello = 2
For macros, the macro definition is returned.
This patch doesn't include:
- markdown support (the client I use doesn't support it yet)
- range support (optional in the Hover response)
- comments associated to variables/functions/classes
They are kept as future work to keep this patch simpler.
I added tests in XRefsTests.cpp. hover.test contains one simple
smoketest to make sure the feature works from a black box perspective.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: sammccall, mgrang, klimek, rwols, ilya-biryukov, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D35894
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325395
2018-02-17 05:38:15 +08:00
|
|
|
/// Get the hover information when hovering at \p Pos.
|
2019-05-28 18:29:58 +08:00
|
|
|
llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
|
2019-07-10 01:59:50 +08:00
|
|
|
format::FormatStyle Style,
|
|
|
|
const SymbolIndex *Index);
|
[clangd] Implement textDocument/hover
Summary: Implemention of textDocument/hover as described in LSP definition.
This patch adds a basic Hover implementation. When hovering a variable,
function, method or namespace, clangd will return a text containing the
declaration's scope, as well as the declaration of the hovered entity.
For example, for a variable:
Declared in class Foo::Bar
int hello = 2
For macros, the macro definition is returned.
This patch doesn't include:
- markdown support (the client I use doesn't support it yet)
- range support (optional in the Hover response)
- comments associated to variables/functions/classes
They are kept as future work to keep this patch simpler.
I added tests in XRefsTests.cpp. hover.test contains one simple
smoketest to make sure the feature works from a black box perspective.
Reviewers: malaperle, krasimir, bkramer, ilya-biryukov
Subscribers: sammccall, mgrang, klimek, rwols, ilya-biryukov, arphaman, cfe-commits
Differential Revision: https://reviews.llvm.org/D35894
Signed-off-by: Simon Marchi <simon.marchi@ericsson.com>
Signed-off-by: William Enright <william.enright@polymtl.ca>
llvm-svn: 325395
2018-02-17 05:38:15 +08:00
|
|
|
|
2018-09-05 18:33:36 +08:00
|
|
|
/// Returns reference locations of the symbol at a specified \p Pos.
|
2019-01-15 02:11:09 +08:00
|
|
|
/// \p Limit limits the number of results returned (0 means no limit).
|
2018-09-05 18:33:36 +08:00
|
|
|
std::vector<Location> findReferences(ParsedAST &AST, Position Pos,
|
2019-01-15 02:11:09 +08:00
|
|
|
uint32_t Limit,
|
2018-09-05 18:33:36 +08:00
|
|
|
const SymbolIndex *Index = nullptr);
|
|
|
|
|
2018-11-28 00:40:46 +08:00
|
|
|
/// Get info about symbols at \p Pos.
|
|
|
|
std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos);
|
|
|
|
|
[clangd] Add support for type hierarchy (super types only for now)
Summary:
Patch by Nathan Ridge(@nridge)!
This is an LSP extension proposed here:
https://github.com/Microsoft/vscode-languageserver-node/pull/426
An example client implementation can be found here:
https://github.com/theia-ide/theia/pull/3802
Reviewers: kadircet, sammccall
Reviewed By: kadircet
Subscribers: jdoerfert, sammccall, cfe-commits, mgorny, dschaefer, simark, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet
Tags: #clang
Differential Revision: https://reviews.llvm.org/D56370
llvm-svn: 356445
2019-03-19 17:27:04 +08:00
|
|
|
/// Find the record type references at \p Pos.
|
|
|
|
const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos);
|
|
|
|
|
|
|
|
/// Given a record type declaration, find its base (parent) types.
|
|
|
|
std::vector<const CXXRecordDecl *> typeParents(const CXXRecordDecl *CXXRD);
|
|
|
|
|
|
|
|
/// Get type hierarchy information at \p Pos.
|
[clangd] Type hierarchy subtypes
Summary:
This builds on the relations support added in D59407, D62459, D62471,
and D62839 to implement type hierarchy subtypes.
Reviewers: kadircet
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, mgrang, arphaman,
jdoerfert, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D58880
llvm-svn: 363506
2019-06-16 10:31:37 +08:00
|
|
|
llvm::Optional<TypeHierarchyItem> getTypeHierarchy(
|
|
|
|
ParsedAST &AST, Position Pos, int Resolve, TypeHierarchyDirection Direction,
|
|
|
|
const SymbolIndex *Index = nullptr, PathRef TUPath = PathRef{});
|
[clangd] Add support for type hierarchy (super types only for now)
Summary:
Patch by Nathan Ridge(@nridge)!
This is an LSP extension proposed here:
https://github.com/Microsoft/vscode-languageserver-node/pull/426
An example client implementation can be found here:
https://github.com/theia-ide/theia/pull/3802
Reviewers: kadircet, sammccall
Reviewed By: kadircet
Subscribers: jdoerfert, sammccall, cfe-commits, mgorny, dschaefer, simark, ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, kadircet
Tags: #clang
Differential Revision: https://reviews.llvm.org/D56370
llvm-svn: 356445
2019-03-19 17:27:04 +08:00
|
|
|
|
2019-07-13 11:24:48 +08:00
|
|
|
void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels,
|
|
|
|
TypeHierarchyDirection Direction,
|
|
|
|
const SymbolIndex *Index);
|
|
|
|
|
2019-07-12 00:04:18 +08:00
|
|
|
/// Retrieves the deduced type at a given location (auto, decltype).
|
|
|
|
/// Retuns None unless SourceLocationBeg starts an auto/decltype token.
|
|
|
|
/// It will return the underlying type.
|
|
|
|
llvm::Optional<QualType> getDeducedType(ParsedAST &AST,
|
|
|
|
SourceLocation SourceLocationBeg);
|
|
|
|
|
|
|
|
/// Check if there is a deduced type at a given location (auto, decltype).
|
|
|
|
/// SourceLocationBeg must point to the first character of the token
|
|
|
|
bool hasDeducedType(ParsedAST &AST, SourceLocation SourceLocationBeg);
|
|
|
|
|
2017-12-20 01:06:07 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
2018-08-15 00:03:32 +08:00
|
|
|
|
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_XREFS_H
|