clang-tools 23.0.0git
FindSymbols.cpp
Go to the documentation of this file.
1//===--- FindSymbols.cpp ------------------------------------*- 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#include "FindSymbols.h"
9
10#include "AST.h"
11#include "FuzzyMatch.h"
12#include "ParsedAST.h"
13#include "Quality.h"
14#include "SourceCode.h"
15#include "index/Index.h"
16#include "support/Logger.h"
17#include "clang/AST/DeclFriend.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/Index/IndexSymbol.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include <limits>
25#include <optional>
26
27#define DEBUG_TYPE "FindSymbols"
28
29namespace clang {
30namespace clangd {
31
32namespace {
33
34// "Static" means many things in C++, only some get the "static" modifier.
35//
36// Meanings that do:
37// - Members associated with the class rather than the instance.
38// This is what 'static' most often means across languages.
39// - static local variables
40// These are similarly "detached from their context" by the static keyword.
41// In practice, these are rarely used inside classes, reducing confusion.
42//
43// Meanings that don't:
44// - Namespace-scoped variables, which have static storage class.
45// This is implicit, so the keyword "static" isn't so strongly associated.
46// If we want a modifier for these, "global scope" is probably the concept.
47// - Namespace-scoped variables/functions explicitly marked "static".
48// There the keyword changes *linkage* , which is a totally different concept.
49// If we want to model this, "file scope" would be a nice modifier.
50//
51// This is confusing, and maybe we should use another name, but because "static"
52// is a standard LSP modifier, having one with that name has advantages.
53bool isStatic(const Decl *D) {
54 if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D))
55 return CMD->isStatic();
56 if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D))
57 return VD->isStaticDataMember() || VD->isStaticLocal();
58 if (const auto *OPD = llvm::dyn_cast<ObjCPropertyDecl>(D))
59 return OPD->isClassProperty();
60 if (const auto *OMD = llvm::dyn_cast<ObjCMethodDecl>(D))
61 return OMD->isClassMethod();
62 if (const auto *FD = llvm::dyn_cast<FunctionDecl>(D))
63 return FD->isStatic();
64 return false;
65}
66
67// Whether T is const in a loose sense - is a variable with this type readonly?
68bool isConst(QualType T) {
69 if (T.isNull())
70 return false;
71 T = T.getNonReferenceType();
72 if (T.isConstQualified())
73 return true;
74 if (const auto *AT = T->getAsArrayTypeUnsafe())
75 return isConst(AT->getElementType());
76 if (isConst(T->getPointeeType()))
77 return true;
78 return false;
79}
80
81// Whether D is const in a loose sense (should it be highlighted as such?)
82// FIXME: This is separate from whether *a particular usage* can mutate D.
83// We may want V in V.size() to be readonly even if V is mutable.
84bool isConst(const Decl *D) {
85 if (llvm::isa<EnumConstantDecl>(D) || llvm::isa<NonTypeTemplateParmDecl>(D))
86 return true;
87 if (llvm::isa<FieldDecl>(D) || llvm::isa<VarDecl>(D) ||
88 llvm::isa<MSPropertyDecl>(D) || llvm::isa<BindingDecl>(D)) {
89 if (isConst(llvm::cast<ValueDecl>(D)->getType()))
90 return true;
91 }
92 if (const auto *OCPD = llvm::dyn_cast<ObjCPropertyDecl>(D)) {
93 if (OCPD->isReadOnly())
94 return true;
95 }
96 if (const auto *MPD = llvm::dyn_cast<MSPropertyDecl>(D)) {
97 if (!MPD->hasSetter())
98 return true;
99 }
100 if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) {
101 if (CMD->isConst())
102 return true;
103 }
104 if (const auto *FD = llvm::dyn_cast<FunctionDecl>(D))
105 return isConst(FD->getReturnType());
106 return false;
107}
108
109// Indicates whether declaration D is abstract in cases where D is a struct or a
110// class.
111bool isAbstract(const Decl *D) {
112 if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D))
113 return CMD->isPureVirtual();
114 if (const auto *CRD = llvm::dyn_cast<CXXRecordDecl>(D))
115 return CRD->hasDefinition() && CRD->isAbstract();
116 return false;
117}
118
119// Indicates whether declaration D is virtual in cases where D is a method.
120bool isVirtual(const Decl *D) {
121 if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D))
122 return CMD->isVirtual();
123 return false;
124}
125
126// Indicates whether declaration D is final in cases where D is a struct, class
127// or method.
128bool isFinal(const Decl *D) {
129 if (const auto *CRD = dyn_cast<CXXMethodDecl>(D))
130 return CRD->hasAttr<FinalAttr>();
131
132 if (const auto *CRD = dyn_cast<CXXRecordDecl>(D))
133 return CRD->hasAttr<FinalAttr>();
134
135 return false;
136}
137
138// Indicates whether declaration D is a unique definition (as opposed to a
139// declaration).
140bool isUniqueDefinition(const NamedDecl *Decl) {
141 if (auto *Func = dyn_cast<FunctionDecl>(Decl))
142 return Func->isThisDeclarationADefinition();
143 if (auto *Klass = dyn_cast<CXXRecordDecl>(Decl))
144 return Klass->isThisDeclarationADefinition();
145 if (auto *Iface = dyn_cast<ObjCInterfaceDecl>(Decl))
146 return Iface->isThisDeclarationADefinition();
147 if (auto *Proto = dyn_cast<ObjCProtocolDecl>(Decl))
148 return Proto->isThisDeclarationADefinition();
149 if (auto *Var = dyn_cast<VarDecl>(Decl))
150 return Var->isThisDeclarationADefinition();
151 return isa<TemplateTypeParmDecl>(Decl) ||
152 isa<NonTypeTemplateParmDecl>(Decl) ||
153 isa<TemplateTemplateParmDecl>(Decl) || isa<ObjCCategoryDecl>(Decl) ||
154 isa<ObjCImplDecl>(Decl);
155}
156} // namespace
157
159 return (1 << static_cast<unsigned>(ST));
160}
161
162SymbolTags computeSymbolTags(const NamedDecl &ND) {
163 SymbolTags Result = 0;
164 const auto IsDef = isUniqueDefinition(&ND);
165
166 if (ND.isDeprecated())
168
169 if (isConst(&ND))
171
172 if (isStatic(&ND))
174
175 if (isVirtual(&ND))
177
178 if (isAbstract(&ND))
180
181 if (isFinal(&ND))
183
184 if (not isa<UnresolvedUsingValueDecl>(ND)) {
185 // Do not treat an UnresolvedUsingValueDecl as a declaration.
186 // It's more common to think of it as a reference to the
187 // underlying declaration.
189
190 if (IsDef)
192 }
193
194 switch (ND.getAccess()) {
195 case AS_public:
197 break;
198 case AS_protected:
200 break;
201 case AS_private:
203 break;
204 default:
205 break;
206 }
207
208 return Result;
209}
210
211std::vector<SymbolTag> getSymbolTags(const NamedDecl &ND) {
212 const auto symbolTags = computeSymbolTags(ND);
213 std::vector<SymbolTag> Tags;
214
215 if (symbolTags == 0)
216 return Tags;
217
218 // Iterate through SymbolTag enum values and collect any that are present in
219 // the bitmask. SymbolTag values are in the numeric range
220 // [FirstTag .. LastTag].
221 constexpr unsigned MinTag = static_cast<unsigned>(SymbolTag::FirstTag);
222 constexpr unsigned MaxTag = static_cast<unsigned>(SymbolTag::LastTag);
223 for (unsigned I = MinTag; I <= MaxTag; ++I) {
224 auto ST = static_cast<SymbolTag>(I);
225 if (symbolTags & toSymbolTagBitmask(ST))
226 Tags.push_back(ST);
227 }
228 return Tags;
229}
230
231namespace {
232using ScoredSymbolInfo = std::pair<float, SymbolInformation>;
233struct ScoredSymbolGreater {
234 bool operator()(const ScoredSymbolInfo &L, const ScoredSymbolInfo &R) {
235 if (L.first != R.first)
236 return L.first > R.first;
237 return L.second.name < R.second.name; // Earlier name is better.
238 }
239};
240
241// Returns true if \p Query can be found as a sub-sequence inside \p Scope.
242bool approximateScopeMatch(llvm::StringRef Scope, llvm::StringRef Query) {
243 assert(Scope.empty() || Scope.ends_with("::"));
244 assert(Query.empty() || Query.ends_with("::"));
245 while (!Scope.empty() && !Query.empty()) {
246 auto Colons = Scope.find("::");
247 assert(Colons != llvm::StringRef::npos);
248
249 llvm::StringRef LeadingSpecifier = Scope.slice(0, Colons + 2);
250 Scope = Scope.slice(Colons + 2, llvm::StringRef::npos);
251 Query.consume_front(LeadingSpecifier);
252 }
253 return Query.empty();
254}
255
256} // namespace
257
258llvm::Expected<Location> indexToLSPLocation(const SymbolLocation &Loc,
259 llvm::StringRef TUPath) {
260 auto Path = URI::resolve(Loc.FileURI, TUPath);
261 if (!Path)
262 return error("Could not resolve path for file '{0}': {1}", Loc.FileURI,
263 Path.takeError());
264 Location L;
265 L.uri = URIForFile::canonicalize(*Path, TUPath);
266 Position Start, End;
267 Start.line = Loc.Start.line();
268 Start.character = Loc.Start.column();
269 End.line = Loc.End.line();
270 End.character = Loc.End.column();
271 L.range = {Start, End};
272 return L;
273}
274
275llvm::Expected<Location> symbolToLocation(const Symbol &Sym,
276 llvm::StringRef TUPath) {
277 // Prefer the definition over e.g. a function declaration in a header
278 return indexToLSPLocation(
279 Sym.Definition ? Sym.Definition : Sym.CanonicalDeclaration, TUPath);
280}
281
282llvm::Expected<std::vector<SymbolInformation>>
283getWorkspaceSymbols(llvm::StringRef Query, int Limit,
284 const SymbolIndex *const Index, llvm::StringRef HintPath) {
285 std::vector<SymbolInformation> Result;
286 if (!Index)
287 return Result;
288
289 // Lookup for qualified names are performed as:
290 // - Exact namespaces are boosted by the index.
291 // - Approximate matches are (sub-scope match) included via AnyScope logic.
292 // - Non-matching namespaces (no sub-scope match) are post-filtered.
293 auto Names = splitQualifiedName(Query);
294
296 Req.Query = std::string(Names.second);
297
298 // FuzzyFind doesn't want leading :: qualifier.
299 auto HasLeadingColons = Names.first.consume_front("::");
300 // Limit the query to specific namespace if it is fully-qualified.
301 Req.AnyScope = !HasLeadingColons;
302 // Boost symbols from desired namespace.
303 if (HasLeadingColons || !Names.first.empty())
304 Req.Scopes = {std::string(Names.first)};
305 if (Limit) {
306 Req.Limit = Limit;
307 // If we are boosting a specific scope allow more results to be retrieved,
308 // since some symbols from preferred namespaces might not make the cut.
309 if (Req.AnyScope && !Req.Scopes.empty())
310 *Req.Limit *= 5;
311 }
313 Req.Limit.value_or(std::numeric_limits<size_t>::max()));
314 FuzzyMatcher Filter(Req.Query);
315
316 Index->fuzzyFind(Req, [HintPath, &Top, &Filter, AnyScope = Req.AnyScope,
317 ReqScope = Names.first](const Symbol &Sym) {
318 llvm::StringRef Scope = Sym.Scope;
319 // Fuzzyfind might return symbols from irrelevant namespaces if query was
320 // not fully-qualified, drop those.
321 if (AnyScope && !approximateScopeMatch(Scope, ReqScope))
322 return;
323
324 auto Loc = symbolToLocation(Sym, HintPath);
325 if (!Loc) {
326 log("Workspace symbols: {0}", Loc.takeError());
327 return;
328 }
329
330 SymbolQualitySignals Quality;
331 Quality.merge(Sym);
332 SymbolRelevanceSignals Relevance;
333 Relevance.Name = Sym.Name;
334 Relevance.Query = SymbolRelevanceSignals::Generic;
335 // If symbol and request scopes do not match exactly, apply a penalty.
336 Relevance.InBaseClass = AnyScope && Scope != ReqScope;
337 if (auto NameMatch = Filter.match(Sym.Name))
338 Relevance.NameMatch = *NameMatch;
339 else {
340 log("Workspace symbol: {0} didn't match query {1}", Sym.Name,
341 Filter.pattern());
342 return;
343 }
344 Relevance.merge(Sym);
345 auto QualScore = Quality.evaluateHeuristics();
346 auto RelScore = Relevance.evaluateHeuristics();
347 auto Score = evaluateSymbolAndRelevance(QualScore, RelScore);
348 dlog("FindSymbols: {0}{1} = {2}\n{3}{4}\n", Sym.Scope, Sym.Name, Score,
349 Quality, Relevance);
350
352 Info.name = (Sym.Name + Sym.TemplateSpecializationArgs).str();
354 Info.location = *Loc;
355 Scope.consume_back("::");
356 Info.containerName = Scope.str();
357
358 // Exposed score excludes fuzzy-match component, for client-side re-ranking.
359 Info.score = Relevance.NameMatch > std::numeric_limits<float>::epsilon()
360 ? Score / Relevance.NameMatch
361 : QualScore;
362 Top.push({Score, std::move(Info)});
363 });
364 for (auto &R : std::move(Top).items())
365 Result.push_back(std::move(R.second));
366 return Result;
367}
368
369namespace {
370std::string getSymbolName(ASTContext &Ctx, const NamedDecl &ND) {
371 // Print `MyClass(Category)` instead of `Category` and `MyClass()` instead
372 // of `anonymous`.
373 if (const auto *Container = dyn_cast<ObjCContainerDecl>(&ND))
374 return printObjCContainer(*Container);
375 // Differentiate between class and instance methods: print `-foo` instead of
376 // `foo` and `+sharedInstance` instead of `sharedInstance`.
377 if (const auto *Method = dyn_cast<ObjCMethodDecl>(&ND)) {
378 std::string Name;
379 llvm::raw_string_ostream OS(Name);
380
381 OS << (Method->isInstanceMethod() ? '-' : '+');
382 Method->getSelector().print(OS);
383
384 return Name;
385 }
386 return printName(Ctx, ND);
387}
388
389std::string getSymbolDetail(ASTContext &Ctx, const NamedDecl &ND) {
390 PrintingPolicy P(Ctx.getPrintingPolicy());
391 P.SuppressScope = true;
392 P.SuppressUnwrittenScope = true;
393 P.AnonymousTagNameStyle =
394 llvm::to_underlying(PrintingPolicy::AnonymousTagMode::Plain);
395 P.PolishForDeclaration = true;
396 std::string Detail;
397 llvm::raw_string_ostream OS(Detail);
398 if (ND.getDescribedTemplateParams()) {
399 OS << "template ";
400 }
401 if (const auto *VD = dyn_cast<ValueDecl>(&ND)) {
402 // FIXME: better printing for dependent type
403 if (isa<CXXConstructorDecl>(VD)) {
404 std::string ConstructorType = VD->getType().getAsString(P);
405 // Print constructor type as "(int)" instead of "void (int)".
406 llvm::StringRef WithoutVoid = ConstructorType;
407 WithoutVoid.consume_front("void ");
408 OS << WithoutVoid;
409 } else if (!isa<CXXDestructorDecl>(VD)) {
410 VD->getType().print(OS, P);
411 }
412 } else if (const auto *TD = dyn_cast<TagDecl>(&ND)) {
413 OS << TD->getKindName();
414 } else if (isa<TypedefNameDecl>(&ND)) {
415 OS << "type alias";
416 } else if (isa<ConceptDecl>(&ND)) {
417 OS << "concept";
418 }
419 return std::move(OS.str());
420}
421
422std::optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) {
423 auto &SM = Ctx.getSourceManager();
424
425 SourceLocation BeginLoc = ND.getBeginLoc();
426 SourceLocation EndLoc = ND.getEndLoc();
427 const auto SymbolRange =
428 toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
429 if (!SymbolRange)
430 return std::nullopt;
431
432 index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
433 // FIXME: This is not classifying constructors, destructors and operators
434 // correctly.
436
437 DocumentSymbol SI;
438 SI.name = getSymbolName(Ctx, ND);
439 SI.kind = SK;
440 SI.deprecated = ND.isDeprecated();
441 SI.range = Range{sourceLocToPosition(SM, SymbolRange->getBegin()),
442 sourceLocToPosition(SM, SymbolRange->getEnd())};
443 SI.detail = getSymbolDetail(Ctx, ND);
444 SI.tags = getSymbolTags(ND);
445
446 SourceLocation NameLoc = ND.getLocation();
447 SourceLocation FallbackNameLoc;
448 if (NameLoc.isMacroID()) {
449 if (isSpelledInSource(NameLoc, SM)) {
450 // Prefer the spelling loc, but save the expansion loc as a fallback.
451 FallbackNameLoc = SM.getExpansionLoc(NameLoc);
452 NameLoc = SM.getSpellingLoc(NameLoc);
453 } else {
454 NameLoc = SM.getExpansionLoc(NameLoc);
455 }
456 }
457 auto ComputeSelectionRange = [&](SourceLocation L) -> Range {
458 Position NameBegin = sourceLocToPosition(SM, L);
459 Position NameEnd = sourceLocToPosition(
460 SM, Lexer::getLocForEndOfToken(L, 0, SM, Ctx.getLangOpts()));
461 return Range{NameBegin, NameEnd};
462 };
463
464 SI.selectionRange = ComputeSelectionRange(NameLoc);
465 if (!SI.range.contains(SI.selectionRange) && FallbackNameLoc.isValid()) {
466 // 'selectionRange' must be contained in 'range'. In cases where clang
467 // reports unrelated ranges, we first try falling back to the expansion
468 // loc for the selection range.
469 SI.selectionRange = ComputeSelectionRange(FallbackNameLoc);
470 }
471 if (!SI.range.contains(SI.selectionRange)) {
472 // If the containment relationship still doesn't hold, throw away
473 // 'range' and use 'selectionRange' for both.
474 SI.range = SI.selectionRange;
475 }
476 return SI;
477}
478
479/// A helper class to build an outline for the parse AST. It traverses the AST
480/// directly instead of using RecursiveASTVisitor (RAV) for three main reasons:
481/// - there is no way to keep RAV from traversing subtrees we are not
482/// interested in. E.g. not traversing function locals or implicit template
483/// instantiations.
484/// - it's easier to combine results of recursive passes,
485/// - visiting decls is actually simple, so we don't hit the complicated
486/// cases that RAV mostly helps with (types, expressions, etc.)
487class DocumentOutline {
488 // A DocumentSymbol we're constructing.
489 // We use this instead of DocumentSymbol directly so that we can keep track
490 // of the nodes we insert for macros.
491 class SymBuilder {
492 std::vector<SymBuilder> Children;
493 DocumentSymbol Symbol; // Symbol.children is empty, use Children instead.
494 // Macro expansions that this node or its parents are associated with.
495 // (Thus we will never create further children for these expansions).
496 llvm::SmallVector<SourceLocation> EnclosingMacroLoc;
497
498 public:
499 DocumentSymbol build() && {
500 for (SymBuilder &C : Children) {
501 Symbol.children.push_back(std::move(C).build());
502 // Expand range to ensure children nest properly, which editors expect.
503 // This can fix some edge-cases in the AST, but is vital for macros.
504 // A macro expansion "contains" AST node if it covers the node's primary
505 // location, but it may not span the node's whole range.
506 Symbol.range.start =
507 std::min(Symbol.range.start, Symbol.children.back().range.start);
508 Symbol.range.end =
509 std::max(Symbol.range.end, Symbol.children.back().range.end);
510 }
511 return std::move(Symbol);
512 }
513
514 // Add a symbol as a child of the current one.
515 SymBuilder &addChild(DocumentSymbol S) {
516 Children.emplace_back();
517 Children.back().EnclosingMacroLoc = EnclosingMacroLoc;
518 Children.back().Symbol = std::move(S);
519 return Children.back();
520 }
521
522 // Get an appropriate container for children of this symbol that were
523 // expanded from a macro (whose spelled name is Tok).
524 //
525 // This may return:
526 // - a macro symbol child of this (either new or previously created)
527 // - this scope itself, if it *is* the macro symbol or is nested within it
528 SymBuilder &inMacro(const syntax::Token &Tok, const SourceManager &SM,
529 std::optional<syntax::TokenBuffer::Expansion> Exp) {
530 if (llvm::is_contained(EnclosingMacroLoc, Tok.location()))
531 return *this;
532 // If there's an existing child for this macro, we expect it to be last.
533 if (!Children.empty() && !Children.back().EnclosingMacroLoc.empty() &&
534 Children.back().EnclosingMacroLoc.back() == Tok.location())
535 return Children.back();
536
537 DocumentSymbol Sym;
538 Sym.name = Tok.text(SM).str();
539 Sym.kind = SymbolKind::Null; // There's no suitable kind!
540 Sym.range = Sym.selectionRange =
541 halfOpenToRange(SM, Tok.range(SM).toCharRange(SM));
542
543 // FIXME: Exp is currently unavailable for nested expansions.
544 if (Exp) {
545 // Full range covers the macro args.
546 Sym.range = halfOpenToRange(SM, CharSourceRange::getCharRange(
547 Exp->Spelled.front().location(),
548 Exp->Spelled.back().endLocation()));
549 // Show macro args as detail.
550 llvm::raw_string_ostream OS(Sym.detail);
551 const syntax::Token *Prev = nullptr;
552 for (const auto &Tok : Exp->Spelled.drop_front()) {
553 // Don't dump arbitrarily long macro args.
554 if (OS.tell() > 80) {
555 OS << " ...)";
556 break;
557 }
558 if (Prev && Prev->endLocation() != Tok.location())
559 OS << ' ';
560 OS << Tok.text(SM);
561 Prev = &Tok;
562 }
563 }
564 SymBuilder &Child = addChild(std::move(Sym));
565 Child.EnclosingMacroLoc.push_back(Tok.location());
566 return Child;
567 }
568 };
569
570public:
571 DocumentOutline(ParsedAST &AST) : AST(AST) {}
572
573 /// Builds the document outline for the generated AST.
574 std::vector<DocumentSymbol> build() {
575 SymBuilder Root;
576 for (auto &TopLevel : AST.getLocalTopLevelDecls())
577 traverseDecl(TopLevel, Root);
578 return std::move(std::move(Root).build().children);
579 }
580
581private:
582 enum class VisitKind { No, OnlyDecl, OnlyChildren, DeclAndChildren };
583
584 void traverseDecl(Decl *D, SymBuilder &Parent) {
585 // Skip symbols which do not originate from the main file.
586 if (!isInsideMainFile(D->getLocation(), AST.getSourceManager()))
587 return;
588
589 if (auto *Templ = llvm::dyn_cast<TemplateDecl>(D)) {
590 // TemplatedDecl might be null, e.g. concepts.
591 if (auto *TD = Templ->getTemplatedDecl())
592 D = TD;
593 }
594
595 // FriendDecls don't act as DeclContexts, but they might wrap a function
596 // definition that won't be visible through other means in the AST. Hence
597 // unwrap it here instead.
598 if (auto *Friend = llvm::dyn_cast<FriendDecl>(D)) {
599 if (auto *Func =
600 llvm::dyn_cast_or_null<FunctionDecl>(Friend->getFriendDecl())) {
601 if (Func->isThisDeclarationADefinition())
602 D = Func;
603 }
604 }
605
606 VisitKind Visit = shouldVisit(D);
607 if (Visit == VisitKind::No)
608 return;
609
610 if (Visit == VisitKind::OnlyChildren)
611 return traverseChildren(D, Parent);
612
613 auto *ND = llvm::cast<NamedDecl>(D);
614 auto Sym = declToSym(AST.getASTContext(), *ND);
615 if (!Sym)
616 return;
617 SymBuilder &MacroParent = possibleMacroContainer(D->getLocation(), Parent);
618 SymBuilder &Child = MacroParent.addChild(std::move(*Sym));
619
620 if (Visit == VisitKind::OnlyDecl)
621 return;
622
623 assert(Visit == VisitKind::DeclAndChildren && "Unexpected VisitKind");
624 traverseChildren(ND, Child);
625 }
626
627 // Determines where a decl should appear in the DocumentSymbol hierarchy.
628 //
629 // This is usually a direct child of the relevant AST parent.
630 // But we may also insert nodes for macros. Given:
631 // #define DECLARE_INT(V) int v;
632 // namespace a { DECLARE_INT(x) }
633 // We produce:
634 // Namespace a
635 // Macro DECLARE_INT(x)
636 // Variable x
637 //
638 // In the absence of macros, this method simply returns Parent.
639 // Otherwise it may return a macro expansion node instead.
640 // Each macro only has at most one node in the hierarchy, even if it expands
641 // to multiple decls.
642 SymBuilder &possibleMacroContainer(SourceLocation TargetLoc,
643 SymBuilder &Parent) {
644 const auto &SM = AST.getSourceManager();
645 // Look at the path of macro-callers from the token to the main file.
646 // Note that along these paths we see the "outer" macro calls first.
647 SymBuilder *CurParent = &Parent;
648 for (SourceLocation Loc = TargetLoc; Loc.isMacroID();
649 Loc = SM.getImmediateMacroCallerLoc(Loc)) {
650 // Find the virtual macro body that our token is being substituted into.
651 FileID MacroBody;
652 if (SM.isMacroArgExpansion(Loc)) {
653 // Loc is part of a macro arg being substituted into a macro body.
654 MacroBody = SM.getFileID(SM.getImmediateExpansionRange(Loc).getBegin());
655 } else {
656 // Loc is already in the macro body.
657 MacroBody = SM.getFileID(Loc);
658 }
659 // The macro body is being substituted for a macro expansion, whose
660 // first token is the name of the macro.
661 SourceLocation MacroName =
662 SM.getSLocEntry(MacroBody).getExpansion().getExpansionLocStart();
663 // Only include the macro expansion in the outline if it was written
664 // directly in the main file, rather than expanded from another macro.
665 if (!MacroName.isValid() || !MacroName.isFileID())
666 continue;
667 // All conditions satisfied, add the macro.
668 if (auto *Tok = AST.getTokens().spelledTokenContaining(MacroName))
669 CurParent = &CurParent->inMacro(
670 *Tok, SM, AST.getTokens().expansionStartingAt(Tok));
671 }
672 return *CurParent;
673 }
674
675 void traverseChildren(Decl *D, SymBuilder &Builder) {
676 auto *Scope = llvm::dyn_cast<DeclContext>(D);
677 if (!Scope)
678 return;
679 for (auto *C : Scope->decls())
680 traverseDecl(C, Builder);
681 }
682
683 VisitKind shouldVisit(Decl *D) {
684 if (D->isImplicit())
685 return VisitKind::No;
686
687 if (llvm::isa<LinkageSpecDecl>(D) || llvm::isa<ExportDecl>(D))
688 return VisitKind::OnlyChildren;
689
690 if (!llvm::isa<NamedDecl>(D))
691 return VisitKind::No;
692
693 if (auto *Func = llvm::dyn_cast<FunctionDecl>(D)) {
694 // Some functions are implicit template instantiations, those should be
695 // ignored.
696 if (auto *Info = Func->getTemplateSpecializationInfo()) {
697 if (!Info->isExplicitInstantiationOrSpecialization())
698 return VisitKind::No;
699 }
700 // Only visit the function itself, do not visit the children (i.e.
701 // function parameters, etc.)
702 return VisitKind::OnlyDecl;
703 }
704 // Handle template instantiations. We have three cases to consider:
705 // - explicit instantiations, e.g. 'template class std::vector<int>;'
706 // Visit the decl itself (it's present in the code), but not the
707 // children.
708 // - implicit instantiations, i.e. not written by the user.
709 // Do not visit at all, they are not present in the code.
710 // - explicit specialization, e.g. 'template <> class vector<bool> {};'
711 // Visit both the decl and its children, both are written in the code.
712 if (auto *TemplSpec = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
713 if (TemplSpec->isExplicitInstantiationOrSpecialization())
714 return TemplSpec->isExplicitSpecialization()
715 ? VisitKind::DeclAndChildren
716 : VisitKind::OnlyDecl;
717 return VisitKind::No;
718 }
719 if (auto *TemplSpec = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
720 if (TemplSpec->isExplicitInstantiationOrSpecialization())
721 return TemplSpec->isExplicitSpecialization()
722 ? VisitKind::DeclAndChildren
723 : VisitKind::OnlyDecl;
724 return VisitKind::No;
725 }
726 // For all other cases, visit both the children and the decl.
727 return VisitKind::DeclAndChildren;
728 }
729
730 ParsedAST &AST;
731};
732
733struct PragmaMarkSymbol {
734 DocumentSymbol DocSym;
735 bool IsGroup;
736};
737
738/// Merge in `PragmaMarkSymbols`, sorted ascending by range, into the given
739/// `DocumentSymbol` tree.
740void mergePragmas(DocumentSymbol &Root, ArrayRef<PragmaMarkSymbol> Pragmas) {
741 while (!Pragmas.empty()) {
742 // We'll figure out where the Pragmas.front() should go.
743 PragmaMarkSymbol P = std::move(Pragmas.front());
744 Pragmas = Pragmas.drop_front();
745 DocumentSymbol *Cur = &Root;
746 while (Cur->range.contains(P.DocSym.range)) {
747 bool Swapped = false;
748 for (auto &C : Cur->children) {
749 // We assume at most 1 child can contain the pragma (as pragmas are on
750 // a single line, and children have disjoint ranges).
751 if (C.range.contains(P.DocSym.range)) {
752 Cur = &C;
753 Swapped = true;
754 break;
755 }
756 }
757 // Cur is the parent of P since none of the children contain P.
758 if (!Swapped)
759 break;
760 }
761 // Pragma isn't a group so we can just insert it and we are done.
762 if (!P.IsGroup) {
763 Cur->children.emplace_back(std::move(P.DocSym));
764 continue;
765 }
766 // Pragma is a group, so we need to figure out where it terminates:
767 // - If the next Pragma is not contained in Cur, P owns all of its
768 // parent's children which occur after P.
769 // - If the next pragma is contained in Cur but actually belongs to one
770 // of the parent's children, we temporarily skip over it and look at
771 // the next pragma to decide where we end.
772 // - Otherwise nest all of its parent's children which occur after P but
773 // before the next pragma.
774 bool TerminatedByNextPragma = false;
775 for (auto &NextPragma : Pragmas) {
776 // If we hit a pragma outside of Cur, the rest will be outside as well.
777 if (!Cur->range.contains(NextPragma.DocSym.range))
778 break;
779
780 // NextPragma cannot terminate P if it is nested inside a child, look for
781 // the next one.
782 if (llvm::any_of(Cur->children, [&NextPragma](const auto &Child) {
783 return Child.range.contains(NextPragma.DocSym.range);
784 }))
785 continue;
786
787 // Pragma owns all the children between P and NextPragma
788 auto It = llvm::partition(Cur->children,
789 [&P, &NextPragma](const auto &S) -> bool {
790 return !(P.DocSym.range < S.range &&
791 S.range < NextPragma.DocSym.range);
792 });
793 P.DocSym.children.assign(make_move_iterator(It),
794 make_move_iterator(Cur->children.end()));
795 Cur->children.erase(It, Cur->children.end());
796 TerminatedByNextPragma = true;
797 break;
798 }
799 if (!TerminatedByNextPragma) {
800 // P is terminated by the end of current symbol, hence it owns all the
801 // children after P.
802 auto It = llvm::partition(Cur->children, [&P](const auto &S) -> bool {
803 return !(P.DocSym.range < S.range);
804 });
805 P.DocSym.children.assign(make_move_iterator(It),
806 make_move_iterator(Cur->children.end()));
807 Cur->children.erase(It, Cur->children.end());
808 }
809 // Update the range for P to cover children and append to Cur.
810 for (DocumentSymbol &Sym : P.DocSym.children)
811 unionRanges(P.DocSym.range, Sym.range);
812 Cur->children.emplace_back(std::move(P.DocSym));
813 }
814}
815
816PragmaMarkSymbol markToSymbol(const PragmaMark &P) {
817 StringRef Name = StringRef(P.Trivia).trim();
818 bool IsGroup = false;
819 // "-\s+<group name>" or "<name>" after an initial trim. The former is
820 // considered a group, the latter just a mark. Like Xcode, we don't consider
821 // `-Foo` to be a group (space(s) after the `-` is required).
822 //
823 // We need to include a name here, otherwise editors won't properly render the
824 // symbol.
825 StringRef MaybeGroupName = Name;
826 if (MaybeGroupName.consume_front("-") &&
827 (MaybeGroupName.ltrim() != MaybeGroupName || MaybeGroupName.empty())) {
828 Name = MaybeGroupName.empty() ? "(unnamed group)" : MaybeGroupName.ltrim();
829 IsGroup = true;
830 } else if (Name.empty()) {
831 Name = "(unnamed mark)";
832 }
833 DocumentSymbol Sym;
834 Sym.name = Name.str();
835 Sym.kind = SymbolKind::File;
836 Sym.range = P.Rng;
837 Sym.selectionRange = P.Rng;
838 return {Sym, IsGroup};
839}
840
841std::vector<DocumentSymbol> collectDocSymbols(ParsedAST &AST) {
842 std::vector<DocumentSymbol> Syms = DocumentOutline(AST).build();
843
844 const auto &PragmaMarks = AST.getMarks();
845 if (PragmaMarks.empty())
846 return Syms;
847
848 std::vector<PragmaMarkSymbol> Pragmas;
849 Pragmas.reserve(PragmaMarks.size());
850 for (const auto &P : PragmaMarks)
851 Pragmas.push_back(markToSymbol(P));
852 Range EntireFile = {
853 {0, 0},
854 {std::numeric_limits<int>::max(), std::numeric_limits<int>::max()}};
855 DocumentSymbol Root;
856 Root.children = std::move(Syms);
857 Root.range = EntireFile;
858 mergePragmas(Root, llvm::ArrayRef(Pragmas));
859 return Root.children;
860}
861
862} // namespace
863
864llvm::Expected<std::vector<DocumentSymbol>> getDocumentSymbols(ParsedAST &AST) {
865 return collectDocSymbols(AST);
866}
867
868} // namespace clangd
869} // namespace clang
#define dlog(...)
Definition Logger.h:101
clang::find_all_symbols::SymbolInfo::SymbolKind SymbolKind
std::optional< float > match(llvm::StringRef Word)
Stores and provides access to parsed AST.
Definition ParsedAST.h:46
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition Index.h:134
virtual bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning.
TopN<T> is a lossy container that preserves only the "best" N elements.
Definition Quality.h:189
bool push(value_type &&V)
Definition Quality.h:197
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
Resolves the absolute path of U.
Definition URI.cpp:244
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
llvm::Expected< Location > indexToLSPLocation(const SymbolLocation &Loc, llvm::StringRef TUPath)
Ensure we have enough bits to represent all SymbolTag values.
@ Info
An information message.
Definition Protocol.h:738
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.
uint32_t SymbolTags
A bitmask type representing symbol tags supported by LSP.
Definition FindSymbols.h:28
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.
Definition AST.cpp:248
SymbolTag
Symbol tags are extra annotations that can be attached to a symbol.
Definition Protocol.h:1109
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)
Definition Logger.h:79
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
SymbolTags toSymbolTagBitmask(const SymbolTag ST)
Converts a single SymbolTag to a bitmask.
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...
@ No
Diagnostics must be generated for this snapshot.
Definition TUScheduler.h:55
std::string Path
A typedef to represent a file path.
Definition Path.h:26
std::vector< SymbolTag > getSymbolTags(const NamedDecl &ND)
Returns the symbol tags for the given declaration.
llvm::Expected< std::vector< SymbolInformation > > getWorkspaceSymbols(llvm::StringRef Query, int Limit, const SymbolIndex *const Index, llvm::StringRef HintPath)
Searches for the symbols matching Query.
SymbolKind indexSymbolKindToSymbolKind(const index::SymbolInfo &Info)
Definition Protocol.cpp:298
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.
Definition Quality.cpp:534
SymbolTags computeSymbolTags(const NamedDecl &ND)
Computes symbol tags for a given NamedDecl.
void addChild(NamespaceInfo *I, FunctionInfo &&R)
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccessCheck P
===– 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.
Definition Index.h:36
std::string Query
A query string for the fuzzy find.
Definition Index.h:29
bool AnyScope
If set to true, allow symbols from any scope.
Definition Index.h:39
std::optional< uint32_t > Limit
The number of top candidates to return.
Definition Index.h:42
URIForFile uri
The text document's URI.
Definition Protocol.h:213
int line
Line position in a document (zero-based).
Definition Protocol.h:158
int character
Character offset on a line in a document (zero-based).
Definition Protocol.h:163
Represents information about programming constructs like variables, classes, interfaces etc.
Definition Protocol.h:1174
Position Start
The symbol range, using half-open range [Start, End).
Attributes of a symbol that affect how much we like it.
Definition Quality.h:56
void merge(const CodeCompletionResult &SemaCCResult)
Definition Quality.cpp:178
Attributes of a symbol-query pair that affect how much we like it.
Definition Quality.h:86
llvm::StringRef Name
The name of the symbol (for ContextWords). Must be explicitly assigned.
Definition Quality.h:88
The class presents a C++ symbol, e.g.
Definition Symbol.h:39
SymbolLocation Definition
The location of the symbol's definition, if one was found.
Definition Symbol.h:50
index::SymbolInfo SymInfo
The symbol information, like symbol kind.
Definition Symbol.h:43
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition Symbol.h:45
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
Definition Symbol.h:47
llvm::StringRef TemplateSpecializationArgs
Argument list in human-readable format, will be displayed to help disambiguate between different spec...
Definition Symbol.h:72
SymbolLocation CanonicalDeclaration
The location of the preferred declaration of the symbol.
Definition Symbol.h:59
static URIForFile canonicalize(llvm::StringRef AbsPath, llvm::StringRef TUPath)
Canonicalizes AbsPath via URI.
Definition Protocol.cpp:46