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
576ArrayRef<const CXXConstructorDecl *>
577SymbolCollector::findIndirectConstructors(const Decl *D) {
578 const auto *FD = llvm::dyn_cast<clang::FunctionDecl>(D);
579 if (!FD)
580 return {};
582}
583
584// Always return true to continue indexing.
586 const Decl *D, index::SymbolRoleSet Roles,
587 llvm::ArrayRef<index::SymbolRelation> Relations, SourceLocation Loc,
588 index::IndexDataConsumer::ASTNodeInfo ASTNode) {
589 assert(ASTCtx && PP && HeaderFileURIs);
590 assert(CompletionAllocator && CompletionTUInfo);
591 assert(ASTNode.OrigD);
592 // Indexing API puts canonical decl into D, which might not have a valid
593 // source location for implicit/built-in decls. Fallback to original decl in
594 // such cases.
595 if (D->getLocation().isInvalid())
596 D = ASTNode.OrigD;
597 // If OrigD is an declaration associated with a friend declaration and it's
598 // not a definition, skip it. Note that OrigD is the occurrence that the
599 // collector is currently visiting.
600 if ((ASTNode.OrigD->getFriendObjectKind() !=
601 Decl::FriendObjectKind::FOK_None) &&
602 !(Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
603 return true;
604 // A declaration created for a friend declaration should not be used as the
605 // canonical declaration in the index. Use OrigD instead, unless we've already
606 // picked a replacement for D
607 if (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None)
608 D = CanonicalDecls.try_emplace(D, ASTNode.OrigD).first->second;
609 // Flag to mark that D should be considered canonical meaning its declaration
610 // will override any previous declaration for the Symbol.
611 bool DeclIsCanonical = false;
612 // Avoid treating ObjCImplementationDecl as a canonical declaration if it has
613 // a corresponding non-implicit and non-forward declared ObjcInterfaceDecl.
614 if (const auto *IID = dyn_cast<ObjCImplementationDecl>(D)) {
615 DeclIsCanonical = true;
616 if (const auto *CID = IID->getClassInterface())
617 if (const auto *DD = CID->getDefinition())
618 if (!DD->isImplicitInterfaceDecl())
619 D = DD;
620 }
621 // Avoid treating ObjCCategoryImplDecl as a canonical declaration in favor of
622 // its ObjCCategoryDecl if it has one.
623 if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(D)) {
624 DeclIsCanonical = true;
625 if (const auto *CD = CID->getCategoryDecl())
626 D = CD;
627 }
628 const NamedDecl *ND = dyn_cast<NamedDecl>(D);
629 if (!ND)
630 return true;
631
632 auto ID = getSymbolIDCached(ND);
633 if (!ID)
634 return true;
635
636 // Mark D as referenced if this is a reference coming from the main file.
637 // D may not be an interesting symbol, but it's cheaper to check at the end.
638 auto &SM = ASTCtx->getSourceManager();
639 if (Opts.CountReferences &&
640 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
641 SM.getFileID(SM.getSpellingLoc(Loc)) == SM.getMainFileID())
642 ReferencedSymbols.insert(ID);
643
644 // ND is the canonical (i.e. first) declaration. If it's in the main file
645 // (which is not a header), then no public declaration was visible, so assume
646 // it's main-file only.
647 auto CheckIsMainFileOnly = [&](const NamedDecl *Decl) {
648 return SM.isWrittenInMainFile(SM.getExpansionLoc(Decl->getBeginLoc())) &&
649 !isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
650 ASTCtx->getLangOpts());
651 };
652 bool IsMainFileOnly = CheckIsMainFileOnly(ND);
653 // In C, printf is a redecl of an implicit builtin! So check OrigD instead.
654 if (ASTNode.OrigD->isImplicit() ||
655 !shouldCollectSymbol(*ND, *ASTCtx, Opts, IsMainFileOnly))
656 return true;
657
658 // Note: we need to process relations for all decl occurrences, including
659 // refs, because the indexing code only populates relations for specific
660 // occurrences. For example, RelationBaseOf is only populated for the
661 // occurrence inside the base-specifier.
662 processRelations(*ND, ID, Relations);
663
664 bool CollectRef = static_cast<bool>(Opts.RefFilter & toRefKind(Roles));
665 // Unlike other fields, e.g. Symbols (which use spelling locations), we use
666 // file locations for references (as it aligns the behavior of clangd's
667 // AST-based xref).
668 // FIXME: we should try to use the file locations for other fields.
669 if (CollectRef &&
670 (!IsMainFileOnly || Opts.CollectMainFileRefs ||
671 ND->isExternallyVisible()) &&
672 !isa<NamespaceDecl>(ND)) {
673 auto FileLoc = SM.getFileLoc(Loc);
674 auto FID = SM.getFileID(FileLoc);
675 if (Opts.RefsInHeaders || FID == SM.getMainFileID()) {
676 auto *Container = getRefContainer(ASTNode.Parent, Opts);
677 addRef(ID, SymbolRef{FileLoc, FID, Roles, index::getSymbolInfo(ND).Kind,
678 Container, isSpelled(FileLoc, *ND)});
679 // Also collect indirect constructor calls like `make_unique`
680 for (auto *Constructor : findIndirectConstructors(ASTNode.OrigD)) {
681 if (!shouldCollectSymbol(*Constructor, *ASTCtx, Opts,
682 CheckIsMainFileOnly(Constructor)))
683 continue;
684 if (auto ConstructorID = getSymbolIDCached(Constructor))
685 addRef(ConstructorID,
686 SymbolRef{FileLoc, FID, Roles,
687 index::getSymbolInfo(Constructor).Kind, Container,
688 false});
689 }
690 }
691 }
692 // Don't continue indexing if this is a mere reference.
693 if (!(Roles & (static_cast<unsigned>(index::SymbolRole::Declaration) |
694 static_cast<unsigned>(index::SymbolRole::Definition))))
695 return true;
696
697 // FIXME: ObjCPropertyDecl are not properly indexed here:
698 // - ObjCPropertyDecl may have an OrigD of ObjCPropertyImplDecl, which is
699 // not a NamedDecl.
700 auto *OriginalDecl = dyn_cast<NamedDecl>(ASTNode.OrigD);
701 if (!OriginalDecl)
702 return true;
703
704 const Symbol *BasicSymbol = Symbols.find(ID);
705 bool SkipDocCheckInDef = false;
706 if (isPreferredDeclaration(*OriginalDecl, Roles)) {
707 // If OriginalDecl is preferred, replace/create the existing canonical
708 // declaration (e.g. a class forward declaration). There should be at most
709 // one duplicate as we expect to see only one preferred declaration per
710 // TU, because in practice they are definitions.
711 BasicSymbol = addDeclaration(*OriginalDecl, std::move(ID), IsMainFileOnly);
712 SkipDocCheckInDef = true;
713 } else if (!BasicSymbol || DeclIsCanonical) {
714 BasicSymbol = addDeclaration(*ND, std::move(ID), IsMainFileOnly);
715 SkipDocCheckInDef = true;
716 }
717
718 if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
719 addDefinition(*OriginalDecl, *BasicSymbol, SkipDocCheckInDef);
720
721 return true;
722}
723
724void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) {
725 assert(HeaderFileURIs && PP);
726 const auto &SM = PP->getSourceManager();
727 const auto MainFileEntryRef = SM.getFileEntryRefForID(SM.getMainFileID());
728 assert(MainFileEntryRef);
729
730 const std::string &MainFileURI = HeaderFileURIs->toURI(*MainFileEntryRef);
731 // Add macro references.
732 for (const auto &IDToRefs : MacroRefsToIndex.MacroRefs) {
733 for (const auto &MacroRef : IDToRefs.second) {
734 const auto &SR = MacroRef.toSourceRange(SM);
735 auto Range = halfOpenToRange(SM, SR);
736 bool IsDefinition = MacroRef.IsDefinition;
737 Ref R;
742 R.Location.FileURI = MainFileURI.c_str();
743 R.Kind = IsDefinition ? RefKind::Definition : RefKind::Reference;
744 Refs.insert(IDToRefs.first, R);
745 if (IsDefinition) {
746 Symbol S;
747 S.ID = IDToRefs.first;
748 S.Name = toSourceCode(SM, SR.getAsRange());
749 S.SymInfo.Kind = index::SymbolKind::Macro;
750 S.SymInfo.SubKind = index::SymbolSubKind::None;
751 S.SymInfo.Properties = index::SymbolPropertySet();
752 S.SymInfo.Lang = index::SymbolLanguage::C;
753 S.Origin = Opts.Origin;
755 // Make the macro visible for code completion if main file is an
756 // include-able header.
757 if (!HeaderFileURIs->getIncludeHeader(SM.getMainFileID()).empty()) {
760 }
761 Symbols.insert(S);
762 }
763 }
764 }
765}
766
767bool SymbolCollector::handleMacroOccurrence(const IdentifierInfo *Name,
768 const MacroInfo *MI,
769 index::SymbolRoleSet Roles,
770 SourceLocation Loc) {
771 assert(PP);
772 // Builtin macros don't have useful locations and aren't needed in completion.
773 if (MI->isBuiltinMacro())
774 return true;
775
776 const auto &SM = PP->getSourceManager();
777 auto DefLoc = MI->getDefinitionLoc();
778 // Also avoid storing macros that aren't defined in any file, i.e. predefined
779 // macros like __DBL_MIN__ and those defined on the command line.
780 if (SM.isWrittenInBuiltinFile(DefLoc) ||
781 SM.isWrittenInCommandLineFile(DefLoc) ||
782 Name->getName() == "__GCC_HAVE_DWARF2_CFI_ASM")
783 return true;
784
785 auto ID = getSymbolIDCached(Name->getName(), MI, SM);
786 if (!ID)
787 return true;
788
789 auto SpellingLoc = SM.getSpellingLoc(Loc);
790 bool IsMainFileOnly =
791 SM.isInMainFile(SM.getExpansionLoc(DefLoc)) &&
792 !isHeaderFile(SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
793 ASTCtx->getLangOpts());
794 // Do not store references to main-file macros.
795 if ((static_cast<unsigned>(Opts.RefFilter) & Roles) && !IsMainFileOnly &&
796 (Opts.RefsInHeaders || SM.getFileID(SpellingLoc) == SM.getMainFileID())) {
797 // FIXME: Populate container information for macro references.
798 // FIXME: All MacroRefs are marked as Spelled now, but this should be
799 // checked.
800 addRef(ID,
801 SymbolRef{Loc, SM.getFileID(Loc), Roles, index::SymbolKind::Macro,
802 /*Container=*/nullptr,
803 /*Spelled=*/true});
804 }
805
806 // Collect symbols.
807 if (!Opts.CollectMacro)
808 return true;
809
810 // Skip main-file macros if we are not collecting them.
811 if (IsMainFileOnly && !Opts.CollectMainFileSymbols)
812 return false;
813
814 // Mark the macro as referenced if this is a reference coming from the main
815 // file. The macro may not be an interesting symbol, but it's cheaper to check
816 // at the end.
817 if (Opts.CountReferences &&
818 (Roles & static_cast<unsigned>(index::SymbolRole::Reference)) &&
819 SM.getFileID(SpellingLoc) == SM.getMainFileID())
820 ReferencedSymbols.insert(ID);
821
822 // Don't continue indexing if this is a mere reference.
823 // FIXME: remove macro with ID if it is undefined.
824 if (!(Roles & static_cast<unsigned>(index::SymbolRole::Declaration) ||
825 Roles & static_cast<unsigned>(index::SymbolRole::Definition)))
826 return true;
827
828 // Only collect one instance in case there are multiple.
829 if (Symbols.find(ID) != nullptr)
830 return true;
831
832 Symbol S;
833 S.ID = std::move(ID);
834 S.Name = Name->getName();
835 if (!IsMainFileOnly) {
838 }
839 S.SymInfo = index::getSymbolInfoForMacro(*MI);
840 S.Origin = Opts.Origin;
841 // FIXME: use the result to filter out symbols.
842 shouldIndexFile(SM.getFileID(Loc));
843 if (auto DeclLoc = getTokenLocation(DefLoc))
844 S.CanonicalDeclaration = *DeclLoc;
845
846 CodeCompletionResult SymbolCompletion(Name);
847 const auto *CCS = SymbolCompletion.CreateCodeCompletionStringForMacro(
848 *PP, *CompletionAllocator, *CompletionTUInfo);
849 std::string Signature;
850 std::string SnippetSuffix;
851 getSignature(*CCS, &Signature, &SnippetSuffix, SymbolCompletion.Kind,
852 SymbolCompletion.CursorKind);
853 S.Signature = Signature;
854 S.CompletionSnippetSuffix = SnippetSuffix;
855
856 IndexedMacros.insert(Name);
857
858 setIncludeLocation(S, DefLoc, include_cleaner::Macro{Name, DefLoc});
859 Symbols.insert(S);
860 return true;
861}
862
863void SymbolCollector::processRelations(
864 const NamedDecl &ND, const SymbolID &ID,
865 ArrayRef<index::SymbolRelation> Relations) {
866 for (const auto &R : Relations) {
867 auto RKind = indexableRelation(R);
868 if (!RKind)
869 continue;
870 const Decl *Object = R.RelatedSymbol;
871
872 auto ObjectID = getSymbolIDCached(Object);
873 if (!ObjectID)
874 continue;
875
876 // Record the relation.
877 // TODO: There may be cases where the object decl is not indexed for some
878 // reason. Those cases should probably be removed in due course, but for
879 // now there are two possible ways to handle it:
880 // (A) Avoid storing the relation in such cases.
881 // (B) Store it anyways. Clients will likely lookup() the SymbolID
882 // in the index and find nothing, but that's a situation they
883 // probably need to handle for other reasons anyways.
884 // We currently do (B) because it's simpler.
885 if (*RKind == RelationKind::BaseOf)
886 this->Relations.insert({ID, *RKind, ObjectID});
887 else if (*RKind == RelationKind::OverriddenBy)
888 this->Relations.insert({ObjectID, *RKind, ID});
889 }
890}
891
892void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation DefLoc,
893 const include_cleaner::Symbol &Sym) {
894 const auto &SM = PP->getSourceManager();
895 if (!Opts.CollectIncludePath ||
896 shouldCollectIncludePath(S.SymInfo.Kind) == Symbol::Invalid)
897 return;
898
899 // Use the expansion location to get the #include header since this is
900 // where the symbol is exposed.
901 if (FileID FID = SM.getDecomposedExpansionLoc(DefLoc).first; FID.isValid())
902 IncludeFiles[S.ID] = FID;
903
904 // We update providers for a symbol with each occurence, as SymbolCollector
905 // might run while parsing, rather than at the end of a translation unit.
906 // Hence we see more and more redecls over time.
907 SymbolProviders[S.ID] =
908 include_cleaner::headersForSymbol(Sym, *PP, Opts.PragmaIncludes);
909}
910
911llvm::StringRef getStdHeader(const Symbol *S, const LangOptions &LangOpts) {
912 tooling::stdlib::Lang Lang = tooling::stdlib::Lang::CXX;
913 if (LangOpts.C11)
914 Lang = tooling::stdlib::Lang::C;
915 else if(!LangOpts.CPlusPlus)
916 return "";
917
918 if (S->Scope == "std::" && S->Name == "move") {
919 if (!S->Signature.contains(','))
920 return "<utility>";
921 return "<algorithm>";
922 }
923
924 if (auto StdSym = tooling::stdlib::Symbol::named(S->Scope, S->Name, Lang))
925 if (auto Header = StdSym->header())
926 return Header->name();
927 return "";
928}
929
931 // At the end of the TU, add 1 to the refcount of all referenced symbols.
932 for (const auto &ID : ReferencedSymbols) {
933 if (const auto *S = Symbols.find(ID)) {
934 // SymbolSlab::Builder returns const symbols because strings are interned
935 // and modifying returned symbols without inserting again wouldn't go
936 // well. const_cast is safe here as we're modifying a data owned by the
937 // Symbol. This reduces time spent in SymbolCollector by ~1%.
938 ++const_cast<Symbol *>(S)->References;
939 }
940 }
941 if (Opts.CollectMacro) {
942 assert(PP);
943 // First, drop header guards. We can't identify these until EOF.
944 for (const IdentifierInfo *II : IndexedMacros) {
945 if (const auto *MI = PP->getMacroDefinition(II).getMacroInfo())
946 if (auto ID =
947 getSymbolIDCached(II->getName(), MI, PP->getSourceManager()))
948 if (MI->isUsedForHeaderGuard())
949 Symbols.erase(ID);
950 }
951 }
952 llvm::DenseMap<FileID, bool> FileToContainsImportsOrObjC;
953 llvm::DenseMap<include_cleaner::Header, std::string> HeaderSpelling;
954 // Fill in IncludeHeaders.
955 // We delay this until end of TU so header guards are all resolved.
956 for (const auto &[SID, Providers] : SymbolProviders) {
957 const Symbol *S = Symbols.find(SID);
958 if (!S)
959 continue;
960
961 FileID FID = IncludeFiles.lookup(SID);
962 // Determine if the FID is #include'd or #import'ed.
964 auto CollectDirectives = shouldCollectIncludePath(S->SymInfo.Kind);
965 if ((CollectDirectives & Symbol::Include) != 0)
966 Directives |= Symbol::Include;
967 // Only allow #import for symbols from ObjC-like files.
968 if ((CollectDirectives & Symbol::Import) != 0 && FID.isValid()) {
969 auto [It, Inserted] = FileToContainsImportsOrObjC.try_emplace(FID);
970 if (Inserted)
971 It->second = FilesWithObjCConstructs.contains(FID) ||
972 tooling::codeContainsImports(
973 ASTCtx->getSourceManager().getBufferData(FID));
974 if (It->second)
975 Directives |= Symbol::Import;
976 }
977
978 if (Directives == Symbol::Invalid)
979 continue;
980
981 // Use the include location-based logic for Objective-C symbols.
982 if (Directives & Symbol::Import) {
983 llvm::StringRef IncludeHeader = getStdHeader(S, ASTCtx->getLangOpts());
984 if (IncludeHeader.empty())
985 IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
986
987 if (!IncludeHeader.empty()) {
988 auto NewSym = *S;
989 NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
990 Symbols.insert(NewSym);
991 }
992 // FIXME: use providers from include-cleaner library once it's polished
993 // for Objective-C.
994 continue;
995 }
996
997 // For #include's, use the providers computed by the include-cleaner
998 // library.
999 assert(Directives == Symbol::Include);
1000 // Ignore providers that are not self-contained, this is especially
1001 // important for symbols defined in the main-file. We want to prefer the
1002 // header, if possible.
1003 // TODO: Limit this to specifically ignore main file, when we're indexing a
1004 // non-header file?
1005 auto SelfContainedProvider =
1006 [this](llvm::ArrayRef<include_cleaner::Header> Providers)
1007 -> std::optional<include_cleaner::Header> {
1008 for (const auto &H : Providers) {
1009 if (H.kind() != include_cleaner::Header::Physical)
1010 return H;
1011 if (tooling::isSelfContainedHeader(H.physical(), PP->getSourceManager(),
1012 PP->getHeaderSearchInfo()))
1013 return H;
1014 }
1015 return std::nullopt;
1016 };
1017 const auto OptionalProvider = SelfContainedProvider(Providers);
1018 if (!OptionalProvider)
1019 continue;
1020 const auto &H = *OptionalProvider;
1021 const auto [SpellingIt, Inserted] = HeaderSpelling.try_emplace(H);
1022 if (Inserted) {
1023 auto &SM = ASTCtx->getSourceManager();
1024 if (H.kind() == include_cleaner::Header::Kind::Physical) {
1025 // FIXME: Get rid of this once include-cleaner has support for system
1026 // headers.
1027 if (auto Canonical =
1028 HeaderFileURIs->mapCanonical(H.physical().getName());
1029 !Canonical.empty())
1030 SpellingIt->second = Canonical;
1031 // For physical files, prefer URIs as spellings might change
1032 // depending on the translation unit.
1033 else if (tooling::isSelfContainedHeader(H.physical(), SM,
1034 PP->getHeaderSearchInfo()))
1035 SpellingIt->second =
1036 HeaderFileURIs->toURI(H.physical());
1037 } else {
1038 SpellingIt->second = include_cleaner::spellHeader(
1039 {H, PP->getHeaderSearchInfo(),
1040 SM.getFileEntryForID(SM.getMainFileID())});
1041 }
1042 }
1043
1044 if (!SpellingIt->second.empty()) {
1045 auto NewSym = *S;
1046 NewSym.IncludeHeaders.push_back({SpellingIt->second, 1, Directives});
1047 Symbols.insert(NewSym);
1048 }
1049 }
1050
1051 ReferencedSymbols.clear();
1052 IncludeFiles.clear();
1053 SymbolProviders.clear();
1054 FilesWithObjCConstructs.clear();
1055}
1056
1057const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
1058 bool IsMainFileOnly) {
1059 auto &Ctx = ND.getASTContext();
1060 auto &SM = Ctx.getSourceManager();
1061
1062 Symbol S;
1063 S.ID = std::move(ID);
1064 std::string QName = printQualifiedName(ND);
1065 // FIXME: this returns foo:bar: for objective-C methods, we prefer only foo:
1066 // for consistency with CodeCompletionString and a clean name/signature split.
1067 std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
1068 std::string TemplateSpecializationArgs = printTemplateSpecializationArgs(ND);
1069 S.TemplateSpecializationArgs = TemplateSpecializationArgs;
1070
1071 // We collect main-file symbols, but do not use them for code completion.
1072 if (!IsMainFileOnly && isIndexedForCodeCompletion(ND, Ctx))
1074 if (isImplementationDetail(&ND))
1076 if (!IsMainFileOnly)
1078 S.SymInfo = index::getSymbolInfo(&ND);
1079 auto Loc = nameLocation(ND, SM);
1080 assert(Loc.isValid() && "Invalid source location for NamedDecl");
1081 // FIXME: use the result to filter out symbols.
1082 auto FID = SM.getFileID(Loc);
1083 shouldIndexFile(FID);
1084 if (auto DeclLoc = getTokenLocation(Loc))
1085 S.CanonicalDeclaration = *DeclLoc;
1086
1087 S.Origin = Opts.Origin;
1088 if (ND.getAvailability() == AR_Deprecated)
1090
1091 // Add completion info.
1092 // FIXME: we may want to choose a different redecl, or combine from several.
1093 assert(ASTCtx && PP && "ASTContext and Preprocessor must be set.");
1094 // We use the primary template, as clang does during code completion.
1095 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
1096 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
1097 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
1098 *CompletionTUInfo,
1099 /*IncludeBriefComments*/ false);
1100 std::string DocComment;
1101 std::string Documentation;
1102 bool AlreadyHasDoc = S.Flags & Symbol::HasDocComment;
1103 if (!AlreadyHasDoc) {
1104 DocComment = getDocComment(Ctx, SymbolCompletion,
1105 /*CommentsFromHeaders=*/true);
1106 Documentation = formatDocumentation(*CCS, DocComment);
1107 }
1108 const auto UpdateDoc = [&] {
1109 if (!AlreadyHasDoc) {
1110 if (!DocComment.empty())
1112 S.Documentation = Documentation;
1113 }
1114 };
1116 if (Opts.StoreAllDocumentation)
1117 UpdateDoc();
1118 Symbols.insert(S);
1119 return Symbols.find(S.ID);
1120 }
1121 UpdateDoc();
1122 std::string Signature;
1123 std::string SnippetSuffix;
1124 getSignature(*CCS, &Signature, &SnippetSuffix, SymbolCompletion.Kind,
1125 SymbolCompletion.CursorKind);
1126 S.Signature = Signature;
1127 S.CompletionSnippetSuffix = SnippetSuffix;
1128 std::string ReturnType = getReturnType(*CCS);
1129 S.ReturnType = ReturnType;
1130
1131 std::optional<OpaqueType> TypeStorage;
1133 TypeStorage = OpaqueType::fromCompletionResult(*ASTCtx, SymbolCompletion);
1134 if (TypeStorage)
1135 S.Type = TypeStorage->raw();
1136 }
1137
1138 Symbols.insert(S);
1139 setIncludeLocation(S, ND.getLocation(), include_cleaner::Symbol{ND});
1140 if (S.SymInfo.Lang == index::SymbolLanguage::ObjC)
1141 FilesWithObjCConstructs.insert(FID);
1142 return Symbols.find(S.ID);
1143}
1144
1145void SymbolCollector::addDefinition(const NamedDecl &ND, const Symbol &DeclSym,
1146 bool SkipDocCheck) {
1147 if (DeclSym.Definition)
1148 return;
1149 const auto &SM = ND.getASTContext().getSourceManager();
1150 auto Loc = nameLocation(ND, SM);
1151 shouldIndexFile(SM.getFileID(Loc));
1152 auto DefLoc = getTokenLocation(Loc);
1153 // If we saw some forward declaration, we end up copying the symbol.
1154 // This is not ideal, but avoids duplicating the "is this a definition" check
1155 // in clang::index. We should only see one definition.
1156 if (!DefLoc)
1157 return;
1158 Symbol S = DeclSym;
1159 // FIXME: use the result to filter out symbols.
1160 S.Definition = *DefLoc;
1161
1162 std::string DocComment;
1163 std::string Documentation;
1164 if (!SkipDocCheck && !(S.Flags & Symbol::HasDocComment) &&
1165 (llvm::isa<FunctionDecl>(ND) || llvm::isa<CXXMethodDecl>(ND))) {
1166 CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
1167 const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
1168 *ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
1169 *CompletionTUInfo,
1170 /*IncludeBriefComments*/ false);
1171 DocComment = getDocComment(ND.getASTContext(), SymbolCompletion,
1172 /*CommentsFromHeaders=*/true);
1173 if (!S.Documentation.empty())
1174 Documentation = S.Documentation.str() + '\n' + DocComment;
1175 else
1176 Documentation = formatDocumentation(*CCS, DocComment);
1177 if (!DocComment.empty())
1178 S.Flags |= Symbol::HasDocComment;
1179 S.Documentation = Documentation;
1180 }
1181
1182 Symbols.insert(S);
1183}
1184
1186 if (!Opts.FileFilter)
1187 return true;
1188 auto I = FilesToIndexCache.try_emplace(FID);
1189 if (I.second)
1190 I.first->second = Opts.FileFilter(ASTCtx->getSourceManager(), FID);
1191 return I.first->second;
1192}
1193
1194static bool refIsCall(index::SymbolKind Kind) {
1195 using SK = index::SymbolKind;
1196 return Kind == SK::Function || Kind == SK::InstanceMethod ||
1197 Kind == SK::ClassMethod || Kind == SK::StaticMethod ||
1198 Kind == SK::Constructor || Kind == SK::Destructor ||
1199 Kind == SK::ConversionFunction;
1200}
1201
1202void SymbolCollector::addRef(SymbolID ID, const SymbolRef &SR) {
1203 const auto &SM = ASTCtx->getSourceManager();
1204 // FIXME: use the result to filter out references.
1205 shouldIndexFile(SR.FID);
1206 if (const auto FE = SM.getFileEntryRefForID(SR.FID)) {
1207 auto Range = getTokenRange(SR.Loc, SM, ASTCtx->getLangOpts());
1208 Ref R;
1209 R.Location.Start = Range.first;
1210 R.Location.End = Range.second;
1211 R.Location.FileURI = HeaderFileURIs->toURI(*FE).c_str();
1212 R.Kind = toRefKind(SR.Roles, SR.Spelled);
1213 if (refIsCall(SR.Kind)) {
1214 R.Kind |= RefKind::Call;
1215 }
1216 R.Container = getSymbolIDCached(SR.Container);
1217 Refs.insert(ID, R);
1218 }
1219}
1220
1221SymbolID SymbolCollector::getSymbolIDCached(const Decl *D) {
1222 auto It = DeclToIDCache.try_emplace(D);
1223 if (It.second)
1224 It.first->second = getSymbolID(D);
1225 return It.first->second;
1226}
1227
1228SymbolID SymbolCollector::getSymbolIDCached(const llvm::StringRef MacroName,
1229 const MacroInfo *MI,
1230 const SourceManager &SM) {
1231 auto It = MacroToIDCache.try_emplace(MI);
1232 if (It.second)
1233 It.first->second = getSymbolID(MacroName, MI, SM);
1234 return It.first->second;
1235}
1236} // namespace clangd
1237} // 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)
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
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.
ArrayRef< const CXXConstructorDecl * > getForwardedConstructors(const FunctionDecl *FD, ForwardingToConstructorCache &Cache)
Returns the constructors that FD forwards to, if FD is a template instantiation of a likely forwardin...
Definition AST.cpp:1119
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
llvm::DenseMap< const FunctionDecl *, SmallVector< const CXXConstructorDecl *, 1 > > ForwardingToConstructorCache
Cache mapping forwarding function instantiations (e.g.
Definition AST.h:269
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