clang-tools  15.0.0git
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/SymbolID.h"
20 #include "support/Path.h"
21 #include "clang/AST/ASTTypeTraits.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <vector>
25 
26 namespace clang {
27 namespace syntax {
28 class Token;
29 class TokenBuffer;
30 } // namespace syntax
31 namespace clangd {
32 class ParsedAST;
33 
34 // Describes where a symbol is declared and defined (as far as clangd knows).
35 // There are three cases:
36 // - a declaration only, no definition is known (e.g. only header seen)
37 // - a declaration and a distinct definition (e.g. function declared in header)
38 // - a declaration and an equal definition (e.g. inline function, or class)
39 // For some types of symbol, e.g. macros, definition == declaration always.
40 struct LocatedSymbol {
41  // The (unqualified) name of the symbol.
42  std::string Name;
43  // The canonical or best declaration: where most users find its interface.
45  // Where the symbol is defined, if known. May equal PreferredDeclaration.
46  llvm::Optional<Location> Definition;
47  // SymbolID of the located symbol if available.
49 };
50 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const LocatedSymbol &);
51 /// Get definition of symbol at a specified \p Pos.
52 /// Multiple locations may be returned, corresponding to distinct symbols.
53 std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
54  const SymbolIndex *Index = nullptr);
55 
56 // Tries to provide a textual fallback for locating a symbol by looking up the
57 // word under the cursor as a symbol name in the index.
58 // The aim is to pick up references to symbols in contexts where
59 // AST-based resolution does not work, such as comments, strings, and PP
60 // disabled regions.
61 // (This is for internal use by locateSymbolAt, and is exposed for testing).
62 std::vector<LocatedSymbol>
64  const SymbolIndex *Index, const std::string &MainFilePath,
65  ASTNodeKind NodeKind);
66 
67 // Try to find a proximate occurrence of `Word` as an identifier, which can be
68 // used to resolve it.
69 // (This is for internal use by locateSymbolAt, and is exposed for testing).
70 const syntax::Token *findNearbyIdentifier(const SpelledWord &Word,
71  const syntax::TokenBuffer &TB);
72 
73 /// Get all document links
74 std::vector<DocumentLink> getDocumentLinks(ParsedAST &AST);
75 
76 /// Returns highlights for all usages of a symbol at \p Pos.
77 std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
78  Position Pos);
79 
81  // Bitmask describing whether the occurrence is a declaration, definition etc.
82  enum ReferenceAttributes : unsigned {
83  Declaration = 1 << 0,
84  Definition = 1 << 1,
85  // The occurrence is an override of the target base method.
86  Override = 1 << 2,
87  };
88  struct Reference {
90  unsigned Attributes = 0;
91  };
92  std::vector<Reference> References;
93  bool HasMore = false;
94 };
95 llvm::raw_ostream &operator<<(llvm::raw_ostream &,
97 
98 /// Returns implementations at a specified \p Pos:
99 /// - overrides for a virtual method;
100 /// - subclasses for a base class;
101 std::vector<LocatedSymbol> findImplementations(ParsedAST &AST, Position Pos,
102  const SymbolIndex *Index);
103 
104 /// Returns symbols for types referenced at \p Pos.
105 ///
106 /// For example, given `b^ar()` wher bar return Foo, this function returns the
107 /// definition of class Foo.
108 std::vector<LocatedSymbol> findType(ParsedAST &AST, Position Pos);
109 
110 /// Returns references of the symbol at a specified \p Pos.
111 /// \p Limit limits the number of results returned (0 means no limit).
113  const SymbolIndex *Index = nullptr);
114 
115 /// Get info about symbols at \p Pos.
116 std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos);
117 
118 /// Find the record type references at \p Pos.
119 const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos);
120 
121 /// Given a record type declaration, find its base (parent) types.
122 std::vector<const CXXRecordDecl *> typeParents(const CXXRecordDecl *CXXRD);
123 
124 /// Get type hierarchy information at \p Pos.
125 llvm::Optional<TypeHierarchyItem> getTypeHierarchy(
126  ParsedAST &AST, Position Pos, int Resolve, TypeHierarchyDirection Direction,
127  const SymbolIndex *Index = nullptr, PathRef TUPath = PathRef{});
128 
129 void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels,
130  TypeHierarchyDirection Direction,
131  const SymbolIndex *Index);
132 
133 /// Get call hierarchy information at \p Pos.
134 std::vector<CallHierarchyItem>
135 prepareCallHierarchy(ParsedAST &AST, Position Pos, PathRef TUPath);
136 
137 std::vector<CallHierarchyIncomingCall>
138 incomingCalls(const CallHierarchyItem &Item, const SymbolIndex *Index);
139 
140 /// Returns all decls that are referenced in the \p FD except local symbols.
141 llvm::DenseSet<const Decl *> getNonLocalDeclRefs(ParsedAST &AST,
142  const FunctionDecl *FD);
143 } // namespace clangd
144 } // namespace clang
145 
146 #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:1742
clang::clangd::incomingCalls
std::vector< CallHierarchyIncomingCall > incomingCalls(const CallHierarchyItem &Item, const SymbolIndex *Index)
Definition: XRefs.cpp:2098
SymbolID.h
clang::clangd::ReferencesResult::References
std::vector< Reference > References
Definition: XRefs.h:92
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:1334
clang::clangd::SpelledWord
Definition: SourceCode.h:233
clang::clangd::ReferencesResult::HasMore
bool HasMore
Definition: XRefs.h:93
clang::clangd::ReferencesResult::Reference::Loc
Location Loc
Definition: XRefs.h:89
clang::clangd::resolveTypeHierarchy
void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels, TypeHierarchyDirection Direction, const SymbolIndex *Index)
Definition: XRefs.cpp:2056
Path.h
clang::clangd::Location
Definition: Protocol.h:208
clang::clangd::ReferencesResult::Declaration
@ Declaration
Definition: XRefs.h:83
clang::clangd::findImplementations
std::vector< LocatedSymbol > findImplementations(ParsedAST &AST, Position Pos, const SymbolIndex *Index)
Returns implementations at a specified Pos:
Definition: XRefs.cpp:1281
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:561
clang::clangd::typeParents
std::vector< const CXXRecordDecl * > typeParents(const CXXRecordDecl *CXXRD)
Given a record type declaration, find its base (parent) types.
Definition: XRefs.cpp:1967
clang::clangd::ReferencesResult::Override
@ Override
Definition: XRefs.h:86
Pos
size_t Pos
Definition: NoLintDirectiveHandler.cpp:97
Protocol.h
clang::clangd::findType
std::vector< LocatedSymbol > findType(ParsedAST &AST, Position Pos)
Returns symbols for types referenced at Pos.
Definition: XRefs.cpp:1942
clang::clangd::findNearbyIdentifier
const syntax::Token * findNearbyIdentifier(const SpelledWord &Word, const syntax::TokenBuffer &TB)
Definition: XRefs.cpp:669
clang::clangd::Position
Definition: Protocol.h:153
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:760
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:2155
clang::clangd::getSymbolInfo
std::vector< SymbolDetails > getSymbolInfo(ParsedAST &AST, Position Pos)
Get info about symbols at Pos.
Definition: XRefs.cpp:1514
clang::clangd::ReferencesResult
Definition: XRefs.h:80
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2172
Word
std::string Word
Definition: FuzzyMatchTests.cpp:39
SourceCode.h
Index
const SymbolIndex * Index
Definition: Dexp.cpp:98
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:113
clang::clangd::TypeHierarchyDirection
TypeHierarchyDirection
Definition: Protocol.h:1371
clang::clangd::ReferencesResult::Reference::Attributes
unsigned Attributes
Definition: XRefs.h:90
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang::clangd::LocatedSymbol::Name
std::string Name
Definition: XRefs.h:42
clang::clangd::ReferencesResult::Reference
Definition: XRefs.h:88
clang::clangd::LocatedSymbol
Definition: XRefs.h:40
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::ReferencesResult::Definition
@ Definition
Definition: XRefs.h:84
clang::clangd::LocatedSymbol::ID
SymbolID ID
Definition: XRefs.h:48
clang::clangd::ReferencesResult::ReferenceAttributes
ReferenceAttributes
Definition: XRefs.h:82
clang::clangd::LocatedSymbol::PreferredDeclaration
Location PreferredDeclaration
Definition: XRefs.h:44
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:2009
clang::clangd::ParsedAST
Stores and provides access to parsed AST.
Definition: ParsedAST.h:45
clang::clangd::SymbolID
Definition: SymbolID.h:32
clang::clangd::getDocumentLinks
std::vector< DocumentLink > getDocumentLinks(ParsedAST &AST)
Get all document links.
Definition: XRefs.cpp:844
clang::clangd::prepareCallHierarchy
std::vector< CallHierarchyItem > prepareCallHierarchy(ParsedAST &AST, Position Pos, PathRef TUPath)
Get call hierarchy information at Pos.
Definition: XRefs.cpp:2076
clang::clangd::LocatedSymbol::Definition
llvm::Optional< Location > Definition
Definition: XRefs.h:46
clang::clangd::findDocumentHighlights
std::vector< DocumentHighlight > findDocumentHighlights(ParsedAST &AST, Position Pos)
Returns highlights for all usages of a symbol at Pos.
Definition: XRefs.cpp:1240