clang-tools 23.0.0git
XRefs.cpp
Go to the documentation of this file.
1//===--- XRefs.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 "XRefs.h"
9#include "AST.h"
10#include "FindSymbols.h"
11#include "FindTarget.h"
12#include "Headers.h"
13#include "IncludeCleaner.h"
14#include "ParsedAST.h"
15#include "Protocol.h"
16#include "Quality.h"
17#include "Selection.h"
18#include "SourceCode.h"
19#include "clang-include-cleaner/Analysis.h"
20#include "clang-include-cleaner/Types.h"
21#include "index/Index.h"
22#include "index/Merge.h"
23#include "index/Ref.h"
24#include "index/Relation.h"
26#include "index/SymbolID.h"
28#include "support/Logger.h"
29#include "clang/AST/ASTContext.h"
30#include "clang/AST/ASTTypeTraits.h"
31#include "clang/AST/Attr.h"
32#include "clang/AST/Attrs.inc"
33#include "clang/AST/Decl.h"
34#include "clang/AST/DeclCXX.h"
35#include "clang/AST/DeclObjC.h"
36#include "clang/AST/DeclTemplate.h"
37#include "clang/AST/DeclVisitor.h"
38#include "clang/AST/ExprCXX.h"
39#include "clang/AST/RecursiveASTVisitor.h"
40#include "clang/AST/Stmt.h"
41#include "clang/AST/StmtCXX.h"
42#include "clang/AST/StmtVisitor.h"
43#include "clang/AST/Type.h"
44#include "clang/Basic/LLVM.h"
45#include "clang/Basic/SourceLocation.h"
46#include "clang/Basic/SourceManager.h"
47#include "clang/Basic/TokenKinds.h"
48#include "clang/Index/IndexDataConsumer.h"
49#include "clang/Index/IndexSymbol.h"
50#include "clang/Index/IndexingAction.h"
51#include "clang/Index/IndexingOptions.h"
52#include "clang/Index/USRGeneration.h"
53#include "clang/Lex/Lexer.h"
54#include "clang/Sema/HeuristicResolver.h"
55#include "clang/Tooling/Syntax/Tokens.h"
56#include "llvm/ADT/ArrayRef.h"
57#include "llvm/ADT/DenseMap.h"
58#include "llvm/ADT/DenseSet.h"
59#include "llvm/ADT/STLExtras.h"
60#include "llvm/ADT/ScopeExit.h"
61#include "llvm/ADT/SmallVector.h"
62#include "llvm/ADT/StringRef.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/Error.h"
65#include "llvm/Support/ErrorHandling.h"
66#include "llvm/Support/Path.h"
67#include "llvm/Support/raw_ostream.h"
68#include <algorithm>
69#include <optional>
70#include <string>
71#include <vector>
72
73namespace clang {
74namespace clangd {
75namespace {
76
77// Returns the single definition of the entity declared by D, if visible.
78// In particular:
79// - for non-redeclarable kinds (e.g. local vars), return D
80// - for kinds that allow multiple definitions (e.g. namespaces), return nullptr
81// Kinds of nodes that always return nullptr here will not have definitions
82// reported by locateSymbolAt().
83const NamedDecl *getDefinition(const NamedDecl *D) {
84 assert(D);
85 // Decl has one definition that we can find.
86 if (const auto *TD = dyn_cast<TagDecl>(D))
87 return TD->getDefinition();
88 if (const auto *VD = dyn_cast<VarDecl>(D))
89 return VD->getDefinition();
90 if (const auto *FD = dyn_cast<FunctionDecl>(D))
91 return FD->getDefinition();
92 if (const auto *CTD = dyn_cast<ClassTemplateDecl>(D))
93 if (const auto *RD = CTD->getTemplatedDecl())
94 return RD->getDefinition();
95 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
96 if (MD->isThisDeclarationADefinition())
97 return MD;
98 // Look for the method definition inside the implementation decl.
99 auto *DeclCtx = cast<Decl>(MD->getDeclContext());
100 if (DeclCtx->isInvalidDecl())
101 return nullptr;
102
103 if (const auto *CD = dyn_cast<ObjCContainerDecl>(DeclCtx))
104 if (const auto *Impl = getCorrespondingObjCImpl(CD))
105 return Impl->getMethod(MD->getSelector(), MD->isInstanceMethod());
106 }
107 if (const auto *CD = dyn_cast<ObjCContainerDecl>(D))
108 return getCorrespondingObjCImpl(CD);
109 // Only a single declaration is allowed.
110 if (isa<ValueDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
111 isa<TemplateTemplateParmDecl>(D)) // except cases above
112 return D;
113 // Multiple definitions are allowed.
114 return nullptr; // except cases above
115}
116
117void logIfOverflow(const SymbolLocation &Loc) {
118 if (Loc.Start.hasOverflow() || Loc.End.hasOverflow())
119 log("Possible overflow in symbol location: {0}", Loc);
120}
121
122// Convert a SymbolLocation to LSP's Location.
123// TUPath is used to resolve the path of URI.
124std::optional<Location> toLSPLocation(const SymbolLocation &Loc,
125 llvm::StringRef TUPath) {
126 if (!Loc)
127 return std::nullopt;
128 auto LSPLoc = indexToLSPLocation(Loc, TUPath);
129 if (!LSPLoc) {
130 elog("{0}", LSPLoc.takeError());
131 return std::nullopt;
132 }
133 logIfOverflow(Loc);
134 return *LSPLoc;
135}
136
137SymbolLocation toIndexLocation(const Location &Loc, std::string &URIStorage) {
138 SymbolLocation SymLoc;
139 URIStorage = Loc.uri.uri();
140 SymLoc.FileURI = URIStorage.c_str();
141 SymLoc.Start.setLine(Loc.range.start.line);
142 SymLoc.Start.setColumn(Loc.range.start.character);
143 SymLoc.End.setLine(Loc.range.end.line);
144 SymLoc.End.setColumn(Loc.range.end.character);
145 return SymLoc;
146}
147
148// Returns the preferred location between an AST location and an index location.
149SymbolLocation getPreferredLocation(const Location &ASTLoc,
150 const SymbolLocation &IdxLoc,
151 std::string &Scratch) {
152 // Also use a mock symbol for the index location so that other fields (e.g.
153 // definition) are not factored into the preference.
154 Symbol ASTSym, IdxSym;
155 ASTSym.ID = IdxSym.ID = SymbolID("mock_symbol_id");
156 ASTSym.CanonicalDeclaration = toIndexLocation(ASTLoc, Scratch);
157 IdxSym.CanonicalDeclaration = IdxLoc;
158 auto Merged = mergeSymbol(ASTSym, IdxSym);
159 return Merged.CanonicalDeclaration;
160}
161
162std::vector<std::pair<const NamedDecl *, DeclRelationSet>>
163getDeclAtPositionWithRelations(ParsedAST &AST, SourceLocation Pos,
164 DeclRelationSet Relations,
165 ASTNodeKind *NodeKind = nullptr) {
166 unsigned Offset = AST.getSourceManager().getDecomposedSpellingLoc(Pos).second;
167 std::vector<std::pair<const NamedDecl *, DeclRelationSet>> Result;
168 auto ResultFromTree = [&](SelectionTree ST) {
169 if (const SelectionTree::Node *N = ST.commonAncestor()) {
170 if (NodeKind)
171 *NodeKind = N->ASTNode.getNodeKind();
172 // Attributes don't target decls, look at the
173 // thing it's attached to.
174 // We still report the original NodeKind!
175 // This makes the `override` hack work.
176 if (N->ASTNode.get<Attr>() && N->Parent)
177 N = N->Parent;
178 llvm::copy_if(allTargetDecls(N->ASTNode, AST.getHeuristicResolver()),
179 std::back_inserter(Result),
180 [&](auto &Entry) { return !(Entry.second & ~Relations); });
181 }
182 return !Result.empty();
183 };
184 SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Offset,
185 Offset, ResultFromTree);
186 return Result;
187}
188
189std::vector<const NamedDecl *>
190getDeclAtPosition(ParsedAST &AST, SourceLocation Pos, DeclRelationSet Relations,
191 ASTNodeKind *NodeKind = nullptr) {
192 std::vector<const NamedDecl *> Result;
193 for (auto &Entry :
194 getDeclAtPositionWithRelations(AST, Pos, Relations, NodeKind))
195 Result.push_back(Entry.first);
196 return Result;
197}
198
199// Expects Loc to be a SpellingLocation, will bail out otherwise as it can't
200// figure out a filename.
201std::optional<Location> makeLocation(const ASTContext &AST, SourceLocation Loc,
202 llvm::StringRef TUPath) {
203 const auto &SM = AST.getSourceManager();
204 const auto F = SM.getFileEntryRefForID(SM.getFileID(Loc));
205 if (!F)
206 return std::nullopt;
207 auto FilePath = getCanonicalPath(*F, SM.getFileManager());
208 if (!FilePath) {
209 log("failed to get path!");
210 return std::nullopt;
211 }
212 Location L;
213 L.uri = URIForFile::canonicalize(*FilePath, TUPath);
214 // We call MeasureTokenLength here as TokenBuffer doesn't store spelled tokens
215 // outside the main file.
216 auto TokLen = Lexer::MeasureTokenLength(Loc, SM, AST.getLangOpts());
217 L.range = halfOpenToRange(
218 SM, CharSourceRange::getCharRange(Loc, Loc.getLocWithOffset(TokLen)));
219 return L;
220}
221
222// Treat #included files as symbols, to enable go-to-definition on them.
223std::optional<LocatedSymbol> locateFileReferent(const Position &Pos,
224 ParsedAST &AST,
225 llvm::StringRef MainFilePath) {
226 for (auto &Inc : AST.getIncludeStructure().MainFileIncludes) {
227 if (!Inc.Resolved.empty() && Inc.HashLine == Pos.line) {
229 File.Name = std::string(llvm::sys::path::filename(Inc.Resolved));
230 File.PreferredDeclaration = {
231 URIForFile::canonicalize(Inc.Resolved, MainFilePath), Range{}};
232 File.Definition = File.PreferredDeclaration;
233 // We're not going to find any further symbols on #include lines.
234 return File;
235 }
236 }
237 return std::nullopt;
238}
239
240// Macros are simple: there's no declaration/definition distinction.
241// As a consequence, there's no need to look them up in the index either.
242std::optional<LocatedSymbol>
243locateMacroReferent(const syntax::Token &TouchedIdentifier, ParsedAST &AST,
244 llvm::StringRef MainFilePath) {
245 if (auto M = locateMacroAt(TouchedIdentifier, AST.getPreprocessor())) {
246 if (auto Loc =
247 makeLocation(AST.getASTContext(), M->NameLoc, MainFilePath)) {
249 Macro.Name = std::string(M->Name);
250 Macro.PreferredDeclaration = *Loc;
251 Macro.Definition = Loc;
252 Macro.ID = getSymbolID(M->Name, M->Info, AST.getSourceManager());
253 return Macro;
254 }
255 }
256 return std::nullopt;
257}
258
259// A wrapper around `Decl::getCanonicalDecl` to support cases where Clang's
260// definition of a canonical declaration doesn't match up to what a programmer
261// would expect. For example, Objective-C classes can have three types of
262// declarations:
263//
264// - forward declaration(s): @class MyClass;
265// - true declaration (interface definition): @interface MyClass ... @end
266// - true definition (implementation): @implementation MyClass ... @end
267//
268// Clang will consider the forward declaration to be the canonical declaration
269// because it is first. We actually want the class definition if it is
270// available since that is what a programmer would consider the primary
271// declaration to be.
272const NamedDecl *getPreferredDecl(const NamedDecl *D) {
273 // FIXME: Canonical declarations of some symbols might refer to built-in
274 // decls with possibly-invalid source locations (e.g. global new operator).
275 // In such cases we should pick up a redecl with valid source location
276 // instead of failing.
277 D = llvm::cast<NamedDecl>(D->getCanonicalDecl());
278
279 // Prefer Objective-C class/protocol definitions over the forward declaration.
280 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(D))
281 if (const auto *DefinitionID = ID->getDefinition())
282 return DefinitionID;
283 if (const auto *PD = dyn_cast<ObjCProtocolDecl>(D))
284 if (const auto *DefinitionID = PD->getDefinition())
285 return DefinitionID;
286
287 return D;
288}
289
290std::vector<LocatedSymbol> findImplementors(llvm::DenseSet<SymbolID> IDs,
291 RelationKind Predicate,
292 const SymbolIndex *Index,
293 llvm::StringRef MainFilePath) {
294 if (IDs.empty() || !Index)
295 return {};
296 static constexpr trace::Metric FindImplementorsMetric(
297 "find_implementors", trace::Metric::Counter, "case");
298 switch (Predicate) {
300 FindImplementorsMetric.record(1, "find-base");
301 break;
303 FindImplementorsMetric.record(1, "find-override");
304 break;
305 }
306
308 Req.Predicate = Predicate;
309 llvm::DenseSet<SymbolID> SeenIDs;
310 llvm::DenseSet<SymbolID> Queue = std::move(IDs);
311 std::vector<LocatedSymbol> Results;
312 while (!Queue.empty()) {
313 Req.Subjects = std::move(Queue);
314 Queue = {};
315 Index->relations(Req, [&](const SymbolID &Subject, const Symbol &Object) {
316 if (!SeenIDs.insert(Object.ID).second)
317 return;
318 Queue.insert(Object.ID);
319 auto DeclLoc =
320 indexToLSPLocation(Object.CanonicalDeclaration, MainFilePath);
321 if (!DeclLoc) {
322 elog("Find overrides: {0}", DeclLoc.takeError());
323 return;
324 }
325 Results.emplace_back();
326 Results.back().Name = Object.Name.str();
327 Results.back().PreferredDeclaration = *DeclLoc;
328 auto DefLoc = indexToLSPLocation(Object.Definition, MainFilePath);
329 if (!DefLoc) {
330 elog("Failed to convert location: {0}", DefLoc.takeError());
331 return;
332 }
333 Results.back().Definition = *DefLoc;
334 });
335 }
336 return Results;
337}
338
339// Given LocatedSymbol results derived from the AST, query the index to obtain
340// definitions and preferred declarations.
341void enhanceLocatedSymbolsFromIndex(llvm::MutableArrayRef<LocatedSymbol> Result,
342 const SymbolIndex *Index,
343 llvm::StringRef MainFilePath) {
344 LookupRequest QueryRequest;
345 llvm::DenseMap<SymbolID, unsigned> ResultIndex;
346 for (unsigned I = 0; I < Result.size(); ++I) {
347 if (auto ID = Result[I].ID) {
348 ResultIndex.try_emplace(ID, I);
349 QueryRequest.IDs.insert(ID);
350 }
351 }
352 if (!Index || QueryRequest.IDs.empty())
353 return;
354 std::string Scratch;
355 Index->lookup(QueryRequest, [&](const Symbol &Sym) {
356 auto &R = Result[ResultIndex.lookup(Sym.ID)];
357
358 if (R.Definition) { // from AST
359 // Special case: if the AST yielded a definition, then it may not be
360 // the right *declaration*. Prefer the one from the index.
361 if (auto Loc = toLSPLocation(Sym.CanonicalDeclaration, MainFilePath))
362 R.PreferredDeclaration = *Loc;
363
364 // We might still prefer the definition from the index, e.g. for
365 // generated symbols.
366 if (auto Loc = toLSPLocation(
367 getPreferredLocation(*R.Definition, Sym.Definition, Scratch),
368 MainFilePath))
369 R.Definition = *Loc;
370 } else {
371 R.Definition = toLSPLocation(Sym.Definition, MainFilePath);
372
373 // Use merge logic to choose AST or index declaration.
374 if (auto Loc = toLSPLocation(
375 getPreferredLocation(R.PreferredDeclaration,
376 Sym.CanonicalDeclaration, Scratch),
377 MainFilePath))
378 R.PreferredDeclaration = *Loc;
379 }
380 });
381}
382
383bool objcMethodIsTouched(const SourceManager &SM, const ObjCMethodDecl *OMD,
384 SourceLocation Loc) {
385 unsigned NumSels = OMD->getNumSelectorLocs();
386 for (unsigned I = 0; I < NumSels; ++I)
387 if (SM.getSpellingLoc(OMD->getSelectorLoc(I)) == Loc)
388 return true;
389 return false;
390}
391
392// Decls are more complicated.
393// The AST contains at least a declaration, maybe a definition.
394// These are up-to-date, and so generally preferred over index results.
395// We perform a single batch index lookup to find additional definitions.
396std::vector<LocatedSymbol>
397locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
398 ParsedAST &AST, llvm::StringRef MainFilePath,
399 const SymbolIndex *Index, ASTNodeKind &NodeKind) {
400 const SourceManager &SM = AST.getSourceManager();
401 // Results follow the order of Symbols.Decls.
402 std::vector<LocatedSymbol> Result;
403
404 static constexpr trace::Metric LocateASTReferentMetric(
405 "locate_ast_referent", trace::Metric::Counter, "case");
406 auto AddResultDecl = [&](const NamedDecl *D) {
407 D = getPreferredDecl(D);
408 auto Loc =
409 makeLocation(AST.getASTContext(), nameLocation(*D, SM), MainFilePath);
410 if (!Loc)
411 return;
412
413 Result.emplace_back();
414 Result.back().Name = printName(AST.getASTContext(), *D);
415 Result.back().PreferredDeclaration = *Loc;
416 Result.back().ID = getSymbolID(D);
417 if (const NamedDecl *Def = getDefinition(D))
418 Result.back().Definition = makeLocation(
419 AST.getASTContext(), nameLocation(*Def, SM), MainFilePath);
420 };
421
422 // Emit all symbol locations (declaration or definition) from AST.
423 DeclRelationSet Relations =
425 auto Candidates =
426 getDeclAtPositionWithRelations(AST, CurLoc, Relations, &NodeKind);
427 llvm::DenseSet<SymbolID> VirtualMethods;
428 for (const auto &E : Candidates) {
429 const NamedDecl *D = E.first;
430 if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) {
431 // Special case: virtual void ^method() = 0: jump to all overrides.
432 // FIXME: extend it to ^virtual, unfortunately, virtual location is not
433 // saved in the AST.
434 if (CMD->isPureVirtual()) {
435 if (TouchedIdentifier && SM.getSpellingLoc(CMD->getLocation()) ==
436 TouchedIdentifier->location()) {
437 VirtualMethods.insert(getSymbolID(CMD));
438 LocateASTReferentMetric.record(1, "method-to-override");
439 }
440 }
441 // Special case: void foo() ^override: jump to the overridden method.
442 if (NodeKind.isSame(ASTNodeKind::getFromNodeKind<OverrideAttr>()) ||
443 NodeKind.isSame(ASTNodeKind::getFromNodeKind<FinalAttr>())) {
444 // We may be overridding multiple methods - offer them all.
445 for (const NamedDecl *ND : CMD->overridden_methods())
446 AddResultDecl(ND);
447 continue;
448 }
449 }
450 // Special case: - (void)^method {} should jump to overrides, but the decl
451 // shouldn't, only the definition. Note that an Objective-C method can
452 // override a parent class or protocol.
453 //
454 // FIXME: Support jumping from a protocol decl to overrides on go-to
455 // definition.
456 if (const auto *OMD = llvm::dyn_cast<ObjCMethodDecl>(D)) {
457 if (OMD->isThisDeclarationADefinition() && TouchedIdentifier &&
458 objcMethodIsTouched(SM, OMD, TouchedIdentifier->location())) {
459 llvm::SmallVector<const ObjCMethodDecl *, 4> Overrides;
460 OMD->getOverriddenMethods(Overrides);
461 if (!Overrides.empty()) {
462 for (const auto *Override : Overrides)
463 AddResultDecl(Override);
464 LocateASTReferentMetric.record(1, "objc-overriden-method");
465 }
466 AddResultDecl(OMD);
467 continue;
468 }
469 }
470
471 // Special case: the cursor is on an alias, prefer other results.
472 // This targets "using ns::^Foo", where the target is more interesting.
473 // This does not trigger on renaming aliases:
474 // `using Foo = ^Bar` already targets Bar via a TypeLoc
475 // `using ^Foo = Bar` has no other results, as Underlying is filtered.
476 if (E.second & DeclRelation::Alias && Candidates.size() > 1 &&
477 // beginLoc/endLoc are a token range, so rewind the identifier we're in.
478 SM.isPointWithin(TouchedIdentifier ? TouchedIdentifier->location()
479 : CurLoc,
480 D->getBeginLoc(), D->getEndLoc()))
481 continue;
482
483 // Special case: the point of declaration of a template specialization,
484 // it's more useful to navigate to the template declaration.
485 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
486 if (TouchedIdentifier &&
487 D->getLocation() == TouchedIdentifier->location()) {
488 LocateASTReferentMetric.record(1, "template-specialization-to-primary");
489 AddResultDecl(CTSD->getSpecializedTemplate());
490 continue;
491 }
492 }
493
494 // Special case: if the class name is selected, also map Objective-C
495 // categories and category implementations back to their class interface.
496 //
497 // Since `TouchedIdentifier` might refer to the `ObjCCategoryImplDecl`
498 // instead of the `ObjCCategoryDecl` we intentionally check the contents
499 // of the locs when checking for class name equivalence.
500 if (const auto *CD = dyn_cast<ObjCCategoryDecl>(D))
501 if (const auto *ID = CD->getClassInterface())
502 if (TouchedIdentifier &&
503 (CD->getLocation() == TouchedIdentifier->location() ||
504 ID->getName() == TouchedIdentifier->text(SM))) {
505 LocateASTReferentMetric.record(1, "objc-category-to-class");
506 AddResultDecl(ID);
507 }
508
509 LocateASTReferentMetric.record(1, "regular");
510 // Otherwise the target declaration is the right one.
511 AddResultDecl(D);
512 }
513 enhanceLocatedSymbolsFromIndex(Result, Index, MainFilePath);
514
515 auto Overrides = findImplementors(VirtualMethods, RelationKind::OverriddenBy,
516 Index, MainFilePath);
517 Result.insert(Result.end(), Overrides.begin(), Overrides.end());
518 return Result;
519}
520
521std::vector<LocatedSymbol> locateSymbolForType(const ParsedAST &AST,
522 const QualType &Type,
523 const SymbolIndex *Index) {
524 const auto &SM = AST.getSourceManager();
525 auto MainFilePath = AST.tuPath();
526
527 // FIXME: this sends unique_ptr<Foo> to unique_ptr<T>.
528 // Likely it would be better to send it to Foo (heuristically) or to both.
529 auto Decls = targetDecl(DynTypedNode::create(Type.getNonReferenceType()),
531 AST.getHeuristicResolver());
532 if (Decls.empty())
533 return {};
534
535 std::vector<LocatedSymbol> Results;
536 const auto &ASTContext = AST.getASTContext();
537
538 for (const NamedDecl *D : Decls) {
539 D = getPreferredDecl(D);
540
541 auto Loc = makeLocation(ASTContext, nameLocation(*D, SM), MainFilePath);
542 if (!Loc)
543 continue;
544
545 Results.emplace_back();
546 Results.back().Name = printName(ASTContext, *D);
547 Results.back().PreferredDeclaration = *Loc;
548 Results.back().ID = getSymbolID(D);
549 if (const NamedDecl *Def = getDefinition(D))
550 Results.back().Definition =
551 makeLocation(ASTContext, nameLocation(*Def, SM), MainFilePath);
552 }
553 enhanceLocatedSymbolsFromIndex(Results, Index, MainFilePath);
554
555 return Results;
556}
557
558bool tokenSpelledAt(SourceLocation SpellingLoc, const syntax::TokenBuffer &TB) {
559 auto ExpandedTokens = TB.expandedTokens(
560 TB.sourceManager().getMacroArgExpandedLocation(SpellingLoc));
561 return !ExpandedTokens.empty();
562}
563
564llvm::StringRef sourcePrefix(SourceLocation Loc, const SourceManager &SM) {
565 auto D = SM.getDecomposedLoc(Loc);
566 bool Invalid = false;
567 llvm::StringRef Buf = SM.getBufferData(D.first, &Invalid);
568 if (Invalid || D.second > Buf.size())
569 return "";
570 return Buf.substr(0, D.second);
571}
572
573bool isDependentName(ASTNodeKind NodeKind) {
574 return NodeKind.isSame(ASTNodeKind::getFromNodeKind<OverloadExpr>()) ||
575 NodeKind.isSame(
576 ASTNodeKind::getFromNodeKind<CXXDependentScopeMemberExpr>()) ||
577 NodeKind.isSame(
578 ASTNodeKind::getFromNodeKind<DependentScopeDeclRefExpr>());
579}
580
581} // namespace
582
583std::vector<LocatedSymbol> locateSymbolTextually(const SpelledWord &Word,
584 ParsedAST &AST,
585 const SymbolIndex *Index,
586 llvm::StringRef MainFilePath,
587 ASTNodeKind NodeKind) {
588 // Don't use heuristics if this is a real identifier, or not an
589 // identifier.
590 // Exception: dependent names, because those may have useful textual
591 // matches that AST-based heuristics cannot find.
592 if ((Word.ExpandedToken && !isDependentName(NodeKind)) ||
593 !Word.LikelyIdentifier || !Index)
594 return {};
595 // We don't want to handle words in string literals. (It'd be nice to list
596 // *allowed* token kinds explicitly, but comment Tokens aren't retained).
597 if (Word.PartOfSpelledToken &&
598 isStringLiteral(Word.PartOfSpelledToken->kind()))
599 return {};
600
601 const auto &SM = AST.getSourceManager();
602 // Look up the selected word in the index.
604 Req.Query = Word.Text.str();
605 Req.ProximityPaths = {MainFilePath.str()};
606 // Find the namespaces to query by lexing the file.
607 Req.Scopes =
608 visibleNamespaces(sourcePrefix(Word.Location, SM), AST.getLangOpts());
609 // FIXME: For extra strictness, consider AnyScope=false.
610 Req.AnyScope = true;
611 // We limit the results to 3 further below. This limit is to avoid fetching
612 // too much data, while still likely having enough for 3 results to remain
613 // after additional filtering.
614 Req.Limit = 10;
615 bool TooMany = false;
616 using ScoredLocatedSymbol = std::pair<float, LocatedSymbol>;
617 std::vector<ScoredLocatedSymbol> ScoredResults;
618 Index->fuzzyFind(Req, [&](const Symbol &Sym) {
619 // Only consider exact name matches, including case.
620 // This is to avoid too many false positives.
621 // We could relax this in the future (e.g. to allow for typos) if we make
622 // the query more accurate by other means.
623 if (Sym.Name != Word.Text)
624 return;
625
626 // Exclude constructor results. They have the same name as the class,
627 // but we don't have enough context to prefer them over the class.
628 if (Sym.SymInfo.Kind == index::SymbolKind::Constructor)
629 return;
630
631 auto MaybeDeclLoc =
632 indexToLSPLocation(Sym.CanonicalDeclaration, MainFilePath);
633 if (!MaybeDeclLoc) {
634 log("locateSymbolNamedTextuallyAt: {0}", MaybeDeclLoc.takeError());
635 return;
636 }
637 LocatedSymbol Located;
638 Located.PreferredDeclaration = *MaybeDeclLoc;
639 Located.Name = (Sym.Name + Sym.TemplateSpecializationArgs).str();
640 Located.ID = Sym.ID;
641 if (Sym.Definition) {
642 auto MaybeDefLoc = indexToLSPLocation(Sym.Definition, MainFilePath);
643 if (!MaybeDefLoc) {
644 log("locateSymbolNamedTextuallyAt: {0}", MaybeDefLoc.takeError());
645 return;
646 }
647 Located.PreferredDeclaration = *MaybeDefLoc;
648 Located.Definition = *MaybeDefLoc;
649 }
650
651 if (ScoredResults.size() >= 5) {
652 // If we have more than 5 results, don't return anything,
653 // as confidence is too low.
654 // FIXME: Alternatively, try a stricter query?
655 TooMany = true;
656 return;
657 }
658
659 SymbolQualitySignals Quality;
660 Quality.merge(Sym);
661 SymbolRelevanceSignals Relevance;
662 Relevance.Name = Sym.Name;
664 Relevance.merge(Sym);
665 auto Score = evaluateSymbolAndRelevance(Quality.evaluateHeuristics(),
666 Relevance.evaluateHeuristics());
667 dlog("locateSymbolNamedTextuallyAt: {0}{1} = {2}\n{3}{4}\n", Sym.Scope,
668 Sym.Name, Score, Quality, Relevance);
669
670 ScoredResults.push_back({Score, std::move(Located)});
671 });
672
673 if (TooMany) {
674 vlog("Heuristic index lookup for {0} returned too many candidates, ignored",
675 Word.Text);
676 return {};
677 }
678
679 llvm::sort(ScoredResults,
680 [](const ScoredLocatedSymbol &A, const ScoredLocatedSymbol &B) {
681 return A.first > B.first;
682 });
683 std::vector<LocatedSymbol> Results;
684 for (auto &Res : std::move(ScoredResults))
685 Results.push_back(std::move(Res.second));
686 if (Results.empty())
687 vlog("No heuristic index definition for {0}", Word.Text);
688 else
689 log("Found definition heuristically in index for {0}", Word.Text);
690 return Results;
691}
692
693const syntax::Token *findNearbyIdentifier(const SpelledWord &Word,
694 const syntax::TokenBuffer &TB) {
695 // Don't use heuristics if this is a real identifier.
696 // Unlikely identifiers are OK if they were used as identifiers nearby.
697 if (Word.ExpandedToken)
698 return nullptr;
699 // We don't want to handle words in string literals. (It'd be nice to list
700 // *allowed* token kinds explicitly, but comment Tokens aren't retained).
701 if (Word.PartOfSpelledToken &&
702 isStringLiteral(Word.PartOfSpelledToken->kind()))
703 return {};
704
705 const SourceManager &SM = TB.sourceManager();
706 // We prefer the closest possible token, line-wise. Backwards is penalized.
707 // Ties are implicitly broken by traversal order (first-one-wins).
708 auto File = SM.getFileID(Word.Location);
709 unsigned WordLine = SM.getSpellingLineNumber(Word.Location);
710 auto Cost = [&](SourceLocation Loc) -> unsigned {
711 assert(SM.getFileID(Loc) == File && "spelled token in wrong file?");
712 unsigned Line = SM.getSpellingLineNumber(Loc);
713 return Line >= WordLine ? Line - WordLine : 2 * (WordLine - Line);
714 };
715 const syntax::Token *BestTok = nullptr;
716 unsigned BestCost = -1;
717 // Search bounds are based on word length:
718 // - forward: 2^N lines
719 // - backward: 2^(N-1) lines.
720 unsigned MaxDistance =
721 1U << std::min<unsigned>(Word.Text.size(),
722 std::numeric_limits<unsigned>::digits - 1);
723 // Line number for SM.translateLineCol() should be one-based, also
724 // SM.translateLineCol() can handle line number greater than
725 // number of lines in the file.
726 // - LineMin = max(1, WordLine + 1 - 2^(N-1))
727 // - LineMax = WordLine + 1 + 2^N
728 unsigned LineMin =
729 WordLine + 1 <= MaxDistance / 2 ? 1 : WordLine + 1 - MaxDistance / 2;
730 unsigned LineMax = WordLine + 1 + MaxDistance;
731 SourceLocation LocMin = SM.translateLineCol(File, LineMin, 1);
732 assert(LocMin.isValid());
733 SourceLocation LocMax = SM.translateLineCol(File, LineMax, 1);
734 assert(LocMax.isValid());
735
736 // Updates BestTok and BestCost if Tok is a good candidate.
737 // May return true if the cost is too high for this token.
738 auto Consider = [&](const syntax::Token &Tok) {
739 if (Tok.location() < LocMin || Tok.location() > LocMax)
740 return true; // we are too far from the word, break the outer loop.
741 if (!(Tok.kind() == tok::identifier && Tok.text(SM) == Word.Text))
742 return false;
743 // No point guessing the same location we started with.
744 if (Tok.location() == Word.Location)
745 return false;
746 // We've done cheap checks, compute cost so we can break the caller's loop.
747 unsigned TokCost = Cost(Tok.location());
748 if (TokCost >= BestCost)
749 return true; // causes the outer loop to break.
750 // Allow locations that might be part of the AST, and macros (even if empty)
751 // but not things like disabled preprocessor sections.
752 if (!(tokenSpelledAt(Tok.location(), TB) || TB.expansionStartingAt(&Tok)))
753 return false;
754 // We already verified this token is an improvement.
755 BestCost = TokCost;
756 BestTok = &Tok;
757 return false;
758 };
759 auto SpelledTokens = TB.spelledTokens(File);
760 // Find where the word occurred in the token stream, to search forward & back.
761 auto *I = llvm::partition_point(SpelledTokens, [&](const syntax::Token &T) {
762 assert(SM.getFileID(T.location()) == SM.getFileID(Word.Location));
763 return T.location() < Word.Location; // Comparison OK: same file.
764 });
765 // Search for matches after the cursor.
766 for (const syntax::Token &Tok : llvm::ArrayRef(I, SpelledTokens.end()))
767 if (Consider(Tok))
768 break; // costs of later tokens are greater...
769 // Search for matches before the cursor.
770 for (const syntax::Token &Tok :
771 llvm::reverse(llvm::ArrayRef(SpelledTokens.begin(), I)))
772 if (Consider(Tok))
773 break;
774
775 if (BestTok)
776 vlog(
777 "Word {0} under cursor {1} isn't a token (after PP), trying nearby {2}",
778 Word.Text, Word.Location.printToString(SM),
779 BestTok->location().printToString(SM));
780
781 return BestTok;
782}
783
784std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
785 const SymbolIndex *Index) {
786 const auto &SM = AST.getSourceManager();
787 auto MainFilePath = AST.tuPath();
788
789 if (auto File = locateFileReferent(Pos, AST, MainFilePath))
790 return {std::move(*File)};
791
792 auto CurLoc = sourceLocationInMainFile(SM, Pos);
793 if (!CurLoc) {
794 elog("locateSymbolAt failed to convert position to source location: {0}",
795 CurLoc.takeError());
796 return {};
797 }
798
799 const syntax::Token *TouchedIdentifier = nullptr;
800 auto TokensTouchingCursor =
801 syntax::spelledTokensTouching(*CurLoc, AST.getTokens());
802 for (const syntax::Token &Tok : TokensTouchingCursor) {
803 if (Tok.kind() == tok::identifier) {
804 if (auto Macro = locateMacroReferent(Tok, AST, MainFilePath))
805 // Don't look at the AST or index if we have a macro result.
806 // (We'd just return declarations referenced from the macro's
807 // expansion.)
808 return {*std::move(Macro)};
809
810 TouchedIdentifier = &Tok;
811 break;
812 }
813
814 if (Tok.kind() == tok::kw_auto || Tok.kind() == tok::kw_decltype) {
815 // go-to-definition on auto should find the definition of the deduced
816 // type, if possible
817 if (auto Deduced =
818 getDeducedType(AST.getASTContext(), AST.getHeuristicResolver(),
819 Tok.location())) {
820 auto LocSym = locateSymbolForType(AST, *Deduced, Index);
821 if (!LocSym.empty())
822 return LocSym;
823 }
824 }
825 }
826
827 ASTNodeKind NodeKind;
828 auto ASTResults = locateASTReferent(*CurLoc, TouchedIdentifier, AST,
829 MainFilePath, Index, NodeKind);
830 if (!ASTResults.empty())
831 return ASTResults;
832
833 // If the cursor can't be resolved directly, try fallback strategies.
834 auto Word =
835 SpelledWord::touching(*CurLoc, AST.getTokens(), AST.getLangOpts());
836 if (Word) {
837 // Is the same word nearby a real identifier that might refer to something?
838 if (const syntax::Token *NearbyIdent =
839 findNearbyIdentifier(*Word, AST.getTokens())) {
840 if (auto Macro = locateMacroReferent(*NearbyIdent, AST, MainFilePath)) {
841 log("Found macro definition heuristically using nearby identifier {0}",
842 Word->Text);
843 return {*std::move(Macro)};
844 }
845 ASTResults = locateASTReferent(NearbyIdent->location(), NearbyIdent, AST,
846 MainFilePath, Index, NodeKind);
847 if (!ASTResults.empty()) {
848 log("Found definition heuristically using nearby identifier {0}",
849 NearbyIdent->text(SM));
850 return ASTResults;
851 }
852 vlog("No definition found using nearby identifier {0} at {1}", Word->Text,
853 Word->Location.printToString(SM));
854 }
855 // No nearby word, or it didn't refer to anything either. Try the index.
856 auto TextualResults =
857 locateSymbolTextually(*Word, AST, Index, MainFilePath, NodeKind);
858 if (!TextualResults.empty())
859 return TextualResults;
860 }
861
862 return {};
863}
864
865std::vector<DocumentLink> getDocumentLinks(ParsedAST &AST) {
866 const auto &SM = AST.getSourceManager();
867
868 std::vector<DocumentLink> Result;
869 for (auto &Inc : AST.getIncludeStructure().MainFileIncludes) {
870 if (Inc.Resolved.empty())
871 continue;
872
873 // Get the location of the # symbole of the "#include ..." statement
874 auto HashLoc = SM.getComposedLoc(SM.getMainFileID(), Inc.HashOffset);
875
876 // get the # Token itself, std::next to get the "include" token and the
877 // first token after (aka "File Token")
878 const auto *HashTok = AST.getTokens().spelledTokenContaining(HashLoc);
879 assert(HashTok && "got inclusion at wrong offset");
880 const auto *IncludeTok = std::next(HashTok);
881 const auto *FileTok = std::next(IncludeTok);
882
883 // The File Token can either be of kind :
884 // "less" if using the "#include <h-char-sequence> new-line" syntax
885 // "string_literal" if using the "#include "q-char-sequence" new-line"
886 // syntax something else (most likely "identifier") if using the "#include
887 // pp-tokens new-line" syntax (#include with macro argument)
888
889 CharSourceRange FileRange;
890
891 if (FileTok->kind() == tok::TokenKind::less) {
892 // FileTok->range would only include the '<' char. Hence we explicitly use
893 // Inc.Written's length.
894 FileRange =
895 syntax::FileRange(SM, FileTok->location(), Inc.Written.length())
896 .toCharRange(SM);
897 } else if (FileTok->kind() == tok::TokenKind::string_literal) {
898 // FileTok->range includes the quotes for string literals so just return
899 // it.
900 FileRange = FileTok->range(SM).toCharRange(SM);
901 } else {
902 // FileTok is the first Token of a macro spelling
903
904 // Report the range of the first token (as it should be the macro
905 // identifier)
906 // We could use the AST to find the last spelled token of the macro and
907 // report a range spanning the full macro expression, but it would require
908 // using token-buffers that are deemed too unstable and crash-prone
909 // due to optimizations in cland
910
911 FileRange = FileTok->range(SM).toCharRange(SM);
912 }
913
914 Result.push_back(
915 DocumentLink({halfOpenToRange(SM, FileRange),
916 URIForFile::canonicalize(Inc.Resolved, AST.tuPath())}));
917 }
918
919 return Result;
920}
921
922namespace {
923
924/// Collects references to symbols within the main file.
925class ReferenceFinder : public index::IndexDataConsumer {
926public:
927 struct Reference {
928 syntax::Token SpelledTok;
929 index::SymbolRoleSet Role;
930 const Decl *Container;
931
932 Range range(const SourceManager &SM) const {
933 return halfOpenToRange(SM, SpelledTok.range(SM).toCharRange(SM));
934 }
935 };
936
937 ReferenceFinder(ParsedAST &AST,
938 const llvm::ArrayRef<const NamedDecl *> Targets,
939 bool PerToken)
940 : PerToken(PerToken), AST(AST) {
941 for (const NamedDecl *ND : Targets) {
942 TargetDecls.insert(ND->getCanonicalDecl());
943 if (auto *Constructor = llvm::dyn_cast<clang::CXXConstructorDecl>(ND))
944 TargetConstructors.insert(Constructor);
945 }
946 }
947
948 std::vector<Reference> take() && {
949 llvm::sort(References, [](const Reference &L, const Reference &R) {
950 auto LTok = L.SpelledTok.location();
951 auto RTok = R.SpelledTok.location();
952 return std::tie(LTok, L.Role) < std::tie(RTok, R.Role);
953 });
954 // We sometimes see duplicates when parts of the AST get traversed twice.
955 References.erase(llvm::unique(References,
956 [](const Reference &L, const Reference &R) {
957 auto LTok = L.SpelledTok.location();
958 auto RTok = R.SpelledTok.location();
959 return std::tie(LTok, L.Role) ==
960 std::tie(RTok, R.Role);
961 }),
962 References.end());
963 return std::move(References);
964 }
965
966 bool forwardsToConstructor(const Decl *D) {
967 if (TargetConstructors.empty())
968 return false;
969 auto *FD = llvm::dyn_cast<clang::FunctionDecl>(D);
970 if (FD == nullptr || !FD->isTemplateInstantiation())
971 return false;
972
973 SmallVector<const CXXConstructorDecl *, 1> *Constructors = nullptr;
974 if (auto Entry = AST.ForwardingToConstructorCache.find(FD);
975 Entry != AST.ForwardingToConstructorCache.end())
976 Constructors = &Entry->getSecond();
977 if (Constructors == nullptr) {
978 if (auto *PT = FD->getPrimaryTemplate();
979 PT == nullptr || !isLikelyForwardingFunction(PT))
980 return false;
981
982 SmallVector<const CXXConstructorDecl *, 1> FoundConstructors =
984 auto Iter = AST.ForwardingToConstructorCache.try_emplace(
985 FD, std::move(FoundConstructors));
986 Constructors = &Iter.first->getSecond();
987 }
988 for (auto *Constructor : *Constructors)
989 if (TargetConstructors.contains(Constructor))
990 return true;
991 return false;
992 }
993
994 bool
995 handleDeclOccurrence(const Decl *D, index::SymbolRoleSet Roles,
996 llvm::ArrayRef<index::SymbolRelation> Relations,
997 SourceLocation Loc,
998 index::IndexDataConsumer::ASTNodeInfo ASTNode) override {
999 if (!TargetDecls.contains(D->getCanonicalDecl()) &&
1000 !forwardsToConstructor(ASTNode.OrigD))
1001 return true;
1002 const SourceManager &SM = AST.getSourceManager();
1003 if (!isInsideMainFile(Loc, SM))
1004 return true;
1005 const auto &TB = AST.getTokens();
1006
1007 llvm::SmallVector<SourceLocation, 1> Locs;
1008 if (PerToken) {
1009 // Check whether this is one of the few constructs where the reference
1010 // can be split over several tokens.
1011 if (auto *OME = llvm::dyn_cast_or_null<ObjCMessageExpr>(ASTNode.OrigE)) {
1012 OME->getSelectorLocs(Locs);
1013 } else if (auto *OMD =
1014 llvm::dyn_cast_or_null<ObjCMethodDecl>(ASTNode.OrigD)) {
1015 OMD->getSelectorLocs(Locs);
1016 }
1017 // Sanity check: we expect the *first* token to match the reported loc.
1018 // Otherwise, maybe it was e.g. some other kind of reference to a Decl.
1019 if (!Locs.empty() && Locs.front() != Loc)
1020 Locs.clear(); // First token doesn't match, assume our guess was wrong.
1021 }
1022 if (Locs.empty())
1023 Locs.push_back(Loc);
1024
1025 SymbolCollector::Options CollectorOpts;
1026 CollectorOpts.CollectMainFileSymbols = true;
1027 for (SourceLocation L : Locs) {
1028 L = SM.getFileLoc(L);
1029 if (const auto *Tok = TB.spelledTokenContaining(L))
1030 References.push_back(
1031 {*Tok, Roles,
1032 SymbolCollector::getRefContainer(ASTNode.Parent, CollectorOpts)});
1033 }
1034 return true;
1035 }
1036
1037private:
1038 bool PerToken; // If true, report 3 references for split ObjC selector names.
1039 std::vector<Reference> References;
1040 ParsedAST &AST;
1041 llvm::DenseSet<const Decl *> TargetDecls;
1042 // Constructors need special handling since they can be hidden behind forwards
1043 llvm::DenseSet<const CXXConstructorDecl *> TargetConstructors;
1044};
1045
1046std::vector<ReferenceFinder::Reference>
1047findRefs(const llvm::ArrayRef<const NamedDecl *> TargetDecls, ParsedAST &AST,
1048 bool PerToken) {
1049 ReferenceFinder RefFinder(AST, TargetDecls, PerToken);
1050 index::IndexingOptions IndexOpts;
1051 IndexOpts.SystemSymbolFilter =
1052 index::IndexingOptions::SystemSymbolFilterKind::All;
1053 IndexOpts.IndexFunctionLocals = true;
1054 IndexOpts.IndexParametersInDeclarations = true;
1055 IndexOpts.IndexTemplateParameters = true;
1056 indexTopLevelDecls(AST.getASTContext(), AST.getPreprocessor(),
1057 AST.getLocalTopLevelDecls(), RefFinder, IndexOpts);
1058 return std::move(RefFinder).take();
1059}
1060
1061const Stmt *getFunctionBody(DynTypedNode N) {
1062 if (const auto *FD = N.get<FunctionDecl>())
1063 return FD->getBody();
1064 if (const auto *FD = N.get<BlockDecl>())
1065 return FD->getBody();
1066 if (const auto *FD = N.get<LambdaExpr>())
1067 return FD->getBody();
1068 if (const auto *FD = N.get<ObjCMethodDecl>())
1069 return FD->getBody();
1070 return nullptr;
1071}
1072
1073const Stmt *getLoopBody(DynTypedNode N) {
1074 if (const auto *LS = N.get<ForStmt>())
1075 return LS->getBody();
1076 if (const auto *LS = N.get<CXXForRangeStmt>())
1077 return LS->getBody();
1078 if (const auto *LS = N.get<WhileStmt>())
1079 return LS->getBody();
1080 if (const auto *LS = N.get<DoStmt>())
1081 return LS->getBody();
1082 return nullptr;
1083}
1084
1085// AST traversal to highlight control flow statements under some root.
1086// Once we hit further control flow we prune the tree (or at least restrict
1087// what we highlight) so we capture e.g. breaks from the outer loop only.
1088class FindControlFlow : public RecursiveASTVisitor<FindControlFlow> {
1089 // Types of control-flow statements we might highlight.
1090 enum Target {
1091 Break = 1,
1092 Continue = 2,
1093 Return = 4,
1094 Case = 8,
1095 Throw = 16,
1096 Goto = 32,
1097 All = Break | Continue | Return | Case | Throw | Goto,
1098 };
1099 int Ignore = 0; // bitmask of Target - what are we *not* highlighting?
1100 SourceRange Bounds; // Half-open, restricts reported targets.
1101 std::vector<SourceLocation> &Result;
1102 const SourceManager &SM;
1103
1104 // Masks out targets for a traversal into D.
1105 // Traverses the subtree using Delegate() if any targets remain.
1106 template <typename Func>
1107 bool filterAndTraverse(DynTypedNode D, const Func &Delegate) {
1108 llvm::scope_exit RestoreIgnore(
1109 [OldIgnore(Ignore), this] { Ignore = OldIgnore; });
1110 if (getFunctionBody(D))
1111 Ignore = All;
1112 else if (getLoopBody(D))
1113 Ignore |= Continue | Break;
1114 else if (D.get<SwitchStmt>())
1115 Ignore |= Break | Case;
1116 // Prune tree if we're not looking for anything.
1117 return (Ignore == All) ? true : Delegate();
1118 }
1119
1120 void found(Target T, SourceLocation Loc) {
1121 if (T & Ignore)
1122 return;
1123 if (SM.isBeforeInTranslationUnit(Loc, Bounds.getBegin()) ||
1124 SM.isBeforeInTranslationUnit(Bounds.getEnd(), Loc))
1125 return;
1126 Result.push_back(Loc);
1127 }
1128
1129public:
1130 FindControlFlow(SourceRange Bounds, std::vector<SourceLocation> &Result,
1131 const SourceManager &SM)
1132 : Bounds(Bounds), Result(Result), SM(SM) {}
1133
1134 // When traversing function or loops, limit targets to those that still
1135 // refer to the original root.
1136 bool TraverseDecl(Decl *D) {
1137 return !D || filterAndTraverse(DynTypedNode::create(*D), [&] {
1138 return RecursiveASTVisitor::TraverseDecl(D);
1139 });
1140 }
1141 bool TraverseStmt(Stmt *S) {
1142 return !S || filterAndTraverse(DynTypedNode::create(*S), [&] {
1143 return RecursiveASTVisitor::TraverseStmt(S);
1144 });
1145 }
1146
1147 // Add leaves that we found and want.
1148 bool VisitReturnStmt(ReturnStmt *R) {
1149 found(Return, R->getReturnLoc());
1150 return true;
1151 }
1152 bool VisitBreakStmt(BreakStmt *B) {
1153 found(Break, B->getKwLoc());
1154 return true;
1155 }
1156 bool VisitContinueStmt(ContinueStmt *C) {
1157 found(Continue, C->getKwLoc());
1158 return true;
1159 }
1160 bool VisitSwitchCase(SwitchCase *C) {
1161 found(Case, C->getKeywordLoc());
1162 return true;
1163 }
1164 bool VisitCXXThrowExpr(CXXThrowExpr *T) {
1165 found(Throw, T->getThrowLoc());
1166 return true;
1167 }
1168 bool VisitGotoStmt(GotoStmt *G) {
1169 // Goto is interesting if its target is outside the root.
1170 if (const auto *LD = G->getLabel()) {
1171 if (SM.isBeforeInTranslationUnit(LD->getLocation(), Bounds.getBegin()) ||
1172 SM.isBeforeInTranslationUnit(Bounds.getEnd(), LD->getLocation()))
1173 found(Goto, G->getGotoLoc());
1174 }
1175 return true;
1176 }
1177};
1178
1179// Given a location within a switch statement, return the half-open range that
1180// covers the case it's contained in.
1181// We treat `case X: case Y: ...` as one case, and assume no other fallthrough.
1182SourceRange findCaseBounds(const SwitchStmt &Switch, SourceLocation Loc,
1183 const SourceManager &SM) {
1184 // Cases are not stored in order, sort them first.
1185 // (In fact they seem to be stored in reverse order, don't rely on this)
1186 std::vector<const SwitchCase *> Cases;
1187 for (const SwitchCase *Case = Switch.getSwitchCaseList(); Case;
1188 Case = Case->getNextSwitchCase())
1189 Cases.push_back(Case);
1190 llvm::sort(Cases, [&](const SwitchCase *L, const SwitchCase *R) {
1191 return SM.isBeforeInTranslationUnit(L->getKeywordLoc(), R->getKeywordLoc());
1192 });
1193
1194 // Find the first case after the target location, the end of our range.
1195 auto CaseAfter = llvm::partition_point(Cases, [&](const SwitchCase *C) {
1196 return !SM.isBeforeInTranslationUnit(Loc, C->getKeywordLoc());
1197 });
1198 SourceLocation End = CaseAfter == Cases.end() ? Switch.getEndLoc()
1199 : (*CaseAfter)->getKeywordLoc();
1200
1201 // Our target can be before the first case - cases are optional!
1202 if (CaseAfter == Cases.begin())
1203 return SourceRange(Switch.getBeginLoc(), End);
1204 // The start of our range is usually the previous case, but...
1205 auto CaseBefore = std::prev(CaseAfter);
1206 // ... rewind CaseBefore to the first in a `case A: case B: ...` sequence.
1207 while (CaseBefore != Cases.begin() &&
1208 (*std::prev(CaseBefore))->getSubStmt() == *CaseBefore)
1209 --CaseBefore;
1210 return SourceRange((*CaseBefore)->getKeywordLoc(), End);
1211}
1212
1213// Returns the locations of control flow statements related to N. e.g.:
1214// for => branches: break/continue/return/throw
1215// break => controlling loop (forwhile/do), and its related control flow
1216// return => all returns/throws from the same function
1217// When an inner block is selected, we include branches bound to outer blocks
1218// as these are exits from the inner block. e.g. return in a for loop.
1219// FIXME: We don't analyze catch blocks, throw is treated the same as return.
1220std::vector<SourceLocation> relatedControlFlow(const SelectionTree::Node &N) {
1221 const SourceManager &SM =
1222 N.getDeclContext().getParentASTContext().getSourceManager();
1223 std::vector<SourceLocation> Result;
1224
1225 // First, check if we're at a node that can resolve to a root.
1226 enum class Cur { None, Break, Continue, Return, Case, Throw } Cursor;
1227 if (N.ASTNode.get<BreakStmt>()) {
1228 Cursor = Cur::Break;
1229 } else if (N.ASTNode.get<ContinueStmt>()) {
1230 Cursor = Cur::Continue;
1231 } else if (N.ASTNode.get<ReturnStmt>()) {
1232 Cursor = Cur::Return;
1233 } else if (N.ASTNode.get<CXXThrowExpr>()) {
1234 Cursor = Cur::Throw;
1235 } else if (N.ASTNode.get<SwitchCase>()) {
1236 Cursor = Cur::Case;
1237 } else if (const GotoStmt *GS = N.ASTNode.get<GotoStmt>()) {
1238 // We don't know what root to associate with, but highlight the goto/label.
1239 Result.push_back(GS->getGotoLoc());
1240 if (const auto *LD = GS->getLabel())
1241 Result.push_back(LD->getLocation());
1242 Cursor = Cur::None;
1243 } else {
1244 Cursor = Cur::None;
1245 }
1246
1247 const Stmt *Root = nullptr; // Loop or function body to traverse.
1248 SourceRange Bounds;
1249 // Look up the tree for a root (or just at this node if we didn't find a leaf)
1250 for (const auto *P = &N; P; P = P->Parent) {
1251 // return associates with enclosing function
1252 if (const Stmt *FunctionBody = getFunctionBody(P->ASTNode)) {
1253 if (Cursor == Cur::Return || Cursor == Cur::Throw) {
1254 Root = FunctionBody;
1255 }
1256 break; // other leaves don't cross functions.
1257 }
1258 // break/continue associate with enclosing loop.
1259 if (const Stmt *LoopBody = getLoopBody(P->ASTNode)) {
1260 if (Cursor == Cur::None || Cursor == Cur::Break ||
1261 Cursor == Cur::Continue) {
1262 Root = LoopBody;
1263 // Highlight the loop keyword itself.
1264 // FIXME: for do-while, this only covers the `do`..
1265 Result.push_back(P->ASTNode.getSourceRange().getBegin());
1266 break;
1267 }
1268 }
1269 // For switches, users think of case statements as control flow blocks.
1270 // We highlight only occurrences surrounded by the same case.
1271 // We don't detect fallthrough (other than 'case X, case Y').
1272 if (const auto *SS = P->ASTNode.get<SwitchStmt>()) {
1273 if (Cursor == Cur::Break || Cursor == Cur::Case) {
1274 Result.push_back(SS->getSwitchLoc()); // Highlight the switch.
1275 Root = SS->getBody();
1276 // Limit to enclosing case, if there is one.
1277 Bounds = findCaseBounds(*SS, N.ASTNode.getSourceRange().getBegin(), SM);
1278 break;
1279 }
1280 }
1281 // If we didn't start at some interesting node, we're done.
1282 if (Cursor == Cur::None)
1283 break;
1284 }
1285 if (Root) {
1286 if (!Bounds.isValid())
1287 Bounds = Root->getSourceRange();
1288 FindControlFlow(Bounds, Result, SM).TraverseStmt(const_cast<Stmt *>(Root));
1289 }
1290 return Result;
1291}
1292
1293DocumentHighlight toHighlight(const ReferenceFinder::Reference &Ref,
1294 const SourceManager &SM) {
1296 DH.range = Ref.range(SM);
1297 if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Write))
1299 else if (Ref.Role & index::SymbolRoleSet(index::SymbolRole::Read))
1301 else
1303 return DH;
1304}
1305
1306std::optional<DocumentHighlight> toHighlight(SourceLocation Loc,
1307 const syntax::TokenBuffer &TB) {
1308 Loc = TB.sourceManager().getFileLoc(Loc);
1309 if (const auto *Tok = TB.spelledTokenContaining(Loc)) {
1310 DocumentHighlight Result;
1311 Result.range = halfOpenToRange(
1312 TB.sourceManager(),
1313 CharSourceRange::getCharRange(Tok->location(), Tok->endLocation()));
1314 return Result;
1315 }
1316 return std::nullopt;
1317}
1318
1319} // namespace
1320
1321std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
1322 Position Pos) {
1323 const SourceManager &SM = AST.getSourceManager();
1324 // FIXME: show references to macro within file?
1325 auto CurLoc = sourceLocationInMainFile(SM, Pos);
1326 if (!CurLoc) {
1327 llvm::consumeError(CurLoc.takeError());
1328 return {};
1329 }
1330 std::vector<DocumentHighlight> Result;
1331 auto TryTree = [&](SelectionTree ST) {
1332 if (const SelectionTree::Node *N = ST.commonAncestor()) {
1333 DeclRelationSet Relations =
1335 auto TargetDecls =
1336 targetDecl(N->ASTNode, Relations, AST.getHeuristicResolver());
1337 if (!TargetDecls.empty()) {
1338 // FIXME: we may get multiple DocumentHighlights with the same location
1339 // and different kinds, deduplicate them.
1340 for (const auto &Ref : findRefs(TargetDecls, AST, /*PerToken=*/true))
1341 Result.push_back(toHighlight(Ref, SM));
1342 return true;
1343 }
1344 auto ControlFlow = relatedControlFlow(*N);
1345 if (!ControlFlow.empty()) {
1346 for (SourceLocation Loc : ControlFlow)
1347 if (auto Highlight = toHighlight(Loc, AST.getTokens()))
1348 Result.push_back(std::move(*Highlight));
1349 return true;
1350 }
1351 }
1352 return false;
1353 };
1354
1355 unsigned Offset =
1356 AST.getSourceManager().getDecomposedSpellingLoc(*CurLoc).second;
1357 SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Offset,
1358 Offset, TryTree);
1359 return Result;
1360}
1361
1362std::vector<LocatedSymbol> findImplementations(ParsedAST &AST, Position Pos,
1363 const SymbolIndex *Index) {
1364 // We rely on index to find the implementations in subclasses.
1365 // FIXME: Index can be stale, so we may loose some latest results from the
1366 // main file.
1367 if (!Index)
1368 return {};
1369 const SourceManager &SM = AST.getSourceManager();
1370 auto CurLoc = sourceLocationInMainFile(SM, Pos);
1371 if (!CurLoc) {
1372 elog("Failed to convert position to source location: {0}",
1373 CurLoc.takeError());
1374 return {};
1375 }
1376 DeclRelationSet Relations =
1378 llvm::DenseSet<SymbolID> IDs;
1380 for (const NamedDecl *ND : getDeclAtPosition(AST, *CurLoc, Relations)) {
1381 if (const auto *CXXMD = llvm::dyn_cast<CXXMethodDecl>(ND)) {
1382 if (CXXMD->isVirtual()) {
1383 IDs.insert(getSymbolID(ND));
1384 QueryKind = RelationKind::OverriddenBy;
1385 }
1386 } else if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) {
1387 IDs.insert(getSymbolID(RD));
1388 QueryKind = RelationKind::BaseOf;
1389 } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
1390 IDs.insert(getSymbolID(OMD));
1391 QueryKind = RelationKind::OverriddenBy;
1392 } else if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) {
1393 IDs.insert(getSymbolID(ID));
1394 QueryKind = RelationKind::BaseOf;
1395 }
1396 }
1397 return findImplementors(std::move(IDs), QueryKind, Index, AST.tuPath());
1398}
1399
1400namespace {
1401// Recursively finds all the overridden methods of `CMD` in complete type
1402// hierarchy.
1403void getOverriddenMethods(const CXXMethodDecl *CMD,
1404 llvm::DenseSet<SymbolID> &OverriddenMethods) {
1405 if (!CMD)
1406 return;
1407 for (const CXXMethodDecl *Base : CMD->overridden_methods()) {
1408 if (auto ID = getSymbolID(Base))
1409 OverriddenMethods.insert(ID);
1410 getOverriddenMethods(Base, OverriddenMethods);
1411 }
1412}
1413
1414// Recursively finds all the overridden methods of `OMD` in complete type
1415// hierarchy.
1416void getOverriddenMethods(const ObjCMethodDecl *OMD,
1417 llvm::DenseSet<SymbolID> &OverriddenMethods) {
1418 if (!OMD)
1419 return;
1420 llvm::SmallVector<const ObjCMethodDecl *, 4> Overrides;
1421 OMD->getOverriddenMethods(Overrides);
1422 for (const ObjCMethodDecl *Base : Overrides) {
1423 if (auto ID = getSymbolID(Base))
1424 OverriddenMethods.insert(ID);
1425 getOverriddenMethods(Base, OverriddenMethods);
1426 }
1427}
1428
1429std::optional<std::string>
1430stringifyContainerForMainFileRef(const Decl *Container) {
1431 // FIXME We might also want to display the signature here
1432 // When doing so, remember to also add the Signature to index results!
1433 if (auto *ND = llvm::dyn_cast_if_present<NamedDecl>(Container))
1434 return printQualifiedName(*ND);
1435 return {};
1436}
1437
1438std::optional<ReferencesResult>
1439maybeFindIncludeReferences(ParsedAST &AST, Position Pos,
1440 URIForFile URIMainFile) {
1441 const auto &Includes = AST.getIncludeStructure().MainFileIncludes;
1442 auto IncludeOnLine = llvm::find_if(Includes, [&Pos](const Inclusion &Inc) {
1443 return Inc.HashLine == Pos.line;
1444 });
1445 if (IncludeOnLine == Includes.end())
1446 return std::nullopt;
1447
1448 const SourceManager &SM = AST.getSourceManager();
1449 ReferencesResult Results;
1450 auto Converted = convertIncludes(AST);
1451 include_cleaner::walkUsed(
1452 AST.getLocalTopLevelDecls(), collectMacroReferences(AST),
1453 &AST.getPragmaIncludes(), AST.getPreprocessor(),
1454 [&](const include_cleaner::SymbolReference &Ref,
1455 llvm::ArrayRef<include_cleaner::Header> Providers) {
1456 if (Ref.RT != include_cleaner::RefType::Explicit ||
1457 !isPreferredProvider(*IncludeOnLine, Converted, Providers))
1458 return;
1459
1460 auto Loc = SM.getFileLoc(Ref.RefLocation);
1461 // File locations can be outside of the main file if macro is
1462 // expanded through an #include.
1463 while (SM.getFileID(Loc) != SM.getMainFileID())
1464 Loc = SM.getIncludeLoc(SM.getFileID(Loc));
1465
1466 ReferencesResult::Reference Result;
1467 const auto *Token = AST.getTokens().spelledTokenContaining(Loc);
1468 assert(Token && "references expected token here");
1469 Result.Loc.range = Range{sourceLocToPosition(SM, Token->location()),
1470 sourceLocToPosition(SM, Token->endLocation())};
1471 Result.Loc.uri = URIMainFile;
1472 Results.References.push_back(std::move(Result));
1473 });
1474 if (Results.References.empty())
1475 return std::nullopt;
1476
1477 // Add the #include line to the references list.
1479 Result.Loc.range = rangeTillEOL(SM.getBufferData(SM.getMainFileID()),
1480 IncludeOnLine->HashOffset);
1481 Result.Loc.uri = URIMainFile;
1482 Results.References.push_back(std::move(Result));
1483 return Results;
1484}
1485} // namespace
1486
1488 const SymbolIndex *Index, bool AddContext) {
1489 ReferencesResult Results;
1490 const SourceManager &SM = AST.getSourceManager();
1491 auto MainFilePath = AST.tuPath();
1492 auto URIMainFile = URIForFile::canonicalize(MainFilePath, MainFilePath);
1493 auto CurLoc = sourceLocationInMainFile(SM, Pos);
1494 if (!CurLoc) {
1495 llvm::consumeError(CurLoc.takeError());
1496 return {};
1497 }
1498
1499 const auto IncludeReferences =
1500 maybeFindIncludeReferences(AST, Pos, URIMainFile);
1501 if (IncludeReferences)
1502 return *IncludeReferences;
1503
1504 llvm::DenseSet<SymbolID> IDsToQuery, OverriddenMethods;
1505
1506 const auto *IdentifierAtCursor =
1507 syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
1508 std::optional<DefinedMacro> Macro;
1509 if (IdentifierAtCursor)
1510 Macro = locateMacroAt(*IdentifierAtCursor, AST.getPreprocessor());
1511 if (Macro) {
1512 // Handle references to macro.
1513 if (auto MacroSID = getSymbolID(Macro->Name, Macro->Info, SM)) {
1514 // Collect macro references from main file.
1515 const auto &IDToRefs = AST.getMacros().MacroRefs;
1516 auto Refs = IDToRefs.find(MacroSID);
1517 if (Refs != IDToRefs.end()) {
1518 for (const auto &Ref : Refs->second) {
1520 Result.Loc.range = Ref.toRange(SM);
1521 Result.Loc.uri = URIMainFile;
1522 if (Ref.IsDefinition) {
1525 }
1526 Results.References.push_back(std::move(Result));
1527 }
1528 }
1529 IDsToQuery.insert(MacroSID);
1530 }
1531 } else {
1532 // Handle references to Decls.
1533
1534 DeclRelationSet Relations =
1536 std::vector<const NamedDecl *> Decls =
1537 getDeclAtPosition(AST, *CurLoc, Relations);
1538 llvm::SmallVector<const NamedDecl *> TargetsInMainFile;
1539 for (const NamedDecl *D : Decls) {
1540 auto ID = getSymbolID(D);
1541 if (!ID)
1542 continue;
1543 TargetsInMainFile.push_back(D);
1544 // Not all symbols can be referenced from outside (e.g. function-locals).
1545 // TODO: we could skip TU-scoped symbols here (e.g. static functions) if
1546 // we know this file isn't a header. The details might be tricky.
1547 if (D->getParentFunctionOrMethod())
1548 continue;
1549 IDsToQuery.insert(ID);
1550 }
1551
1553 if (Index) {
1555 for (const NamedDecl *ND : Decls) {
1556 // Special case: For virtual methods, report decl/def of overrides and
1557 // references to all overridden methods in complete type hierarchy.
1558 if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(ND)) {
1559 if (CMD->isVirtual()) {
1560 if (auto ID = getSymbolID(CMD))
1561 OverriddenBy.Subjects.insert(ID);
1562 getOverriddenMethods(CMD, OverriddenMethods);
1563 }
1564 }
1565 // Special case: Objective-C methods can override a parent class or
1566 // protocol, we should be sure to report references to those.
1567 if (const auto *OMD = llvm::dyn_cast<ObjCMethodDecl>(ND)) {
1568 OverriddenBy.Subjects.insert(getSymbolID(OMD));
1569 getOverriddenMethods(OMD, OverriddenMethods);
1570 }
1571 }
1572 }
1573
1574 // We traverse the AST to find references in the main file.
1575 auto MainFileRefs = findRefs(TargetsInMainFile, AST, /*PerToken=*/false);
1576 // We may get multiple refs with the same location and different Roles, as
1577 // cross-reference is only interested in locations, we deduplicate them
1578 // by the location to avoid emitting duplicated locations.
1579 MainFileRefs.erase(llvm::unique(MainFileRefs,
1580 [](const ReferenceFinder::Reference &L,
1581 const ReferenceFinder::Reference &R) {
1582 return L.SpelledTok.location() ==
1583 R.SpelledTok.location();
1584 }),
1585 MainFileRefs.end());
1586 for (const auto &Ref : MainFileRefs) {
1588 Result.Loc.range = Ref.range(SM);
1589 Result.Loc.uri = URIMainFile;
1590 if (AddContext)
1591 Result.Loc.containerName =
1592 stringifyContainerForMainFileRef(Ref.Container);
1593 if (Ref.Role & static_cast<unsigned>(index::SymbolRole::Declaration))
1595 // clang-index doesn't report definitions as declarations, but they are.
1596 if (Ref.Role & static_cast<unsigned>(index::SymbolRole::Definition))
1597 Result.Attributes |=
1599 Results.References.push_back(std::move(Result));
1600 }
1601 // Add decl/def of overridding methods.
1602 if (Index && !OverriddenBy.Subjects.empty()) {
1603 LookupRequest ContainerLookup;
1604 // Different overrides will always be contained in different classes, so
1605 // we have a one-to-one mapping between SymbolID and index here, thus we
1606 // don't need to use std::vector as the map's value type.
1607 llvm::DenseMap<SymbolID, size_t> RefIndexForContainer;
1608 Index->relations(OverriddenBy, [&](const SymbolID &Subject,
1609 const Symbol &Object) {
1610 if (Limit && Results.References.size() >= Limit) {
1611 Results.HasMore = true;
1612 return;
1613 }
1614 const auto LSPLocDecl =
1615 toLSPLocation(Object.CanonicalDeclaration, MainFilePath);
1616 const auto LSPLocDef = toLSPLocation(Object.Definition, MainFilePath);
1617 if (LSPLocDecl && LSPLocDecl != LSPLocDef) {
1619 Result.Loc = {std::move(*LSPLocDecl), std::nullopt};
1620 Result.Attributes =
1622 RefIndexForContainer.insert({Object.ID, Results.References.size()});
1623 ContainerLookup.IDs.insert(Object.ID);
1624 Results.References.push_back(std::move(Result));
1625 }
1626 if (LSPLocDef) {
1628 Result.Loc = {std::move(*LSPLocDef), std::nullopt};
1632 RefIndexForContainer.insert({Object.ID, Results.References.size()});
1633 ContainerLookup.IDs.insert(Object.ID);
1634 Results.References.push_back(std::move(Result));
1635 }
1636 });
1637
1638 if (!ContainerLookup.IDs.empty() && AddContext)
1639 Index->lookup(ContainerLookup, [&](const Symbol &Container) {
1640 auto Ref = RefIndexForContainer.find(Container.ID);
1641 assert(Ref != RefIndexForContainer.end());
1642 Results.References[Ref->getSecond()].Loc.containerName =
1643 Container.Scope.str() + Container.Name.str();
1644 });
1645 }
1646 }
1647 // Now query the index for references from other files.
1648 auto QueryIndex = [&](llvm::DenseSet<SymbolID> IDs, bool AllowAttributes,
1649 bool AllowMainFileSymbols) {
1650 if (IDs.empty() || !Index || Results.HasMore)
1651 return;
1652 RefsRequest Req;
1653 Req.IDs = std::move(IDs);
1654 if (Limit) {
1655 if (Limit < Results.References.size()) {
1656 // We've already filled our quota, still check the index to correctly
1657 // return the `HasMore` info.
1658 Req.Limit = 0;
1659 } else {
1660 // Query index only for the remaining size.
1661 Req.Limit = Limit - Results.References.size();
1662 }
1663 }
1664 LookupRequest ContainerLookup;
1665 llvm::DenseMap<SymbolID, std::vector<size_t>> RefIndicesForContainer;
1666 Results.HasMore |= Index->refs(Req, [&](const Ref &R) {
1667 auto LSPLoc = toLSPLocation(R.Location, MainFilePath);
1668 // Avoid indexed results for the main file - the AST is authoritative.
1669 if (!LSPLoc ||
1670 (!AllowMainFileSymbols && LSPLoc->uri.file() == MainFilePath))
1671 return;
1673 Result.Loc = {std::move(*LSPLoc), std::nullopt};
1674 if (AllowAttributes) {
1677 // FIXME: our index should definitely store def | decl separately!
1679 Result.Attributes |=
1681 }
1682 if (AddContext) {
1683 SymbolID Container = R.Container;
1684 ContainerLookup.IDs.insert(Container);
1685 RefIndicesForContainer[Container].push_back(Results.References.size());
1686 }
1687 Results.References.push_back(std::move(Result));
1688 });
1689
1690 if (!ContainerLookup.IDs.empty() && AddContext)
1691 Index->lookup(ContainerLookup, [&](const Symbol &Container) {
1692 auto Ref = RefIndicesForContainer.find(Container.ID);
1693 assert(Ref != RefIndicesForContainer.end());
1694 auto ContainerName = Container.Scope.str() + Container.Name.str();
1695 for (auto I : Ref->getSecond()) {
1696 Results.References[I].Loc.containerName = ContainerName;
1697 }
1698 });
1699 };
1700 QueryIndex(std::move(IDsToQuery), /*AllowAttributes=*/true,
1701 /*AllowMainFileSymbols=*/false);
1702 // For a virtual method: Occurrences of BaseMethod should be treated as refs
1703 // and not as decl/def. Allow symbols from main file since AST does not report
1704 // these.
1705 QueryIndex(std::move(OverriddenMethods), /*AllowAttributes=*/false,
1706 /*AllowMainFileSymbols=*/true);
1707 return Results;
1708}
1709
1710std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
1711 const SourceManager &SM = AST.getSourceManager();
1712 auto CurLoc = sourceLocationInMainFile(SM, Pos);
1713 if (!CurLoc) {
1714 llvm::consumeError(CurLoc.takeError());
1715 return {};
1716 }
1717 auto MainFilePath = AST.tuPath();
1718 std::vector<SymbolDetails> Results;
1719
1720 // We also want the targets of using-decls, so we include
1721 // DeclRelation::Underlying.
1724 for (const NamedDecl *D : getDeclAtPosition(AST, *CurLoc, Relations)) {
1725 D = getPreferredDecl(D);
1726
1727 SymbolDetails NewSymbol;
1728 std::string QName = printQualifiedName(*D);
1729 auto SplitQName = splitQualifiedName(QName);
1730 NewSymbol.containerName = std::string(SplitQName.first);
1731 NewSymbol.name = std::string(SplitQName.second);
1732
1733 if (NewSymbol.containerName.empty()) {
1734 if (const auto *ParentND =
1735 dyn_cast_or_null<NamedDecl>(D->getDeclContext()))
1736 NewSymbol.containerName = printQualifiedName(*ParentND);
1737 }
1738 llvm::SmallString<32> USR;
1739 if (!index::generateUSRForDecl(D, USR)) {
1740 NewSymbol.USR = std::string(USR);
1741 NewSymbol.ID = SymbolID(NewSymbol.USR);
1742 }
1743 if (const NamedDecl *Def = getDefinition(D))
1744 NewSymbol.definitionRange = makeLocation(
1745 AST.getASTContext(), nameLocation(*Def, SM), MainFilePath);
1746 NewSymbol.declarationRange =
1747 makeLocation(AST.getASTContext(), nameLocation(*D, SM), MainFilePath);
1748
1749 Results.push_back(std::move(NewSymbol));
1750 }
1751
1752 const auto *IdentifierAtCursor =
1753 syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
1754 if (!IdentifierAtCursor)
1755 return Results;
1756
1757 if (auto M = locateMacroAt(*IdentifierAtCursor, AST.getPreprocessor())) {
1758 SymbolDetails NewMacro;
1759 NewMacro.name = std::string(M->Name);
1760 llvm::SmallString<32> USR;
1761 if (!index::generateUSRForMacro(NewMacro.name, M->Info->getDefinitionLoc(),
1762 SM, USR)) {
1763 NewMacro.USR = std::string(USR);
1764 NewMacro.ID = SymbolID(NewMacro.USR);
1765 }
1766 Results.push_back(std::move(NewMacro));
1767 }
1768
1769 return Results;
1770}
1771
1772llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const LocatedSymbol &S) {
1773 OS << S.Name << ": " << S.PreferredDeclaration;
1774 if (S.Definition)
1775 OS << " def=" << *S.Definition;
1776 return OS;
1777}
1778
1779llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
1780 const ReferencesResult::Reference &R) {
1781 OS << R.Loc;
1783 OS << " [decl]";
1785 OS << " [def]";
1787 OS << " [override]";
1788 return OS;
1789}
1790
1791template <typename HierarchyItem>
1792static std::optional<HierarchyItem>
1793declToHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath) {
1794 ASTContext &Ctx = ND.getASTContext();
1795 auto &SM = Ctx.getSourceManager();
1796 SourceLocation NameLoc = nameLocation(ND, Ctx.getSourceManager());
1797 SourceLocation BeginLoc = SM.getFileLoc(ND.getBeginLoc());
1798 SourceLocation EndLoc = SM.getFileLoc(ND.getEndLoc());
1799 const auto DeclRange =
1800 toHalfOpenFileRange(SM, Ctx.getLangOpts(), {BeginLoc, EndLoc});
1801 if (!DeclRange)
1802 return std::nullopt;
1803 const auto FE = SM.getFileEntryRefForID(SM.getFileID(NameLoc));
1804 if (!FE)
1805 return std::nullopt;
1806 auto FilePath = getCanonicalPath(*FE, SM.getFileManager());
1807 if (!FilePath)
1808 return std::nullopt; // Not useful without a uri.
1809
1810 Position NameBegin = sourceLocToPosition(SM, NameLoc);
1811 Position NameEnd = sourceLocToPosition(
1812 SM, Lexer::getLocForEndOfToken(NameLoc, 0, SM, Ctx.getLangOpts()));
1813
1814 index::SymbolInfo SymInfo = index::getSymbolInfo(&ND);
1815 // FIXME: This is not classifying constructors, destructors and operators
1816 // correctly.
1817 SymbolKind SK = indexSymbolKindToSymbolKind(SymInfo.Kind);
1818
1819 HierarchyItem HI;
1820 HI.name = printName(Ctx, ND);
1821 // FIXME: Populate HI.detail the way we do in symbolToHierarchyItem?
1822 HI.kind = SK;
1823 HI.range = Range{sourceLocToPosition(SM, DeclRange->getBegin()),
1824 sourceLocToPosition(SM, DeclRange->getEnd())};
1825 HI.selectionRange = Range{NameBegin, NameEnd};
1826 if (!HI.range.contains(HI.selectionRange)) {
1827 // 'selectionRange' must be contained in 'range', so in cases where clang
1828 // reports unrelated ranges we need to reconcile somehow.
1829 HI.range = HI.selectionRange;
1830 }
1831
1832 HI.uri = URIForFile::canonicalize(*FilePath, TUPath);
1833
1834 return HI;
1835}
1836
1837static std::optional<TypeHierarchyItem>
1838declToTypeHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath) {
1839 auto Result = declToHierarchyItem<TypeHierarchyItem>(ND, TUPath);
1840 if (Result) {
1841 Result->deprecated = ND.isDeprecated();
1842 // Compute the SymbolID and store it in the 'data' field.
1843 // This allows typeHierarchy/resolve to be used to
1844 // resolve children of items returned in a previous request
1845 // for parents.
1846 Result->data.symbolID = getSymbolID(&ND);
1847 }
1848 return Result;
1849}
1850
1851static std::optional<CallHierarchyItem>
1852declToCallHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath) {
1853 auto Result = declToHierarchyItem<CallHierarchyItem>(ND, TUPath);
1854 if (!Result)
1855 return Result;
1856 if (ND.isDeprecated())
1857 Result->tags.push_back(SymbolTag::Deprecated);
1858 if (auto ID = getSymbolID(&ND))
1859 Result->data = ID.str();
1860 return Result;
1861}
1862
1863template <typename HierarchyItem>
1864static std::optional<HierarchyItem> symbolToHierarchyItem(const Symbol &S,
1865 PathRef TUPath) {
1866 auto Loc = symbolToLocation(S, TUPath);
1867 if (!Loc) {
1868 elog("Failed to convert symbol to hierarchy item: {0}", Loc.takeError());
1869 return std::nullopt;
1870 }
1871 HierarchyItem HI;
1872 HI.name = std::string(S.Name);
1873 HI.detail = (S.Scope + S.Name).str();
1874 HI.kind = indexSymbolKindToSymbolKind(S.SymInfo.Kind);
1875 HI.selectionRange = Loc->range;
1876 // FIXME: Populate 'range' correctly
1877 // (https://github.com/clangd/clangd/issues/59).
1878 HI.range = HI.selectionRange;
1879 HI.uri = Loc->uri;
1880
1881 return HI;
1882}
1883
1884static std::optional<TypeHierarchyItem>
1886 auto Result = symbolToHierarchyItem<TypeHierarchyItem>(S, TUPath);
1887 if (Result) {
1888 Result->deprecated = (S.Flags & Symbol::Deprecated);
1889 Result->data.symbolID = S.ID;
1890 }
1891 return Result;
1892}
1893
1894static std::optional<CallHierarchyItem>
1896 auto Result = symbolToHierarchyItem<CallHierarchyItem>(S, TUPath);
1897 if (!Result)
1898 return Result;
1899 Result->data = S.ID.str();
1900 if (S.Flags & Symbol::Deprecated)
1901 Result->tags.push_back(SymbolTag::Deprecated);
1902 return Result;
1903}
1904
1905static void fillSubTypes(const SymbolID &ID,
1906 std::vector<TypeHierarchyItem> &SubTypes,
1907 const SymbolIndex *Index, int Levels, PathRef TUPath) {
1908 RelationsRequest Req;
1909 Req.Subjects.insert(ID);
1911 Index->relations(Req, [&](const SymbolID &Subject, const Symbol &Object) {
1912 if (std::optional<TypeHierarchyItem> ChildSym =
1914 if (Levels > 1) {
1915 ChildSym->children.emplace();
1916 fillSubTypes(Object.ID, *ChildSym->children, Index, Levels - 1, TUPath);
1917 }
1918 SubTypes.emplace_back(std::move(*ChildSym));
1919 }
1920 });
1921}
1922
1923using RecursionProtectionSet = llvm::SmallPtrSet<const CXXRecordDecl *, 4>;
1924
1925// Extracts parents from AST and populates the type hierarchy item.
1926static void fillSuperTypes(const CXXRecordDecl &CXXRD, llvm::StringRef TUPath,
1927 TypeHierarchyItem &Item,
1928 RecursionProtectionSet &RPSet) {
1929 Item.parents.emplace();
1930 Item.data.parents.emplace();
1931 // typeParents() will replace dependent template specializations
1932 // with their class template, so to avoid infinite recursion for
1933 // certain types of hierarchies, keep the templates encountered
1934 // along the parent chain in a set, and stop the recursion if one
1935 // starts to repeat.
1936 auto *Pattern = CXXRD.getDescribedTemplate() ? &CXXRD : nullptr;
1937 if (Pattern) {
1938 if (!RPSet.insert(Pattern).second) {
1939 return;
1940 }
1941 }
1942
1943 for (const CXXRecordDecl *ParentDecl : typeParents(&CXXRD)) {
1944 if (std::optional<TypeHierarchyItem> ParentSym =
1945 declToTypeHierarchyItem(*ParentDecl, TUPath)) {
1946 fillSuperTypes(*ParentDecl, TUPath, *ParentSym, RPSet);
1947 Item.data.parents->emplace_back(ParentSym->data);
1948 Item.parents->emplace_back(std::move(*ParentSym));
1949 }
1950 }
1951
1952 if (Pattern) {
1953 RPSet.erase(Pattern);
1954 }
1955}
1956
1957std::vector<const CXXRecordDecl *> findRecordTypeAt(ParsedAST &AST,
1958 Position Pos) {
1959 auto RecordFromNode = [&AST](const SelectionTree::Node *N) {
1960 std::vector<const CXXRecordDecl *> Records;
1961 if (!N)
1962 return Records;
1963
1964 // Note: explicitReferenceTargets() will search for both template
1965 // instantiations and template patterns, and prefer the former if available
1966 // (generally, one will be available for non-dependent specializations of a
1967 // class template).
1968 auto Decls = explicitReferenceTargets(N->ASTNode, DeclRelation::Underlying,
1969 AST.getHeuristicResolver());
1970 for (const NamedDecl *D : Decls) {
1971
1972 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1973 // If this is a variable, use the type of the variable.
1974 if (const auto *RD = VD->getType().getTypePtr()->getAsCXXRecordDecl())
1975 Records.push_back(RD);
1976 continue;
1977 }
1978
1979 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
1980 // If this is a method, use the type of the class.
1981 Records.push_back(Method->getParent());
1982 continue;
1983 }
1984
1985 // We don't handle FieldDecl because it's not clear what behaviour
1986 // the user would expect: the enclosing class type (as with a
1987 // method), or the field's type (as with a variable).
1988
1989 if (auto *RD = dyn_cast<CXXRecordDecl>(D))
1990 Records.push_back(RD);
1991 }
1992 return Records;
1993 };
1994
1995 const SourceManager &SM = AST.getSourceManager();
1996 std::vector<const CXXRecordDecl *> Result;
1997 auto Offset = positionToOffset(SM.getBufferData(SM.getMainFileID()), Pos);
1998 if (!Offset) {
1999 llvm::consumeError(Offset.takeError());
2000 return Result;
2001 }
2002 SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), *Offset,
2003 *Offset, [&](SelectionTree ST) {
2004 Result = RecordFromNode(ST.commonAncestor());
2005 return !Result.empty();
2006 });
2007 return Result;
2008}
2009
2010// Return the type most associated with an AST node.
2011// This isn't precisely defined: we want "go to type" to do something useful.
2012static QualType typeForNode(const ASTContext &Ctx, const HeuristicResolver *H,
2013 const SelectionTree::Node *N) {
2014 // If we're looking at a namespace qualifier, walk up to what it's qualifying.
2015 // (If we're pointing at a *class* inside a NNS, N will be a TypeLoc).
2016 while (N && N->ASTNode.get<NestedNameSpecifierLoc>())
2017 N = N->Parent;
2018 if (!N)
2019 return QualType();
2020
2021 // If we're pointing at a type => return it.
2022 if (const TypeLoc *TL = N->ASTNode.get<TypeLoc>()) {
2023 if (llvm::isa<DeducedType>(TL->getTypePtr()))
2024 if (auto Deduced = getDeducedType(
2025 N->getDeclContext().getParentASTContext(), H, TL->getBeginLoc()))
2026 return *Deduced;
2027 // Exception: an alias => underlying type.
2028 if (llvm::isa<TypedefType>(TL->getTypePtr()))
2029 return TL->getTypePtr()->getLocallyUnqualifiedSingleStepDesugaredType();
2030 return TL->getType();
2031 }
2032
2033 // Constructor initializers => the type of thing being initialized.
2034 if (const auto *CCI = N->ASTNode.get<CXXCtorInitializer>()) {
2035 if (const FieldDecl *FD = CCI->getAnyMember())
2036 return FD->getType();
2037 if (const Type *Base = CCI->getBaseClass())
2038 return QualType(Base, 0);
2039 }
2040
2041 // Base specifier => the base type.
2042 if (const auto *CBS = N->ASTNode.get<CXXBaseSpecifier>())
2043 return CBS->getType();
2044
2045 if (const Decl *D = N->ASTNode.get<Decl>()) {
2046 struct Visitor : ConstDeclVisitor<Visitor, QualType> {
2047 const ASTContext &Ctx;
2048 Visitor(const ASTContext &Ctx) : Ctx(Ctx) {}
2049
2050 QualType VisitValueDecl(const ValueDecl *D) { return D->getType(); }
2051 // Declaration of a type => that type.
2052 QualType VisitTypeDecl(const TypeDecl *D) {
2053 return Ctx.getTypeDeclType(D);
2054 }
2055 // Exception: alias declaration => the underlying type, not the alias.
2056 QualType VisitTypedefNameDecl(const TypedefNameDecl *D) {
2057 return D->getUnderlyingType();
2058 }
2059 // Look inside templates.
2060 QualType VisitTemplateDecl(const TemplateDecl *D) {
2061 return Visit(D->getTemplatedDecl());
2062 }
2063 } V(Ctx);
2064 return V.Visit(D);
2065 }
2066
2067 if (const Stmt *S = N->ASTNode.get<Stmt>()) {
2068 struct Visitor : ConstStmtVisitor<Visitor, QualType> {
2069 // Null-safe version of visit simplifies recursive calls below.
2070 QualType type(const Stmt *S) { return S ? Visit(S) : QualType(); }
2071
2072 // In general, expressions => type of expression.
2073 QualType VisitExpr(const Expr *S) {
2074 return S->IgnoreImplicitAsWritten()->getType();
2075 }
2076 QualType VisitMemberExpr(const MemberExpr *S) {
2077 // The `foo` in `s.foo()` pretends not to have a real type!
2078 if (S->getType()->isSpecificBuiltinType(BuiltinType::BoundMember))
2079 return Expr::findBoundMemberType(S);
2080 return VisitExpr(S);
2081 }
2082 // Exceptions for void expressions that operate on a type in some way.
2083 QualType VisitCXXDeleteExpr(const CXXDeleteExpr *S) {
2084 return S->getDestroyedType();
2085 }
2086 QualType VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *S) {
2087 return S->getDestroyedType();
2088 }
2089 QualType VisitCXXThrowExpr(const CXXThrowExpr *S) {
2090 return S->getSubExpr()->getType();
2091 }
2092 QualType VisitCoyieldExpr(const CoyieldExpr *S) {
2093 return type(S->getOperand());
2094 }
2095 // Treat a designated initializer like a reference to the field.
2096 QualType VisitDesignatedInitExpr(const DesignatedInitExpr *S) {
2097 // In .foo.bar we want to jump to bar's type, so find *last* field.
2098 for (auto &D : llvm::reverse(S->designators()))
2099 if (D.isFieldDesignator())
2100 if (const auto *FD = D.getFieldDecl())
2101 return FD->getType();
2102 return QualType();
2103 }
2104
2105 // Control flow statements that operate on data: use the data type.
2106 QualType VisitSwitchStmt(const SwitchStmt *S) {
2107 return type(S->getCond());
2108 }
2109 QualType VisitWhileStmt(const WhileStmt *S) { return type(S->getCond()); }
2110 QualType VisitDoStmt(const DoStmt *S) { return type(S->getCond()); }
2111 QualType VisitIfStmt(const IfStmt *S) { return type(S->getCond()); }
2112 QualType VisitCaseStmt(const CaseStmt *S) { return type(S->getLHS()); }
2113 QualType VisitCXXForRangeStmt(const CXXForRangeStmt *S) {
2114 return S->getLoopVariable()->getType();
2115 }
2116 QualType VisitReturnStmt(const ReturnStmt *S) {
2117 return type(S->getRetValue());
2118 }
2119 QualType VisitCoreturnStmt(const CoreturnStmt *S) {
2120 return type(S->getOperand());
2121 }
2122 QualType VisitCXXCatchStmt(const CXXCatchStmt *S) {
2123 return S->getCaughtType();
2124 }
2125 QualType VisitObjCAtThrowStmt(const ObjCAtThrowStmt *S) {
2126 return type(S->getThrowExpr());
2127 }
2128 QualType VisitObjCAtCatchStmt(const ObjCAtCatchStmt *S) {
2129 return S->getCatchParamDecl() ? S->getCatchParamDecl()->getType()
2130 : QualType();
2131 }
2132 } V;
2133 return V.Visit(S);
2134 }
2135
2136 return QualType();
2137}
2138
2139// Given a type targeted by the cursor, return one or more types that are more interesting
2140// to target.
2141static void unwrapFindType(
2142 QualType T, const HeuristicResolver* H, llvm::SmallVector<QualType>& Out) {
2143 if (T.isNull())
2144 return;
2145
2146 // If there's a specific type alias, point at that rather than unwrapping.
2147 if (const auto* TDT = T->getAs<TypedefType>())
2148 return Out.push_back(QualType(TDT, 0));
2149
2150 // Pointers etc => pointee type.
2151 if (const auto *PT = T->getAs<PointerType>())
2152 return unwrapFindType(PT->getPointeeType(), H, Out);
2153 if (const auto *RT = T->getAs<ReferenceType>())
2154 return unwrapFindType(RT->getPointeeType(), H, Out);
2155 if (const auto *AT = T->getAsArrayTypeUnsafe())
2156 return unwrapFindType(AT->getElementType(), H, Out);
2157
2158 // Function type => return type.
2159 if (auto *FT = T->getAs<FunctionType>())
2160 return unwrapFindType(FT->getReturnType(), H, Out);
2161 if (auto *CRD = T->getAsCXXRecordDecl()) {
2162 if (CRD->isLambda())
2163 return unwrapFindType(CRD->getLambdaCallOperator()->getReturnType(), H,
2164 Out);
2165 // FIXME: more cases we'd prefer the return type of the call operator?
2166 // std::function etc?
2167 }
2168
2169 // For smart pointer types, add the underlying type
2170 if (H)
2171 if (auto PointeeType = H->getPointeeType(T.getNonReferenceType());
2172 !PointeeType.isNull()) {
2173 unwrapFindType(PointeeType, H, Out);
2174 return Out.push_back(T);
2175 }
2176
2177 return Out.push_back(T);
2178}
2179
2180// Convenience overload, to allow calling this without the out-parameter
2181static llvm::SmallVector<QualType> unwrapFindType(
2182 QualType T, const HeuristicResolver* H) {
2183 llvm::SmallVector<QualType> Result;
2184 unwrapFindType(T, H, Result);
2185 return Result;
2186}
2187
2188std::vector<LocatedSymbol> findType(ParsedAST &AST, Position Pos,
2189 const SymbolIndex *Index) {
2190 const SourceManager &SM = AST.getSourceManager();
2191 auto Offset = positionToOffset(SM.getBufferData(SM.getMainFileID()), Pos);
2192 std::vector<LocatedSymbol> Result;
2193 if (!Offset) {
2194 elog("failed to convert position {0} for findTypes: {1}", Pos,
2195 Offset.takeError());
2196 return Result;
2197 }
2198 // The general scheme is: position -> AST node -> type -> declaration.
2199 auto SymbolsFromNode =
2200 [&](const SelectionTree::Node *N) -> std::vector<LocatedSymbol> {
2201 std::vector<LocatedSymbol> LocatedSymbols;
2202
2203 // NOTE: unwrapFindType might return duplicates for something like
2204 // unique_ptr<unique_ptr<T>>. Let's *not* remove them, because it gives you some
2205 // information about the type you may have not known before
2206 // (since unique_ptr<unique_ptr<T>> != unique_ptr<T>).
2207 for (const QualType &Type : unwrapFindType(
2208 typeForNode(AST.getASTContext(), AST.getHeuristicResolver(), N),
2209 AST.getHeuristicResolver()))
2210 llvm::copy(locateSymbolForType(AST, Type, Index),
2211 std::back_inserter(LocatedSymbols));
2212
2213 return LocatedSymbols;
2214 };
2215 SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), *Offset,
2216 *Offset, [&](SelectionTree ST) {
2217 Result = SymbolsFromNode(ST.commonAncestor());
2218 return !Result.empty();
2219 });
2220 return Result;
2221}
2222
2223std::vector<const CXXRecordDecl *> typeParents(const CXXRecordDecl *CXXRD) {
2224 std::vector<const CXXRecordDecl *> Result;
2225
2226 // If this is an invalid instantiation, instantiation of the bases
2227 // may not have succeeded, so fall back to the template pattern.
2228 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CXXRD)) {
2229 if (CTSD->isInvalidDecl())
2230 CXXRD = CTSD->getSpecializedTemplate()->getTemplatedDecl();
2231 }
2232
2233 // Can't query bases without a definition.
2234 if (!CXXRD->hasDefinition())
2235 return Result;
2236
2237 for (auto Base : CXXRD->bases()) {
2238 const CXXRecordDecl *ParentDecl = nullptr;
2239
2240 const Type *Type = Base.getType().getTypePtr();
2241 if (const RecordType *RT = Type->getAs<RecordType>()) {
2242 ParentDecl = RT->getAsCXXRecordDecl();
2243 }
2244
2245 if (!ParentDecl) {
2246 // Handle a dependent base such as "Base<T>" by using the primary
2247 // template.
2248 if (const TemplateSpecializationType *TS =
2249 Type->getAs<TemplateSpecializationType>()) {
2250 TemplateName TN = TS->getTemplateName();
2251 if (TemplateDecl *TD = TN.getAsTemplateDecl()) {
2252 ParentDecl = dyn_cast<CXXRecordDecl>(TD->getTemplatedDecl());
2253 }
2254 }
2255 }
2256
2257 if (ParentDecl)
2258 Result.push_back(ParentDecl);
2259 }
2260
2261 return Result;
2262}
2263
2264std::vector<TypeHierarchyItem>
2265getTypeHierarchy(ParsedAST &AST, Position Pos, int ResolveLevels,
2266 TypeHierarchyDirection Direction, const SymbolIndex *Index,
2267 PathRef TUPath) {
2268 std::vector<TypeHierarchyItem> Results;
2269 for (const auto *CXXRD : findRecordTypeAt(AST, Pos)) {
2270
2271 bool WantChildren = Direction == TypeHierarchyDirection::Children ||
2272 Direction == TypeHierarchyDirection::Both;
2273
2274 // If we're looking for children, we're doing the lookup in the index.
2275 // The index does not store relationships between implicit
2276 // specializations, so if we have one, use the template pattern instead.
2277 // Note that this needs to be done before the declToTypeHierarchyItem(),
2278 // otherwise the type hierarchy item would misleadingly contain the
2279 // specialization parameters, while the children would involve classes
2280 // that derive from other specializations of the template.
2281 if (WantChildren) {
2282 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CXXRD))
2283 CXXRD = CTSD->getTemplateInstantiationPattern();
2284 }
2285
2286 std::optional<TypeHierarchyItem> Result =
2287 declToTypeHierarchyItem(*CXXRD, AST.tuPath());
2288 if (!Result)
2289 continue;
2290
2292 fillSuperTypes(*CXXRD, AST.tuPath(), *Result, RPSet);
2293
2294 if (WantChildren && ResolveLevels > 0) {
2295 Result->children.emplace();
2296
2297 if (Index) {
2298 if (auto ID = getSymbolID(CXXRD))
2299 fillSubTypes(ID, *Result->children, Index, ResolveLevels, TUPath);
2300 }
2301 }
2302 Results.emplace_back(std::move(*Result));
2303 }
2304
2305 return Results;
2306}
2307
2308std::optional<std::vector<TypeHierarchyItem>>
2309superTypes(const TypeHierarchyItem &Item, const SymbolIndex *Index) {
2310 std::vector<TypeHierarchyItem> Results;
2311 if (!Item.data.parents)
2312 return std::nullopt;
2313 if (Item.data.parents->empty())
2314 return Results;
2315 LookupRequest Req;
2316 llvm::DenseMap<SymbolID, const TypeHierarchyItem::ResolveParams *> IDToData;
2317 for (const auto &Parent : *Item.data.parents) {
2318 Req.IDs.insert(Parent.symbolID);
2319 IDToData[Parent.symbolID] = &Parent;
2320 }
2321 Index->lookup(Req, [&Item, &Results, &IDToData](const Symbol &S) {
2322 if (auto THI = symbolToTypeHierarchyItem(S, Item.uri.file())) {
2323 THI->data = *IDToData.lookup(S.ID);
2324 Results.emplace_back(std::move(*THI));
2325 }
2326 });
2327 return Results;
2328}
2329
2330std::vector<TypeHierarchyItem> subTypes(const TypeHierarchyItem &Item,
2331 const SymbolIndex *Index) {
2332 std::vector<TypeHierarchyItem> Results;
2333 fillSubTypes(Item.data.symbolID, Results, Index, 1, Item.uri.file());
2334 for (auto &ChildSym : Results)
2335 ChildSym.data.parents = {Item.data};
2336 return Results;
2337}
2338
2339void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels,
2340 TypeHierarchyDirection Direction,
2341 const SymbolIndex *Index) {
2342 // We only support typeHierarchy/resolve for children, because for parents
2343 // we ignore ResolveLevels and return all levels of parents eagerly.
2344 if (!Index || Direction == TypeHierarchyDirection::Parents ||
2345 ResolveLevels == 0)
2346 return;
2347
2348 Item.children.emplace();
2349 fillSubTypes(Item.data.symbolID, *Item.children, Index, ResolveLevels,
2350 Item.uri.file());
2351}
2352
2353std::vector<CallHierarchyItem>
2355 std::vector<CallHierarchyItem> Result;
2356 const auto &SM = AST.getSourceManager();
2357 auto Loc = sourceLocationInMainFile(SM, Pos);
2358 if (!Loc) {
2359 elog("prepareCallHierarchy failed to convert position to source location: "
2360 "{0}",
2361 Loc.takeError());
2362 return Result;
2363 }
2364 for (const NamedDecl *Decl : getDeclAtPosition(AST, *Loc, {})) {
2365 if (!(isa<DeclContext>(Decl) &&
2366 cast<DeclContext>(Decl)->isFunctionOrMethod()) &&
2367 Decl->getKind() != Decl::Kind::FunctionTemplate &&
2368 !(Decl->getKind() == Decl::Kind::Var &&
2369 !cast<VarDecl>(Decl)->isLocalVarDecl()) &&
2370 Decl->getKind() != Decl::Kind::Field &&
2371 Decl->getKind() != Decl::Kind::EnumConstant)
2372 continue;
2373 if (auto CHI = declToCallHierarchyItem(*Decl, AST.tuPath()))
2374 Result.emplace_back(std::move(*CHI));
2375 }
2376 return Result;
2377}
2378
2379std::vector<CallHierarchyIncomingCall>
2380incomingCalls(const CallHierarchyItem &Item, const SymbolIndex *Index) {
2381 std::vector<CallHierarchyIncomingCall> Results;
2382 if (!Index || Item.data.empty())
2383 return Results;
2384 auto ID = SymbolID::fromStr(Item.data);
2385 if (!ID) {
2386 elog("incomingCalls failed to find symbol: {0}", ID.takeError());
2387 return Results;
2388 }
2389 // In this function, we find incoming calls based on the index only.
2390 // In principle, the AST could have more up-to-date information about
2391 // occurrences within the current file. However, going from a SymbolID
2392 // to an AST node isn't cheap, particularly when the declaration isn't
2393 // in the main file.
2394 // FIXME: Consider also using AST information when feasible.
2395 auto QueryIndex = [&](llvm::DenseSet<SymbolID> IDs, bool MightNeverCall) {
2396 RefsRequest Request;
2397 Request.IDs = std::move(IDs);
2398 Request.WantContainer = true;
2399 // We could restrict more specifically to calls by introducing a new
2400 // RefKind, but non-call references (such as address-of-function) can still
2401 // be interesting as they can indicate indirect calls.
2402 Request.Filter = RefKind::Reference;
2403 // Initially store the ranges in a map keyed by SymbolID of the caller.
2404 // This allows us to group different calls with the same caller
2405 // into the same CallHierarchyIncomingCall.
2406 llvm::DenseMap<SymbolID, std::vector<Location>> CallsIn;
2407 // We can populate the ranges based on a refs request only. As we do so, we
2408 // also accumulate the container IDs into a lookup request.
2409 LookupRequest ContainerLookup;
2410 Index->refs(Request, [&](const Ref &R) {
2411 auto Loc = indexToLSPLocation(R.Location, Item.uri.file());
2412 if (!Loc) {
2413 elog("incomingCalls failed to convert location: {0}", Loc.takeError());
2414 return;
2415 }
2416 CallsIn[R.Container].push_back(*Loc);
2417
2418 ContainerLookup.IDs.insert(R.Container);
2419 });
2420 // Perform the lookup request and combine its results with CallsIn to
2421 // get complete CallHierarchyIncomingCall objects.
2422 Index->lookup(ContainerLookup, [&](const Symbol &Caller) {
2423 auto It = CallsIn.find(Caller.ID);
2424 assert(It != CallsIn.end());
2425 if (auto CHI = symbolToCallHierarchyItem(Caller, Item.uri.file())) {
2426 std::vector<Range> FromRanges;
2427 for (const Location &L : It->second) {
2428 if (L.uri != CHI->uri) {
2429 // Call location not in same file as caller.
2430 // This can happen in some edge cases. There's not much we can do,
2431 // since the protocol only allows returning ranges interpreted as
2432 // being in the caller's file.
2433 continue;
2434 }
2435 FromRanges.push_back(L.range);
2436 }
2437 Results.push_back(CallHierarchyIncomingCall{
2438 std::move(*CHI), std::move(FromRanges), MightNeverCall});
2439 }
2440 });
2441 };
2442 QueryIndex({ID.get()}, false);
2443 // In the case of being a virtual function we also want to return
2444 // potential calls through the base function.
2445 if (Item.kind == SymbolKind::Method) {
2446 llvm::DenseSet<SymbolID> IDs;
2447 RelationsRequest Req{{ID.get()}, RelationKind::OverriddenBy, std::nullopt};
2448 Index->reverseRelations(Req, [&](const SymbolID &, const Symbol &Caller) {
2449 IDs.insert(Caller.ID);
2450 });
2451 QueryIndex(std::move(IDs), true);
2452 }
2453 // Sort results by name of container.
2454 llvm::sort(Results, [](const CallHierarchyIncomingCall &A,
2455 const CallHierarchyIncomingCall &B) {
2456 return A.from.name < B.from.name;
2457 });
2458 return Results;
2459}
2460
2461std::vector<CallHierarchyOutgoingCall>
2462outgoingCalls(const CallHierarchyItem &Item, const SymbolIndex *Index) {
2463 std::vector<CallHierarchyOutgoingCall> Results;
2464 if (!Index || Item.data.empty())
2465 return Results;
2466 auto ID = SymbolID::fromStr(Item.data);
2467 if (!ID) {
2468 elog("outgoingCalls failed to find symbol: {0}", ID.takeError());
2469 return Results;
2470 }
2471 // In this function, we find outgoing calls based on the index only.
2472 ContainedRefsRequest Request;
2473 Request.ID = *ID;
2474 // Initially store the ranges in a map keyed by SymbolID of the callee.
2475 // This allows us to group different calls to the same function
2476 // into the same CallHierarchyOutgoingCall.
2477 llvm::DenseMap<SymbolID, std::vector<Location>> CallsOut;
2478 // We can populate the ranges based on a refs request only. As we do so, we
2479 // also accumulate the callee IDs into a lookup request.
2480 LookupRequest CallsOutLookup;
2481 Index->containedRefs(Request, [&](const auto &R) {
2482 auto Loc = indexToLSPLocation(R.Location, Item.uri.file());
2483 if (!Loc) {
2484 elog("outgoingCalls failed to convert location: {0}", Loc.takeError());
2485 return;
2486 }
2487 auto It = CallsOut.try_emplace(R.Symbol, std::vector<Location>{}).first;
2488 It->second.push_back(*Loc);
2489
2490 CallsOutLookup.IDs.insert(R.Symbol);
2491 });
2492 // Perform the lookup request and combine its results with CallsOut to
2493 // get complete CallHierarchyOutgoingCall objects.
2494 Index->lookup(CallsOutLookup, [&](const Symbol &Callee) {
2495 // The containedRefs request should only return symbols which are
2496 // function-like, i.e. symbols for which references to them can be "calls".
2497 using SK = index::SymbolKind;
2498 auto Kind = Callee.SymInfo.Kind;
2499 assert(Kind == SK::Function || Kind == SK::InstanceMethod ||
2500 Kind == SK::ClassMethod || Kind == SK::StaticMethod ||
2501 Kind == SK::Constructor || Kind == SK::Destructor ||
2502 Kind == SK::ConversionFunction);
2503 (void)Kind;
2504 (void)SK::Function;
2505
2506 auto It = CallsOut.find(Callee.ID);
2507 assert(It != CallsOut.end());
2508 if (auto CHI = symbolToCallHierarchyItem(Callee, Item.uri.file())) {
2509 std::vector<Range> FromRanges;
2510 for (const Location &L : It->second) {
2511 if (L.uri != Item.uri) {
2512 // Call location not in same file as the item that outgoingCalls was
2513 // requested for. This can happen when Item is a declaration separate
2514 // from the implementation. There's not much we can do, since the
2515 // protocol only allows returning ranges interpreted as being in
2516 // Item's file.
2517 continue;
2518 }
2519 FromRanges.push_back(L.range);
2520 }
2521 Results.push_back(
2522 CallHierarchyOutgoingCall{std::move(*CHI), std::move(FromRanges)});
2523 }
2524 });
2525 // Sort results by name of the callee.
2526 llvm::sort(Results, [](const CallHierarchyOutgoingCall &A,
2527 const CallHierarchyOutgoingCall &B) {
2528 return A.to.name < B.to.name;
2529 });
2530 return Results;
2531}
2532
2533llvm::DenseSet<const Decl *> getNonLocalDeclRefs(ParsedAST &AST,
2534 const FunctionDecl *FD) {
2535 if (!FD->hasBody())
2536 return {};
2537 llvm::DenseSet<const Decl *> DeclRefs;
2539 FD,
2540 [&](ReferenceLoc Ref) {
2541 for (const Decl *D : Ref.Targets) {
2542 if (!index::isFunctionLocalSymbol(D) && !D->isTemplateParameter() &&
2543 !Ref.IsDecl)
2544 DeclRefs.insert(D);
2545 }
2546 },
2547 AST.getHeuristicResolver());
2548 return DeclRefs;
2549}
2550
2551} // namespace clangd
2552} // namespace clang
Include Cleaner is clangd functionality for providing diagnostics for misuse of transitive headers an...
#define dlog(...)
Definition Logger.h:101
void elog(const char *Fmt, Ts &&... Vals)
Definition Logger.h:61
Stores and provides access to parsed AST.
Definition ParsedAST.h:46
static bool createEach(ASTContext &AST, const syntax::TokenBuffer &Tokens, unsigned Begin, unsigned End, llvm::function_ref< bool(SelectionTree)> Func)
static const Decl * getRefContainer(const Decl *Enclosing, const SymbolCollector::Options &Opts)
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
Definition SymbolID.cpp:37
std::string str() const
Definition SymbolID.cpp:35
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.
virtual bool containedRefs(const ContainedRefsRequest &Req, llvm::function_ref< void(const ContainedRefsResult &)> Callback) const =0
Find all symbols that are referenced by a symbol and apply Callback on each result.
virtual void relations(const RelationsRequest &Req, llvm::function_ref< void(const SymbolID &Subject, const Symbol &Object)> Callback) const =0
Finds all relations (S, P, O) stored in the index such that S is among Req.Subjects and P is Req....
virtual bool refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const =0
Finds all occurrences (e.g.
virtual void lookup(const LookupRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Looks up symbols with any of the given symbol IDs and applies Callback on each matched symbol.
virtual void reverseRelations(const RelationsRequest &Req, llvm::function_ref< void(const SymbolID &Subject, const Symbol &Object)> Callback) const =0
Finds all relations (O, P, S) stored in the index such that S is among Req.Subjects and P is Req....
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
std::vector< TypeHierarchyItem > subTypes(const TypeHierarchyItem &Item, const SymbolIndex *Index)
Returns direct children of a TypeHierarchyItem.
Definition XRefs.cpp:2330
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
std::optional< std::vector< TypeHierarchyItem > > superTypes(const TypeHierarchyItem &Item, const SymbolIndex *Index)
Returns direct parents of a TypeHierarchyItem using SymbolIDs stored inside the item.
Definition XRefs.cpp:2309
llvm::Expected< Location > indexToLSPLocation(const SymbolLocation &Loc, llvm::StringRef TUPath)
Ensure we have enough bits to represent all SymbolTag values.
std::vector< CallHierarchyIncomingCall > incomingCalls(const CallHierarchyItem &Item, const SymbolIndex *Index)
Definition XRefs.cpp:2380
bool isLikelyForwardingFunction(const FunctionTemplateDecl *FT)
Heuristic that checks if FT is likely to be forwarding a parameter pack to another function (e....
Definition AST.cpp:1042
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
Definition AST.cpp:353
static std::optional< TypeHierarchyItem > symbolToTypeHierarchyItem(const Symbol &S, PathRef TUPath)
Definition XRefs.cpp:1885
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.
static std::optional< CallHierarchyItem > symbolToCallHierarchyItem(const Symbol &S, PathRef TUPath)
Definition XRefs.cpp:1895
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
llvm::SmallVector< std::pair< const NamedDecl *, DeclRelationSet >, 1 > allTargetDecls(const DynTypedNode &N, const HeuristicResolver *Resolver)
Similar to targetDecl(), however instead of applying a filter, all possible decls are returned along ...
std::vector< DocumentHighlight > findDocumentHighlights(ParsedAST &AST, Position Pos)
Returns highlights for all usages of a symbol at Pos.
Definition XRefs.cpp:1321
llvm::SmallVector< const NamedDecl *, 1 > explicitReferenceTargets(DynTypedNode N, DeclRelationSet Mask, const HeuristicResolver *Resolver)
Find declarations explicitly referenced in the source code defined by N.
std::vector< LocatedSymbol > locateSymbolTextually(const SpelledWord &Word, ParsedAST &AST, const SymbolIndex *Index, llvm::StringRef MainFilePath, ASTNodeKind NodeKind)
Definition XRefs.cpp:583
std::vector< DocumentLink > getDocumentLinks(ParsedAST &AST)
Get all document links.
Definition XRefs.cpp:865
Symbol mergeSymbol(const Symbol &L, const Symbol &R)
Definition Merge.cpp:266
std::vector< SymbolDetails > getSymbolInfo(ParsedAST &AST, Position Pos)
Get info about symbols at Pos.
Definition XRefs.cpp:1710
std::vector< include_cleaner::SymbolReference > collectMacroReferences(ParsedAST &AST)
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.
SourceLocation nameLocation(const clang::Decl &D, const SourceManager &SM)
Find the source location of the identifier for D.
Definition AST.cpp:196
void vlog(const char *Fmt, Ts &&... Vals)
Definition Logger.h:72
include_cleaner::Includes convertIncludes(const ParsedAST &AST)
Converts the clangd include representation to include-cleaner include representation.
std::vector< LocatedSymbol > findType(ParsedAST &AST, Position Pos, const SymbolIndex *Index)
Returns symbols for types referenced at Pos.
Definition XRefs.cpp:2188
void findExplicitReferences(const Stmt *S, llvm::function_ref< void(ReferenceLoc)> Out, const HeuristicResolver *Resolver)
Recursively traverse S and report all references explicitly written in the code.
static QualType typeForNode(const ASTContext &Ctx, const HeuristicResolver *H, const SelectionTree::Node *N)
Definition XRefs.cpp:2012
std::vector< TypeHierarchyItem > getTypeHierarchy(ParsedAST &AST, Position Pos, int ResolveLevels, TypeHierarchyDirection Direction, const SymbolIndex *Index, PathRef TUPath)
Get type hierarchy information at Pos.
Definition XRefs.cpp:2265
static std::optional< TypeHierarchyItem > declToTypeHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath)
Definition XRefs.cpp:1838
std::optional< QualType > getDeducedType(ASTContext &ASTCtx, const HeuristicResolver *Resolver, SourceLocation Loc)
Retrieves the deduced type at a given location (auto, decltype).
Definition AST.cpp:622
llvm::SmallVector< const NamedDecl *, 1 > targetDecl(const DynTypedNode &N, DeclRelationSet Mask, const HeuristicResolver *Resolver)
targetDecl() finds the declaration referred to by an AST node.
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit, const SymbolIndex *Index, bool AddContext)
Returns references of the symbol at a specified Pos.
Definition XRefs.cpp:1487
static void fillSuperTypes(const CXXRecordDecl &CXXRD, llvm::StringRef TUPath, TypeHierarchyItem &Item, RecursionProtectionSet &RPSet)
Definition XRefs.cpp:1926
std::optional< DefinedMacro > locateMacroAt(const syntax::Token &SpelledTok, Preprocessor &PP)
Gets the macro referenced by SpelledTok.
std::vector< LocatedSymbol > locateSymbolAt(ParsedAST &AST, Position Pos, const SymbolIndex *Index)
Get definition of symbol at a specified Pos.
Definition XRefs.cpp:784
std::vector< std::string > visibleNamespaces(llvm::StringRef Code, const LangOptions &LangOpts)
Heuristically determine namespaces visible at a point, without parsing Code.
static std::optional< HierarchyItem > declToHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath)
Definition XRefs.cpp:1793
std::optional< std::string > getCanonicalPath(const FileEntryRef F, FileManager &FileMgr)
Get the canonical path of F.
static void unwrapFindType(QualType T, const HeuristicResolver *H, llvm::SmallVector< QualType > &Out)
Definition XRefs.cpp:2141
static std::optional< HierarchyItem > symbolToHierarchyItem(const Symbol &S, PathRef TUPath)
Definition XRefs.cpp:1864
SmallVector< const CXXConstructorDecl *, 1 > searchConstructorsInForwardingFunction(const FunctionDecl *FD)
Only call if FD is a likely forwarding function.
Definition AST.cpp:1109
const syntax::Token * findNearbyIdentifier(const SpelledWord &Word, const syntax::TokenBuffer &TB)
Definition XRefs.cpp:693
llvm::SmallPtrSet< const CXXRecordDecl *, 4 > RecursionProtectionSet
Definition XRefs.cpp:1923
static void fillSubTypes(const SymbolID &ID, std::vector< TypeHierarchyItem > &SubTypes, const SymbolIndex *Index, int Levels, PathRef TUPath)
Definition XRefs.cpp:1905
void log(const char *Fmt, Ts &&... Vals)
Definition Logger.h:67
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
SymbolKind indexSymbolKindToSymbolKind(index::SymbolKind Kind)
Definition Protocol.cpp:298
llvm::Expected< SourceLocation > sourceLocationInMainFile(const SourceManager &SM, Position P)
Return the file location, corresponding to P.
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition Path.h:29
@ Type
An inlay hint that for a type annotation.
Definition Protocol.h:1710
std::vector< LocatedSymbol > findImplementations(ParsedAST &AST, Position Pos, const SymbolIndex *Index)
Returns implementations at a specified Pos:
Definition XRefs.cpp:1362
const ObjCImplDecl * getCorrespondingObjCImpl(const ObjCContainerDecl *D)
Return the corresponding implementation/definition for the given ObjC container if it has one,...
Definition AST.cpp:370
void resolveTypeHierarchy(TypeHierarchyItem &Item, int ResolveLevels, TypeHierarchyDirection Direction, const SymbolIndex *Index)
Definition XRefs.cpp:2339
llvm::DenseSet< const Decl * > getNonLocalDeclRefs(ParsedAST &AST, const FunctionDecl *FD)
Returns all decls that are referenced in the FD except local symbols.
Definition XRefs.cpp:2533
clangd::Range rangeTillEOL(llvm::StringRef Code, unsigned HashOffset)
Returns the range starting at offset and spanning the whole line.
float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance)
Combine symbol quality and relevance into a single score.
Definition Quality.cpp:534
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
Definition AST.cpp:206
std::vector< CallHierarchyOutgoingCall > outgoingCalls(const CallHierarchyItem &Item, const SymbolIndex *Index)
Definition XRefs.cpp:2462
void elog(const char *Fmt, Ts &&... Vals)
Definition Logger.h:61
@ Underlying
This is the underlying declaration for a renaming-alias, decltype etc.
Definition FindTarget.h:121
@ TemplatePattern
This is the pattern the template specialization was instantiated from.
Definition FindTarget.h:104
@ Alias
This declaration is an alias that was referred to.
Definition FindTarget.h:112
static std::optional< CallHierarchyItem > declToCallHierarchyItem(const NamedDecl &ND, llvm::StringRef TUPath)
Definition XRefs.cpp:1852
std::vector< const CXXRecordDecl * > findRecordTypeAt(ParsedAST &AST, Position Pos)
Find the record types referenced at Pos.
Definition XRefs.cpp:1957
std::vector< CallHierarchyItem > prepareCallHierarchy(ParsedAST &AST, Position Pos, PathRef TUPath)
Get call hierarchy information at Pos.
Definition XRefs.cpp:2354
std::vector< const CXXRecordDecl * > typeParents(const CXXRecordDecl *CXXRD)
Given a record type declaration, find its base (parent) types.
Definition XRefs.cpp:2223
SymbolKind
A symbol kind.
Definition Protocol.h:380
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccessCheck P
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Represents an incoming call, e.g. a caller of a method or constructor.
Definition Protocol.h:1655
Represents programming constructs like functions or constructors in the context of call hierarchy.
Definition Protocol.h:1615
URIForFile uri
The resource identifier of this item.
Definition Protocol.h:1629
SymbolKind kind
The kind of this item.
Definition Protocol.h:1620
std::string data
An optional 'data' field, which can be used to identify a call hierarchy item in an incomingCalls or ...
Definition Protocol.h:1642
Represents an outgoing call, e.g.
Definition Protocol.h:1680
A document highlight is a range inside a text document which deserves special attention.
Definition Protocol.h:1503
Range range
The range this highlight applies to.
Definition Protocol.h:1505
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
std::vector< std::string > ProximityPaths
Contextually relevant files (e.g.
Definition Index.h:47
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
Location PreferredDeclaration
Definition XRefs.h:45
std::optional< Location > Definition
Definition XRefs.h:47
URIForFile uri
The text document's URI.
Definition Protocol.h:213
llvm::DenseSet< SymbolID > IDs
Definition Index.h:65
Represents a symbol occurrence in the source file.
Definition Ref.h:88
RefKind Kind
Definition Ref.h:91
SymbolID Container
The ID of the symbol whose definition contains this reference.
Definition Ref.h:95
SymbolLocation Location
The source location where the symbol is named.
Definition Ref.h:90
Information about a reference written in the source code, independent of the actual AST node that thi...
Definition FindTarget.h:128
std::optional< std::string > containerName
clangd extension: contains the name of the function or class in which the reference occurs
Definition Protocol.h:236
std::vector< Reference > References
Definition XRefs.h:94
bool WantContainer
If set, populates the container of the reference.
Definition Index.h:77
llvm::DenseSet< SymbolID > IDs
Definition Index.h:69
std::optional< uint32_t > Limit
If set, limit the number of refers returned from the index.
Definition Index.h:74
llvm::DenseSet< SymbolID > Subjects
Definition Index.h:94
const DeclContext & getDeclContext() const
static std::optional< SpelledWord > touching(SourceLocation SpelledLoc, const syntax::TokenBuffer &TB, const LangOptions &LangOpts)
const syntax::Token * ExpandedToken
Definition SourceCode.h:256
const syntax::Token * PartOfSpelledToken
Definition SourceCode.h:251
Represents information about identifier.
Definition Protocol.h:1203
std::optional< Location > definitionRange
Definition Protocol.h:1219
std::optional< Location > declarationRange
Definition Protocol.h:1217
std::string USR
Unified Symbol Resolution identifier This is an opaque string uniquely identifying a symbol.
Definition Protocol.h:1213
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
void merge(const CodeCompletionResult &SemaResult)
Definition Quality.cpp:329
enum clang::clangd::SymbolRelevanceSignals::QueryType Query
The class presents a C++ symbol, e.g.
Definition Symbol.h:39
SymbolFlag Flags
Definition Symbol.h:151
@ Deprecated
Indicates if the symbol is deprecated.
Definition Symbol.h:143
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
SymbolID ID
The ID of the symbol.
Definition Symbol.h:41
std::optional< std::vector< ResolveParams > > parents
std::nullopt means parents aren't resolved and empty is no parents.
Definition Protocol.h:1569
URIForFile uri
The resource identifier of this item.
Definition Protocol.h:1555
std::optional< std::vector< TypeHierarchyItem > > children
If this type hierarchy item is resolved, it contains the direct children of the current item.
Definition Protocol.h:1588
std::optional< std::vector< TypeHierarchyItem > > parents
This is a clangd exntesion.
Definition Protocol.h:1582
ResolveParams data
A data entry field that is preserved between a type hierarchy prepare and supertypes or subtypes requ...
Definition Protocol.h:1575
std::string uri() const
Definition Protocol.h:107
static URIForFile canonicalize(llvm::StringRef AbsPath, llvm::StringRef TUPath)
Canonicalizes AbsPath via URI.
Definition Protocol.cpp:46
llvm::StringRef file() const
Retrieves absolute path to the file.
Definition Protocol.h:104
Represents measurements of clangd events, e.g.
Definition Trace.h:38
@ Counter
An aggregate number whose rate of change over time is meaningful.
Definition Trace.h:46