17 #include "clang/AST/DeclTemplate.h" 18 #include "clang/Index/IndexDataConsumer.h" 19 #include "clang/Index/IndexSymbol.h" 20 #include "clang/Index/IndexingAction.h" 21 #include "llvm/Support/FormatVariadic.h" 22 #include "llvm/Support/Path.h" 23 #include "llvm/Support/ScopedPrinter.h" 25 #define DEBUG_TYPE "FindSymbols" 31 using ScoredSymbolInfo = std::pair<float, SymbolInformation>;
32 struct ScoredSymbolGreater {
33 bool operator()(
const ScoredSymbolInfo &L,
const ScoredSymbolInfo &R) {
34 if (L.first != R.first)
35 return L.first > R.first;
36 return L.second.name < R.second.name;
43 llvm::StringRef HintPath) {
48 return llvm::make_error<llvm::StringError>(
49 formatv(
"Could not resolve path for symbol '{0}': {1}",
51 llvm::inconvertibleErrorCode());
58 Start.
line = CD.Start.line();
60 End.
line = CD.End.line();
62 L.range = {Start, End};
66 llvm::Expected<std::vector<SymbolInformation>>
69 std::vector<SymbolInformation> Result;
70 if (Query.empty() || !
Index)
76 Req.
Query = Names.second;
79 bool IsGlobalQuery = Names.first.consume_front(
"::");
82 if (IsGlobalQuery || !Names.first.empty())
83 Req.
Scopes = {Names.first};
88 TopN<ScoredSymbolInfo, ScoredSymbolGreater> Top(
89 Req.Limit ? *Req.Limit : std::numeric_limits<size_t>::max());
90 FuzzyMatcher Filter(Req.Query);
91 Index->fuzzyFind(Req, [HintPath, &Top, &Filter](
const Symbol &Sym) {
94 log(
"Workspace symbols: {0}",
Loc.takeError());
99 std::string Scope = Sym.Scope;
100 llvm::StringRef ScopeRef = Scope;
101 ScopeRef.consume_back(
"::");
102 SymbolInformation
Info = {(Sym.Name + Sym.TemplateSpecializationArgs).str(),
107 SymbolRelevanceSignals Relevance;
108 Relevance.Name = Sym.Name;
110 if (
auto NameMatch = Filter.match(Sym.Name))
111 Relevance.NameMatch = *NameMatch;
113 log(
"Workspace symbol: {0} didn't match query {1}", Sym.Name,
117 Relevance.merge(Sym);
120 dlog(
"FindSymbols: {0}{1} = {2}\n{3}{4}\n", Sym.Scope, Sym.Name,
Score,
123 Top.push({
Score, std::move(Info)});
125 for (
auto &R : std::move(Top).items())
126 Result.push_back(std::move(R.second));
131 llvm::Optional<DocumentSymbol> declToSym(ASTContext &
Ctx,
const NamedDecl &ND) {
132 auto &SM = Ctx.getSourceManager();
139 SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc()));
140 SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
141 if (NameLoc.isInvalid() || BeginLoc.isInvalid() || EndLoc.isInvalid())
144 if (!SM.isWrittenInMainFile(NameLoc) || !SM.isWrittenInMainFile(BeginLoc) ||
145 !SM.isWrittenInMainFile(EndLoc))
150 SM, Lexer::getLocForEndOfToken(NameLoc, 0, SM, Ctx.getLangOpts()));
160 SI.deprecated = ND.isDeprecated();
163 SI.selectionRange =
Range{NameBegin, NameEnd};
164 if (!SI.range.contains(SI.selectionRange)) {
167 SI.range = SI.selectionRange;
180 class DocumentOutline {
182 DocumentOutline(ParsedAST &
AST) : AST(AST) {}
185 std::vector<DocumentSymbol> build() {
186 std::vector<DocumentSymbol>
Results;
187 for (
auto &TopLevel :
AST.getLocalTopLevelDecls())
188 traverseDecl(TopLevel, Results);
193 enum class VisitKind {
No, OnlyDecl, DeclAndChildren };
195 void traverseDecl(
Decl *D, std::vector<DocumentSymbol> &Results) {
196 if (
auto *Templ = llvm::dyn_cast<TemplateDecl>(D))
197 D = Templ->getTemplatedDecl();
198 auto *ND = llvm::dyn_cast<NamedDecl>(D);
201 VisitKind Visit = shouldVisit(ND);
204 llvm::Optional<DocumentSymbol> Sym = declToSym(
AST.getASTContext(), *ND);
207 if (Visit == VisitKind::DeclAndChildren)
208 traverseChildren(D, Sym->children);
209 Results.push_back(std::move(*Sym));
212 void traverseChildren(
Decl *D, std::vector<DocumentSymbol> &Results) {
213 auto *Scope = llvm::dyn_cast<DeclContext>(D);
216 for (
auto *C : Scope->decls())
217 traverseDecl(C, Results);
220 VisitKind shouldVisit(NamedDecl *D) {
224 if (
auto Func = llvm::dyn_cast<FunctionDecl>(D)) {
227 if (
auto *Info = Func->getTemplateSpecializationInfo()) {
228 if (!Info->isExplicitInstantiationOrSpecialization())
233 return VisitKind::OnlyDecl;
243 if (
auto *TemplSpec = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
244 if (TemplSpec->isExplicitInstantiationOrSpecialization())
245 return TemplSpec->isExplicitSpecialization()
246 ? VisitKind::DeclAndChildren
247 : VisitKind::OnlyDecl;
250 if (
auto *TemplSpec = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
251 if (TemplSpec->isExplicitInstantiationOrSpecialization())
252 return TemplSpec->isExplicitSpecialization()
253 ? VisitKind::DeclAndChildren
254 : VisitKind::OnlyDecl;
258 return VisitKind::DeclAndChildren;
264 std::vector<DocumentSymbol> collectDocSymbols(ParsedAST &
AST) {
265 return DocumentOutline(AST).build();
270 return collectDocSymbols(AST);
SourceLocation Loc
'#' location in the include directive
std::string printName(const ASTContext &Ctx, const NamedDecl &ND)
Prints unqualified name of the decl for the purpose of displaying it to the user. ...
const FunctionDecl * Decl
SignatureQualitySignals Quality
llvm::Expected< std::vector< SymbolInformation > > getWorkspaceSymbols(llvm::StringRef Query, int Limit, const SymbolIndex *const Index, llvm::StringRef HintPath)
Searches for the symbols matching Query.
SourceLocation nameLocation(const clang::Decl &D, const SourceManager &SM)
Find the source location of the identifier for D.
Diagnostics must be generated for this snapshot.
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
SymbolKind indexSymbolKindToSymbolKind(index::SymbolKind Kind)
URIForFile uri
The text document's URI.
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
std::vector< CodeCompletionResult > Results
Documents should not be synced at all.
std::vector< std::string > Scopes
If this is non-empty, symbols must be in at least one of the scopes (e.g.
llvm::Optional< float > Score
SymbolLocation Definition
The location of the symbol's definition, if one was found.
std::vector< SymbolDetails > getSymbolInfo(ParsedAST &AST, Position Pos)
Get info about symbols at Pos.
clang::find_all_symbols::SymbolInfo SymbolInfo
void log(const char *Fmt, Ts &&... Vals)
std::string Path
A typedef to represent a file path.
std::string Query
A query string for the fuzzy find.
static URIForFile canonicalize(llvm::StringRef AbsPath, llvm::StringRef TUPath)
Canonicalizes AbsPath via URI.
SymbolKind
The SymbolInfo Type.
SymbolLocation CanonicalDeclaration
The location of the preferred declaration of the symbol.
llvm::Expected< Location > symbolToLocation(const Symbol &Sym, llvm::StringRef HintPath)
Helper function for deriving an LSP Location for a Symbol.
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
Stores and provides access to parsed AST.
float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance)
Combine symbol quality and relevance into a single score.
int line
Line position in a document (zero-based).
int character
Character offset on a line in a document (zero-based).
The class presents a C++ symbol, e.g.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
llvm::Expected< std::vector< DocumentSymbol > > getDocumentSymbols(ParsedAST &AST)
Retrieves the symbols contained in the "main file" section of an AST in the same order that they appe...
CharSourceRange Range
SourceRange for the file name.
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
const SymbolIndex * Index