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/DeclTemplate.h" 18 #include "clang/Index/IndexDataConsumer.h" 19 #include "clang/Index/IndexSymbol.h" 20 #include "clang/Index/IndexingAction.h" 21 #include "llvm/ADT/ArrayRef.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/Support/FormatVariadic.h" 26 #include "llvm/Support/Path.h" 27 #include "llvm/Support/ScopedPrinter.h" 28 #include <limits> 29 #include <tuple> 30 31 #define DEBUG_TYPE "FindSymbols" 32 33 namespace clang { 34 namespace clangd { 35 36 namespace { 37 using ScoredSymbolInfo = std::pair<float, SymbolInformation>; 38 struct ScoredSymbolGreater { 39 bool operator()(const ScoredSymbolInfo &L, const ScoredSymbolInfo &R) { 40 if (L.first != R.first) 41 return L.first > R.first; 42 return L.second.name < R.second.name; // Earlier name is better. 43 } 44 }; 45 46 // Returns true if \p Query can be found as a sub-sequence inside \p Scope. 47 bool approximateScopeMatch(llvm::StringRef Scope, llvm::StringRef Query) { 48 assert(Scope.empty() || Scope.endswith("::")); 49 assert(Query.empty() || Query.endswith("::")); 50 while (!Scope.empty() && !Query.empty()) { 51 auto Colons = Scope.find("::"); 52 assert(Colons != llvm::StringRef::npos); 53 54 llvm::StringRef LeadingSpecifier = Scope.slice(0, Colons + 2); 55 Scope = Scope.slice(Colons + 2, llvm::StringRef::npos); 56 Query.consume_front(LeadingSpecifier); 57 } 58 return Query.empty(); 59 } 60 61 } // namespace 62 63 llvm::Expected<Location> indexToLSPLocation(const SymbolLocation &Loc, 64 llvm::StringRef TUPath) { 65 auto Path = URI::resolve(Loc.FileURI, TUPath); 66 if (!Path) 67 return error("Could not resolve path for file '{0}': {1}", Loc.FileURI, 68 Path.takeError()); 69 Location L; 70 L.uri = URIForFile::canonicalize(*Path, TUPath); 71 Position Start, End; 72 Start.line = Loc.Start.line(); 73 Start.character = Loc.Start.column(); 74 End.line = Loc.End.line(); 75 End.character = Loc.End.column(); 76 L.range = {Start, End}; 77 return L; 78 } 79 80 llvm::Expected<Location> symbolToLocation(const Symbol &Sym, 81 llvm::StringRef TUPath) { 82 // Prefer the definition over e.g. a function declaration in a header 83 return indexToLSPLocation( 84 Sym.Definition ? Sym.Definition : Sym.CanonicalDeclaration, TUPath); 85 } 86 87 llvm::Expected<std::vector<SymbolInformation>> 88 getWorkspaceSymbols(llvm::StringRef Query, int Limit, 89 const SymbolIndex *const Index, llvm::StringRef HintPath) { 90 std::vector<SymbolInformation> Result; 91 if (!Index) 92 return Result; 93 94 // Lookup for qualified names are performed as: 95 // - Exact namespaces are boosted by the index. 96 // - Approximate matches are (sub-scope match) included via AnyScope logic. 97 // - Non-matching namespaces (no sub-scope match) are post-filtered. 98 auto Names = splitQualifiedName(Query); 99 100 FuzzyFindRequest Req; 101 Req.Query = std::string(Names.second); 102 103 // FuzzyFind doesn't want leading :: qualifier. 104 auto HasLeadingColons = Names.first.consume_front("::"); 105 // Limit the query to specific namespace if it is fully-qualified. 106 Req.AnyScope = !HasLeadingColons; 107 // Boost symbols from desired namespace. 108 if (HasLeadingColons || !Names.first.empty()) 109 Req.Scopes = {std::string(Names.first)}; 110 if (Limit) { 111 Req.Limit = Limit; 112 // If we are boosting a specific scope allow more results to be retrieved, 113 // since some symbols from preferred namespaces might not make the cut. 114 if (Req.AnyScope && !Req.Scopes.empty()) 115 *Req.Limit *= 5; 116 } 117 TopN<ScoredSymbolInfo, ScoredSymbolGreater> Top( 118 Req.Limit ? *Req.Limit : std::numeric_limits<size_t>::max()); 119 FuzzyMatcher Filter(Req.Query); 120 121 Index->fuzzyFind(Req, [HintPath, &Top, &Filter, AnyScope = Req.AnyScope, 122 ReqScope = Names.first](const Symbol &Sym) { 123 llvm::StringRef Scope = Sym.Scope; 124 // Fuzzyfind might return symbols from irrelevant namespaces if query was 125 // not fully-qualified, drop those. 126 if (AnyScope && !approximateScopeMatch(Scope, ReqScope)) 127 return; 128 129 auto Loc = symbolToLocation(Sym, HintPath); 130 if (!Loc) { 131 log("Workspace symbols: {0}", Loc.takeError()); 132 return; 133 } 134 135 SymbolQualitySignals Quality; 136 Quality.merge(Sym); 137 SymbolRelevanceSignals Relevance; 138 Relevance.Name = Sym.Name; 139 Relevance.Query = SymbolRelevanceSignals::Generic; 140 // If symbol and request scopes do not match exactly, apply a penalty. 141 Relevance.InBaseClass = AnyScope && Scope != ReqScope; 142 if (auto NameMatch = Filter.match(Sym.Name)) 143 Relevance.NameMatch = *NameMatch; 144 else { 145 log("Workspace symbol: {0} didn't match query {1}", Sym.Name, 146 Filter.pattern()); 147 return; 148 } 149 Relevance.merge(Sym); 150 auto QualScore = Quality.evaluateHeuristics(); 151 auto RelScore = Relevance.evaluateHeuristics(); 152 auto Score = evaluateSymbolAndRelevance(QualScore, RelScore); 153 dlog("FindSymbols: {0}{1} = {2}\n{3}{4}\n", Sym.Scope, Sym.Name, Score, 154 Quality, Relevance); 155 156 SymbolInformation Info; 157 Info.name = (Sym.Name + Sym.TemplateSpecializationArgs).str(); 158 Info.kind = indexSymbolKindToSymbolKind(Sym.SymInfo.Kind); 159 Info.location = *Loc; 160 Scope.consume_back("::"); 161 Info.containerName = Scope.str(); 162 163 // Exposed score excludes fuzzy-match component, for client-side re-ranking. 164 Info.score = Relevance.NameMatch > std::numeric_limits<float>::epsilon() 165 ? Score / Relevance.NameMatch 166 : QualScore; 167 Top.push({Score, std::move(Info)}); 168 }); 169 for (auto &R : std::move(Top).items()) 170 Result.push_back(std::move(R.second)); 171 return Result; 172 } 173 174 namespace { 175 std::string getSymbolName(ASTContext &Ctx, const NamedDecl &ND) { 176 // Print `MyClass(Category)` instead of `Category` and `MyClass()` instead 177 // of `anonymous`. 178 if (const auto *Container = dyn_cast<ObjCContainerDecl>(&ND)) 179 return printObjCContainer(*Container); 180 // Differentiate between class and instance methods: print `-foo` instead of 181 // `foo` and `+sharedInstance` instead of `sharedInstance`. 182 if (const auto *Method = dyn_cast<ObjCMethodDecl>(&ND)) { 183 std::string Name; 184 llvm::raw_string_ostream OS(Name); 185 186 OS << (Method->isInstanceMethod() ? '-' : '+'); 187 Method->getSelector().print(OS); 188 189 OS.flush(); 190 return Name; 191 } 192 return printName(Ctx, ND); 193 } 194 195 std::string getSymbolDetail(ASTContext &Ctx, const NamedDecl &ND) { 196 PrintingPolicy P(Ctx.getPrintingPolicy()); 197 P.SuppressScope = true; 198 P.SuppressUnwrittenScope = true; 199 P.AnonymousTagLocations = false; 200 P.PolishForDeclaration = true; 201 std::string Detail; 202 llvm::raw_string_ostream OS(Detail); 203 if (ND.getDescribedTemplateParams()) { 204 OS << "template "; 205 } 206 if (const auto *VD = dyn_cast<ValueDecl>(&ND)) { 207 // FIXME: better printing for dependent type 208 if (isa<CXXConstructorDecl>(VD)) { 209 std::string ConstructorType = VD->getType().getAsString(P); 210 // Print constructor type as "(int)" instead of "void (int)". 211 llvm::StringRef WithoutVoid = ConstructorType; 212 WithoutVoid.consume_front("void "); 213 OS << WithoutVoid; 214 } else if (!isa<CXXDestructorDecl>(VD)) { 215 VD->getType().print(OS, P); 216 } 217 } else if (const auto *TD = dyn_cast<TagDecl>(&ND)) { 218 OS << TD->getKindName(); 219 } else if (isa<TypedefNameDecl>(&ND)) { 220 OS << "type alias"; 221 } else if (isa<ConceptDecl>(&ND)) { 222 OS << "concept"; 223 } 224 return std::move(OS.str()); 225 } 226 227 llvm::Optional<DocumentSymbol> declToSym(ASTContext &Ctx, const NamedDecl &ND) { 228 auto &SM = Ctx.getSourceManager(); 229 230 SourceLocation BeginLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getBeginLoc())); 231 SourceLocation EndLoc = SM.getSpellingLoc(SM.getFileLoc(ND.getEndLoc())); 232 const auto SymbolRange = 233 toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc}); 234 if (!SymbolRange) 235 return llvm::None; 236 237 index::SymbolInfo SymInfo = index::getSymbolInfo(&ND); 238 // FIXME: This is not classifying constructors, destructors and operators 239 // correctly. 240 SymbolKind SK = indexSymbolKindToSymbolKind(SymInfo.Kind); 241 242 DocumentSymbol SI; 243 SI.name = getSymbolName(Ctx, ND); 244 SI.kind = SK; 245 SI.deprecated = ND.isDeprecated(); 246 SI.range = Range{sourceLocToPosition(SM, SymbolRange->getBegin()), 247 sourceLocToPosition(SM, SymbolRange->getEnd())}; 248 SI.detail = getSymbolDetail(Ctx, ND); 249 250 SourceLocation NameLoc = ND.getLocation(); 251 SourceLocation FallbackNameLoc; 252 if (NameLoc.isMacroID()) { 253 if (isSpelledInSource(NameLoc, SM)) { 254 // Prefer the spelling loc, but save the expansion loc as a fallback. 255 FallbackNameLoc = SM.getExpansionLoc(NameLoc); 256 NameLoc = SM.getSpellingLoc(NameLoc); 257 } else { 258 NameLoc = SM.getExpansionLoc(NameLoc); 259 } 260 } 261 auto ComputeSelectionRange = [&](SourceLocation L) -> Range { 262 Position NameBegin = sourceLocToPosition(SM, L); 263 Position NameEnd = sourceLocToPosition( 264 SM, Lexer::getLocForEndOfToken(L, 0, SM, Ctx.getLangOpts())); 265 return Range{NameBegin, NameEnd}; 266 }; 267 268 SI.selectionRange = ComputeSelectionRange(NameLoc); 269 if (!SI.range.contains(SI.selectionRange) && FallbackNameLoc.isValid()) { 270 // 'selectionRange' must be contained in 'range'. In cases where clang 271 // reports unrelated ranges, we first try falling back to the expansion 272 // loc for the selection range. 273 SI.selectionRange = ComputeSelectionRange(FallbackNameLoc); 274 } 275 if (!SI.range.contains(SI.selectionRange)) { 276 // If the containment relationship still doesn't hold, throw away 277 // 'range' and use 'selectionRange' for both. 278 SI.range = SI.selectionRange; 279 } 280 return SI; 281 } 282 283 /// A helper class to build an outline for the parse AST. It traverses the AST 284 /// directly instead of using RecursiveASTVisitor (RAV) for three main reasons: 285 /// - there is no way to keep RAV from traversing subtrees we are not 286 /// interested in. E.g. not traversing function locals or implicit template 287 /// instantiations. 288 /// - it's easier to combine results of recursive passes, 289 /// - visiting decls is actually simple, so we don't hit the complicated 290 /// cases that RAV mostly helps with (types, expressions, etc.) 291 class DocumentOutline { 292 // A DocumentSymbol we're constructing. 293 // We use this instead of DocumentSymbol directly so that we can keep track 294 // of the nodes we insert for macros. 295 class SymBuilder { 296 std::vector<SymBuilder> Children; 297 DocumentSymbol Symbol; // Symbol.children is empty, use Children instead. 298 // Macro expansions that this node or its parents are associated with. 299 // (Thus we will never create further children for these expansions). 300 llvm::SmallVector<SourceLocation> EnclosingMacroLoc; 301 302 public: 303 DocumentSymbol build() && { 304 for (SymBuilder &C : Children) { 305 Symbol.children.push_back(std::move(C).build()); 306 // Expand range to ensure children nest properly, which editors expect. 307 // This can fix some edge-cases in the AST, but is vital for macros. 308 // A macro expansion "contains" AST node if it covers the node's primary 309 // location, but it may not span the node's whole range. 310 Symbol.range.start = 311 std::min(Symbol.range.start, Symbol.children.back().range.start); 312 Symbol.range.end = 313 std::max(Symbol.range.end, Symbol.children.back().range.end); 314 } 315 return std::move(Symbol); 316 } 317 318 // Add a symbol as a child of the current one. 319 SymBuilder &addChild(DocumentSymbol S) { 320 Children.emplace_back(); 321 Children.back().EnclosingMacroLoc = EnclosingMacroLoc; 322 Children.back().Symbol = std::move(S); 323 return Children.back(); 324 } 325 326 // Get an appropriate container for children of this symbol that were 327 // expanded from a macro (whose spelled name is Tok). 328 // 329 // This may return: 330 // - a macro symbol child of this (either new or previously created) 331 // - this scope itself, if it *is* the macro symbol or is nested within it 332 SymBuilder &inMacro(const syntax::Token &Tok, const SourceManager &SM, 333 llvm::Optional<syntax::TokenBuffer::Expansion> Exp) { 334 if (llvm::is_contained(EnclosingMacroLoc, Tok.location())) 335 return *this; 336 // If there's an existing child for this macro, we expect it to be last. 337 if (!Children.empty() && !Children.back().EnclosingMacroLoc.empty() && 338 Children.back().EnclosingMacroLoc.back() == Tok.location()) 339 return Children.back(); 340 341 DocumentSymbol Sym; 342 Sym.name = Tok.text(SM).str(); 343 Sym.kind = SymbolKind::Null; // There's no suitable kind! 344 Sym.range = Sym.selectionRange = 345 halfOpenToRange(SM, Tok.range(SM).toCharRange(SM)); 346 347 // FIXME: Exp is currently unavailable for nested expansions. 348 if (Exp) { 349 // Full range covers the macro args. 350 Sym.range = halfOpenToRange(SM, CharSourceRange::getCharRange( 351 Exp->Spelled.front().location(), 352 Exp->Spelled.back().endLocation())); 353 // Show macro args as detail. 354 llvm::raw_string_ostream OS(Sym.detail); 355 const syntax::Token *Prev = nullptr; 356 for (const auto &Tok : Exp->Spelled.drop_front()) { 357 // Don't dump arbitrarily long macro args. 358 if (OS.tell() > 80) { 359 OS << " ...)"; 360 break; 361 } 362 if (Prev && Prev->endLocation() != Tok.location()) 363 OS << ' '; 364 OS << Tok.text(SM); 365 Prev = &Tok; 366 } 367 } 368 SymBuilder &Child = addChild(std::move(Sym)); 369 Child.EnclosingMacroLoc.push_back(Tok.location()); 370 return Child; 371 } 372 }; 373 374 public: 375 DocumentOutline(ParsedAST &AST) : AST(AST) {} 376 377 /// Builds the document outline for the generated AST. 378 std::vector<DocumentSymbol> build() { 379 SymBuilder Root; 380 for (auto &TopLevel : AST.getLocalTopLevelDecls()) 381 traverseDecl(TopLevel, Root); 382 return std::move(std::move(Root).build().children); 383 } 384 385 private: 386 enum class VisitKind { No, OnlyDecl, OnlyChildren, DeclAndChildren }; 387 388 void traverseDecl(Decl *D, SymBuilder &Parent) { 389 // Skip symbols which do not originate from the main file. 390 if (!isInsideMainFile(D->getLocation(), AST.getSourceManager())) 391 return; 392 393 if (auto *Templ = llvm::dyn_cast<TemplateDecl>(D)) { 394 // TemplatedDecl might be null, e.g. concepts. 395 if (auto *TD = Templ->getTemplatedDecl()) 396 D = TD; 397 } 398 399 VisitKind Visit = shouldVisit(D); 400 if (Visit == VisitKind::No) 401 return; 402 403 if (Visit == VisitKind::OnlyChildren) 404 return traverseChildren(D, Parent); 405 406 auto *ND = llvm::cast<NamedDecl>(D); 407 auto Sym = declToSym(AST.getASTContext(), *ND); 408 if (!Sym) 409 return; 410 SymBuilder &MacroParent = possibleMacroContainer(D->getLocation(), Parent); 411 SymBuilder &Child = MacroParent.addChild(std::move(*Sym)); 412 413 if (Visit == VisitKind::OnlyDecl) 414 return; 415 416 assert(Visit == VisitKind::DeclAndChildren && "Unexpected VisitKind"); 417 traverseChildren(ND, Child); 418 } 419 420 // Determines where a decl should appear in the DocumentSymbol hierarchy. 421 // 422 // This is usually a direct child of the relevant AST parent. 423 // But we may also insert nodes for macros. Given: 424 // #define DECLARE_INT(V) int v; 425 // namespace a { DECLARE_INT(x) } 426 // We produce: 427 // Namespace a 428 // Macro DECLARE_INT(x) 429 // Variable x 430 // 431 // In the absence of macros, this method simply returns Parent. 432 // Otherwise it may return a macro expansion node instead. 433 // Each macro only has at most one node in the hierarchy, even if it expands 434 // to multiple decls. 435 SymBuilder &possibleMacroContainer(SourceLocation TargetLoc, 436 SymBuilder &Parent) { 437 const auto &SM = AST.getSourceManager(); 438 // Look at the path of macro-callers from the token to the main file. 439 // Note that along these paths we see the "outer" macro calls first. 440 SymBuilder *CurParent = &Parent; 441 for (SourceLocation Loc = TargetLoc; Loc.isMacroID(); 442 Loc = SM.getImmediateMacroCallerLoc(Loc)) { 443 // Find the virtual macro body that our token is being substituted into. 444 FileID MacroBody; 445 if (SM.isMacroArgExpansion(Loc)) { 446 // Loc is part of a macro arg being substituted into a macro body. 447 MacroBody = SM.getFileID(SM.getImmediateExpansionRange(Loc).getBegin()); 448 } else { 449 // Loc is already in the macro body. 450 MacroBody = SM.getFileID(Loc); 451 } 452 // The macro body is being substituted for a macro expansion, whose 453 // first token is the name of the macro. 454 SourceLocation MacroName = 455 SM.getSLocEntry(MacroBody).getExpansion().getExpansionLocStart(); 456 // Only include the macro expansion in the outline if it was written 457 // directly in the main file, rather than expanded from another macro. 458 if (!MacroName.isValid() || !MacroName.isFileID()) 459 continue; 460 // All conditions satisfied, add the macro. 461 if (auto *Tok = AST.getTokens().spelledTokenAt(MacroName)) 462 CurParent = &CurParent->inMacro( 463 *Tok, SM, AST.getTokens().expansionStartingAt(Tok)); 464 } 465 return *CurParent; 466 } 467 468 void traverseChildren(Decl *D, SymBuilder &Builder) { 469 auto *Scope = llvm::dyn_cast<DeclContext>(D); 470 if (!Scope) 471 return; 472 for (auto *C : Scope->decls()) 473 traverseDecl(C, Builder); 474 } 475 476 VisitKind shouldVisit(Decl *D) { 477 if (D->isImplicit()) 478 return VisitKind::No; 479 480 if (llvm::isa<LinkageSpecDecl>(D) || llvm::isa<ExportDecl>(D)) 481 return VisitKind::OnlyChildren; 482 483 if (!llvm::isa<NamedDecl>(D)) 484 return VisitKind::No; 485 486 if (auto Func = llvm::dyn_cast<FunctionDecl>(D)) { 487 // Some functions are implicit template instantiations, those should be 488 // ignored. 489 if (auto *Info = Func->getTemplateSpecializationInfo()) { 490 if (!Info->isExplicitInstantiationOrSpecialization()) 491 return VisitKind::No; 492 } 493 // Only visit the function itself, do not visit the children (i.e. 494 // function parameters, etc.) 495 return VisitKind::OnlyDecl; 496 } 497 // Handle template instantiations. We have three cases to consider: 498 // - explicit instantiations, e.g. 'template class std::vector<int>;' 499 // Visit the decl itself (it's present in the code), but not the 500 // children. 501 // - implicit instantiations, i.e. not written by the user. 502 // Do not visit at all, they are not present in the code. 503 // - explicit specialization, e.g. 'template <> class vector<bool> {};' 504 // Visit both the decl and its children, both are written in the code. 505 if (auto *TemplSpec = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D)) { 506 if (TemplSpec->isExplicitInstantiationOrSpecialization()) 507 return TemplSpec->isExplicitSpecialization() 508 ? VisitKind::DeclAndChildren 509 : VisitKind::OnlyDecl; 510 return VisitKind::No; 511 } 512 if (auto *TemplSpec = llvm::dyn_cast<VarTemplateSpecializationDecl>(D)) { 513 if (TemplSpec->isExplicitInstantiationOrSpecialization()) 514 return TemplSpec->isExplicitSpecialization() 515 ? VisitKind::DeclAndChildren 516 : VisitKind::OnlyDecl; 517 return VisitKind::No; 518 } 519 // For all other cases, visit both the children and the decl. 520 return VisitKind::DeclAndChildren; 521 } 522 523 ParsedAST &AST; 524 }; 525 526 struct PragmaMarkSymbol { 527 DocumentSymbol DocSym; 528 bool IsGroup; 529 }; 530 531 /// Merge in `PragmaMarkSymbols`, sorted ascending by range, into the given 532 /// `DocumentSymbol` tree. 533 void mergePragmas(DocumentSymbol &Root, ArrayRef<PragmaMarkSymbol> Pragmas) { 534 while (!Pragmas.empty()) { 535 // We'll figure out where the Pragmas.front() should go. 536 PragmaMarkSymbol P = std::move(Pragmas.front()); 537 Pragmas = Pragmas.drop_front(); 538 DocumentSymbol *Cur = &Root; 539 while (Cur->range.contains(P.DocSym.range)) { 540 bool Swapped = false; 541 for (auto &C : Cur->children) { 542 // We assume at most 1 child can contain the pragma (as pragmas are on 543 // a single line, and children have disjoint ranges). 544 if (C.range.contains(P.DocSym.range)) { 545 Cur = &C; 546 Swapped = true; 547 break; 548 } 549 } 550 // Cur is the parent of P since none of the children contain P. 551 if (!Swapped) 552 break; 553 } 554 // Pragma isn't a group so we can just insert it and we are done. 555 if (!P.IsGroup) { 556 Cur->children.emplace_back(std::move(P.DocSym)); 557 continue; 558 } 559 // Pragma is a group, so we need to figure out where it terminates: 560 // - If the next Pragma is not contained in Cur, P owns all of its 561 // parent's children which occur after P. 562 // - If the next pragma is contained in Cur but actually belongs to one 563 // of the parent's children, we temporarily skip over it and look at 564 // the next pragma to decide where we end. 565 // - Otherwise nest all of its parent's children which occur after P but 566 // before the next pragma. 567 bool TerminatedByNextPragma = false; 568 for (auto &NextPragma : Pragmas) { 569 // If we hit a pragma outside of Cur, the rest will be outside as well. 570 if (!Cur->range.contains(NextPragma.DocSym.range)) 571 break; 572 573 // NextPragma cannot terminate P if it is nested inside a child, look for 574 // the next one. 575 if (llvm::any_of(Cur->children, [&NextPragma](const auto &Child) { 576 return Child.range.contains(NextPragma.DocSym.range); 577 })) 578 continue; 579 580 // Pragma owns all the children between P and NextPragma 581 auto It = llvm::partition(Cur->children, 582 [&P, &NextPragma](const auto &S) -> bool { 583 return !(P.DocSym.range < S.range && 584 S.range < NextPragma.DocSym.range); 585 }); 586 P.DocSym.children.assign(make_move_iterator(It), 587 make_move_iterator(Cur->children.end())); 588 Cur->children.erase(It, Cur->children.end()); 589 TerminatedByNextPragma = true; 590 break; 591 } 592 if (!TerminatedByNextPragma) { 593 // P is terminated by the end of current symbol, hence it owns all the 594 // children after P. 595 auto It = llvm::partition(Cur->children, [&P](const auto &S) -> bool { 596 return !(P.DocSym.range < S.range); 597 }); 598 P.DocSym.children.assign(make_move_iterator(It), 599 make_move_iterator(Cur->children.end())); 600 Cur->children.erase(It, Cur->children.end()); 601 } 602 // Update the range for P to cover children and append to Cur. 603 for (DocumentSymbol &Sym : P.DocSym.children) 604 unionRanges(P.DocSym.range, Sym.range); 605 Cur->children.emplace_back(std::move(P.DocSym)); 606 } 607 } 608 609 PragmaMarkSymbol markToSymbol(const PragmaMark &P) { 610 StringRef Name = StringRef(P.Trivia).trim(); 611 bool IsGroup = false; 612 // "-\s+<group name>" or "<name>" after an initial trim. The former is 613 // considered a group, the latter just a mark. Like Xcode, we don't consider 614 // `-Foo` to be a group (space(s) after the `-` is required). 615 // 616 // We need to include a name here, otherwise editors won't properly render the 617 // symbol. 618 StringRef MaybeGroupName = Name; 619 if (MaybeGroupName.consume_front("-") && 620 (MaybeGroupName.ltrim() != MaybeGroupName || MaybeGroupName.empty())) { 621 Name = MaybeGroupName.empty() ? "(unnamed group)" : MaybeGroupName.ltrim(); 622 IsGroup = true; 623 } else if (Name.empty()) { 624 Name = "(unnamed mark)"; 625 } 626 DocumentSymbol Sym; 627 Sym.name = Name.str(); 628 Sym.kind = SymbolKind::File; 629 Sym.range = P.Rng; 630 Sym.selectionRange = P.Rng; 631 return {Sym, IsGroup}; 632 } 633 634 std::vector<DocumentSymbol> collectDocSymbols(ParsedAST &AST) { 635 std::vector<DocumentSymbol> Syms = DocumentOutline(AST).build(); 636 637 const auto &PragmaMarks = AST.getMarks(); 638 if (PragmaMarks.empty()) 639 return Syms; 640 641 std::vector<PragmaMarkSymbol> Pragmas; 642 Pragmas.reserve(PragmaMarks.size()); 643 for (const auto &P : PragmaMarks) 644 Pragmas.push_back(markToSymbol(P)); 645 Range EntireFile = { 646 {0, 0}, 647 {std::numeric_limits<int>::max(), std::numeric_limits<int>::max()}}; 648 DocumentSymbol Root; 649 Root.children = std::move(Syms); 650 Root.range = EntireFile; 651 mergePragmas(Root, llvm::makeArrayRef(Pragmas)); 652 return Root.children; 653 } 654 655 } // namespace 656 657 llvm::Expected<std::vector<DocumentSymbol>> getDocumentSymbols(ParsedAST &AST) { 658 return collectDocSymbols(AST); 659 } 660 661 } // namespace clangd 662 } // namespace clang 663