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 ? *Req.
Limit : 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);
191std::string getSymbolDetail(ASTContext &Ctx,
const NamedDecl &ND) {
192 PrintingPolicy P(Ctx.getPrintingPolicy());
193 P.SuppressScope =
true;
194 P.SuppressUnwrittenScope =
true;
195 P.AnonymousTagLocations =
false;
196 P.PolishForDeclaration =
true;
198 llvm::raw_string_ostream
OS(Detail);
199 if (ND.getDescribedTemplateParams()) {
202 if (
const auto *VD = dyn_cast<ValueDecl>(&ND)) {
204 if (isa<CXXConstructorDecl>(VD)) {
205 std::string ConstructorType = VD->getType().getAsString(P);
207 llvm::StringRef WithoutVoid = ConstructorType;
208 WithoutVoid.consume_front(
"void ");
210 }
else if (!isa<CXXDestructorDecl>(VD)) {
211 VD->getType().print(
OS, P);
213 }
else if (
const auto *TD = dyn_cast<TagDecl>(&ND)) {
214 OS << TD->getKindName();
215 }
else if (isa<TypedefNameDecl>(&ND)) {
217 }
else if (isa<ConceptDecl>(&ND)) {
220 return std::move(
OS.str());
223std::optional<DocumentSymbol> declToSym(ASTContext &Ctx,
const NamedDecl &ND) {
224 auto &SM = Ctx.getSourceManager();
226 SourceLocation BeginLoc = ND.getBeginLoc();
227 SourceLocation EndLoc = ND.getEndLoc();
228 const auto SymbolRange =
233 index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
239 SI.name = getSymbolName(Ctx, ND);
241 SI.deprecated = ND.isDeprecated();
244 SI.detail = getSymbolDetail(Ctx, ND);
246 SourceLocation NameLoc = ND.getLocation();
247 SourceLocation FallbackNameLoc;
248 if (NameLoc.isMacroID()) {
251 FallbackNameLoc = SM.getExpansionLoc(NameLoc);
252 NameLoc = SM.getSpellingLoc(NameLoc);
254 NameLoc = SM.getExpansionLoc(NameLoc);
257 auto ComputeSelectionRange = [&](SourceLocation L) ->
Range {
260 SM, Lexer::getLocForEndOfToken(L, 0, SM, Ctx.getLangOpts()));
261 return Range{NameBegin, NameEnd};
264 SI.selectionRange = ComputeSelectionRange(NameLoc);
265 if (!SI.range.contains(SI.selectionRange) && FallbackNameLoc.isValid()) {
269 SI.selectionRange = ComputeSelectionRange(FallbackNameLoc);
271 if (!SI.range.contains(SI.selectionRange)) {
274 SI.range = SI.selectionRange;
287class DocumentOutline {
293 DocumentSymbol Symbol;
296 llvm::SmallVector<SourceLocation> EnclosingMacroLoc;
299 DocumentSymbol build() && {
301 Symbol.children.push_back(std::move(
C).build());
307 std::min(Symbol.range.start, Symbol.children.back().range.start);
309 std::max(Symbol.range.end, Symbol.children.back().range.end);
311 return std::move(Symbol);
315 SymBuilder &
addChild(DocumentSymbol S) {
317 Children.back().EnclosingMacroLoc = EnclosingMacroLoc;
318 Children.back().Symbol = std::move(S);
328 SymBuilder &inMacro(
const syntax::Token &Tok,
const SourceManager &SM,
329 std::optional<syntax::TokenBuffer::Expansion> Exp) {
330 if (llvm::is_contained(EnclosingMacroLoc, Tok.location()))
334 Children.back().EnclosingMacroLoc.back() == Tok.location())
338 Sym.name = Tok.text(SM).str();
339 Sym.kind = SymbolKind::Null;
340 Sym.range = Sym.selectionRange =
347 Exp->Spelled.front().location(),
348 Exp->Spelled.back().endLocation()));
350 llvm::raw_string_ostream
OS(Sym.detail);
351 const syntax::Token *Prev =
nullptr;
352 for (
const auto &Tok : Exp->Spelled.drop_front()) {
354 if (
OS.tell() > 80) {
358 if (Prev && Prev->endLocation() != Tok.location())
364 SymBuilder &Child =
addChild(std::move(Sym));
365 Child.EnclosingMacroLoc.push_back(Tok.location());
371 DocumentOutline(ParsedAST &AST) : AST(AST) {}
374 std::vector<DocumentSymbol> build() {
376 for (
auto &TopLevel : AST.getLocalTopLevelDecls())
377 traverseDecl(TopLevel,
Root);
378 return std::move(std::move(
Root).build().children);
382 enum class VisitKind {
No, OnlyDecl, OnlyChildren, DeclAndChildren };
384 void traverseDecl(
Decl *D, SymBuilder &
Parent) {
389 if (
auto *Templ = llvm::dyn_cast<TemplateDecl>(D)) {
391 if (
auto *TD = Templ->getTemplatedDecl())
395 VisitKind Visit = shouldVisit(D);
396 if (Visit == VisitKind::No)
399 if (Visit == VisitKind::OnlyChildren)
400 return traverseChildren(D,
Parent);
402 auto *ND = llvm::cast<NamedDecl>(D);
403 auto Sym = declToSym(AST.getASTContext(), *ND);
406 SymBuilder &MacroParent = possibleMacroContainer(D->getLocation(),
Parent);
407 SymBuilder &Child = MacroParent.addChild(std::move(*Sym));
409 if (Visit == VisitKind::OnlyDecl)
412 assert(Visit == VisitKind::DeclAndChildren &&
"Unexpected VisitKind");
413 traverseChildren(ND, Child);
431 SymBuilder &possibleMacroContainer(SourceLocation TargetLoc,
433 const auto &SM = AST.getSourceManager();
436 SymBuilder *CurParent = &
Parent;
437 for (SourceLocation
Loc = TargetLoc;
Loc.isMacroID();
438 Loc = SM.getImmediateMacroCallerLoc(
Loc)) {
441 if (SM.isMacroArgExpansion(
Loc)) {
443 MacroBody = SM.getFileID(SM.getImmediateExpansionRange(
Loc).getBegin());
446 MacroBody = SM.getFileID(
Loc);
451 SM.getSLocEntry(MacroBody).getExpansion().getExpansionLocStart();
457 if (
auto *Tok = AST.getTokens().spelledTokenContaining(
MacroName))
458 CurParent = &CurParent->inMacro(
459 *Tok, SM, AST.getTokens().expansionStartingAt(Tok));
464 void traverseChildren(
Decl *D, SymBuilder &
Builder) {
465 auto *Scope = llvm::dyn_cast<DeclContext>(D);
468 for (
auto *
C : Scope->decls())
472 VisitKind shouldVisit(
Decl *D) {
474 return VisitKind::No;
476 if (llvm::isa<LinkageSpecDecl>(D) || llvm::isa<ExportDecl>(D))
477 return VisitKind::OnlyChildren;
479 if (!llvm::isa<NamedDecl>(D))
480 return VisitKind::No;
482 if (
auto *Func = llvm::dyn_cast<FunctionDecl>(D)) {
485 if (
auto *
Info = Func->getTemplateSpecializationInfo()) {
486 if (!
Info->isExplicitInstantiationOrSpecialization())
487 return VisitKind::No;
491 return VisitKind::OnlyDecl;
501 if (
auto *TemplSpec = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
502 if (TemplSpec->isExplicitInstantiationOrSpecialization())
503 return TemplSpec->isExplicitSpecialization()
504 ? VisitKind::DeclAndChildren
505 : VisitKind::OnlyDecl;
506 return VisitKind::No;
508 if (
auto *TemplSpec = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
509 if (TemplSpec->isExplicitInstantiationOrSpecialization())
510 return TemplSpec->isExplicitSpecialization()
511 ? VisitKind::DeclAndChildren
512 : VisitKind::OnlyDecl;
513 return VisitKind::No;
516 return VisitKind::DeclAndChildren;
522struct PragmaMarkSymbol {
529void mergePragmas(DocumentSymbol &
Root, ArrayRef<PragmaMarkSymbol> Pragmas) {
530 while (!Pragmas.empty()) {
532 PragmaMarkSymbol P = std::move(Pragmas.front());
533 Pragmas = Pragmas.drop_front();
534 DocumentSymbol *Cur = &
Root;
535 while (Cur->range.contains(P.DocSym.range)) {
536 bool Swapped =
false;
537 for (
auto &
C : Cur->children) {
540 if (
C.range.contains(P.DocSym.range)) {
552 Cur->children.emplace_back(std::move(P.DocSym));
563 bool TerminatedByNextPragma =
false;
564 for (
auto &NextPragma : Pragmas) {
566 if (!Cur->range.contains(NextPragma.DocSym.range))
571 if (llvm::any_of(Cur->children, [&NextPragma](
const auto &Child) {
572 return Child.range.contains(NextPragma.DocSym.range);
577 auto It = llvm::partition(Cur->children,
578 [&P, &NextPragma](
const auto &S) ->
bool {
579 return !(P.DocSym.range < S.range &&
580 S.range < NextPragma.DocSym.range);
582 P.DocSym.children.assign(make_move_iterator(It),
583 make_move_iterator(Cur->children.end()));
584 Cur->children.erase(It, Cur->children.end());
585 TerminatedByNextPragma =
true;
588 if (!TerminatedByNextPragma) {
591 auto It = llvm::partition(Cur->children, [&P](
const auto &S) ->
bool {
592 return !(P.DocSym.range < S.range);
594 P.DocSym.children.assign(make_move_iterator(It),
595 make_move_iterator(Cur->children.end()));
596 Cur->children.erase(It, Cur->children.end());
599 for (DocumentSymbol &Sym : P.DocSym.children)
601 Cur->children.emplace_back(std::move(P.DocSym));
605PragmaMarkSymbol markToSymbol(
const PragmaMark &P) {
606 StringRef
Name = StringRef(P.Trivia).trim();
614 StringRef MaybeGroupName =
Name;
615 if (MaybeGroupName.consume_front(
"-") &&
616 (MaybeGroupName.ltrim() != MaybeGroupName || MaybeGroupName.empty())) {
617 Name = MaybeGroupName.empty() ?
"(unnamed group)" : MaybeGroupName.ltrim();
619 }
else if (
Name.empty()) {
620 Name =
"(unnamed mark)";
623 Sym.name =
Name.str();
624 Sym.kind = SymbolKind::File;
626 Sym.selectionRange = P.Rng;
630std::vector<DocumentSymbol> collectDocSymbols(ParsedAST &AST) {
631 std::vector<DocumentSymbol> Syms = DocumentOutline(AST).build();
633 const auto &PragmaMarks = AST.getMarks();
634 if (PragmaMarks.empty())
637 std::vector<PragmaMarkSymbol> Pragmas;
638 Pragmas.reserve(PragmaMarks.size());
639 for (
const auto &P : PragmaMarks)
640 Pragmas.push_back(markToSymbol(P));
643 {std::numeric_limits<int>::max(), std::numeric_limits<int>::max()}};
645 Root.children = std::move(Syms);
646 Root.range = EntireFile;
647 mergePragmas(
Root, llvm::ArrayRef(Pragmas));
648 return Root.children;
654 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.
llvm::raw_string_ostream OS
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.