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 parse URI '{0}' for symbol '{1}'.", CD.FileURI,
51 llvm::inconvertibleErrorCode());
55 return llvm::make_error<llvm::StringError>(
56 formatv(
"Could not resolve path for URI '{0}' for symbol '{1}'.",
57 Uri->toString(), Sym.
Name),
58 llvm::inconvertibleErrorCode());
65 Start.
line = CD.Start.line();
67 End.
line = CD.End.line();
69 L.range = {Start, End};
73 llvm::Expected<std::vector<SymbolInformation>>
76 std::vector<SymbolInformation>
Result;
77 if (Query.empty() || !
Index)
83 Req.
Query = Names.second;
86 bool IsGlobalQuery = Names.first.consume_front(
"::");
89 if (IsGlobalQuery || !Names.first.empty())
90 Req.
Scopes = {Names.first};
95 TopN<ScoredSymbolInfo, ScoredSymbolGreater> Top(
96 Req.Limit ? *Req.Limit : std::numeric_limits<size_t>::max());
97 FuzzyMatcher Filter(Req.Query);
98 Index->fuzzyFind(Req, [HintPath, &Top, &Filter](
const Symbol &Sym) {
101 log(
"Workspace symbols: {0}",
Loc.takeError());
106 std::string Scope = Sym.Scope;
107 llvm::StringRef ScopeRef = Scope;
108 ScopeRef.consume_back(
"::");
109 SymbolInformation
Info = {(Sym.Name + Sym.TemplateSpecializationArgs).str(),
114 SymbolRelevanceSignals Relevance;
115 Relevance.Name = Sym.Name;
117 if (
auto NameMatch = Filter.match(Sym.Name))
118 Relevance.NameMatch = *NameMatch;
120 log(
"Workspace symbol: {0} didn't match query {1}", Sym.Name,
124 Relevance.merge(Sym);
127 dlog(
"FindSymbols: {0}{1} = {2}\n{3}{4}\n", Sym.Scope, Sym.Name,
Score,
130 Top.push({
Score, std::move(Info)});
132 for (
auto &R : std::move(Top).items())
133 Result.push_back(std::move(R.second));
138 llvm::Optional<DocumentSymbol> declToSym(ASTContext &
Ctx,
const NamedDecl &ND) {
139 auto &SM = Ctx.getSourceManager();
146 SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc()));
147 SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc()));
148 if (NameLoc.isInvalid() || BeginLoc.isInvalid() || EndLoc.isInvalid())
151 if (!SM.isWrittenInMainFile(NameLoc) || !SM.isWrittenInMainFile(BeginLoc) ||
152 !SM.isWrittenInMainFile(EndLoc))
157 SM, Lexer::getLocForEndOfToken(NameLoc, 0, SM, Ctx.getLangOpts()));
167 SI.deprecated = ND.isDeprecated();
170 SI.selectionRange =
Range{NameBegin, NameEnd};
171 if (!SI.range.contains(SI.selectionRange)) {
174 SI.range = SI.selectionRange;
187 class DocumentOutline {
189 DocumentOutline(ParsedAST &
AST) : AST(AST) {}
192 std::vector<DocumentSymbol> build() {
193 std::vector<DocumentSymbol>
Results;
194 for (
auto &TopLevel :
AST.getLocalTopLevelDecls())
195 traverseDecl(TopLevel, Results);
200 enum class VisitKind {
No, OnlyDecl, DeclAndChildren };
202 void traverseDecl(Decl *
D, std::vector<DocumentSymbol> &Results) {
203 if (
auto *Templ = llvm::dyn_cast<TemplateDecl>(D))
204 D = Templ->getTemplatedDecl();
205 auto *ND = llvm::dyn_cast<NamedDecl>(
D);
208 VisitKind Visit = shouldVisit(ND);
211 llvm::Optional<DocumentSymbol> Sym = declToSym(
AST.getASTContext(), *ND);
214 if (Visit == VisitKind::DeclAndChildren)
215 traverseChildren(D, Sym->children);
216 Results.push_back(std::move(*Sym));
219 void traverseChildren(Decl *D, std::vector<DocumentSymbol> &Results) {
220 auto *Scope = llvm::dyn_cast<DeclContext>(
D);
223 for (
auto *C : Scope->decls())
224 traverseDecl(C, Results);
227 VisitKind shouldVisit(NamedDecl *D) {
231 if (
auto Func = llvm::dyn_cast<FunctionDecl>(D)) {
234 if (
auto *Info = Func->getTemplateSpecializationInfo()) {
235 if (!Info->isExplicitInstantiationOrSpecialization())
240 return VisitKind::OnlyDecl;
250 if (
auto *TemplSpec = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
251 if (TemplSpec->isExplicitInstantiationOrSpecialization())
252 return TemplSpec->isExplicitSpecialization()
253 ? VisitKind::DeclAndChildren
254 : VisitKind::OnlyDecl;
257 if (
auto *TemplSpec = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
258 if (TemplSpec->isExplicitInstantiationOrSpecialization())
259 return TemplSpec->isExplicitSpecialization()
260 ? VisitKind::DeclAndChildren
261 : VisitKind::OnlyDecl;
265 return VisitKind::DeclAndChildren;
271 std::vector<DocumentSymbol> collectDocSymbols(ParsedAST &
AST) {
272 return DocumentOutline(AST).build();
277 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. ...
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.
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)
SourceLocation findNameLoc(const clang::Decl *D)
Find the identifier source location of the given D.
URIForFile uri
The text document's URI.
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.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
static llvm::Expected< URI > parse(llvm::StringRef Uri)
Parse a URI string "<scheme>:[//<authority>/]<path>".
const SymbolIndex * Index