clang-tools  11.0.0
XRefs.h
Go to the documentation of this file.
1 //===--- XRefs.h -------------------------------------------------*- 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 // Features that traverse references between symbols.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_XREFS_H
14 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_XREFS_H
15 
16 #include "Protocol.h"
17 #include "SourceCode.h"
18 #include "index/Index.h"
19 #include "index/SymbolLocation.h"
20 #include "support/Path.h"
21 #include "clang/AST/ASTTypeTraits.h"
22 #include "clang/AST/Type.h"
23 #include "clang/Format/Format.h"
24 #include "clang/Index/IndexSymbol.h"
25 #include "llvm/ADT/Optional.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <vector>
28 
29 namespace clang {
30 namespace syntax {
31 class Token;
32 class TokenBuffer;
33 } // namespace syntax
34 namespace clangd {
35 class ParsedAST;
36 
37 // Describes where a symbol is declared and defined (as far as clangd knows).
38 // There are three cases:
39 // - a declaration only, no definition is known (e.g. only header seen)
40 // - a declaration and a distinct definition (e.g. function declared in header)
41 // - a declaration and an equal definition (e.g. inline function, or class)
42 // For some types of symbol, e.g. macros, definition == declaration always.
43 struct LocatedSymbol {
44  // The (unqualified) name of the symbol.
45  std::string Name;
46  // The canonical or best declaration: where most users find its interface.
48  // Where the symbol is defined, if known. May equal PreferredDeclaration.
49  llvm::Optional<Location> Definition;
50 };
51 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const LocatedSymbol &);
52 /// Get definition of symbol at a specified \p Pos.
53 /// Multiple locations may be returned, corresponding to distinct symbols.
54 std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
55  const SymbolIndex *Index = nullptr);
56 
57 // Tries to provide a textual fallback for locating a symbol by looking up the
58 // word under the cursor as a symbol name in the index.
59 // The aim is to pick up references to symbols in contexts where
60 // AST-based resolution does not work, such as comments, strings, and PP
61 // disabled regions.
62 // (This is for internal use by locateSymbolAt, and is exposed for testing).
63 std::vector<LocatedSymbol>
65  const SymbolIndex *Index, const std::string &MainFilePath,
66  ASTNodeKind NodeKind);
67 
68 // Try to find a proximate occurrence of `Word` as an identifier, which can be
69 // used to resolve it.
70 // (This is for internal use by locateSymbolAt, and is exposed for testing).
71 const syntax::Token *findNearbyIdentifier(const SpelledWord &Word,
72  const syntax::TokenBuffer &TB);
73 
74 /// Get all document links
75 std::vector<DocumentLink> getDocumentLinks(ParsedAST &AST);
76 
77 /// Returns highlights for all usages of a symbol at \p Pos.
78 std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
79  Position Pos);
80 
82  std::vector<Location> References;
83  bool HasMore = false;
84 };
85 /// Returns references of the symbol at a specified \p Pos.
86 /// \p Limit limits the number of results returned (0 means no limit).
88  const SymbolIndex *Index = nullptr);
89 
90 /// Get info about symbols at \p Pos.
91 std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos);
92 
93 /// Find the record type references at \p Pos.
94 const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos);
95 
96 /// Given a record type declaration, find its base (parent) types.
97 std::vector<const CXXRecordDecl *> typeParents(const CXXRecordDecl *CXXRD);
98 
99 /// Get type hierarchy information at \p Pos.
100 llvm::Optional<TypeHierarchyItem> getTypeHierarchy(
101  ParsedAST &AST, Position Pos, int Resolve, TypeHierarchyDirection Direction,
102  const SymbolIndex *Index = nullptr, PathRef TUPath = PathRef{});
103 
104 void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels,
105  TypeHierarchyDirection Direction,
106  const SymbolIndex *Index);
107 
108 /// Returns all decls that are referenced in the \p FD except local symbols.
109 llvm::DenseSet<const Decl *> getNonLocalDeclRefs(ParsedAST &AST,
110  const FunctionDecl *FD);
111 } // namespace clangd
112 } // namespace clang
113 
114 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_XREFS_H
clang::clangd::findRecordTypeAt
const CXXRecordDecl * findRecordTypeAt(ParsedAST &AST, Position Pos)
Find the record type references at Pos.
Definition: XRefs.cpp:1313
clang::clangd::findReferences
ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit, const SymbolIndex *Index)
Returns references of the symbol at a specified Pos.
Definition: XRefs.cpp:1020
clang::clangd::SpelledWord
Definition: SourceCode.h:223
clang::clangd::ReferencesResult::HasMore
bool HasMore
Definition: XRefs.h:83
SymbolLocation.h
clang::clangd::resolveTypeHierarchy
void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels, TypeHierarchyDirection Direction, const SymbolIndex *Index)
Definition: XRefs.cpp:1439
Path.h
clang::clangd::Location
Definition: Protocol.h:199
Index.h
clang::clangd::locateSymbolTextually
std::vector< LocatedSymbol > locateSymbolTextually(const SpelledWord &Word, ParsedAST &AST, const SymbolIndex *Index, const std::string &MainFilePath, ASTNodeKind NodeKind)
Definition: XRefs.cpp:356
clang::clangd::typeParents
std::vector< const CXXRecordDecl * > typeParents(const CXXRecordDecl *CXXRD)
Given a record type declaration, find its base (parent) types.
Definition: XRefs.cpp:1361
Protocol.h
clang::clangd::findNearbyIdentifier
const syntax::Token * findNearbyIdentifier(const SpelledWord &Word, const syntax::TokenBuffer &TB)
Definition: XRefs.cpp:466
clang::clangd::Position
Definition: Protocol.h:144
clang::clangd::locateSymbolAt
std::vector< LocatedSymbol > locateSymbolAt(ParsedAST &AST, Position Pos, const SymbolIndex *Index)
Get definition of symbol at a specified Pos.
Definition: XRefs.cpp:542
clang::clangd::getNonLocalDeclRefs
llvm::DenseSet< const Decl * > getNonLocalDeclRefs(ParsedAST &AST, const FunctionDecl *FD)
Returns all decls that are referenced in the FD except local symbols.
Definition: XRefs.cpp:1458
clang::clangd::getSymbolInfo
std::vector< SymbolDetails > getSymbolInfo(ParsedAST &AST, Position Pos)
Get info about symbols at Pos.
Definition: XRefs.cpp:1123
clang::clangd::ReferencesResult
Definition: XRefs.h:81
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1912
Word
std::string Word
Definition: FuzzyMatchTests.cpp:40
SourceCode.h
Index
const SymbolIndex * Index
Definition: Dexp.cpp:95
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
clang::clangd::TypeHierarchyDirection
TypeHierarchyDirection
Definition: Protocol.h:1270
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
clang::clangd::LocatedSymbol::Name
std::string Name
Definition: XRefs.h:45
clang::clangd::LocatedSymbol
Definition: XRefs.h:43
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::ReferencesResult::References
std::vector< Location > References
Definition: XRefs.h:82
clang::clangd::LocatedSymbol::PreferredDeclaration
Location PreferredDeclaration
Definition: XRefs.h:47
clang::clangd::getTypeHierarchy
llvm::Optional< TypeHierarchyItem > getTypeHierarchy(ParsedAST &AST, Position Pos, int ResolveLevels, TypeHierarchyDirection Direction, const SymbolIndex *Index, PathRef TUPath)
Get type hierarchy information at Pos.
Definition: XRefs.cpp:1399
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:48
Pos
Position Pos
Definition: SourceCode.cpp:649
clang::clangd::getDocumentLinks
std::vector< DocumentLink > getDocumentLinks(ParsedAST &AST)
Get all document links.
Definition: XRefs.cpp:612
clang::clangd::LocatedSymbol::Definition
llvm::Optional< Location > Definition
Definition: XRefs.h:49
clang::clangd::findDocumentHighlights
std::vector< DocumentHighlight > findDocumentHighlights(ParsedAST &AST, Position Pos)
Returns highlights for all usages of a symbol at Pos.
Definition: XRefs.cpp:980