Bug Summary

File:src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/lib/AST/DeclBase.cpp
Warning:line 1674, column 9
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name DeclBase.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangAST/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include/clang/AST -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../include -I /usr/src/gnu/usr.bin/clang/libclangAST/obj -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangAST/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/lib/AST/DeclBase.cpp
1//===- DeclBase.cpp - Declaration AST Node Implementation -----------------===//
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// This file implements the Decl and DeclContext classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/DeclBase.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTLambda.h"
16#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
18#include "clang/AST/AttrIterator.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclContextInternals.h"
22#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclObjC.h"
24#include "clang/AST/DeclOpenMP.h"
25#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/DependentDiagnostic.h"
27#include "clang/AST/ExternalASTSource.h"
28#include "clang/AST/Stmt.h"
29#include "clang/AST/Type.h"
30#include "clang/Basic/IdentifierTable.h"
31#include "clang/Basic/LLVM.h"
32#include "clang/Basic/LangOptions.h"
33#include "clang/Basic/ObjCRuntime.h"
34#include "clang/Basic/PartialDiagnostic.h"
35#include "clang/Basic/SourceLocation.h"
36#include "clang/Basic/TargetInfo.h"
37#include "llvm/ADT/ArrayRef.h"
38#include "llvm/ADT/PointerIntPair.h"
39#include "llvm/ADT/SmallVector.h"
40#include "llvm/ADT/StringRef.h"
41#include "llvm/Support/Casting.h"
42#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/MathExtras.h"
44#include "llvm/Support/VersionTuple.h"
45#include "llvm/Support/raw_ostream.h"
46#include <algorithm>
47#include <cassert>
48#include <cstddef>
49#include <string>
50#include <tuple>
51#include <utility>
52
53using namespace clang;
54
55//===----------------------------------------------------------------------===//
56// Statistics
57//===----------------------------------------------------------------------===//
58
59#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
60#define ABSTRACT_DECL(DECL)
61#include "clang/AST/DeclNodes.inc"
62
63void Decl::updateOutOfDate(IdentifierInfo &II) const {
64 getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
65}
66
67#define DECL(DERIVED, BASE) \
68 static_assert(alignof(Decl) >= alignof(DERIVED##Decl), \
69 "Alignment sufficient after objects prepended to " #DERIVED);
70#define ABSTRACT_DECL(DECL)
71#include "clang/AST/DeclNodes.inc"
72
73void *Decl::operator new(std::size_t Size, const ASTContext &Context,
74 unsigned ID, std::size_t Extra) {
75 // Allocate an extra 8 bytes worth of storage, which ensures that the
76 // resulting pointer will still be 8-byte aligned.
77 static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
78 "Decl won't be misaligned");
79 void *Start = Context.Allocate(Size + Extra + 8);
80 void *Result = (char*)Start + 8;
81
82 unsigned *PrefixPtr = (unsigned *)Result - 2;
83
84 // Zero out the first 4 bytes; this is used to store the owning module ID.
85 PrefixPtr[0] = 0;
86
87 // Store the global declaration ID in the second 4 bytes.
88 PrefixPtr[1] = ID;
89
90 return Result;
91}
92
93void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
94 DeclContext *Parent, std::size_t Extra) {
95 assert(!Parent || &Parent->getParentASTContext() == &Ctx)((void)0);
96 // With local visibility enabled, we track the owning module even for local
97 // declarations. We create the TU decl early and may not yet know what the
98 // LangOpts are, so conservatively allocate the storage.
99 if (Ctx.getLangOpts().trackLocalOwningModule() || !Parent) {
100 // Ensure required alignment of the resulting object by adding extra
101 // padding at the start if required.
102 size_t ExtraAlign =
103 llvm::offsetToAlignment(sizeof(Module *), llvm::Align(alignof(Decl)));
104 auto *Buffer = reinterpret_cast<char *>(
105 ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
106 Buffer += ExtraAlign;
107 auto *ParentModule =
108 Parent ? cast<Decl>(Parent)->getOwningModule() : nullptr;
109 return new (Buffer) Module*(ParentModule) + 1;
110 }
111 return ::operator new(Size + Extra, Ctx);
112}
113
114Module *Decl::getOwningModuleSlow() const {
115 assert(isFromASTFile() && "Not from AST file?")((void)0);
116 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
117}
118
119bool Decl::hasLocalOwningModuleStorage() const {
120 return getASTContext().getLangOpts().trackLocalOwningModule();
121}
122
123const char *Decl::getDeclKindName() const {
124 switch (DeclKind) {
125 default: llvm_unreachable("Declaration not in DeclNodes.inc!")__builtin_unreachable();
126#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
127#define ABSTRACT_DECL(DECL)
128#include "clang/AST/DeclNodes.inc"
129 }
130}
131
132void Decl::setInvalidDecl(bool Invalid) {
133 InvalidDecl = Invalid;
134 assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition())((void)0);
135 if (!Invalid) {
136 return;
137 }
138
139 if (!isa<ParmVarDecl>(this)) {
140 // Defensive maneuver for ill-formed code: we're likely not to make it to
141 // a point where we set the access specifier, so default it to "public"
142 // to avoid triggering asserts elsewhere in the front end.
143 setAccess(AS_public);
144 }
145
146 // Marking a DecompositionDecl as invalid implies all the child BindingDecl's
147 // are invalid too.
148 if (auto *DD = dyn_cast<DecompositionDecl>(this)) {
149 for (auto *Binding : DD->bindings()) {
150 Binding->setInvalidDecl();
151 }
152 }
153}
154
155const char *DeclContext::getDeclKindName() const {
156 switch (getDeclKind()) {
157#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
158#define ABSTRACT_DECL(DECL)
159#include "clang/AST/DeclNodes.inc"
160 }
161 llvm_unreachable("Declaration context not in DeclNodes.inc!")__builtin_unreachable();
162}
163
164bool Decl::StatisticsEnabled = false;
165void Decl::EnableStatistics() {
166 StatisticsEnabled = true;
167}
168
169void Decl::PrintStats() {
170 llvm::errs() << "\n*** Decl Stats:\n";
171
172 int totalDecls = 0;
173#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
174#define ABSTRACT_DECL(DECL)
175#include "clang/AST/DeclNodes.inc"
176 llvm::errs() << " " << totalDecls << " decls total.\n";
177
178 int totalBytes = 0;
179#define DECL(DERIVED, BASE) \
180 if (n##DERIVED##s > 0) { \
181 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
182 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
183 << sizeof(DERIVED##Decl) << " each (" \
184 << n##DERIVED##s * sizeof(DERIVED##Decl) \
185 << " bytes)\n"; \
186 }
187#define ABSTRACT_DECL(DECL)
188#include "clang/AST/DeclNodes.inc"
189
190 llvm::errs() << "Total bytes = " << totalBytes << "\n";
191}
192
193void Decl::add(Kind k) {
194 switch (k) {
195#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
196#define ABSTRACT_DECL(DECL)
197#include "clang/AST/DeclNodes.inc"
198 }
199}
200
201bool Decl::isTemplateParameterPack() const {
202 if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(this))
203 return TTP->isParameterPack();
204 if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(this))
205 return NTTP->isParameterPack();
206 if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(this))
207 return TTP->isParameterPack();
208 return false;
209}
210
211bool Decl::isParameterPack() const {
212 if (const auto *Var = dyn_cast<VarDecl>(this))
213 return Var->isParameterPack();
214
215 return isTemplateParameterPack();
216}
217
218FunctionDecl *Decl::getAsFunction() {
219 if (auto *FD = dyn_cast<FunctionDecl>(this))
220 return FD;
221 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
222 return FTD->getTemplatedDecl();
223 return nullptr;
224}
225
226bool Decl::isTemplateDecl() const {
227 return isa<TemplateDecl>(this);
228}
229
230TemplateDecl *Decl::getDescribedTemplate() const {
231 if (auto *FD = dyn_cast<FunctionDecl>(this))
232 return FD->getDescribedFunctionTemplate();
233 if (auto *RD = dyn_cast<CXXRecordDecl>(this))
234 return RD->getDescribedClassTemplate();
235 if (auto *VD = dyn_cast<VarDecl>(this))
236 return VD->getDescribedVarTemplate();
237 if (auto *AD = dyn_cast<TypeAliasDecl>(this))
238 return AD->getDescribedAliasTemplate();
239
240 return nullptr;
241}
242
243const TemplateParameterList *Decl::getDescribedTemplateParams() const {
244 if (auto *TD = getDescribedTemplate())
245 return TD->getTemplateParameters();
246 if (auto *CTPSD = dyn_cast<ClassTemplatePartialSpecializationDecl>(this))
247 return CTPSD->getTemplateParameters();
248 if (auto *VTPSD = dyn_cast<VarTemplatePartialSpecializationDecl>(this))
249 return VTPSD->getTemplateParameters();
250 return nullptr;
251}
252
253bool Decl::isTemplated() const {
254 // A declaration is templated if it is a template or a template pattern, or
255 // is within (lexcially for a friend, semantically otherwise) a dependent
256 // context.
257 // FIXME: Should local extern declarations be treated like friends?
258 if (auto *AsDC = dyn_cast<DeclContext>(this))
259 return AsDC->isDependentContext();
260 auto *DC = getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext();
261 return DC->isDependentContext() || isTemplateDecl() ||
262 getDescribedTemplateParams();
263}
264
265unsigned Decl::getTemplateDepth() const {
266 if (auto *DC = dyn_cast<DeclContext>(this))
267 if (DC->isFileContext())
268 return 0;
269
270 if (auto *TPL = getDescribedTemplateParams())
271 return TPL->getDepth() + 1;
272
273 // If this is a dependent lambda, there might be an enclosing variable
274 // template. In this case, the next step is not the parent DeclContext (or
275 // even a DeclContext at all).
276 auto *RD = dyn_cast<CXXRecordDecl>(this);
277 if (RD && RD->isDependentLambda())
278 if (Decl *Context = RD->getLambdaContextDecl())
279 return Context->getTemplateDepth();
280
281 const DeclContext *DC =
282 getFriendObjectKind() ? getLexicalDeclContext() : getDeclContext();
283 return cast<Decl>(DC)->getTemplateDepth();
284}
285
286const DeclContext *Decl::getParentFunctionOrMethod() const {
287 for (const DeclContext *DC = getDeclContext();
288 DC && !DC->isTranslationUnit() && !DC->isNamespace();
289 DC = DC->getParent())
290 if (DC->isFunctionOrMethod())
291 return DC;
292
293 return nullptr;
294}
295
296//===----------------------------------------------------------------------===//
297// PrettyStackTraceDecl Implementation
298//===----------------------------------------------------------------------===//
299
300void PrettyStackTraceDecl::print(raw_ostream &OS) const {
301 SourceLocation TheLoc = Loc;
302 if (TheLoc.isInvalid() && TheDecl)
303 TheLoc = TheDecl->getLocation();
304
305 if (TheLoc.isValid()) {
306 TheLoc.print(OS, SM);
307 OS << ": ";
308 }
309
310 OS << Message;
311
312 if (const auto *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
313 OS << " '";
314 DN->printQualifiedName(OS);
315 OS << '\'';
316 }
317 OS << '\n';
318}
319
320//===----------------------------------------------------------------------===//
321// Decl Implementation
322//===----------------------------------------------------------------------===//
323
324// Out-of-line virtual method providing a home for Decl.
325Decl::~Decl() = default;
326
327void Decl::setDeclContext(DeclContext *DC) {
328 DeclCtx = DC;
329}
330
331void Decl::setLexicalDeclContext(DeclContext *DC) {
332 if (DC == getLexicalDeclContext())
333 return;
334
335 if (isInSemaDC()) {
336 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
337 } else {
338 getMultipleDC()->LexicalDC = DC;
339 }
340
341 // FIXME: We shouldn't be changing the lexical context of declarations
342 // imported from AST files.
343 if (!isFromASTFile()) {
344 setModuleOwnershipKind(getModuleOwnershipKindForChildOf(DC));
345 if (hasOwningModule())
346 setLocalOwningModule(cast<Decl>(DC)->getOwningModule());
347 }
348
349 assert(((void)0)
350 (getModuleOwnershipKind() != ModuleOwnershipKind::VisibleWhenImported ||((void)0)
351 getOwningModule()) &&((void)0)
352 "hidden declaration has no owning module")((void)0);
353}
354
355void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
356 ASTContext &Ctx) {
357 if (SemaDC == LexicalDC) {
358 DeclCtx = SemaDC;
359 } else {
360 auto *MDC = new (Ctx) Decl::MultipleDC();
361 MDC->SemanticDC = SemaDC;
362 MDC->LexicalDC = LexicalDC;
363 DeclCtx = MDC;
364 }
365}
366
367bool Decl::isInLocalScopeForInstantiation() const {
368 const DeclContext *LDC = getLexicalDeclContext();
369 if (!LDC->isDependentContext())
370 return false;
371 while (true) {
372 if (LDC->isFunctionOrMethod())
373 return true;
374 if (!isa<TagDecl>(LDC))
375 return false;
376 if (const auto *CRD = dyn_cast<CXXRecordDecl>(LDC))
377 if (CRD->isLambda())
378 return true;
379 LDC = LDC->getLexicalParent();
380 }
381 return false;
382}
383
384bool Decl::isInAnonymousNamespace() const {
385 for (const DeclContext *DC = getDeclContext(); DC; DC = DC->getParent()) {
386 if (const auto *ND = dyn_cast<NamespaceDecl>(DC))
387 if (ND->isAnonymousNamespace())
388 return true;
389 }
390
391 return false;
392}
393
394bool Decl::isInStdNamespace() const {
395 const DeclContext *DC = getDeclContext();
396 return DC && DC->isStdNamespace();
397}
398
399TranslationUnitDecl *Decl::getTranslationUnitDecl() {
400 if (auto *TUD = dyn_cast<TranslationUnitDecl>(this))
401 return TUD;
402
403 DeclContext *DC = getDeclContext();
404 assert(DC && "This decl is not contained in a translation unit!")((void)0);
405
406 while (!DC->isTranslationUnit()) {
407 DC = DC->getParent();
408 assert(DC && "This decl is not contained in a translation unit!")((void)0);
409 }
410
411 return cast<TranslationUnitDecl>(DC);
412}
413
414ASTContext &Decl::getASTContext() const {
415 return getTranslationUnitDecl()->getASTContext();
416}
417
418/// Helper to get the language options from the ASTContext.
419/// Defined out of line to avoid depending on ASTContext.h.
420const LangOptions &Decl::getLangOpts() const {
421 return getASTContext().getLangOpts();
422}
423
424ASTMutationListener *Decl::getASTMutationListener() const {
425 return getASTContext().getASTMutationListener();
426}
427
428unsigned Decl::getMaxAlignment() const {
429 if (!hasAttrs())
430 return 0;
431
432 unsigned Align = 0;
433 const AttrVec &V = getAttrs();
434 ASTContext &Ctx = getASTContext();
435 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
436 for (; I != E; ++I) {
437 if (!I->isAlignmentErrorDependent())
438 Align = std::max(Align, I->getAlignment(Ctx));
439 }
440 return Align;
441}
442
443bool Decl::isUsed(bool CheckUsedAttr) const {
444 const Decl *CanonD = getCanonicalDecl();
445 if (CanonD->Used)
446 return true;
447
448 // Check for used attribute.
449 // Ask the most recent decl, since attributes accumulate in the redecl chain.
450 if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>())
451 return true;
452
453 // The information may have not been deserialized yet. Force deserialization
454 // to complete the needed information.
455 return getMostRecentDecl()->getCanonicalDecl()->Used;
456}
457
458void Decl::markUsed(ASTContext &C) {
459 if (isUsed(false))
460 return;
461
462 if (C.getASTMutationListener())
463 C.getASTMutationListener()->DeclarationMarkedUsed(this);
464
465 setIsUsed();
466}
467
468bool Decl::isReferenced() const {
469 if (Referenced)
470 return true;
471
472 // Check redeclarations.
473 for (const auto *I : redecls())
474 if (I->Referenced)
475 return true;
476
477 return false;
478}
479
480ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const {
481 const Decl *Definition = nullptr;
482 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
483 Definition = ID->getDefinition();
484 } else if (auto *PD = dyn_cast<ObjCProtocolDecl>(this)) {
485 Definition = PD->getDefinition();
486 } else if (auto *TD = dyn_cast<TagDecl>(this)) {
487 Definition = TD->getDefinition();
488 }
489 if (!Definition)
490 Definition = this;
491
492 if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>())
493 return attr;
494 if (auto *dcd = dyn_cast<Decl>(getDeclContext())) {
495 return dcd->getAttr<ExternalSourceSymbolAttr>();
496 }
497
498 return nullptr;
499}
500
501bool Decl::hasDefiningAttr() const {
502 return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>() ||
503 hasAttr<LoaderUninitializedAttr>();
504}
505
506const Attr *Decl::getDefiningAttr() const {
507 if (auto *AA = getAttr<AliasAttr>())
508 return AA;
509 if (auto *IFA = getAttr<IFuncAttr>())
510 return IFA;
511 if (auto *NZA = getAttr<LoaderUninitializedAttr>())
512 return NZA;
513 return nullptr;
514}
515
516static StringRef getRealizedPlatform(const AvailabilityAttr *A,
517 const ASTContext &Context) {
518 // Check if this is an App Extension "platform", and if so chop off
519 // the suffix for matching with the actual platform.
520 StringRef RealizedPlatform = A->getPlatform()->getName();
521 if (!Context.getLangOpts().AppExt)
522 return RealizedPlatform;
523 size_t suffix = RealizedPlatform.rfind("_app_extension");
524 if (suffix != StringRef::npos)
525 return RealizedPlatform.slice(0, suffix);
526 return RealizedPlatform;
527}
528
529/// Determine the availability of the given declaration based on
530/// the target platform.
531///
532/// When it returns an availability result other than \c AR_Available,
533/// if the \p Message parameter is non-NULL, it will be set to a
534/// string describing why the entity is unavailable.
535///
536/// FIXME: Make these strings localizable, since they end up in
537/// diagnostics.
538static AvailabilityResult CheckAvailability(ASTContext &Context,
539 const AvailabilityAttr *A,
540 std::string *Message,
541 VersionTuple EnclosingVersion) {
542 if (EnclosingVersion.empty())
543 EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion();
544
545 if (EnclosingVersion.empty())
546 return AR_Available;
547
548 StringRef ActualPlatform = A->getPlatform()->getName();
549 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
550
551 // Match the platform name.
552 if (getRealizedPlatform(A, Context) != TargetPlatform)
553 return AR_Available;
554
555 StringRef PrettyPlatformName
556 = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
557
558 if (PrettyPlatformName.empty())
559 PrettyPlatformName = ActualPlatform;
560
561 std::string HintMessage;
562 if (!A->getMessage().empty()) {
563 HintMessage = " - ";
564 HintMessage += A->getMessage();
565 }
566
567 // Make sure that this declaration has not been marked 'unavailable'.
568 if (A->getUnavailable()) {
569 if (Message) {
570 Message->clear();
571 llvm::raw_string_ostream Out(*Message);
572 Out << "not available on " << PrettyPlatformName
573 << HintMessage;
574 }
575
576 return AR_Unavailable;
577 }
578
579 // Make sure that this declaration has already been introduced.
580 if (!A->getIntroduced().empty() &&
581 EnclosingVersion < A->getIntroduced()) {
582 if (Message) {
583 Message->clear();
584 llvm::raw_string_ostream Out(*Message);
585 VersionTuple VTI(A->getIntroduced());
586 Out << "introduced in " << PrettyPlatformName << ' '
587 << VTI << HintMessage;
588 }
589
590 return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced;
591 }
592
593 // Make sure that this declaration hasn't been obsoleted.
594 if (!A->getObsoleted().empty() && EnclosingVersion >= A->getObsoleted()) {
595 if (Message) {
596 Message->clear();
597 llvm::raw_string_ostream Out(*Message);
598 VersionTuple VTO(A->getObsoleted());
599 Out << "obsoleted in " << PrettyPlatformName << ' '
600 << VTO << HintMessage;
601 }
602
603 return AR_Unavailable;
604 }
605
606 // Make sure that this declaration hasn't been deprecated.
607 if (!A->getDeprecated().empty() && EnclosingVersion >= A->getDeprecated()) {
608 if (Message) {
609 Message->clear();
610 llvm::raw_string_ostream Out(*Message);
611 VersionTuple VTD(A->getDeprecated());
612 Out << "first deprecated in " << PrettyPlatformName << ' '
613 << VTD << HintMessage;
614 }
615
616 return AR_Deprecated;
617 }
618
619 return AR_Available;
620}
621
622AvailabilityResult Decl::getAvailability(std::string *Message,
623 VersionTuple EnclosingVersion,
624 StringRef *RealizedPlatform) const {
625 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
626 return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion,
627 RealizedPlatform);
628
629 AvailabilityResult Result = AR_Available;
630 std::string ResultMessage;
631
632 for (const auto *A : attrs()) {
633 if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
634 if (Result >= AR_Deprecated)
635 continue;
636
637 if (Message)
638 ResultMessage = std::string(Deprecated->getMessage());
639
640 Result = AR_Deprecated;
641 continue;
642 }
643
644 if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
645 if (Message)
646 *Message = std::string(Unavailable->getMessage());
647 return AR_Unavailable;
648 }
649
650 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
651 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
652 Message, EnclosingVersion);
653
654 if (AR == AR_Unavailable) {
655 if (RealizedPlatform)
656 *RealizedPlatform = Availability->getPlatform()->getName();
657 return AR_Unavailable;
658 }
659
660 if (AR > Result) {
661 Result = AR;
662 if (Message)
663 ResultMessage.swap(*Message);
664 }
665 continue;
666 }
667 }
668
669 if (Message)
670 Message->swap(ResultMessage);
671 return Result;
672}
673
674VersionTuple Decl::getVersionIntroduced() const {
675 const ASTContext &Context = getASTContext();
676 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
677 for (const auto *A : attrs()) {
678 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
679 if (getRealizedPlatform(Availability, Context) != TargetPlatform)
680 continue;
681 if (!Availability->getIntroduced().empty())
682 return Availability->getIntroduced();
683 }
684 }
685 return {};
686}
687
688bool Decl::canBeWeakImported(bool &IsDefinition) const {
689 IsDefinition = false;
690
691 // Variables, if they aren't definitions.
692 if (const auto *Var = dyn_cast<VarDecl>(this)) {
693 if (Var->isThisDeclarationADefinition()) {
694 IsDefinition = true;
695 return false;
696 }
697 return true;
698 }
699 // Functions, if they aren't definitions.
700 if (const auto *FD = dyn_cast<FunctionDecl>(this)) {
701 if (FD->hasBody()) {
702 IsDefinition = true;
703 return false;
704 }
705 return true;
706
707 }
708 // Objective-C classes, if this is the non-fragile runtime.
709 if (isa<ObjCInterfaceDecl>(this) &&
710 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
711 return true;
712 }
713 // Nothing else.
714 return false;
715}
716
717bool Decl::isWeakImported() const {
718 bool IsDefinition;
719 if (!canBeWeakImported(IsDefinition))
720 return false;
721
722 for (const auto *A : getMostRecentDecl()->attrs()) {
723 if (isa<WeakImportAttr>(A))
724 return true;
725
726 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
727 if (CheckAvailability(getASTContext(), Availability, nullptr,
728 VersionTuple()) == AR_NotYetIntroduced)
729 return true;
730 }
731 }
732
733 return false;
734}
735
736unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
737 switch (DeclKind) {
738 case Function:
739 case CXXDeductionGuide:
740 case CXXMethod: