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 "Protocol.h"
|
2020-03-03 05:45:25 +08:00
|
|
|
#include "SourceCode.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"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Path.h"
|
2020-03-20 04:28:53 +08:00
|
|
|
#include "clang/AST/ASTTypeTraits.h"
|
2019-09-26 15:27:43 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2019-09-04 17:46:06 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2019-05-28 18:29:58 +08:00
|
|
|
#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 {
|
2020-03-03 05:45:25 +08:00
|
|
|
namespace syntax {
|
|
|
|
class Token;
|
|
|
|
class TokenBuffer;
|
|
|
|
} // namespace syntax
|
2017-12-20 01:06:07 +08:00
|
|
|
namespace clangd {
|
2019-09-04 17:46:06 +08:00
|
|
|
class ParsedAST;
|
2017-12-20 01:06:07 +08:00
|
|
|
|
[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
|
|
|
|
2020-03-03 05:45:25 +08:00
|
|
|
// Tries to provide a textual fallback for locating a symbol by looking up the
|
|
|
|
// word under the cursor as a symbol name in the index.
|
|
|
|
// The aim is to pick up references to symbols in contexts where
|
[clangd] Add a textual fallback for go-to-definition
Summary:
This facilitates performing go-to-definition in contexts where AST-based
resolution does not work, such as comments, string literals, preprocessor
disabled regions, and macro definitions, based on textual lookup in the index.
Partially fixes https://github.com/clangd/clangd/issues/241
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72874
2020-03-06 05:47:32 +08:00
|
|
|
// AST-based resolution does not work, such as comments, strings, and PP
|
2020-03-03 05:45:25 +08:00
|
|
|
// disabled regions.
|
[clangd] Add a textual fallback for go-to-definition
Summary:
This facilitates performing go-to-definition in contexts where AST-based
resolution does not work, such as comments, string literals, preprocessor
disabled regions, and macro definitions, based on textual lookup in the index.
Partially fixes https://github.com/clangd/clangd/issues/241
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72874
2020-03-06 05:47:32 +08:00
|
|
|
// (This is for internal use by locateSymbolAt, and is exposed for testing).
|
|
|
|
std::vector<LocatedSymbol>
|
2020-03-03 05:45:25 +08:00
|
|
|
locateSymbolTextually(const SpelledWord &Word, ParsedAST &AST,
|
2020-03-20 04:28:53 +08:00
|
|
|
const SymbolIndex *Index, const std::string &MainFilePath,
|
|
|
|
ASTNodeKind NodeKind);
|
2020-03-03 05:45:25 +08:00
|
|
|
|
|
|
|
// Try to find a proximate occurrence of `Word` as an identifier, which can be
|
|
|
|
// used to resolve it.
|
|
|
|
// (This is for internal use by locateSymbolAt, and is exposed for testing).
|
|
|
|
const syntax::Token *findNearbyIdentifier(const SpelledWord &Word,
|
|
|
|
const syntax::TokenBuffer &TB);
|
[clangd] Add a textual fallback for go-to-definition
Summary:
This facilitates performing go-to-definition in contexts where AST-based
resolution does not work, such as comments, string literals, preprocessor
disabled regions, and macro definitions, based on textual lookup in the index.
Partially fixes https://github.com/clangd/clangd/issues/241
Reviewers: sammccall
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D72874
2020-03-06 05:47:32 +08:00
|
|
|
|
2019-12-17 02:08:51 +08:00
|
|
|
/// Get all document links
|
|
|
|
std::vector<DocumentLink> getDocumentLinks(ParsedAST &AST);
|
|
|
|
|
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-11-18 18:35:00 +08:00
|
|
|
struct ReferencesResult {
|
|
|
|
std::vector<Location> References;
|
|
|
|
bool HasMore = false;
|
|
|
|
};
|
2020-11-19 01:13:11 +08:00
|
|
|
|
2020-12-03 16:42:46 +08:00
|
|
|
/// Returns implementations at a specified \p Pos:
|
|
|
|
/// - overrides for a virtual method;
|
|
|
|
/// - subclasses for a base class;
|
2020-11-19 01:13:11 +08:00
|
|
|
std::vector<LocatedSymbol> findImplementations(ParsedAST &AST, Position Pos,
|
|
|
|
const SymbolIndex *Index);
|
|
|
|
|
2019-11-18 18:35:00 +08:00
|
|
|
/// Returns references 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).
|
2019-11-18 18:35:00 +08:00
|
|
|
ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
|
|
|
|
const SymbolIndex *Index = nullptr);
|
2018-09-05 18:33:36 +08:00
|
|
|
|
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);
|
|
|
|
|
2020-11-16 11:59:10 +08:00
|
|
|
/// Get call hierarchy information at \p Pos.
|
|
|
|
std::vector<CallHierarchyItem>
|
|
|
|
prepareCallHierarchy(ParsedAST &AST, Position Pos, PathRef TUPath);
|
|
|
|
|
|
|
|
std::vector<CallHierarchyIncomingCall>
|
|
|
|
incomingCalls(const CallHierarchyItem &Item, const SymbolIndex *Index);
|
|
|
|
|
2019-09-26 15:27:43 +08:00
|
|
|
/// Returns all decls that are referenced in the \p FD except local symbols.
|
|
|
|
llvm::DenseSet<const Decl *> getNonLocalDeclRefs(ParsedAST &AST,
|
|
|
|
const FunctionDecl *FD);
|
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
|