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.AnonymousTagLocations = false;
394 P.PolishForDeclaration = true;
395 std::string Detail;
396 llvm::raw_string_ostream OS(Detail);
397 if (ND.getDescribedTemplateParams()) {
398 OS << "template ";
399 }
400 if (const auto *VD = dyn_cast<ValueDecl>(&ND)) {
401 // FIXME: better printing for dependent type
402 if (isa<CXXConstructorDecl>(VD)) {
403 std::string ConstructorType = VD->getType().getAsString(P);
404 // Print constructor type as "(int)" instead of "void (int)".
405 llvm::StringRef WithoutVoid = ConstructorType;
406 WithoutVoid.consume_front("void ");
407 OS << WithoutVoid;
408 } else if (!isa<CXXDestructorDecl>(VD)) {
409 VD->getType().print(OS, P);
410 }
411 } else if (const auto *TD = dyn_cast<TagDecl>(&ND)) {
412 OS << TD->getKindName();
413 } else if (isa<TypedefNameDecl>(&ND)) {
414 OS << "type alias";
415 } else if (isa<ConceptDecl>(&ND)) {
416 OS << "concept";
417 }
418 return std::move(OS.str());
419}
420
421std::optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) {
422 auto &SM = Ctx.getSourceManager();
423
424 SourceLocation BeginLoc = ND.getBeginLoc();
425 SourceLocation EndLoc = ND.getEndLoc();
426 const auto SymbolRange =
427 toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
428 if (!SymbolRange)
429 return std::nullopt;
430
431 index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
432 // FIXME: This is not classifying constructors, destructors and operators
433 // correctly.
434 SymbolKind SK = indexSymbolKindToSymbolKind(SymInfo.Kind);
435
436 DocumentSymbol SI;
437 SI.name = getSymbolName(Ctx, ND);
438 SI.kind = SK;
439 SI.deprecated = ND.isDeprecated();
440 SI.range = Range{sourceLocToPosition(SM, SymbolRange->getBegin()),
441 sourceLocToPosition(SM, SymbolRange->getEnd())};
442 SI.detail = getSymbolDetail(Ctx, ND);
443 SI.tags = getSymbolTags(ND);
444
445 SourceLocation NameLoc = ND.getLocation();
446 SourceLocation FallbackNameLoc;
447 if (NameLoc.isMacroID()) {
448 if (isSpelledInSource(NameLoc, SM)) {
449 // Prefer the spelling loc, but save the expansion loc as a fallback.
450 FallbackNameLoc = SM.getExpansionLoc(NameLoc);
451 NameLoc = SM.getSpellingLoc(NameLoc);
452 } else {
453 NameLoc = SM.getExpansionLoc(NameLoc);
454 }
455 }
456 auto ComputeSelectionRange = [&](SourceLocation L) -> Range {
457 Position NameBegin = sourceLocToPosition(SM, L);
458 Position NameEnd = sourceLocToPosition(
459 SM, Lexer::getLocForEndOfToken(L, 0, SM, Ctx.getLangOpts()));
460 return Range{NameBegin, NameEnd};
461 };
462
463 SI.selectionRange = ComputeSelectionRange(NameLoc);
464 if (!SI.range.contains(SI.selectionRange) && FallbackNameLoc.isValid()) {
465 // 'selectionRange' must be contained in 'range'. In cases where clang
466 // reports unrelated ranges, we first try falling back to the expansion
467 // loc for the selection range.
468 SI.selectionRange = ComputeSelectionRange(FallbackNameLoc);
469 }
470 if (!SI.range.contains(SI.selectionRange)) {
471 // If the containment relationship still doesn't hold, throw away
472 // 'range' and use 'selectionRange' for both.
473 SI.range = SI.selectionRange;
474 }
475 return SI;
476}
477
478/// A helper class to build an outline for the parse AST. It traverses the AST
479/// directly instead of using RecursiveASTVisitor (RAV) for three main reasons:
480/// - there is no way to keep RAV from traversing subtrees we are not
481/// interested in. E.g. not traversing function locals or implicit template
482/// instantiations.
483/// - it's easier to combine results of recursive passes,
484/// - visiting decls is actually simple, so we don't hit the complicated
485/// cases that RAV mostly helps with (types, expressions, etc.)
486class DocumentOutline {
487 // A DocumentSymbol we're constructing.
488 // We use this instead of DocumentSymbol directly so that we can keep track
489 // of the nodes we insert for macros.
490 class SymBuilder {
491 std::vector<SymBuilder> Children;
492 DocumentSymbol Symbol; // Symbol.children is empty, use Children instead.
493 // Macro expansions that this node or its parents are associated with.
494 // (Thus we will never create further children for these expansions).
495 llvm::SmallVector<SourceLocation> EnclosingMacroLoc;
496
497 public:
498 DocumentSymbol build() && {
499 for (SymBuilder &C : Children) {
500 Symbol.children.push_back(std::move(C).build());
501 // Expand range to ensure children nest properly, which editors expect.
502 // This can fix some edge-cases in the AST, but is vital for macros.
503 // A macro expansion "contains" AST node if it covers the node's primary
504 // location, but it may not span the node's whole range.
505 Symbol.range.start =
506 std::min(Symbol.range.start, Symbol.children.back().range.start);
507 Symbol.range.end =
508 std::max(Symbol.range.end, Symbol.children.back().range.end);
509 }
510 return std::move(Symbol);
511 }
512
513 // Add a symbol as a child of the current one.
514 SymBuilder &addChild(DocumentSymbol S) {
515 Children.emplace_back();
516 Children.back().EnclosingMacroLoc = EnclosingMacroLoc;
517 Children.back().Symbol = std::move(S);
518 return Children.back();
519 }
520
521 // Get an appropriate container for children of this symbol that were
522 // expanded from a macro (whose spelled name is Tok).
523 //
524 // This may return:
525 // - a macro symbol child of this (either new or previously created)
526 // - this scope itself, if it *is* the macro symbol or is nested within it
527 SymBuilder &inMacro(const syntax::Token &Tok, const SourceManager &SM,
528 std::optional<syntax::TokenBuffer::Expansion> Exp) {
529 if (llvm::is_contained(EnclosingMacroLoc, Tok.location()))
530 return *this;
531 // If there's an existing child for this macro, we expect it to be last.
532 if (!Children.empty() && !Children.back().EnclosingMacroLoc.empty() &&
533 Children.back().EnclosingMacroLoc.back() == Tok.location())
534 return Children.back();
535
536 DocumentSymbol Sym;
537 Sym.name = Tok.text(SM).str();
538 Sym.kind = SymbolKind::Null; // There's no suitable kind!
539 Sym.range = Sym.selectionRange =
540 halfOpenToRange(SM, Tok.range(SM).toCharRange(SM));
541
542 // FIXME: Exp is currently unavailable for nested expansions.
543 if (Exp) {
544 // Full range covers the macro args.
545 Sym.range = halfOpenToRange(SM, CharSourceRange::getCharRange(
546 Exp->Spelled.front().location(),
547 Exp->Spelled.back().endLocation()));
548 // Show macro args as detail.
549 llvm::raw_string_ostream OS(Sym.detail);
550 const syntax::Token *Prev = nullptr;
551 for (const auto &Tok : Exp->Spelled.drop_front()) {
552 // Don't dump arbitrarily long macro args.
553 if (OS.tell() > 80) {
554 OS << " ...)";
555 break;
556 }
557 if (Prev && Prev->endLocation() != Tok.location())
558 OS << ' ';
559 OS << Tok.text(SM);
560 Prev = &Tok;
561 }
562 }
563 SymBuilder &Child = addChild(std::move(Sym));
564 Child.EnclosingMacroLoc.push_back(Tok.location());
565 return Child;
566 }
567 };
568
569public:
570 DocumentOutline(ParsedAST &AST) : AST(AST) {}
571
572 /// Builds the document outline for the generated AST.
573 std::vector<DocumentSymbol> build() {
574 SymBuilder Root;
575 for (auto &TopLevel : AST.getLocalTopLevelDecls())
576 traverseDecl(TopLevel, Root);
577 return std::move(std::move(Root).build().children);
578 }
579
580private:
581 enum class VisitKind { No, OnlyDecl, OnlyChildren, DeclAndChildren };
582
583 void traverseDecl(Decl *D, SymBuilder &Parent) {
584 // Skip symbols which do not originate from the main file.
585 if (!isInsideMainFile(D->getLocation(), AST.getSourceManager()))
586 return;
587
588 if (auto *Templ = llvm::dyn_cast<TemplateDecl>(D)) {
589 // TemplatedDecl might be null, e.g. concepts.
590 if (auto *TD = Templ->getTemplatedDecl())
591 D = TD;
592 }
593
594 // FriendDecls don't act as DeclContexts, but they might wrap a function
595 // definition that won't be visible through other means in the AST. Hence
596 // unwrap it here instead.
597 if (auto *Friend = llvm::dyn_cast<FriendDecl>(D)) {
598 if (auto *Func =
599 llvm::dyn_cast_or_null<FunctionDecl>(Friend->getFriendDecl())) {
600 if (Func->isThisDeclarationADefinition())
601 D = Func;
602 }
603 }
604
605 VisitKind Visit = shouldVisit(D);
606 if (Visit == VisitKind::No)
607 return;
608
609 if (Visit == VisitKind::OnlyChildren)
610 return traverseChildren(D, Parent);
611
612 auto *ND = llvm::cast<NamedDecl>(D);
613 auto Sym = declToSym(AST.getASTContext(), *ND);
614 if (!Sym)
615 return;
616 SymBuilder &MacroParent = possibleMacroContainer(D->getLocation(), Parent);
617 SymBuilder &Child = MacroParent.addChild(std::move(*Sym));
618
619 if (Visit == VisitKind::OnlyDecl)
620 return;
621
622 assert(Visit == VisitKind::DeclAndChildren && "Unexpected VisitKind");
623 traverseChildren(ND, Child);
624 }
625
626 // Determines where a decl should appear in the DocumentSymbol hierarchy.
627 //
628 // This is usually a direct child of the relevant AST parent.
629 // But we may also insert nodes for macros. Given:
630 // #define DECLARE_INT(V) int v;
631 // namespace a { DECLARE_INT(x) }
632 // We produce:
633 // Namespace a
634 // Macro DECLARE_INT(x)
635 // Variable x
636 //
637 // In the absence of macros, this method simply returns Parent.
638 // Otherwise it may return a macro expansion node instead.
639 // Each macro only has at most one node in the hierarchy, even if it expands
640 // to multiple decls.
641 SymBuilder &possibleMacroContainer(SourceLocation TargetLoc,
642 SymBuilder &Parent) {
643 const auto &SM = AST.getSourceManager();
644 // Look at the path of macro-callers from the token to the main file.
645 // Note that along these paths we see the "outer" macro calls first.
646 SymBuilder *CurParent = &Parent;
647 for (SourceLocation Loc = TargetLoc; Loc.isMacroID();
648 Loc = SM.getImmediateMacroCallerLoc(Loc)) {
649 // Find the virtual macro body that our token is being substituted into.
650 FileID MacroBody;
651 if (SM.isMacroArgExpansion(Loc)) {
652 // Loc is part of a macro arg being substituted into a macro body.
653 MacroBody = SM.getFileID(SM.getImmediateExpansionRange(Loc).getBegin());
654 } else {
655 // Loc is already in the macro body.
656 MacroBody = SM.getFileID(Loc);
657 }
658 // The macro body is being substituted for a macro expansion, whose
659 // first token is the name of the macro.
660 SourceLocation MacroName =
661 SM.getSLocEntry(MacroBody).getExpansion().getExpansionLocStart();
662 // Only include the macro expansion in the outline if it was written
663 // directly in the main file, rather than expanded from another macro.
664 if (!MacroName.isValid() || !MacroName.isFileID())
665 continue;
666 // All conditions satisfied, add the macro.
667 if (auto *Tok = AST.getTokens().spelledTokenContaining(MacroName))
668 CurParent = &CurParent->inMacro(
669 *Tok, SM, AST.getTokens().expansionStartingAt(Tok));
670 }
671 return *CurParent;
672 }
673
674 void traverseChildren(Decl *D, SymBuilder &Builder) {
675 auto *Scope = llvm::dyn_cast<DeclContext>(D);
676 if (!Scope)
677 return;
678 for (auto *C : Scope->decls())
679 traverseDecl(C, Builder);
680 }
681
682 VisitKind shouldVisit(Decl *D) {
683 if (D->isImplicit())
684 return VisitKind::No;
685
686 if (llvm::isa<LinkageSpecDecl>(D) || llvm::isa<ExportDecl>(D))
687 return VisitKind::OnlyChildren;
688
689 if (!llvm::isa<NamedDecl>(D))
690 return VisitKind::No;
691
692 if (auto *Func = llvm::dyn_cast<FunctionDecl>(D)) {
693 // Some functions are implicit template instantiations, those should be
694 // ignored.
695 if (auto *Info = Func->getTemplateSpecializationInfo()) {
696 if (!Info->isExplicitInstantiationOrSpecialization())
697 return VisitKind::No;
698 }
699 // Only visit the function itself, do not visit the children (i.e.
700 // function parameters, etc.)
701 return VisitKind::OnlyDecl;
702 }
703 // Handle template instantiations. We have three cases to consider:
704 // - explicit instantiations, e.g. 'template class std::vector<int>;'
705 // Visit the decl itself (it's present in the code), but not the
706 // children.
707 // - implicit instantiations, i.e. not written by the user.
708 // Do not visit at all, they are not present in the code.
709 // - explicit specialization, e.g. 'template <> class vector<bool> {};'
710 // Visit both the decl and its children, both are written in the code.
711 if (auto *TemplSpec = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) {
712 if (TemplSpec->isExplicitInstantiationOrSpecialization())
713 return TemplSpec->isExplicitSpecialization()
714 ? VisitKind::DeclAndChildren
715 : VisitKind::OnlyDecl;
716 return VisitKind::No;
717 }
718 if (auto *TemplSpec = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) {
719 if (TemplSpec->isExplicitInstantiationOrSpecialization())
720 return TemplSpec->isExplicitSpecialization()
721 ? VisitKind::DeclAndChildren
722 : VisitKind::OnlyDecl;
723 return VisitKind::No;
724 }
725 // For all other cases, visit both the children and the decl.
726 return VisitKind::DeclAndChildren;
727 }
728
729 ParsedAST &AST;
730};
731
732struct PragmaMarkSymbol {
733 DocumentSymbol DocSym;
734 bool IsGroup;
735};
736
737/// Merge in `PragmaMarkSymbols`, sorted ascending by range, into the given
738/// `DocumentSymbol` tree.
739void mergePragmas(DocumentSymbol &Root, ArrayRef<PragmaMarkSymbol> Pragmas) {
740 while (!Pragmas.empty()) {
741 // We'll figure out where the Pragmas.front() should go.
742 PragmaMarkSymbol P = std::move(Pragmas.front());
743 Pragmas = Pragmas.drop_front();
744 DocumentSymbol *Cur = &Root;
745 while (Cur->range.contains(P.DocSym.range)) {
746 bool Swapped = false;
747 for (auto &C : Cur->children) {
748 // We assume at most 1 child can contain the pragma (as pragmas are on
749 // a single line, and children have disjoint ranges).
750 if (C.range.contains(P.DocSym.range)) {
751 Cur = &C;
752 Swapped = true;
753 break;
754 }
755 }
756 // Cur is the parent of P since none of the children contain P.
757 if (!Swapped)
758 break;
759 }
760 // Pragma isn't a group so we can just insert it and we are done.
761 if (!P.IsGroup) {
762 Cur->children.emplace_back(std::move(P.DocSym));
763 continue;
764 }
765 // Pragma is a group, so we need to figure out where it terminates:
766 // - If the next Pragma is not contained in Cur, P owns all of its
767 // parent's children which occur after P.
768 // - If the next pragma is contained in Cur but actually belongs to one
769 // of the parent's children, we temporarily skip over it and look at
770 // the next pragma to decide where we end.
771 // - Otherwise nest all of its parent's children which occur after P but
772 // before the next pragma.
773 bool TerminatedByNextPragma = false;
774 for (auto &NextPragma : Pragmas) {
775 // If we hit a pragma outside of Cur, the rest will be outside as well.
776 if (!Cur->range.contains(NextPragma.DocSym.range))
777 break;
778
779 // NextPragma cannot terminate P if it is nested inside a child, look for
780 // the next one.
781 if (llvm::any_of(Cur->children, [&NextPragma](const auto &Child) {
782 return Child.range.contains(NextPragma.DocSym.range);
783 }))
784 continue;
785
786 // Pragma owns all the children between P and NextPragma
787 auto It = llvm::partition(Cur->children,
788 [&P, &NextPragma](const auto &S) -> bool {
789 return !(P.DocSym.range < S.range &&
790 S.range < NextPragma.DocSym.range);
791 });
792 P.DocSym.children.assign(make_move_iterator(It),
793 make_move_iterator(Cur->children.end()));
794 Cur->children.erase(It, Cur->children.end());
795 TerminatedByNextPragma = true;
796 break;
797 }
798 if (!TerminatedByNextPragma) {
799 // P is terminated by the end of current symbol, hence it owns all the
800 // children after P.
801 auto It = llvm::partition(Cur->children, [&P](const auto &S) -> bool {
802 return !(P.DocSym.range < S.range);
803 });
804 P.DocSym.children.assign(make_move_iterator(It),
805 make_move_iterator(Cur->children.end()));
806 Cur->children.erase(It, Cur->children.end());
807 }
808 // Update the range for P to cover children and append to Cur.
809 for (DocumentSymbol &Sym : P.DocSym.children)
810 unionRanges(P.DocSym.range, Sym.range);
811 Cur->children.emplace_back(std::move(P.DocSym));
812 }
813}
814
815PragmaMarkSymbol markToSymbol(const PragmaMark &P) {
816 StringRef Name = StringRef(P.Trivia).trim();
817 bool IsGroup = false;
818 // "-\s+<group name>" or "<name>" after an initial trim. The former is
819 // considered a group, the latter just a mark. Like Xcode, we don't consider
820 // `-Foo` to be a group (space(s) after the `-` is required).
821 //
822 // We need to include a name here, otherwise editors won't properly render the
823 // symbol.
824 StringRef MaybeGroupName = Name;
825 if (MaybeGroupName.consume_front("-") &&
826 (MaybeGroupName.ltrim() != MaybeGroupName || MaybeGroupName.empty())) {
827 Name = MaybeGroupName.empty() ? "(unnamed group)" : MaybeGroupName.ltrim();
828 IsGroup = true;
829 } else if (Name.empty()) {
830 Name = "(unnamed mark)";
831 }
832 DocumentSymbol Sym;
833 Sym.name = Name.str();
834 Sym.kind = SymbolKind::File;
835 Sym.range = P.Rng;
836 Sym.selectionRange = P.Rng;
837 return {Sym, IsGroup};
838}
839
840std::vector<DocumentSymbol> collectDocSymbols(ParsedAST &AST) {
841 std::vector<DocumentSymbol> Syms = DocumentOutline(AST).build();
842
843 const auto &PragmaMarks = AST.getMarks();
844 if (PragmaMarks.empty())
845 return Syms;
846
847 std::vector<PragmaMarkSymbol> Pragmas;
848 Pragmas.reserve(PragmaMarks.size());
849 for (const auto &P : PragmaMarks)
850 Pragmas.push_back(markToSymbol(P));
851 Range EntireFile = {
852 {0, 0},
853 {std::numeric_limits<int>::max(), std::numeric_limits<int>::max()}};
854 DocumentSymbol Root;
855 Root.children = std::move(Syms);
856 Root.range = EntireFile;
857 mergePragmas(Root, llvm::ArrayRef(Pragmas));
858 return Root.children;
859}
860
861} // namespace
862
863llvm::Expected<std::vector<DocumentSymbol>> getDocumentSymbols(ParsedAST &AST) {
864 return collectDocSymbols(AST);
865}
866
867} // namespace clangd
868} // 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:247
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...
SymbolKind indexSymbolKindToSymbolKind(index::SymbolKind Kind)
Definition Protocol.cpp:298
@ 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.
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