17#include "clang/AST/DeclTemplate.h"
18#include "clang/Index/IndexSymbol.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
27#define DEBUG_TYPE "FindSymbols"
33using ScoredSymbolInfo = std::pair<float, SymbolInformation>;
34struct ScoredSymbolGreater {
35 bool operator()(
const ScoredSymbolInfo &L,
const ScoredSymbolInfo &R) {
36 if (L.first != R.first)
37 return L.first > R.first;
38 return L.second.name < R.second.name;
43bool approximateScopeMatch(llvm::StringRef Scope, llvm::StringRef Query) {
44 assert(Scope.empty() || Scope.ends_with(
"::"));
45 assert(Query.empty() || Query.ends_with(
"::"));
46 while (!Scope.empty() && !Query.empty()) {
47 auto Colons = Scope.find(
"::");
48 assert(Colons != llvm::StringRef::npos);
50 llvm::StringRef LeadingSpecifier = Scope.slice(0, Colons + 2);
51 Scope = Scope.slice(Colons + 2, llvm::StringRef::npos);
52 Query.consume_front(LeadingSpecifier);
60 llvm::StringRef TUPath) {
63 return error(
"Could not resolve path for file '{0}': {1}",
Loc.FileURI,
68 Start.line =
Loc.Start.line();
69 Start.character =
Loc.Start.column();
70 End.line =
Loc.End.line();
71 End.character =
Loc.End.column();
72 L.range = {Start, End};
77 llvm::StringRef TUPath) {
83llvm::Expected<std::vector<SymbolInformation>>
85 const SymbolIndex *
const Index, llvm::StringRef HintPath) {
86 std::vector<SymbolInformation> Result;
97 Req.
Query = std::string(Names.second);
100 auto HasLeadingColons = Names.first.consume_front(
"::");
104 if (HasLeadingColons || !Names.first.empty())
105 Req.
Scopes = {std::string(Names.first)};
114 Req.
Limit.value_or(std::numeric_limits<size_t>::max()));
117 Index->fuzzyFind(Req, [HintPath, &Top, &Filter, AnyScope = Req.
AnyScope,
118 ReqScope = Names.first](
const Symbol &Sym) {
119 llvm::StringRef Scope = Sym.Scope;
122 if (AnyScope && !approximateScopeMatch(Scope, ReqScope))
125 auto Loc = symbolToLocation(Sym, HintPath);
127 log(
"Workspace symbols: {0}", Loc.takeError());
137 Relevance.InBaseClass = AnyScope && Scope != ReqScope;
138 if (
auto NameMatch = Filter.match(Sym.
Name))
139 Relevance.NameMatch = *NameMatch;
141 log(
"Workspace symbol: {0} didn't match query {1}", Sym.Name,
145 Relevance.merge(Sym);
146 auto QualScore =
Quality.evaluateHeuristics();
147 auto RelScore = Relevance.evaluateHeuristics();
156 Scope.consume_back(
"::");
157 Info.containerName = Scope.str();
160 Info.score = Relevance.NameMatch > std::numeric_limits<float>::epsilon()
161 ?
Score / Relevance.NameMatch
165 for (
auto &R : std::move(Top).items())
166 Result.push_back(std::move(R.second));
171std::string getSymbolName(ASTContext &Ctx,
const NamedDecl &ND) {
174 if (
const auto *
Container = dyn_cast<ObjCContainerDecl>(&ND))
178 if (
const auto *Method = dyn_cast<ObjCMethodDecl>(&ND)) {
180 llvm::raw_string_ostream
OS(
Name);
182 OS << (Method->isInstanceMethod() ?
'-' :
'+');
183 Method->getSelector().print(
OS);
190std::string getSymbolDetail(ASTContext &Ctx,
const NamedDecl &ND) {
191 PrintingPolicy P(Ctx.getPrintingPolicy());
192 P.SuppressScope =
true;
193 P.SuppressUnwrittenScope =
true;
194 P.AnonymousTagLocations =
false;
195 P.PolishForDeclaration =
true;
197 llvm::raw_string_ostream
OS(Detail);
198 if (ND.getDescribedTemplateParams()) {
201 if (
const auto *VD = dyn_cast<ValueDecl>(&ND)) {
203 if (isa<CXXConstructorDecl>(VD)) {
204 std::string ConstructorType = VD->getType().getAsString(P);
206 llvm::StringRef WithoutVoid = ConstructorType;
207 WithoutVoid.consume_front(
"void ");
209 }
else if (!isa<CXXDestructorDecl>(VD)) {
210 VD->getType().print(
OS, P);
212 }
else if (
const auto *TD = dyn_cast<TagDecl>(&ND)) {
213 OS << TD->getKindName();
214 }
else if (isa<TypedefNameDecl>(&ND)) {
216 }
else if (isa<ConceptDecl>(&ND)) {
219 return std::move(
OS.str());
222std::optional<DocumentSymbol> declToSym(ASTContext &Ctx,
const NamedDecl &ND) {
223 auto &SM = Ctx.getSourceManager();
225 SourceLocation BeginLoc = ND.getBeginLoc();
226 SourceLocation EndLoc = ND.getEndLoc();
227 const auto SymbolRange =
232 index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
238 SI.name = getSymbolName(Ctx, ND);
240 SI.deprecated = ND.isDeprecated();
243 SI.detail = getSymbolDetail(Ctx, ND);
245 SourceLocation NameLoc = ND.getLocation();
246 SourceLocation FallbackNameLoc;
247 if (NameLoc.isMacroID()) {
250 FallbackNameLoc = SM.getExpansionLoc(NameLoc);
251 NameLoc = SM.getSpellingLoc(NameLoc);
253 NameLoc = SM.getExpansionLoc(NameLoc);
256 auto ComputeSelectionRange = [&](SourceLocation L) ->
Range {
259 SM, Lexer::getLocForEndOfToken(L, 0, SM, Ctx.getLangOpts()));
260 return Range{NameBegin, NameEnd};
263 SI.selectionRange = ComputeSelectionRange(NameLoc);
264 if (!SI.range.contains(SI.selectionRange) && FallbackNameLoc.isValid()) {
268 SI.selectionRange = ComputeSelectionRange(FallbackNameLoc);
270 if (!SI.range.contains(SI.selectionRange)) {
273 SI.range = SI.selectionRange;
286class DocumentOutline {
292 DocumentSymbol Symbol;
295 llvm::SmallVector<SourceLocation> EnclosingMacroLoc;
298 DocumentSymbol build() && {
300 Symbol.children.push_back(std::move(
C).build());
306 std::min(Symbol.range.start, Symbol.children.back().range.start);
308 std::max(Symbol.range.end, Symbol.children.back().range.end);
310 return std::move(Symbol);
314 SymBuilder &
addChild(DocumentSymbol S) {
316 Children.back().EnclosingMacroLoc = EnclosingMacroLoc;
317 Children.back().Symbol = std::move(S);
327 SymBuilder &inMacro(
const syntax::Token &Tok,
const SourceManager &SM,
328 std::optional<syntax::TokenBuffer::Expansion> Exp) {
329 if (llvm::is_contained(EnclosingMacroLoc, Tok.location()))
333 Children.back().EnclosingMacroLoc.back() == Tok.location())
337 Sym.name = Tok.text(SM).str();
338 Sym.kind = SymbolKind::Null;
339 Sym.range = Sym.selectionRange =
346 Exp->Spelled.front().location(),
347 Exp->Spelled.back().endLocation()));
349 llvm::raw_string_ostream
OS(Sym.detail);
350 const syntax::Token *Prev =
nullptr;
351 for (
const auto &Tok : Exp->Spelled.drop_front()) {
353 if (
OS.tell() > 80) {
357 if (Prev && Prev->endLocation() != Tok.location())
363 SymBuilder &Child =
addChild(std::move(Sym));
364 Child.EnclosingMacroLoc.push_back(Tok.location());
370 DocumentOutline(ParsedAST &AST) : AST(AST) {}
373 std::vector<DocumentSymbol> build() {
375 for (
auto &TopLevel : AST.getLocalTopLevelDecls())
376 traverseDecl(TopLevel,
Root);
377 return std::move(std::move(
Root).build().children);
381 enum class VisitKind {
No, OnlyDecl, OnlyChildren, DeclAndChildren };
383 void traverseDecl(
Decl *D, SymBuilder &
Parent) {
388 if (
auto *Templ = llvm::dyn_cast<TemplateDecl>(D)) {
390 if (
auto *TD = Templ->getTemplatedDecl())
394 VisitKind Visit = shouldVisit(D);
395 if (Visit == VisitKind::No)
398 if (Visit == VisitKind::OnlyChildren)
399 return traverseChildren(D,
Parent);
401 auto *ND = llvm::cast<NamedDecl>(D);
402 auto Sym = declToSym(AST.getASTContext(), *ND);
405 SymBuilder &MacroParent = possibleMacroContainer(D->getLocation(),
Parent);
406 SymBuilder &Child = MacroParent.addChild(std::move(*Sym));
408 if (Visit == VisitKind::OnlyDecl)
411 assert(Visit == VisitKind::DeclAndChildren &&
"Unexpected VisitKind");
412 traverseChildren(ND, Child);
430 SymBuilder &possibleMacroContainer(SourceLocation TargetLoc,
432 const auto &SM = AST.getSourceManager();
435 SymBuilder *CurParent = &
Parent;
436 for (SourceLocation
Loc = TargetLoc;
Loc.isMacroID();
437 Loc = SM.getImmediateMacroCallerLoc(
Loc)) {
440 if (SM.isMacroArgExpansion(
Loc)) {
442 MacroBody = SM.getFileID(SM.getImmediateExpansionRange(
Loc).getBegin());
445 MacroBody = SM.getFileID(
Loc);
450 SM.getSLocEntry(MacroBody).getExpansion().getExpansionLocStart();
456 if (
auto *Tok = AST.getTokens().spelledTokenContaining(
MacroName))
457 CurParent = &CurParent->inMacro(
458 *Tok, SM, AST.getTokens().expansionStartingAt(Tok));
463 void traverseChildren(
Decl *D, SymBuilder &
Builder) {
464 auto *Scope = llvm::dyn_cast<DeclContext>(D);
467 for (
auto *
C : Scope->decls())
471 VisitKind shouldVisit(
Decl *D) {
473 return VisitKind::No;
475 if (llvm::isa<LinkageSpecDecl>(D) || llvm::isa<ExportDecl>(D))
476 return VisitKind::OnlyChildren;
478 if (!llvm::isa<NamedDecl>(D))
479 return VisitKind::No;
481 if (
auto *Func = llvm::dyn_cast<FunctionDecl>(D)) {
484 if (
auto *
Info = Func->getTemplateSpecializationInfo()) {
485 if (!
Info->isExplicitInstantiationOrSpecialization())
486 return VisitKind::No;
490 return VisitKind::OnlyDecl;
500 if (
auto *TemplSpec = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
501 if (TemplSpec->isExplicitInstantiationOrSpecialization())
502 return TemplSpec->isExplicitSpecialization()
503 ? VisitKind::DeclAndChildren
504 : VisitKind::OnlyDecl;
505 return VisitKind::No;
507 if (
auto *TemplSpec = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
508 if (TemplSpec->isExplicitInstantiationOrSpecialization())
509 return TemplSpec->isExplicitSpecialization()
510 ? VisitKind::DeclAndChildren
511 : VisitKind::OnlyDecl;
512 return VisitKind::No;
515 return VisitKind::DeclAndChildren;
521struct PragmaMarkSymbol {
528void mergePragmas(DocumentSymbol &
Root, ArrayRef<PragmaMarkSymbol> Pragmas) {
529 while (!Pragmas.empty()) {
531 PragmaMarkSymbol P = std::move(Pragmas.front());
532 Pragmas = Pragmas.drop_front();
533 DocumentSymbol *Cur = &
Root;
534 while (Cur->range.contains(P.DocSym.range)) {
535 bool Swapped =
false;
536 for (
auto &
C : Cur->children) {
539 if (
C.range.contains(P.DocSym.range)) {
551 Cur->children.emplace_back(std::move(P.DocSym));
562 bool TerminatedByNextPragma =
false;
563 for (
auto &NextPragma : Pragmas) {
565 if (!Cur->range.contains(NextPragma.DocSym.range))
570 if (llvm::any_of(Cur->children, [&NextPragma](
const auto &Child) {
571 return Child.range.contains(NextPragma.DocSym.range);
576 auto It = llvm::partition(Cur->children,
577 [&P, &NextPragma](
const auto &S) ->
bool {
578 return !(P.DocSym.range < S.range &&
579 S.range < NextPragma.DocSym.range);
581 P.DocSym.children.assign(make_move_iterator(It),
582 make_move_iterator(Cur->children.end()));
583 Cur->children.erase(It, Cur->children.end());
584 TerminatedByNextPragma =
true;
587 if (!TerminatedByNextPragma) {
590 auto It = llvm::partition(Cur->children, [&P](
const auto &S) ->
bool {
591 return !(P.DocSym.range < S.range);
593 P.DocSym.children.assign(make_move_iterator(It),
594 make_move_iterator(Cur->children.end()));
595 Cur->children.erase(It, Cur->children.end());
598 for (DocumentSymbol &Sym : P.DocSym.children)
600 Cur->children.emplace_back(std::move(P.DocSym));
604PragmaMarkSymbol markToSymbol(
const PragmaMark &P) {
605 StringRef
Name = StringRef(P.Trivia).trim();
613 StringRef MaybeGroupName =
Name;
614 if (MaybeGroupName.consume_front(
"-") &&
615 (MaybeGroupName.ltrim() != MaybeGroupName || MaybeGroupName.empty())) {
616 Name = MaybeGroupName.empty() ?
"(unnamed group)" : MaybeGroupName.ltrim();
618 }
else if (
Name.empty()) {
619 Name =
"(unnamed mark)";
622 Sym.name =
Name.str();
623 Sym.kind = SymbolKind::File;
625 Sym.selectionRange = P.Rng;
629std::vector<DocumentSymbol> collectDocSymbols(ParsedAST &AST) {
630 std::vector<DocumentSymbol> Syms = DocumentOutline(AST).build();
632 const auto &PragmaMarks = AST.getMarks();
633 if (PragmaMarks.empty())
636 std::vector<PragmaMarkSymbol> Pragmas;
637 Pragmas.reserve(PragmaMarks.size());
638 for (
const auto &P : PragmaMarks)
639 Pragmas.push_back(markToSymbol(P));
642 {std::numeric_limits<int>::max(), std::numeric_limits<int>::max()}};
644 Root.children = std::move(Syms);
645 Root.range = EntireFile;
646 mergePragmas(
Root, llvm::ArrayRef(Pragmas));
647 return Root.children;
653 return collectDocSymbols(AST);
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
SignatureQualitySignals Quality
CodeCompletionBuilder Builder
std::optional< float > Score
std::vector< std::unique_ptr< HTMLNode > > Children
CharSourceRange Range
SourceRange for the file name.
Stores and provides access to parsed AST.
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
TopN<T> is a lossy container that preserves only the "best" N elements.
bool push(value_type &&V)
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
SymbolKind
The SymbolInfo Type.
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
llvm::Expected< Location > indexToLSPLocation(const SymbolLocation &Loc, llvm::StringRef TUPath)
Helper function for deriving an LSP Location from an index SymbolLocation.
@ Info
An information message.
std::optional< SourceRange > toHalfOpenFileRange(const SourceManager &SM, const LangOptions &LangOpts, SourceRange R)
Turns a token range into a half-open range and checks its correctness.
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
std::string printName(const ASTContext &Ctx, const NamedDecl &ND)
Prints unqualified name of the decl for the purpose of displaying it to the user.
std::string Path
A typedef to represent a file path.
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
llvm::Expected< Location > symbolToLocation(const Symbol &Sym, llvm::StringRef TUPath)
Helper function for deriving an LSP Location for a Symbol.
void unionRanges(Range &A, Range B)
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
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...
SymbolKind indexSymbolKindToSymbolKind(index::SymbolKind Kind)
@ No
Diagnostics must be generated for this snapshot.
llvm::Expected< std::vector< SymbolInformation > > getWorkspaceSymbols(llvm::StringRef Query, int Limit, const SymbolIndex *const Index, llvm::StringRef HintPath)
Searches for the symbols matching Query.
bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM)
Returns true if the token at Loc is spelled in the source code.
float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance)
Combine symbol quality and relevance into a single score.
void addChild(T I, ChildInfoType &&R)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< std::string > Scopes
If this is non-empty, symbols must be in at least one of the scopes (e.g.
std::string Query
A query string for the fuzzy find.
bool AnyScope
If set to true, allow symbols from any scope.
std::optional< uint32_t > Limit
The number of top candidates to return.
Attributes of a symbol that affect how much we like it.
Attributes of a symbol-query pair that affect how much we like it.
llvm::StringRef Name
The name of the symbol (for ContextWords). Must be explicitly assigned.
The class presents a C++ symbol, e.g.
SymbolLocation Definition
The location of the symbol's definition, if one was found.
index::SymbolInfo SymInfo
The symbol information, like symbol kind.
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
llvm::StringRef TemplateSpecializationArgs
Argument list in human-readable format, will be displayed to help disambiguate between different spec...
SymbolLocation CanonicalDeclaration
The location of the preferred declaration of the symbol.
static URIForFile canonicalize(llvm::StringRef AbsPath, llvm::StringRef TUPath)
Canonicalizes AbsPath via URI.