clang-tools 23.0.0git
SymbolCollector.cpp
Go to the documentation of this file.
1//===--- SymbolCollector.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
9#include "SymbolCollector.h"
10#include "AST.h"
11#include "CodeComplete.h"
13#include "ExpectedTypes.h"
14#include "SourceCode.h"
15#include "URI.h"
16#include "clang-include-cleaner/Analysis.h"
17#include "clang-include-cleaner/IncludeSpeller.h"
18#include "clang-include-cleaner/Record.h"
19#include "clang-include-cleaner/Types.h"
21#include "index/Ref.h"
22#include "index/Relation.h"
23#include "index/Symbol.h"
24#include "index/SymbolID.h"
26#include "clang/AST/Decl.h"
27#include "clang/AST/DeclBase.h"
28#include "clang/AST/DeclCXX.h"
29#include "clang/AST/DeclObjC.h"
30#include "clang/AST/DeclTemplate.h"
31#include "clang/AST/DeclarationName.h"
32#include "clang/AST/Expr.h"
33#include "clang/Basic/FileEntry.h"
34#include "clang/Basic/LangOptions.h"
35#include "clang/Basic/SourceLocation.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Index/IndexSymbol.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/Token.h"
40#include "clang/Tooling/Inclusions/HeaderAnalysis.h"
41#include "clang/Tooling/Inclusions/StandardLibrary.h"
42#include "llvm/ADT/ArrayRef.h"
43#include "llvm/ADT/DenseMap.h"
44#include "llvm/ADT/SmallVector.h"
45#include "llvm/ADT/StringRef.h"
46#include "llvm/Support/Casting.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/Path.h"
49#include <cassert>
50#include <memory>
51#include <optional>
52#include <string>
53#include <utility>
54
55namespace clang {
56namespace clangd {
57namespace {
58
59/// If \p ND is a template specialization, returns the described template.
60/// Otherwise, returns \p ND.
61const NamedDecl &getTemplateOrThis(const NamedDecl &ND) {
62 if (auto *T = ND.getDescribedTemplate())
63 return *T;
64 return ND;
65}
66
67// Checks whether the decl is a private symbol in a header generated by
68// protobuf compiler.
69// FIXME: make filtering extensible when there are more use cases for symbol
70// filters.
71bool isPrivateProtoDecl(const NamedDecl &ND) {
72 const auto &SM = ND.getASTContext().getSourceManager();
73 if (!isProtoFile(nameLocation(ND, SM), SM))
74 return false;
75
76 // ND without identifier can be operators.
77 if (ND.getIdentifier() == nullptr)
78 return false;
79 auto Name = ND.getIdentifier()->getName();
80 // There are some internal helpers like _internal_set_foo();
81 if (Name.contains("_internal_"))
82 return true;
83
84 // https://protobuf.dev/reference/cpp/cpp-generated/#nested-types
85 // Nested entities (messages/enums) has two names, one at the top-level scope,
86 // with a mangled name created by prepending all the outer types. These names
87 // are almost never preferred by the developers, so exclude them from index.
88 // e.g.
89 // message Foo {
90 // message Bar {}
91 // enum E { A }
92 // }
93 //
94 // yields:
95 // class Foo_Bar {};
96 // enum Foo_E { Foo_E_A };
97 // class Foo {
98 // using Bar = Foo_Bar;
99 // static constexpr Foo_E A = Foo_E_A;
100 // };
101
102 // We get rid of Foo_Bar and Foo_E by discarding any top-level entries with
103 // `_` in the name. This relies on original message/enum not having `_` in the
104 // name. Hence might go wrong in certain cases.
105 if (ND.getDeclContext()->isNamespace()) {
106 // Strip off some known public suffix helpers for enums, rest of the helpers
107 // are generated inside record decls so we don't care.
108 // https://protobuf.dev/reference/cpp/cpp-generated/#enum
109 Name.consume_back("_descriptor");
110 Name.consume_back("_IsValid");
111 Name.consume_back("_Name");
112 Name.consume_back("_Parse");
113 Name.consume_back("_MIN");
114 Name.consume_back("_MAX");
115 Name.consume_back("_ARRAYSIZE");
116 return Name.contains('_');
117 }
118
119 // EnumConstantDecls need some special attention, despite being nested in a
120 // TagDecl, they might still have mangled names. We filter those by checking
121 // if it has parent's name as a prefix.
122 // This might go wrong if a nested entity has a name that starts with parent's
123 // name, e.g: enum Foo { Foo_X }.
124 if (llvm::isa<EnumConstantDecl>(&ND)) {
125 auto *DC = llvm::cast<EnumDecl>(ND.getDeclContext());
126 if (!DC || !DC->getIdentifier())
127 return false;
128 auto CtxName = DC->getIdentifier()->getName();
129 return !CtxName.empty() && Name.consume_front(CtxName) &&
130 Name.consume_front("_");
131 }
132
133 // Now we're only left with fields/methods without an `_internal_` in the
134 // name, they're intended for public use.
135 return false;
136}
137
138// We only collect #include paths for symbols that are suitable for global code
139// completion, except for namespaces since #include path for a namespace is hard
140// to define.
141Symbol::IncludeDirective shouldCollectIncludePath(index::SymbolKind Kind) {
142 using SK = index::SymbolKind;
143 switch (Kind) {
144 case SK::Macro:
145 case SK::Enum:
146 case SK::Struct:
147 case SK::Class:
148 case SK::Union:
149 case SK::TypeAlias:
150 case SK::Using:
151 case SK::Function:
152 case SK::Variable:
153 case SK::EnumConstant:
154 case SK::Concept:
156 case SK::Protocol:
157 return Symbol::Import;
158 default:
159 return Symbol::Invalid;
160 }
161}
162
163// Return the symbol range of the token at \p TokLoc.
164std::pair<SymbolLocation::Position, SymbolLocation::Position>
165getTokenRange(SourceLocation TokLoc, const SourceManager &SM,
166 const LangOptions &LangOpts) {
167 auto CreatePosition = [&SM](SourceLocation Loc) {
168 auto LSPLoc = sourceLocToPosition(SM, Loc);
170 Pos.setLine(LSPLoc.line);
171 Pos.setColumn(LSPLoc.character);
172 return Pos;
173 };
174
175 auto TokenLength = clang::Lexer::MeasureTokenLength(TokLoc, SM, LangOpts);
176 return {CreatePosition(TokLoc),
177 CreatePosition(TokLoc.getLocWithOffset(TokenLength))};
178}
179
180// Checks whether \p ND is a good candidate to be the *canonical* declaration of
181// its symbol (e.g. a go-to-declaration target). This overrides the default of
182// using Clang's canonical declaration, which is the first in the TU.
183//
184// Example: preferring a class declaration over its forward declaration.
185bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
186 const auto &SM = ND.getASTContext().getSourceManager();
187 if (isa<TagDecl>(ND))
188 return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
189 !isInsideMainFile(ND.getLocation(), SM);
190 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(&ND))
191 return ID->isThisDeclarationADefinition();
192 if (const auto *PD = dyn_cast<ObjCProtocolDecl>(&ND))
193 return PD->isThisDeclarationADefinition();
194 return false;
195}
196
197RefKind toRefKind(index::SymbolRoleSet Roles, bool Spelled = false) {
198 RefKind Result = RefKind::Unknown;
199 if (Roles & static_cast<unsigned>(index::SymbolRole::Declaration))
200 Result |= RefKind::Declaration;
201 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
202 Result |= RefKind::Definition;
203 if (Roles & static_cast<unsigned>(index::SymbolRole::Reference))
204 Result |= RefKind::Reference;
205 if (Spelled)
206 Result |= RefKind::Spelled;
207 return Result;
208}
209
210std::optional<RelationKind> indexableRelation(const index::SymbolRelation &R) {
211 if (R.Roles & static_cast<unsigned>(index::SymbolRole::RelationBaseOf))
213 if (R.Roles & static_cast<unsigned>(index::SymbolRole::RelationOverrideOf))
215 return std::nullopt;
216}
217
218// Check if there is an exact spelling of \p ND at \p Loc.
219bool isSpelled(SourceLocation Loc, const NamedDecl &ND) {
220 auto Name = ND.getDeclName();
221 const auto NameKind = Name.getNameKind();
222 if (NameKind != DeclarationName::Identifier &&
223 NameKind != DeclarationName::CXXConstructorName &&
224 NameKind != DeclarationName::ObjCZeroArgSelector &&
225 NameKind != DeclarationName::ObjCOneArgSelector &&
226 NameKind != DeclarationName::ObjCMultiArgSelector)
227 return false;
228 const auto &AST = ND.getASTContext();
229 const auto &SM = AST.getSourceManager();
230 const auto &LO = AST.getLangOpts();
231 clang::Token Tok;
232 if (clang::Lexer::getRawToken(Loc, Tok, SM, LO))
233 return false;
234 auto TokSpelling = clang::Lexer::getSpelling(Tok, SM, LO);
235 if (const auto *MD = dyn_cast<ObjCMethodDecl>(&ND))
236 return TokSpelling == MD->getSelector().getNameForSlot(0);
237 return TokSpelling == Name.getAsString();
238}
239} // namespace
240
241// Encapsulates decisions about how to record header paths in the index,
242// including filename normalization, URI conversion etc.
243// Expensive checks are cached internally.
245 struct FrameworkUmbrellaSpelling {
246 // Spelling for the public umbrella header, e.g. <Foundation/Foundation.h>
247 std::optional<std::string> PublicHeader;
248 // Spelling for the private umbrella header, e.g.
249 // <Foundation/Foundation_Private.h>
250 std::optional<std::string> PrivateHeader;
251 };
252 // Weird double-indirect access to PP, which might not be ready yet when
253 // HeaderFiles is created but will be by the time it's used.
254 // (IndexDataConsumer::setPreprocessor can happen before or after initialize)
255 Preprocessor *&PP;
256 const SourceManager &SM;
257 const include_cleaner::PragmaIncludes *PI;
258 llvm::StringRef FallbackDir;
259 llvm::DenseMap<const FileEntry *, const std::string *> CacheFEToURI;
260 llvm::StringMap<std::string> CachePathToURI;
261 llvm::DenseMap<FileID, llvm::StringRef> CacheFIDToInclude;
262 llvm::StringMap<std::string> CachePathToFrameworkSpelling;
263 llvm::StringMap<FrameworkUmbrellaSpelling>
264 CacheFrameworkToUmbrellaHeaderSpelling;
265
266public:
267 HeaderFileURICache(Preprocessor *&PP, const SourceManager &SM,
268 const SymbolCollector::Options &Opts)
269 : PP(PP), SM(SM), PI(Opts.PragmaIncludes), FallbackDir(Opts.FallbackDir) {
270 }
271
272 // Returns a canonical URI for the file \p FE.
273 // We attempt to make the path absolute first.
274 const std::string &toURI(const FileEntryRef FE) {
275 auto R = CacheFEToURI.try_emplace(FE);
276 if (R.second) {
277 auto CanonPath = getCanonicalPath(FE, SM.getFileManager());
278 R.first->second = &toURIInternal(CanonPath ? *CanonPath : FE.getName());
279 }
280 return *R.first->second;
281 }
282
283 // Returns a canonical URI for \p Path.
284 // If the file is in the FileManager, use that to canonicalize the path.
285 // We attempt to make the path absolute in any case.
286 const std::string &toURI(llvm::StringRef Path) {
287 if (auto File = SM.getFileManager().getFileRef(Path))
288 return toURI(*File);
289 return toURIInternal(Path);
290 }
291
292 // Gets a canonical include (URI of the header or <header> or "header") for
293 // header of \p FID (which should usually be the *expansion* file).
294 // This does not account for any per-symbol overrides!
295 // Returns "" if includes should not be inserted for this file.
296 llvm::StringRef getIncludeHeader(FileID FID) {
297 auto R = CacheFIDToInclude.try_emplace(FID);
298 if (R.second)
299 R.first->second = getIncludeHeaderUncached(FID);
300 return R.first->second;
301 }
302
303 // If a file is mapped by canonical headers, use that mapping, regardless
304 // of whether it's an otherwise-good header (header guards etc).
305 llvm::StringRef mapCanonical(llvm::StringRef HeaderPath) {
306 if (!PP)
307 return "";
308 // Populate the system header mapping as late as possible to
309 // ensure the preprocessor has been set already.
310 CanonicalIncludes SysHeaderMapping;
311 SysHeaderMapping.addSystemHeadersMapping(PP->getLangOpts());
312 auto Canonical = SysHeaderMapping.mapHeader(HeaderPath);
313 if (Canonical.empty())
314 return "";
315 // If we had a mapping, always use it.
316 assert(Canonical.starts_with("<") || Canonical.starts_with("\""));
317 return Canonical;
318 }
319
320private:
321 // This takes care of making paths absolute and path->URI caching, but no
322 // FileManager-based canonicalization.
323 const std::string &toURIInternal(llvm::StringRef Path) {
324 auto R = CachePathToURI.try_emplace(Path);
325 if (R.second) {
326 llvm::SmallString<256> AbsPath = Path;
327 if (!llvm::sys::path::is_absolute(AbsPath) && !FallbackDir.empty())
328 llvm::sys::path::make_absolute(FallbackDir, AbsPath);
329 assert(llvm::sys::path::is_absolute(AbsPath) &&
330 "If the VFS can't make paths absolute, a FallbackDir must be "
331 "provided");
332 llvm::sys::path::remove_dots(AbsPath, /*remove_dot_dot=*/true);
333 R.first->second = URI::create(AbsPath).toString();
334 }
335 return R.first->second;
336 }
337
338 struct FrameworkHeaderPath {
339 // Path to the frameworks directory containing the .framework directory.
340 llvm::StringRef FrameworkParentDir;
341 // Name of the framework.
342 llvm::StringRef FrameworkName;
343 // Subpath relative to the Headers or PrivateHeaders dir, e.g. NSObject.h
344 // Note: This is NOT relative to the `HeadersParentDir`.
345 llvm::StringRef HeaderSubpath;
346 // Whether this header is under the PrivateHeaders dir
347 bool IsPrivateHeader;
348 };
349
350 std::optional<FrameworkHeaderPath>
351 splitFrameworkHeaderPath(llvm::StringRef Path) {
352 using namespace llvm::sys;
353 path::reverse_iterator I = path::rbegin(Path);
354 path::reverse_iterator Prev = I;
355 path::reverse_iterator E = path::rend(Path);
356 FrameworkHeaderPath HeaderPath;
357 while (I != E) {
358 if (*I == "Headers" || *I == "PrivateHeaders") {
359 HeaderPath.HeaderSubpath = Path.substr(Prev - E);
360 HeaderPath.IsPrivateHeader = *I == "PrivateHeaders";
361 if (++I == E)
362 break;
363 HeaderPath.FrameworkName = *I;
364 if (!HeaderPath.FrameworkName.consume_back(".framework"))
365 break;
366 HeaderPath.FrameworkParentDir = Path.substr(0, I - E);
367 return HeaderPath;
368 }
369 Prev = I;
370 ++I;
371 }
372 // Unexpected, must not be a framework header.
373 return std::nullopt;
374 }
375
376 // Frameworks typically have an umbrella header of the same name, e.g.
377 // <Foundation/Foundation.h> instead of <Foundation/NSObject.h> or
378 // <Foundation/Foundation_Private.h> instead of
379 // <Foundation/NSObject_Private.h> which should be used instead of directly
380 // importing the header.
381 std::optional<std::string>
382 getFrameworkUmbrellaSpelling(const HeaderSearch &HS,
383 FrameworkHeaderPath &HeaderPath) {
384 StringRef Framework = HeaderPath.FrameworkName;
385 auto Res = CacheFrameworkToUmbrellaHeaderSpelling.try_emplace(Framework);
386 auto *CachedSpelling = &Res.first->second;
387 if (!Res.second) {
388 return HeaderPath.IsPrivateHeader ? CachedSpelling->PrivateHeader
389 : CachedSpelling->PublicHeader;
390 }
391 SmallString<256> UmbrellaPath(HeaderPath.FrameworkParentDir);
392 llvm::sys::path::append(UmbrellaPath, Framework + ".framework", "Headers",
393 Framework + ".h");
394
395 if (HS.getFileMgr().getOptionalFileRef(UmbrellaPath))
396 CachedSpelling->PublicHeader = llvm::formatv("<{0}/{0}.h>", Framework);
397
398 UmbrellaPath = HeaderPath.FrameworkParentDir;
399 llvm::sys::path::append(UmbrellaPath, Framework + ".framework",
400 "PrivateHeaders", Framework + "_Private.h");
401
402 if (HS.getFileMgr().getOptionalFileRef(UmbrellaPath))
403 CachedSpelling->PrivateHeader =
404 llvm::formatv("<{0}/{0}_Private.h>", Framework);
405
406 return HeaderPath.IsPrivateHeader ? CachedSpelling->PrivateHeader
407 : CachedSpelling->PublicHeader;
408 }
409
410 // Compute the framework include spelling for `FE` which is in a framework
411 // named `Framework`, e.g. `NSObject.h` in framework `Foundation` would
412 // give <Foundation/Foundation.h> if the umbrella header exists, otherwise
413 // <Foundation/NSObject.h>.
414 std::optional<llvm::StringRef>
415 getFrameworkHeaderIncludeSpelling(FileEntryRef FE, HeaderSearch &HS) {
416 auto Res = CachePathToFrameworkSpelling.try_emplace(FE.getName());
417 auto *CachedHeaderSpelling = &Res.first->second;
418 if (!Res.second)
419 return llvm::StringRef(*CachedHeaderSpelling);
420
421 auto HeaderPath = splitFrameworkHeaderPath(FE.getName());
422 if (!HeaderPath) {
423 // Unexpected: must not be a proper framework header, don't cache the
424 // failure.
425 CachePathToFrameworkSpelling.erase(Res.first);
426 return std::nullopt;
427 }
428 if (auto UmbrellaSpelling =
429 getFrameworkUmbrellaSpelling(HS, *HeaderPath)) {
430 *CachedHeaderSpelling = *UmbrellaSpelling;
431 return llvm::StringRef(*CachedHeaderSpelling);
432 }
433
434 *CachedHeaderSpelling =
435 llvm::formatv("<{0}/{1}>", HeaderPath->FrameworkName,
436 HeaderPath->HeaderSubpath)
437 .str();
438 return llvm::StringRef(*CachedHeaderSpelling);
439 }
440
441 llvm::StringRef getIncludeHeaderUncached(FileID FID) {
442 const auto FE = SM.getFileEntryRefForID(FID);
443 if (!FE || FE->getName().empty())
444 return "";
445
446 if (auto Verbatim = PI->getPublic(*FE); !Verbatim.empty())
447 return Verbatim;
448
449 llvm::StringRef Filename = FE->getName();
450 if (auto Canonical = mapCanonical(Filename); !Canonical.empty())
451 return Canonical;
452
453 // Framework headers are spelled as <FrameworkName/Foo.h>, not
454 // "path/FrameworkName.framework/Headers/Foo.h".
455 auto &HS = PP->getHeaderSearchInfo();
456 if (auto Spelling = getFrameworkHeaderIncludeSpelling(*FE, HS))
457 return *Spelling;
458
459 if (!tooling::isSelfContainedHeader(*FE, PP->getSourceManager(),
460 PP->getHeaderSearchInfo())) {
461 // A .inc or .def file is often included into a real header to define
462 // symbols (e.g. LLVM tablegen files).
463 if (Filename.ends_with(".inc") || Filename.ends_with(".def"))
464 // Don't use cache reentrantly due to iterator invalidation.
465 return getIncludeHeaderUncached(SM.getFileID(SM.getIncludeLoc(FID)));
466 // Conservatively refuse to insert #includes to files without guards.
467 return "";
468 }
469 // Standard case: just insert the file itself.
470 return toURI(*FE);
471 }
472};
473
474// Return the symbol location of the token at \p TokLoc.
475std::optional<SymbolLocation>
476SymbolCollector::getTokenLocation(SourceLocation TokLoc) {
477 const auto &SM = ASTCtx->getSourceManager();
478 const auto FE = SM.getFileEntryRefForID(SM.getFileID(TokLoc));
479 if (!FE)
480 return std::nullopt;
481
482 SymbolLocation Result;
483 Result.FileURI = HeaderFileURIs->toURI(*FE).c_str();
484 auto Range = getTokenRange(TokLoc, SM, ASTCtx->getLangOpts());
485 Result.Start = Range.first;
486 Result.End = Range.second;
487
488 return Result;
489}
490
493
494void SymbolCollector::initialize(ASTContext &Ctx) {
495 ASTCtx = &Ctx;
496 HeaderFileURIs = std::make_unique<HeaderFileURICache>(
497 this->PP, ASTCtx->getSourceManager(), Opts);
498 CompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>();
499 CompletionTUInfo =
500 std::make_unique<CodeCompletionTUInfo>(CompletionAllocator);
501}
502
504 const ASTContext &ASTCtx,
505 const Options &Opts,
506 bool IsMainFileOnly) {
507 // Skip anonymous declarations, e.g (anonymous enum/class/struct).
508 if (ND.getDeclName().isEmpty())
509 return false;
510
511 // Skip main-file symbols if we are not collecting them.
512 if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
513 return false;
514
515 // Skip symbols in anonymous namespaces in header files.
516 if (!IsMainFileOnly && ND.isInAnonymousNamespace())
517 return false;
518
519 // For function local symbols, index only classes and its member functions.
520 if (index::isFunctionLocalSymbol(&ND))
521 return isa<RecordDecl>(ND) ||
522 (ND.isCXXInstanceMember() && ND.isFunctionOrFunctionTemplate());
523
524 // We want most things but not "local" symbols such as symbols inside
525 // FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
526 // FIXME: Need a matcher for ExportDecl in order to include symbols declared
527 // within an export.
528 const auto *DeclCtx = ND.getDeclContext();
529 switch (DeclCtx->getDeclKind()) {
530 case Decl::TranslationUnit:
531 case Decl::Namespace:
532 case Decl::LinkageSpec:
533 case Decl::Enum:
534 case Decl::ObjCProtocol:
535 case Decl::ObjCInterface:
536 case Decl::ObjCCategory:
537 case Decl::ObjCCategoryImpl:
538 case Decl::ObjCImplementation:
539 break;
540 default:
541 // Record has a few derivations (e.g. CXXRecord, Class specialization), it's
542 // easier to cast.
543 if (!isa<RecordDecl>(DeclCtx))
544 return false;
545 }
546
547 // Avoid indexing internal symbols in protobuf generated headers.
548 if (isPrivateProtoDecl(ND))
549 return false;
550
551 // System headers that end with `intrin.h` likely contain useful symbols.
552 if (!Opts.CollectReserved &&
553 (hasReservedName(ND) || hasReservedScope(*ND.getDeclContext())) &&
554 ASTCtx.getSourceManager().isInSystemHeader(ND.getLocation()) &&
555 !ASTCtx.getSourceManager()
556 .getFilename(ND.getLocation())
557 .ends_with("intrin.h"))
558 return false;
559
560 return true;
561}
562
563const Decl *
565 const SymbolCollector::Options &Opts) {
566 while (Enclosing) {
567 const auto *ND = dyn_cast<NamedDecl>(Enclosing);
568 if (ND && shouldCollectSymbol(*ND, ND->getASTContext(), Opts, true)) {
569 break;
570 }
571 Enclosing = dyn_cast_or_null<Decl>(Enclosing->getDeclContext());
572 }
573 return Enclosing;
574}
575
577SymbolCollector::findIndirectConstructors(const Decl *D) {
578 auto *FD = llvm::dyn_cast<clang::FunctionDecl>(D);
579 if (FD == nullptr || !FD->isTemplateInstantiation())
580 return {};
581 if (auto Entry = ForwardingToConstructorCache.find(FD);
582 Entry != ForwardingToConstructorCache.end())
583 return Entry->getSecond();
584 if (auto *PT = FD->getPrimaryTemplate();
585 PT == nullptr || !isLikelyForwardingFunction(PT))
586 return {};
587
590 auto Iter = ForwardingToConstructorCache.try_emplace(
591 FD, std::move(FoundConstructors));
592 return Iter.first->getSecond();
593}
594
595// Always return true to continue indexing.
597 const Decl *D, index::SymbolRoleSet Roles,
598 llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
599 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
600 assert(ASTCtx && PP && HeaderFileURIs);
601 assert(CompletionAllocator && CompletionTUInfo);
602 assert(ASTNode.OrigD);
603 // Indexing API puts canonical decl into D, which might not have a valid
604 // source location for implicit/built-in decls. Fallback to original decl in
605 // such cases.
606 if (D->getLocation().isInvalid())
607 D = ASTNode.OrigD;
608 // If OrigD is an declaration associated with a friend declaration and it's
609 // not a definition, skip it. Note that OrigD is the occurrence that the
610 // collector is currently visiting.
611 if ((ASTNode.OrigD->getFriendObjectKind() !=
612 Decl::FriendObjectKind::FOK_None) &&
613 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
614 return true;
615 // A declaration created for a friend declaration should not be used as the
616 // canonical declaration in the index. Use OrigD instead, unless we've already
617 // picked a replacement for D
618 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
619 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
620 // Flag to mark that D should be considered canonical meaning its declaration
621 // will override any previous declaration for the Symbol.
622 bool DeclIsCanonical = false;
623 // Avoid treating ObjCImplementationDecl as a canonical declaration if it has
624 // a corresponding non-implicit and non-forward declared ObjcInterfaceDecl.
625 if (const auto *IID = dyn_cast<ObjCImplementationDecl>(D)) {
626 DeclIsCanonical = true;
627 if (const auto *CID = IID->getClassInterface())
628 if (const auto *DD = CID->getDefinition())
629 if (!DD->isImplicitInterfaceDecl())
630 D = DD;
631 }
632 // Avoid treating ObjCCategoryImplDecl as a canonical declaration in favor of
633 // its ObjCCategoryDecl if it has one.
634 if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(D)) {
635 DeclIsCanonical = true;
636 if (const auto *CD = CID->getCategoryDecl())
637 D = CD;
638 }
639 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
640 if (!ND)
641 return true;
642
643 auto ID = getSymbolIDCached(ND);
644 if (!ID)
645 return true;
646
647 // Mark D as referenced if this is a reference coming from the main file.
648 // D may not be an interesting symbol, but it's cheaper to check at the end.
649 auto &SM = ASTCtx->getSourceManager();
650 if (Opts.CountReferences &&
651 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
652 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
653 ReferencedSymbols.insert(ID);
654
655 // ND is the canonical (i.e. first) declaration. If it's in the main file
656 // (which is not a header), then no public declaration was visible, so assume
657 // it's main-file only.
658 auto CheckIsMainFileOnly = [&](const NamedDecl *Decl) {
659 return SM.isWrittenInMainFile(SM.getExpansionLoc(Decl->getBeginLoc())) &&
660 !isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
661 ASTCtx->getLangOpts());
662 };
663 bool IsMainFileOnly = CheckIsMainFileOnly(ND);
664 // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
665 if (ASTNode.OrigD->isImplicit() ||
666 !shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))
667 return true;
668
669 // Note: we need to process relations for all decl occurrences, including
670 // refs, because the indexing code only populates relations for specific
671 // occurrences. For example, RelationBaseOf is only populated for the
672 // occurrence inside the base-specifier.
673 processRelations(*ND, ID, Relations);
674
675 bool CollectRef = static_cast<bool>(Opts.RefFilter & toRefKind(Roles));
676 // Unlike other fields, e.g. Symbols (which use spelling locations), we use
677 // file locations for references (as it aligns the behavior of clangd's
678 // AST-based xref).
679 // FIXME: we should try to use the file locations for other fields.
680 if (CollectRef &&
681 (!IsMainFileOnly || Opts.CollectMainFileRefs ||
682 ND->isExternallyVisible()) &&
683 !isa<NamespaceDecl>(ND)) {
684 auto FileLoc = SM.getFileLoc(Loc);
685 auto FID = SM.getFileID(FileLoc);
686 if (Opts.RefsInHeaders || FID == SM.getMainFileID()) {
687 auto *Container = getRefContainer(ASTNode.Parent, Opts);
688 addRef(ID, SymbolRef{FileLoc, FID, Roles, index::getSymbolInfo(ND).Kind,
689 Container, isSpelled(FileLoc, *ND)});
690 // Also collect indirect constructor calls like `make_unique`
691 for (auto *Constructor : findIndirectConstructors(ASTNode.OrigD)) {
692 if (!shouldCollectSymbol(*Constructor, *ASTCtx, Opts,
693 CheckIsMainFileOnly(Constructor)))
694 continue;
695 if (auto ConstructorID = getSymbolIDCached(Constructor))
696 addRef(ConstructorID,
697 SymbolRef{FileLoc, FID, Roles,
698 index::getSymbolInfo(Constructor).Kind, Container,
699 false});
700 }
701 }
702 }
703 // Don't continue indexing if this is a mere reference.
704 if (!(Roles & (static_cast<unsigned>(index::SymbolRole::Declaration) |
705 static_cast<unsigned>(index::SymbolRole::Definition))))
706 return true;
707
708 // FIXME: ObjCPropertyDecl are not properly indexed here:
709 // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
710 // not a NamedDecl.
711 auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
712 if (!OriginalDecl)
713 return true;
714
715 const Symbol *BasicSymbol = Symbols.find(ID);
716 bool SkipDocCheckInDef = false;
717 if (isPreferredDeclaration(*OriginalDecl, Roles)) {
718 // If OriginalDecl is preferred, replace/create the existing canonical
719 // declaration (e.g. a class forward declaration). There should be at most
720 // one duplicate as we expect to see only one preferred declaration per
721 // TU, because in practice they are definitions.
722 BasicSymbol = addDeclaration(*OriginalDecl, std::move(ID), IsMainFileOnly);
723 SkipDocCheckInDef = true;
724 } else if (!BasicSymbol || DeclIsCanonical) {
725 BasicSymbol = addDeclaration(*ND, std::move(ID), IsMainFileOnly);
726 SkipDocCheckInDef = true;
727 }
728
729 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
730 addDefinition(*OriginalDecl, *BasicSymbol, SkipDocCheckInDef);
731
732 return true;
733}
734
735void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) {
736 assert(HeaderFileURIs && PP);
737 const auto &SM = PP->getSourceManager();
738 const auto MainFileEntryRef = SM.getFileEntryRefForID(SM.getMainFileID());
739 assert(MainFileEntryRef);
740
741 const std::string &MainFileURI = HeaderFileURIs->toURI(*MainFileEntryRef);
742 // Add macro references.
743 for (const auto &IDToRefs : MacroRefsToIndex.MacroRefs) {
744 for (const auto &MacroRef : IDToRefs.second) {
745 const auto &SR = MacroRef.toSourceRange(SM);
746 auto Range = halfOpenToRange(SM, SR);
747 bool IsDefinition = MacroRef.IsDefinition;
748 Ref R;
753 R.Location.FileURI = MainFileURI.c_str();
754 R.Kind = IsDefinition ? RefKind::Definition : RefKind::Reference;
755 Refs.insert(IDToRefs.first, R);
756 if (IsDefinition) {
757 Symbol S;
758 S.ID = IDToRefs.first;
759 S.Name = toSourceCode(SM, SR.getAsRange());
760 S.SymInfo.Kind = index::SymbolKind::Macro;
761 S.SymInfo.SubKind = index::SymbolSubKind::None;
762 S.SymInfo.Properties = index::SymbolPropertySet();
763 S.SymInfo.Lang = index::SymbolLanguage::C;
764 S.Origin = Opts.Origin;
766 // Make the macro visible for code completion if main file is an
767 // include-able header.
768 if (!HeaderFileURIs->getIncludeHeader(SM.getMainFileID()).empty()) {
771 }
772 Symbols.insert(S);
773 }
774 }
775 }
776}
777
778bool SymbolCollector::handleMacroOccurrence(const IdentifierInfo *Name,
779 const MacroInfo *MI,
780 index::SymbolRoleSet Roles,
781 SourceLocation Loc) {
782 assert(PP);
783 // Builtin macros don't have useful locations and aren't needed in completion.
784 if (MI->isBuiltinMacro())
785 return true;
786
787 const auto &SM = PP->getSourceManager();
788 auto DefLoc = MI->getDefinitionLoc();
789 // Also avoid storing macros that aren't defined in any file, i.e. predefined
790 // macros like __DBL_MIN__ and those defined on the command line.
791 if (SM.isWrittenInBuiltinFile(DefLoc) ||
792 SM.isWrittenInCommandLineFile(DefLoc) ||
793 Name->getName() == "__GCC_HAVE_DWARF2_CFI_ASM")
794 return true;
795
796 auto ID = getSymbolIDCached(Name->getName(), MI, SM);
797 if (!ID)
798 return true;
799
800 auto SpellingLoc = SM.getSpellingLoc(Loc);
801 bool IsMainFileOnly =
802 SM.isInMainFile(SM.getExpansionLoc(DefLoc)) &&
803 !isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
804 ASTCtx->getLangOpts());
805 // Do not store references to main-file macros.
806 if ((static_cast<unsigned>(Opts.RefFilter) & Roles) && !IsMainFileOnly &&
807 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID())) {
808 // FIXME: Populate container information for macro references.
809 // FIXME: All MacroRefs are marked as Spelled now, but this should be
810 // checked.
811 addRef(ID,
812 SymbolRef{Loc, SM.getFileID(Loc), Roles, index::SymbolKind::Macro,
813 /*Container=*/nullptr,
814 /*Spelled=*/true});
815 }
816
817 // Collect symbols.
818 if (!Opts.CollectMacro)
819 return true;
820
821 // Skip main-file macros if we are not collecting them.
822 if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
823 return false;
824
825 // Mark the macro as referenced if this is a reference coming from the main
826 // file. The macro may not be an interesting symbol, but it's cheaper to check
827 // at the end.
828 if (Opts.CountReferences &&
829 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
830 SM.getFileID(SpellingLoc) == SM.getMainFileID())
831 ReferencedSymbols.insert(ID);
832
833 // Don't continue indexing if this is a mere reference.
834 // FIXME: remove macro with ID if it is undefined.
835 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
836 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
837 return true;
838
839 // Only collect one instance in case there are multiple.
840 if (Symbols.find(ID) != nullptr)
841 return true;
842
843 Symbol S;
844 S.ID = std::move(ID);
845 S.Name = Name->getName();
846 if (!IsMainFileOnly) {
849 }
850 S.SymInfo = index::getSymbolInfoForMacro(*MI);
851 S.Origin = Opts.Origin;
852 // FIXME: use the result to filter out symbols.
853 shouldIndexFile(SM.getFileID(Loc));
854 if (auto DeclLoc = getTokenLocation(DefLoc))
855 S.CanonicalDeclaration = *DeclLoc;
856
857 CodeCompletionResult SymbolCompletion(Name);
858 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
859 *PP, *CompletionAllocator, *CompletionTUInfo);
860 std::string Signature;
861 std::string SnippetSuffix;
862 getSignature(*CCS, &Signature, &SnippetSuffix, SymbolCompletion.Kind,
863 SymbolCompletion.CursorKind);
864 S.Signature = Signature;
865 S.CompletionSnippetSuffix = SnippetSuffix;
866
867 IndexedMacros.insert(Name);
868
869 setIncludeLocation(S, DefLoc, include_cleaner::Macro{Name, DefLoc});
870 Symbols.insert(S);
871 return true;
872}
873
874void SymbolCollector::processRelations(
875 const NamedDecl &ND, const SymbolID &ID,
876 ArrayRef<index::SymbolRelation> Relations) {
877 for (const auto &R : Relations) {
878 auto RKind = indexableRelation(R);
879 if (!RKind)
880 continue;
881 const Decl *Object = R.RelatedSymbol;
882
883 auto ObjectID = getSymbolIDCached(Object);
884 if (!ObjectID)
885 continue;
886
887 // Record the relation.
888 // TODO: There may be cases where the object decl is not indexed for some
889 // reason. Those cases should probably be removed in due course, but for
890 // now there are two possible ways to handle it:
891 // (A) Avoid storing the relation in such cases.
892 // (B) Store it anyways. Clients will likely lookup() the SymbolID
893 // in the index and find nothing, but that's a situation they
894 // probably need to handle for other reasons anyways.
895 // We currently do (B) because it's simpler.
896 if (*RKind == RelationKind::BaseOf)
897 this->Relations.insert({ID, *RKind, ObjectID});
898 else if (*RKind == RelationKind::OverriddenBy)
899 this->Relations.insert({ObjectID, *RKind, ID});
900 }
901}
902
903void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation DefLoc,
904 const include_cleaner::Symbol &Sym) {
905 const auto &SM = PP->getSourceManager();
906 if (!Opts.CollectIncludePath ||
907 shouldCollectIncludePath(S.SymInfo.Kind) == Symbol::Invalid)
908 return;
909
910 // Use the expansion location to get the #include header since this is
911 // where the symbol is exposed.
912 if (FileID FID = SM.getDecomposedExpansionLoc(DefLoc).first; FID.isValid())
913 IncludeFiles[S.ID] = FID;
914
915 // We update providers for a symbol with each occurence, as SymbolCollector
916 // might run while parsing, rather than at the end of a translation unit.
917 // Hence we see more and more redecls over time.
918 SymbolProviders[S.ID] =
919 include_cleaner::headersForSymbol(Sym, *PP, Opts.PragmaIncludes);
920}
921
922llvm::StringRef getStdHeader(const Symbol *S, const LangOptions &LangOpts) {
923 tooling::stdlib::Lang Lang = tooling::stdlib::Lang::CXX;
924 if (LangOpts.C11)
925 Lang = tooling::stdlib::Lang::C;
926 else if(!LangOpts.CPlusPlus)
927 return "";
928
929 if (S->Scope == "std::" && S->Name == "move") {
930 if (!S->Signature.contains(','))
931 return "<utility>";
932 return "<algorithm>";
933 }
934
935 if (auto StdSym = tooling::stdlib::Symbol::named(S->Scope, S->Name, Lang))
936 if (auto Header = StdSym->header())
937 return Header->name();
938 return "";
939}
940
942 // At the end of the TU, add 1 to the refcount of all referenced symbols.
943 for (const auto &ID : ReferencedSymbols) {
944 if (const auto *S = Symbols.find(ID)) {
945 // SymbolSlab::Builder returns const symbols because strings are interned
946 // and modifying returned symbols without inserting again wouldn't go
947 // well. const_cast is safe here as we're modifying a data owned by the
948 // Symbol. This reduces time spent in SymbolCollector by ~1%.
949 ++const_cast<Symbol *>(S)->References;
950 }
951 }
952 if (Opts.CollectMacro) {
953 assert(PP);
954 // First, drop header guards. We can't identify these until EOF.
955 for (const IdentifierInfo *II : IndexedMacros) {
956 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
957 if (auto ID =
958 getSymbolIDCached(II->getName(), MI, PP->getSourceManager()))
959 if (MI->isUsedForHeaderGuard())
960 Symbols.erase(ID);
961 }
962 }
963 llvm::DenseMap<FileID, bool> FileToContainsImportsOrObjC;
964 llvm::DenseMap<include_cleaner::Header, std::string> HeaderSpelling;
965 // Fill in IncludeHeaders.
966 // We delay this until end of TU so header guards are all resolved.
967 for (const auto &[SID, Providers] : SymbolProviders) {
968 const Symbol *S = Symbols.find(SID);
969 if (!S)
970 continue;
971
972 FileID FID = IncludeFiles.lookup(SID);
973 // Determine if the FID is #include'd or #import'ed.
975 auto CollectDirectives = shouldCollectIncludePath(S->SymInfo.Kind);
976 if ((CollectDirectives & Symbol::Include) != 0)
977 Directives |= Symbol::Include;
978 // Only allow #import for symbols from ObjC-like files.
979 if ((CollectDirectives & Symbol::Import) != 0 && FID.isValid()) {
980 auto [It, Inserted] = FileToContainsImportsOrObjC.try_emplace(FID);
981 if (Inserted)
982 It->second = FilesWithObjCConstructs.contains(FID) ||
983 tooling::codeContainsImports(
984 ASTCtx->getSourceManager().getBufferData(FID));
985 if (It->second)
986 Directives |= Symbol::Import;
987 }
988
989 if (Directives == Symbol::Invalid)
990 continue;
991
992 // Use the include location-based logic for Objective-C symbols.
993 if (Directives & Symbol::Import) {
994 llvm::StringRef IncludeHeader = getStdHeader(S, ASTCtx->getLangOpts());
995 if (IncludeHeader.empty())
996 IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
997
998 if (!IncludeHeader.empty()) {
999 auto NewSym = *S;
1000 NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
1001 Symbols.insert(NewSym);
1002 }
1003 // FIXME: use providers from include-cleaner library once it's polished
1004 // for Objective-C.
1005 continue;
1006 }
1007
1008 // For #include's, use the providers computed by the include-cleaner
1009 // library.
1010 assert(Directives == Symbol::Include);
1011 // Ignore providers that are not self-contained, this is especially
1012 // important for symbols defined in the main-file. We want to prefer the
1013 // header, if possible.
1014 // TODO: Limit this to specifically ignore main file, when we're indexing a
1015 // non-header file?
1016 auto SelfContainedProvider =
1017 [this](llvm::ArrayRef<include_cleaner::Header> Providers)
1018 -> std::optional<include_cleaner::Header> {
1019 for (const auto &H : Providers) {
1020 if (H.kind() != include_cleaner::Header::Physical)
1021 return H;
1022 if (tooling::isSelfContainedHeader(H.physical(), PP->getSourceManager(),
1023 PP->getHeaderSearchInfo()))
1024 return H;
1025 }
1026 return std::nullopt;
1027 };
1028 const auto OptionalProvider = SelfContainedProvider(Providers);
1029 if (!OptionalProvider)
1030 continue;
1031 const auto &H = *OptionalProvider;
1032 const auto [SpellingIt, Inserted] = HeaderSpelling.try_emplace(H);
1033 if (Inserted) {
1034 auto &SM = ASTCtx->getSourceManager();
1035 if (H.kind() == include_cleaner::Header::Kind::Physical) {
1036 // FIXME: Get rid of this once include-cleaner has support for system
1037 // headers.
1038 if (auto Canonical =
1039 HeaderFileURIs->mapCanonical(H.physical().getName());
1040 !Canonical.empty())
1041 SpellingIt->second = Canonical;
1042 // For physical files, prefer URIs as spellings might change
1043 // depending on the translation unit.
1044 else if (tooling::isSelfContainedHeader(H.physical(), SM,
1045 PP->getHeaderSearchInfo()))
1046 SpellingIt->second =
1047 HeaderFileURIs->toURI(H.physical());
1048 } else {
1049 SpellingIt->second = include_cleaner::spellHeader(
1050 {H, PP->getHeaderSearchInfo(),
1051 SM.getFileEntryForID(SM.getMainFileID())});
1052 }
1053 }
1054
1055 if (!SpellingIt->second.empty()) {
1056 auto NewSym = *S;
1057 NewSym.IncludeHeaders.push_back({SpellingIt->second, 1, Directives});
1058 Symbols.insert(NewSym);
1059 }
1060 }
1061
1062 ReferencedSymbols.clear();
1063 IncludeFiles.clear();
1064 SymbolProviders.clear();
1065 FilesWithObjCConstructs.clear();
1066}
1067
1068const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
1069 bool IsMainFileOnly) {
1070 auto &Ctx = ND.getASTContext();
1071 auto &SM = Ctx.getSourceManager();
1072
1073 Symbol S;
1074 S.ID = std::move(ID);
1075 std::string QName = printQualifiedName(ND);
1076 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
1077 // for consistency with CodeCompletionString and a clean name/signature split.
1078 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
1079 std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
1080 S.TemplateSpecializationArgs = TemplateSpecializationArgs;
1081
1082 // We collect main-file symbols, but do not use them for code completion.
1083 if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
1085 if (isImplementationDetail(&ND))
1087 if (!IsMainFileOnly)
1089 S.SymInfo = index::getSymbolInfo(&ND);
1090 auto Loc = nameLocation(ND, SM);
1091 assert(Loc.isValid() && "Invalid source location for NamedDecl");
1092 // FIXME: use the result to filter out symbols.
1093 auto FID = SM.getFileID(Loc);
1094 shouldIndexFile(FID);
1095 if (auto DeclLoc = getTokenLocation(Loc))
1096 S.CanonicalDeclaration = *DeclLoc;
1097
1098 S.Origin = Opts.Origin;
1099 if (ND.getAvailability() == AR_Deprecated)
1101
1102 // Add completion info.
1103 // FIXME: we may want to choose a different redecl, or combine from several.
1104 assert(ASTCtx && PP && "ASTContext and Preprocessor must be set.");
1105 // We use the primary template, as clang does during code completion.
1106 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
1107 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
1108 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
1109 *CompletionTUInfo,
1110 /*IncludeBriefComments*/ false);
1111 std::string DocComment;
1112 std::string Documentation;
1113 bool AlreadyHasDoc = S.Flags & Symbol::HasDocComment;
1114 if (!AlreadyHasDoc) {
1115 DocComment = getDocComment(Ctx, SymbolCompletion,
1116 /*CommentsFromHeaders=*/true);
1117 Documentation = formatDocumentation(*CCS, DocComment);
1118 }
1119 const auto UpdateDoc = [&] {
1120 if (!AlreadyHasDoc) {
1121 if (!DocComment.empty())
1123 S.Documentation = Documentation;
1124 }
1125 };
1127 if (Opts.StoreAllDocumentation)
1128 UpdateDoc();
1129 Symbols.insert(S);
1130 return Symbols.find(S.ID);
1131 }
1132 UpdateDoc();
1133 std::string Signature;
1134 std::string SnippetSuffix;
1135 getSignature(*CCS, &Signature, &SnippetSuffix, SymbolCompletion.Kind,
1136 SymbolCompletion.CursorKind);
1137 S.Signature = Signature;
1138 S.CompletionSnippetSuffix = SnippetSuffix;
1139 std::string ReturnType = getReturnType(*CCS);
1140 S.ReturnType = ReturnType;
1141
1142 std::optional<OpaqueType> TypeStorage;
1144 TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
1145 if (TypeStorage)
1146 S.Type = TypeStorage->raw();
1147 }
1148
1149 Symbols.insert(S);
1150 setIncludeLocation(S, ND.getLocation(), include_cleaner::Symbol{ND});
1151 if (S.SymInfo.Lang == index::SymbolLanguage::ObjC)
1152 FilesWithObjCConstructs.insert(FID);
1153 return Symbols.find(S.ID);
1154}
1155
1156void SymbolCollector::addDefinition(const NamedDecl &ND, const Symbol &DeclSym,
1157 bool SkipDocCheck) {
1158 if (DeclSym.Definition)
1159 return;
1160 const auto &SM = ND.getASTContext().getSourceManager();
1161 auto Loc = nameLocation(ND, SM);
1162 shouldIndexFile(SM.getFileID(Loc));
1163 auto DefLoc = getTokenLocation(Loc);
1164 // If we saw some forward declaration, we end up copying the symbol.
1165 // This is not ideal, but avoids duplicating the "is this a definition" check
1166 // in clang::index. We should only see one definition.
1167 if (!DefLoc)
1168 return;
1169 Symbol S = DeclSym;
1170 // FIXME: use the result to filter out symbols.
1171 S.Definition = *DefLoc;
1172
1173 std::string DocComment;
1174 std::string Documentation;
1175 if (!SkipDocCheck && !(S.Flags & Symbol::HasDocComment) &&
1176 (llvm::isa<FunctionDecl>(ND) || llvm::isa<CXXMethodDecl>(ND))) {
1177 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
1178 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
1179 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
1180 *CompletionTUInfo,
1181 /*IncludeBriefComments*/ false);
1182 DocComment = getDocComment(ND.getASTContext(), SymbolCompletion,
1183 /*CommentsFromHeaders=*/true);
1184 if (!S.Documentation.empty())
1185 Documentation = S.Documentation.str() + '\n' + DocComment;
1186 else
1187 Documentation = formatDocumentation(*CCS, DocComment);
1188 if (!DocComment.empty())
1189 S.Flags |= Symbol::HasDocComment;
1190 S.Documentation = Documentation;
1191 }
1192
1193 Symbols.insert(S);
1194}
1195
1197 if (!Opts.FileFilter)
1198 return true;
1199 auto I = FilesToIndexCache.try_emplace(FID);
1200 if (I.second)
1201 I.first->second = Opts.FileFilter(ASTCtx->getSourceManager(), FID);
1202 return I.first->second;
1203}
1204
1205static bool refIsCall(index::SymbolKind Kind) {
1206 using SK = index::SymbolKind;
1207 return Kind == SK::Function || Kind == SK::InstanceMethod ||
1208 Kind == SK::ClassMethod || Kind == SK::StaticMethod ||
1209 Kind == SK::Constructor || Kind == SK::Destructor ||
1210 Kind == SK::ConversionFunction;
1211}
1212
1213void SymbolCollector::addRef(SymbolID ID, const SymbolRef &SR) {
1214 const auto &SM = ASTCtx->getSourceManager();
1215 // FIXME: use the result to filter out references.
1216 shouldIndexFile(SR.FID);
1217 if (const auto FE = SM.getFileEntryRefForID(SR.FID)) {
1218 auto Range = getTokenRange(SR.Loc, SM, ASTCtx->getLangOpts());
1219 Ref R;
1220 R.Location.Start = Range.first;
1221 R.Location.End = Range.second;
1222 R.Location.FileURI = HeaderFileURIs->toURI(*FE).c_str();
1223 R.Kind = toRefKind(SR.Roles, SR.Spelled);
1224 if (refIsCall(SR.Kind)) {
1225 R.Kind |= RefKind::Call;
1226 }
1227 R.Container = getSymbolIDCached(SR.Container);
1228 Refs.insert(ID, R);
1229 }
1230}
1231
1232SymbolID SymbolCollector::getSymbolIDCached(const Decl *D) {
1233 auto It = DeclToIDCache.try_emplace(D);
1234 if (It.second)
1235 It.first->second = getSymbolID(D);
1236 return It.first->second;
1237}
1238
1239SymbolID SymbolCollector::getSymbolIDCached(const llvm::StringRef MacroName,
1240 const MacroInfo *MI,
1241 const SourceManager &SM) {
1242 auto It = MacroToIDCache.try_emplace(MI);
1243 if (It.second)
1244 It.first->second = getSymbolID(MacroName, MI, SM);
1245 return It.first->second;
1246}
1247} // namespace clangd
1248} // namespace clang
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
Maps a definition location onto an include file, based on a set of filename rules.
void addSystemHeadersMapping(const LangOptions &Language)
Adds mapping for system headers and some special symbols (e.g.
llvm::StringRef mapHeader(llvm::StringRef HeaderPath) const
Returns the overridden verbatim spelling for files in Header that can be directly included (i....
static std::optional< OpaqueType > fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R)
Create a type from a code completion result.
void insert(const SymbolID &ID, const Ref &S)
Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
Definition Ref.cpp:36
llvm::StringRef mapCanonical(llvm::StringRef HeaderPath)
HeaderFileURICache(Preprocessor *&PP, const SourceManager &SM, const SymbolCollector::Options &Opts)
const std::string & toURI(const FileEntryRef FE)
const std::string & toURI(llvm::StringRef Path)
bool shouldIndexFile(FileID FID)
Returns true if we are interested in references and declarations from FID.
static bool shouldCollectSymbol(const NamedDecl &ND, const ASTContext &ASTCtx, const Options &Opts, bool IsMainFileSymbol)
Returns true is ND should be collected.
static const Decl * getRefContainer(const Decl *Enclosing, const SymbolCollector::Options &Opts)
bool handleDeclOccurrence(const Decl *D, index::SymbolRoleSet Roles, ArrayRef< index::SymbolRelation > Relations, SourceLocation Loc, index::IndexDataConsumer::ASTNodeInfo ASTNode) override
void handleMacros(const MainFileMacros &MacroRefsToIndex)
void initialize(ASTContext &Ctx) override
bool handleMacroOccurrence(const IdentifierInfo *Name, const MacroInfo *MI, index::SymbolRoleSet Roles, SourceLocation Loc) override
static llvm::Expected< URI > create(llvm::StringRef AbsolutePath, llvm::StringRef Scheme)
Creates a URI for a file in the given scheme.
Definition URI.cpp:208
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
std::string printTemplateSpecializationArgs(const NamedDecl &ND)
Prints template arguments of a decl as written in the source code, including enclosing '<' and '>',...
Definition AST.cpp:287
std::pair< StringRef, StringRef > splitQualifiedName(StringRef QName)
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:1043
SymbolID getSymbolID(const Decl *D)
Gets the symbol ID for a declaration. Returned SymbolID might be null.
Definition AST.cpp:354
std::string formatDocumentation(const CodeCompletionString &CCS, llvm::StringRef DocComment)
Assembles formatted documentation for a completion result.
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
static bool refIsCall(index::SymbolKind Kind)
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM)
Returns true iff Loc is inside the main file.
SourceLocation nameLocation(const clang::Decl &D, const SourceManager &SM)
Find the source location of the identifier for D.
Definition AST.cpp:196
std::string getReturnType(const CodeCompletionString &CCS)
Gets detail to be used as the detail field in an LSP completion item.
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
std::optional< std::string > getCanonicalPath(const FileEntryRef F, FileManager &FileMgr)
Get the canonical path of F.
bool hasReservedName(const Decl &D)
Returns true if this is a NamedDecl with a reserved name.
Definition AST.cpp:444
SmallVector< const CXXConstructorDecl *, 1 > searchConstructorsInForwardingFunction(const FunctionDecl *FD)
Only call if FD is a likely forwarding function.
Definition AST.cpp:1110
llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R)
Returns the source code covered by the source range.
RefKind
Describes the kind of a cross-reference.
Definition Ref.h:28
void getSignature(const CodeCompletionString &CCS, std::string *Signature, std::string *Snippet, CodeCompletionResult::ResultKind ResultKind, CXCursorKind CursorKind, bool IncludeFunctionArguments, std::string *RequiredQualifiers)
Formats the signature for an item, as a display string and snippet.
bool isImplementationDetail(const Decl *D)
Returns true if the declaration is considered implementation detail based on heuristics.
Definition AST.cpp:191
std::string Path
A typedef to represent a file path.
Definition Path.h:26
bool hasReservedScope(const DeclContext &DC)
Returns true if this scope would be written with a reserved name.
Definition AST.cpp:451
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
Definition AST.cpp:206
bool isProtoFile(SourceLocation Loc, const SourceManager &SM)
Returns true if the given location is in a generated protobuf file.
std::string getDocComment(const ASTContext &Ctx, const CodeCompletionResult &Result, bool CommentsFromHeaders)
Gets a minimally formatted documentation comment of Result, with comment markers stripped.
llvm::StringRef getStdHeader(const Symbol *S, const LangOptions &LangOpts)
bool isHeaderFile(llvm::StringRef FileName, std::optional< LangOptions > LangOpts)
Infers whether this is a header from the FileName and LangOpts (if presents).
bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Simplified description of a clang AST node.
Definition Protocol.h:2079
llvm::DenseMap< SymbolID, std::vector< MacroOccurrence > > MacroRefs
int line
Line position in a document (zero-based).
Definition Protocol.h:159
int character
Character offset on a line in a document (zero-based).
Definition Protocol.h:164
Position start
The range's start position.
Definition Protocol.h:188
Position end
The range's end position.
Definition Protocol.h:191
Represents a symbol occurrence in the source file.
Definition Ref.h:88
RefKind Kind
Definition Ref.h:91
SymbolLocation Location
The source location where the symbol is named.
Definition Ref.h:90
Position Start
The symbol range, using half-open range [Start, End).
The class presents a C++ symbol, e.g.
Definition Symbol.h:39
SymbolFlag Flags
Definition Symbol.h:151
@ IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition Symbol.h:141
@ Deprecated
Indicates if the symbol is deprecated.
Definition Symbol.h:143
@ ImplementationDetail
Symbol is an implementation detail.
Definition Symbol.h:145
@ HasDocComment
Symbol has an attached documentation comment.
Definition Symbol.h:149
@ VisibleOutsideFile
Symbol is visible to other files (not e.g. a static helper function).
Definition Symbol.h:147
@ Include
#include "header.h"
Definition Symbol.h:93
@ Import
#import "header.h"
Definition Symbol.h:95
llvm::StringRef Type
Raw representation of the OpaqueType of the symbol, used for scoring purposes.
Definition Symbol.h:88
llvm::StringRef Documentation
Documentation including comment for the symbol declaration.
Definition Symbol.h:79
index::SymbolInfo SymInfo
The symbol information, like symbol kind.
Definition Symbol.h:43
llvm::SmallVector< IncludeHeaderWithReferences, 1 > IncludeHeaders
One Symbol can potentially be included via different headers.
Definition Symbol.h:133
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 Signature
A brief description of the symbol that can be appended in the completion candidate list.
Definition Symbol.h:68
llvm::StringRef ReturnType
Type when this symbol is used in an expression.
Definition Symbol.h:83
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
llvm::StringRef CompletionSnippetSuffix
What to insert when completing this symbol, after the symbol name.
Definition Symbol.h:77
SymbolID ID
The ID of the symbol.
Definition Symbol.h:41
SymbolOrigin Origin
Where this symbol came from. Usually an index provides a constant value.
Definition Symbol.h:64