clang 18.0.0git
SemaAvailability.cpp
Go to the documentation of this file.
1//===--- SemaAvailability.cpp - Availability attribute handling -----------===//
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 processes the availability attribute.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/Attr.h"
14#include "clang/AST/Decl.h"
21#include "clang/Sema/Sema.h"
22#include <optional>
23
24using namespace clang;
25using namespace sema;
26
27static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context,
28 const Decl *D) {
29 // Check each AvailabilityAttr to find the one for this platform.
30 for (const auto *A : D->attrs()) {
31 if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) {
32 // FIXME: this is copied from CheckAvailability. We should try to
33 // de-duplicate.
34
35 // Check if this is an App Extension "platform", and if so chop off
36 // the suffix for matching with the actual platform.
37 StringRef ActualPlatform = Avail->getPlatform()->getName();
38 StringRef RealizedPlatform = ActualPlatform;
39 if (Context.getLangOpts().AppExt) {
40 size_t suffix = RealizedPlatform.rfind("_app_extension");
41 if (suffix != StringRef::npos)
42 RealizedPlatform = RealizedPlatform.slice(0, suffix);
43 }
44
45 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
46
47 // Match the platform name.
48 if (RealizedPlatform == TargetPlatform)
49 return Avail;
50 }
51 }
52 return nullptr;
53}
54
55/// The diagnostic we should emit for \c D, and the declaration that
56/// originated it, or \c AR_Available.
57///
58/// \param D The declaration to check.
59/// \param Message If non-null, this will be populated with the message from
60/// the availability attribute that is selected.
61/// \param ClassReceiver If we're checking the method of a class message
62/// send, the class. Otherwise nullptr.
63static std::pair<AvailabilityResult, const NamedDecl *>
65 std::string *Message,
66 ObjCInterfaceDecl *ClassReceiver) {
67 AvailabilityResult Result = D->getAvailability(Message);
68
69 // For typedefs, if the typedef declaration appears available look
70 // to the underlying type to see if it is more restrictive.
71 while (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
72 if (Result == AR_Available) {
73 if (const auto *TT = TD->getUnderlyingType()->getAs<TagType>()) {
74 D = TT->getDecl();
75 Result = D->getAvailability(Message);
76 continue;
77 }
78 }
79 break;
80 }
81
82 // Forward class declarations get their attributes from their definition.
83 if (const auto *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
84 if (IDecl->getDefinition()) {
85 D = IDecl->getDefinition();
86 Result = D->getAvailability(Message);
87 }
88 }
89
90 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D))
91 if (Result == AR_Available) {
92 const DeclContext *DC = ECD->getDeclContext();
93 if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) {
94 Result = TheEnumDecl->getAvailability(Message);
95 D = TheEnumDecl;
96 }
97 }
98
99 // For +new, infer availability from -init.
100 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
101 if (S.NSAPIObj && ClassReceiver) {
102 ObjCMethodDecl *Init = ClassReceiver->lookupInstanceMethod(
103 S.NSAPIObj->getInitSelector());
104 if (Init && Result == AR_Available && MD->isClassMethod() &&
105 MD->getSelector() == S.NSAPIObj->getNewSelector() &&
106 MD->definedInNSObject(S.getASTContext())) {
107 Result = Init->getAvailability(Message);
108 D = Init;
109 }
110 }
111 }
112
113 return {Result, D};
114}
115
116
117/// whether we should emit a diagnostic for \c K and \c DeclVersion in
118/// the context of \c Ctx. For example, we should emit an unavailable diagnostic
119/// in a deprecated context, but not the other way around.
120static bool
122 VersionTuple DeclVersion, Decl *Ctx,
123 const NamedDecl *OffendingDecl) {
124 assert(K != AR_Available && "Expected an unavailable declaration here!");
125
126 // If this was defined using CF_OPTIONS, etc. then ignore the diagnostic.
127 auto DeclLoc = Ctx->getBeginLoc();
128 // This is only a problem in Foundation's C++ implementation for CF_OPTIONS.
129 if (DeclLoc.isMacroID() && S.getLangOpts().CPlusPlus &&
130 isa<TypedefDecl>(OffendingDecl)) {
131 StringRef MacroName = S.getPreprocessor().getImmediateMacroName(DeclLoc);
132 if (MacroName == "CF_OPTIONS" || MacroName == "OBJC_OPTIONS" ||
133 MacroName == "SWIFT_OPTIONS" || MacroName == "NS_OPTIONS") {
134 return false;
135 }
136 }
137
138 // Checks if we should emit the availability diagnostic in the context of C.
139 auto CheckContext = [&](const Decl *C) {
140 if (K == AR_NotYetIntroduced) {
141 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C))
142 if (AA->getIntroduced() >= DeclVersion)
143 return true;
144 } else if (K == AR_Deprecated) {
145 if (C->isDeprecated())
146 return true;
147 } else if (K == AR_Unavailable) {
148 // It is perfectly fine to refer to an 'unavailable' Objective-C method
149 // when it is referenced from within the @implementation itself. In this
150 // context, we interpret unavailable as a form of access control.
151 if (const auto *MD = dyn_cast<ObjCMethodDecl>(OffendingDecl)) {
152 if (const auto *Impl = dyn_cast<ObjCImplDecl>(C)) {
153 if (MD->getClassInterface() == Impl->getClassInterface())
154 return true;
155 }
156 }
157 }
158
159 if (C->isUnavailable())
160 return true;
161 return false;
162 };
163
164 do {
165 if (CheckContext(Ctx))
166 return false;
167
168 // An implementation implicitly has the availability of the interface.
169 // Unless it is "+load" method.
170 if (const auto *MethodD = dyn_cast<ObjCMethodDecl>(Ctx))
171 if (MethodD->isClassMethod() &&
172 MethodD->getSelector().getAsString() == "load")
173 return true;
174
175 if (const auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) {
176 if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface())
177 if (CheckContext(Interface))
178 return false;
179 }
180 // A category implicitly has the availability of the interface.
181 else if (const auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx))
182 if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface())
183 if (CheckContext(Interface))
184 return false;
185 } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext())));
186
187 return true;
188}
189
190static bool
192 const VersionTuple &DeploymentVersion,
193 const VersionTuple &DeclVersion) {
194 const auto &Triple = Context.getTargetInfo().getTriple();
195 VersionTuple ForceAvailabilityFromVersion;
196 switch (Triple.getOS()) {
197 case llvm::Triple::IOS:
198 case llvm::Triple::TvOS:
199 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11);
200 break;
201 case llvm::Triple::WatchOS:
202 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4);
203 break;
204 case llvm::Triple::Darwin:
205 case llvm::Triple::MacOSX:
206 ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13);
207 break;
208 case llvm::Triple::ShaderModel:
209 // Always enable availability diagnostics for shader models.
210 return true;
211 default:
212 // New targets should always warn about availability.
213 return Triple.getVendor() == llvm::Triple::Apple;
214 }
215 return DeploymentVersion >= ForceAvailabilityFromVersion ||
216 DeclVersion >= ForceAvailabilityFromVersion;
217}
218
220 for (Decl *Ctx = OrigCtx; Ctx;
221 Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) {
222 if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx))
223 return cast<NamedDecl>(Ctx);
224 if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) {
225 if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx))
226 return Imp->getClassInterface();
227 return CD;
228 }
229 }
230
231 return dyn_cast<NamedDecl>(OrigCtx);
232}
233
234namespace {
235
236struct AttributeInsertion {
237 StringRef Prefix;
238 SourceLocation Loc;
239 StringRef Suffix;
240
241 static AttributeInsertion createInsertionAfter(const NamedDecl *D) {
242 return {" ", D->getEndLoc(), ""};
243 }
244 static AttributeInsertion createInsertionAfter(SourceLocation Loc) {
245 return {" ", Loc, ""};
246 }
247 static AttributeInsertion createInsertionBefore(const NamedDecl *D) {
248 return {"", D->getBeginLoc(), "\n"};
249 }
250};
251
252} // end anonymous namespace
253
254/// Tries to parse a string as ObjC method name.
255///
256/// \param Name The string to parse. Expected to originate from availability
257/// attribute argument.
258/// \param SlotNames The vector that will be populated with slot names. In case
259/// of unsuccessful parsing can contain invalid data.
260/// \returns A number of method parameters if parsing was successful,
261/// std::nullopt otherwise.
262static std::optional<unsigned>
264 const LangOptions &LangOpts) {
265 // Accept replacements starting with - or + as valid ObjC method names.
266 if (!Name.empty() && (Name.front() == '-' || Name.front() == '+'))
267 Name = Name.drop_front(1);
268 if (Name.empty())
269 return std::nullopt;
270 Name.split(SlotNames, ':');
271 unsigned NumParams;
272 if (Name.back() == ':') {
273 // Remove an empty string at the end that doesn't represent any slot.
274 SlotNames.pop_back();
275 NumParams = SlotNames.size();
276 } else {
277 if (SlotNames.size() != 1)
278 // Not a valid method name, just a colon-separated string.
279 return std::nullopt;
280 NumParams = 0;
281 }
282 // Verify all slot names are valid.
283 bool AllowDollar = LangOpts.DollarIdents;
284 for (StringRef S : SlotNames) {
285 if (S.empty())
286 continue;
287 if (!isValidAsciiIdentifier(S, AllowDollar))
288 return std::nullopt;
289 }
290 return NumParams;
291}
292
293/// Returns a source location in which it's appropriate to insert a new
294/// attribute for the given declaration \D.
295static std::optional<AttributeInsertion>
297 const LangOptions &LangOpts) {
298 if (isa<ObjCPropertyDecl>(D))
299 return AttributeInsertion::createInsertionAfter(D);
300 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
301 if (MD->hasBody())
302 return std::nullopt;
303 return AttributeInsertion::createInsertionAfter(D);
304 }
305 if (const auto *TD = dyn_cast<TagDecl>(D)) {
306 SourceLocation Loc =
307 Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts);
308 if (Loc.isInvalid())
309 return std::nullopt;
310 // Insert after the 'struct'/whatever keyword.
311 return AttributeInsertion::createInsertionAfter(Loc);
312 }
313 return AttributeInsertion::createInsertionBefore(D);
314}
315
316/// Actually emit an availability diagnostic for a reference to an unavailable
317/// decl.
318///
319/// \param Ctx The context that the reference occurred in
320/// \param ReferringDecl The exact declaration that was referenced.
321/// \param OffendingDecl A related decl to \c ReferringDecl that has an
322/// availability attribute corresponding to \c K attached to it. Note that this
323/// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and
324/// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl
325/// and OffendingDecl is the EnumDecl.
327 Decl *Ctx, const NamedDecl *ReferringDecl,
328 const NamedDecl *OffendingDecl,
329 StringRef Message,
331 const ObjCInterfaceDecl *UnknownObjCClass,
332 const ObjCPropertyDecl *ObjCProperty,
333 bool ObjCPropertyAccess) {
334 // Diagnostics for deprecated or unavailable.
335 unsigned diag, diag_message, diag_fwdclass_message;
336 unsigned diag_available_here = diag::note_availability_specified_here;
337 SourceLocation NoteLocation = OffendingDecl->getLocation();
338
339 // Matches 'diag::note_property_attribute' options.
340 unsigned property_note_select;
341
342 // Matches diag::note_availability_specified_here.
343 unsigned available_here_select_kind;
344
345 VersionTuple DeclVersion;
346 if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl))
347 DeclVersion = AA->getIntroduced();
348
349 if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx,
350 OffendingDecl))
351 return;
352
353 SourceLocation Loc = Locs.front();
354
355 // The declaration can have multiple availability attributes, we are looking
356 // at one of them.
357 const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl);
358 if (A && A->isInherited()) {
359 for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl;
360 Redecl = Redecl->getPreviousDecl()) {
361 const AvailabilityAttr *AForRedecl =
362 getAttrForPlatform(S.Context, Redecl);
363 if (AForRedecl && !AForRedecl->isInherited()) {
364 // If D is a declaration with inherited attributes, the note should
365 // point to the declaration with actual attributes.
366 NoteLocation = Redecl->getLocation();
367 break;
368 }
369 }
370 }
371
372 switch (K) {
373 case AR_NotYetIntroduced: {
374 // We would like to emit the diagnostic even if -Wunguarded-availability is
375 // not specified for deployment targets >= to iOS 11 or equivalent or
376 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
377 // later.
378 const AvailabilityAttr *AA =
379 getAttrForPlatform(S.getASTContext(), OffendingDecl);
380 VersionTuple Introduced = AA->getIntroduced();
381
382 bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
384 Introduced);
385 unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
386 : diag::warn_unguarded_availability;
387
388 std::string PlatformName(AvailabilityAttr::getPrettyPlatformName(
390
391 S.Diag(Loc, Warning) << OffendingDecl << PlatformName
392 << Introduced.getAsString();
393
394 S.Diag(OffendingDecl->getLocation(),
395 diag::note_partial_availability_specified_here)
396 << OffendingDecl << PlatformName << Introduced.getAsString()
397 << S.Context.getTargetInfo().getPlatformMinVersion().getAsString();
398
399 if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) {
400 if (const auto *TD = dyn_cast<TagDecl>(Enclosing))
401 if (TD->getDeclName().isEmpty()) {
402 S.Diag(TD->getLocation(),
403 diag::note_decl_unguarded_availability_silence)
404 << /*Anonymous*/ 1 << TD->getKindName();
405 return;
406 }
407 auto FixitNoteDiag =
408 S.Diag(Enclosing->getLocation(),
409 diag::note_decl_unguarded_availability_silence)
410 << /*Named*/ 0 << Enclosing;
411 // Don't offer a fixit for declarations with availability attributes.
412 if (Enclosing->hasAttr<AvailabilityAttr>())
413 return;
414 if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE"))
415 return;
416 std::optional<AttributeInsertion> Insertion = createAttributeInsertion(
417 Enclosing, S.getSourceManager(), S.getLangOpts());
418 if (!Insertion)
419 return;
420 std::string PlatformName =
421 AvailabilityAttr::getPlatformNameSourceSpelling(
423 .lower();
424 std::string Introduced =
425 OffendingDecl->getVersionIntroduced().getAsString();
426 FixitNoteDiag << FixItHint::CreateInsertion(
427 Insertion->Loc,
428 (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName +
429 "(" + Introduced + "))" + Insertion->Suffix)
430 .str());
431 }
432 return;
433 }
434 case AR_Deprecated:
435 diag = !ObjCPropertyAccess ? diag::warn_deprecated
436 : diag::warn_property_method_deprecated;
437 diag_message = diag::warn_deprecated_message;
438 diag_fwdclass_message = diag::warn_deprecated_fwdclass_message;
439 property_note_select = /* deprecated */ 0;
440 available_here_select_kind = /* deprecated */ 2;
441 if (const auto *AL = OffendingDecl->getAttr<DeprecatedAttr>())
442 NoteLocation = AL->getLocation();
443 break;
444
445 case AR_Unavailable:
446 diag = !ObjCPropertyAccess ? diag::err_unavailable
447 : diag::err_property_method_unavailable;
448 diag_message = diag::err_unavailable_message;
449 diag_fwdclass_message = diag::warn_unavailable_fwdclass_message;
450 property_note_select = /* unavailable */ 1;
451 available_here_select_kind = /* unavailable */ 0;
452
453 if (auto AL = OffendingDecl->getAttr<UnavailableAttr>()) {
454 if (AL->isImplicit() && AL->getImplicitReason()) {
455 // Most of these failures are due to extra restrictions in ARC;
456 // reflect that in the primary diagnostic when applicable.
457 auto flagARCError = [&] {
458 if (S.getLangOpts().ObjCAutoRefCount &&
460 OffendingDecl->getLocation()))
461 diag = diag::err_unavailable_in_arc;
462 };
463
464 switch (AL->getImplicitReason()) {
465 case UnavailableAttr::IR_None: break;
466
467 case UnavailableAttr::IR_ARCForbiddenType:
468 flagARCError();
469 diag_available_here = diag::note_arc_forbidden_type;
470 break;
471
472 case UnavailableAttr::IR_ForbiddenWeak:
473 if (S.getLangOpts().ObjCWeakRuntime)
474 diag_available_here = diag::note_arc_weak_disabled;
475 else
476 diag_available_here = diag::note_arc_weak_no_runtime;
477 break;
478
479 case UnavailableAttr::IR_ARCForbiddenConversion:
480 flagARCError();
481 diag_available_here = diag::note_performs_forbidden_arc_conversion;
482 break;
483
484 case UnavailableAttr::IR_ARCInitReturnsUnrelated:
485 flagARCError();
486 diag_available_here = diag::note_arc_init_returns_unrelated;
487 break;
488
489 case UnavailableAttr::IR_ARCFieldWithOwnership:
490 flagARCError();
491 diag_available_here = diag::note_arc_field_with_ownership;
492 break;
493 }
494 }
495 }
496 break;
497
498 case AR_Available:
499 llvm_unreachable("Warning for availability of available declaration?");
500 }
501
503 if (K == AR_Deprecated) {
504 StringRef Replacement;
505 if (auto AL = OffendingDecl->getAttr<DeprecatedAttr>())
506 Replacement = AL->getReplacement();
507 if (auto AL = getAttrForPlatform(S.Context, OffendingDecl))
508 Replacement = AL->getReplacement();
509
510 CharSourceRange UseRange;
511 if (!Replacement.empty())
512 UseRange =
514 if (UseRange.isValid()) {
515 if (const auto *MethodDecl = dyn_cast<ObjCMethodDecl>(ReferringDecl)) {
516 Selector Sel = MethodDecl->getSelector();
517 SmallVector<StringRef, 12> SelectorSlotNames;
518 std::optional<unsigned> NumParams = tryParseObjCMethodName(
519 Replacement, SelectorSlotNames, S.getLangOpts());
520 if (NumParams && *NumParams == Sel.getNumArgs()) {
521 assert(SelectorSlotNames.size() == Locs.size());
522 for (unsigned I = 0; I < Locs.size(); ++I) {
523 if (!Sel.getNameForSlot(I).empty()) {
525 Locs[I], S.getLocForEndOfToken(Locs[I]));
526 FixIts.push_back(FixItHint::CreateReplacement(
527 NameRange, SelectorSlotNames[I]));
528 } else
529 FixIts.push_back(
530 FixItHint::CreateInsertion(Locs[I], SelectorSlotNames[I]));
531 }
532 } else
533 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
534 } else
535 FixIts.push_back(FixItHint::CreateReplacement(UseRange, Replacement));
536 }
537 }
538
539 if (!Message.empty()) {
540 S.Diag(Loc, diag_message) << ReferringDecl << Message << FixIts;
541 if (ObjCProperty)
542 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
543 << ObjCProperty->getDeclName() << property_note_select;
544 } else if (!UnknownObjCClass) {
545 S.Diag(Loc, diag) << ReferringDecl << FixIts;
546 if (ObjCProperty)
547 S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute)
548 << ObjCProperty->getDeclName() << property_note_select;
549 } else {
550 S.Diag(Loc, diag_fwdclass_message) << ReferringDecl << FixIts;
551 S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
552 }
553
554 S.Diag(NoteLocation, diag_available_here)
555 << OffendingDecl << available_here_select_kind;
556}
557
560 "Expected an availability diagnostic here");
561
562 DD.Triggered = true;
567 DD.getObjCProperty(), false);
568}
569
571 const NamedDecl *ReferringDecl,
572 const NamedDecl *OffendingDecl,
573 StringRef Message,
575 const ObjCInterfaceDecl *UnknownObjCClass,
576 const ObjCPropertyDecl *ObjCProperty,
577 bool ObjCPropertyAccess) {
578 // Delay if we're currently parsing a declaration.
582 AR, Locs, ReferringDecl, OffendingDecl, UnknownObjCClass,
583 ObjCProperty, Message, ObjCPropertyAccess));
584 return;
585 }
586
587 Decl *Ctx = cast<Decl>(S.getCurLexicalContext());
588 DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl,
589 Message, Locs, UnknownObjCClass, ObjCProperty,
590 ObjCPropertyAccess);
591}
592
593namespace {
594
595/// Returns true if the given statement can be a body-like child of \p Parent.
596bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) {
597 switch (Parent->getStmtClass()) {
598 case Stmt::IfStmtClass:
599 return cast<IfStmt>(Parent)->getThen() == S ||
600 cast<IfStmt>(Parent)->getElse() == S;
601 case Stmt::WhileStmtClass:
602 return cast<WhileStmt>(Parent)->getBody() == S;
603 case Stmt::DoStmtClass:
604 return cast<DoStmt>(Parent)->getBody() == S;
605 case Stmt::ForStmtClass:
606 return cast<ForStmt>(Parent)->getBody() == S;
607 case Stmt::CXXForRangeStmtClass:
608 return cast<CXXForRangeStmt>(Parent)->getBody() == S;
609 case Stmt::ObjCForCollectionStmtClass:
610 return cast<ObjCForCollectionStmt>(Parent)->getBody() == S;
611 case Stmt::CaseStmtClass:
612 case Stmt::DefaultStmtClass:
613 return cast<SwitchCase>(Parent)->getSubStmt() == S;
614 default:
615 return false;
616 }
617}
618
619class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> {
620 const Stmt *Target;
621
622public:
623 bool VisitStmt(Stmt *S) { return S != Target; }
624
625 /// Returns true if the given statement is present in the given declaration.
626 static bool isContained(const Stmt *Target, const Decl *D) {
627 StmtUSEFinder Visitor;
628 Visitor.Target = Target;
629 return !Visitor.TraverseDecl(const_cast<Decl *>(D));
630 }
631};
632
633/// Traverses the AST and finds the last statement that used a given
634/// declaration.
635class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> {
636 const Decl *D;
637
638public:
639 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
640 if (DRE->getDecl() == D)
641 return false;
642 return true;
643 }
644
645 static const Stmt *findLastStmtThatUsesDecl(const Decl *D,
646 const CompoundStmt *Scope) {
647 LastDeclUSEFinder Visitor;
648 Visitor.D = D;
649 for (const Stmt *S : llvm::reverse(Scope->body())) {
650 if (!Visitor.TraverseStmt(const_cast<Stmt *>(S)))
651 return S;
652 }
653 return nullptr;
654 }
655};
656
657/// This class implements -Wunguarded-availability.
658///
659/// This is done with a traversal of the AST of a function that makes reference
660/// to a partially available declaration. Whenever we encounter an \c if of the
661/// form: \c if(@available(...)), we use the version from the condition to visit
662/// the then statement.
663class DiagnoseUnguardedAvailability
664 : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> {
666
667 Sema &SemaRef;
668 Decl *Ctx;
669
670 /// Stack of potentially nested 'if (@available(...))'s.
671 SmallVector<VersionTuple, 8> AvailabilityStack;
673
674 void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range,
675 ObjCInterfaceDecl *ClassReceiver = nullptr);
676
677public:
678 DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx)
679 : SemaRef(SemaRef), Ctx(Ctx) {
680 AvailabilityStack.push_back(
682 }
683
684 bool TraverseStmt(Stmt *S) {
685 if (!S)
686 return true;
687 StmtStack.push_back(S);
688 bool Result = Base::TraverseStmt(S);
689 StmtStack.pop_back();
690 return Result;
691 }
692
693 void IssueDiagnostics(Stmt *S) { TraverseStmt(S); }
694
695 bool TraverseIfStmt(IfStmt *If);
696
697 // for 'case X:' statements, don't bother looking at the 'X'; it can't lead
698 // to any useful diagnostics.
699 bool TraverseCaseStmt(CaseStmt *CS) { return TraverseStmt(CS->getSubStmt()); }
700
701 bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) { return true; }
702
703 bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) {
704 if (ObjCMethodDecl *D = Msg->getMethodDecl()) {
705 ObjCInterfaceDecl *ID = nullptr;
706 QualType ReceiverTy = Msg->getClassReceiver();
707 if (!ReceiverTy.isNull() && ReceiverTy->getAsObjCInterfaceType())
708 ID = ReceiverTy->getAsObjCInterfaceType()->getInterface();
709
710 DiagnoseDeclAvailability(
711 D, SourceRange(Msg->getSelectorStartLoc(), Msg->getEndLoc()), ID);
712 }
713 return true;
714 }
715
716 bool VisitDeclRefExpr(DeclRefExpr *DRE) {
717 DiagnoseDeclAvailability(DRE->getDecl(),
718 SourceRange(DRE->getBeginLoc(), DRE->getEndLoc()));
719 return true;
720 }
721
722 bool VisitMemberExpr(MemberExpr *ME) {
723 DiagnoseDeclAvailability(ME->getMemberDecl(),
724 SourceRange(ME->getBeginLoc(), ME->getEndLoc()));
725 return true;
726 }
727
728 bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
729 SemaRef.Diag(E->getBeginLoc(), diag::warn_at_available_unchecked_use)
730 << (!SemaRef.getLangOpts().ObjC);
731 return true;
732 }
733
734 bool VisitTypeLoc(TypeLoc Ty);
735};
736
737void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
738 NamedDecl *D, SourceRange Range, ObjCInterfaceDecl *ReceiverClass) {
740 const NamedDecl *OffendingDecl;
741 std::tie(Result, OffendingDecl) =
742 ShouldDiagnoseAvailabilityOfDecl(SemaRef, D, nullptr, ReceiverClass);
743 if (Result != AR_Available) {
744 // All other diagnostic kinds have already been handled in
745 // DiagnoseAvailabilityOfDecl.
747 return;
748
749 const AvailabilityAttr *AA =
750 getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl);
751 VersionTuple Introduced = AA->getIntroduced();
752
753 if (AvailabilityStack.back() >= Introduced)
754 return;
755
756 // If the context of this function is less available than D, we should not
757 // emit a diagnostic.
758 if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx,
759 OffendingDecl))
760 return;
761
762 // We would like to emit the diagnostic even if -Wunguarded-availability is
763 // not specified for deployment targets >= to iOS 11 or equivalent or
764 // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
765 // later.
766 unsigned DiagKind =
768 SemaRef.Context,
769 SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced)
770 ? diag::warn_unguarded_availability_new
771 : diag::warn_unguarded_availability;
772
773 std::string PlatformName(AvailabilityAttr::getPrettyPlatformName(
775
776 SemaRef.Diag(Range.getBegin(), DiagKind)
777 << Range << D << PlatformName << Introduced.getAsString();
778
779 SemaRef.Diag(OffendingDecl->getLocation(),
780 diag::note_partial_availability_specified_here)
781 << OffendingDecl << PlatformName << Introduced.getAsString()
782 << SemaRef.Context.getTargetInfo()
784 .getAsString();
785
786 auto FixitDiag =
787 SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence)
788 << Range << D
789 << (SemaRef.getLangOpts().ObjC ? /*@available*/ 0
790 : /*__builtin_available*/ 1);
791
792 // Find the statement which should be enclosed in the if @available check.
793 if (StmtStack.empty())
794 return;
795 const Stmt *StmtOfUse = StmtStack.back();
796 const CompoundStmt *Scope = nullptr;
797 for (const Stmt *S : llvm::reverse(StmtStack)) {
798 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
799 Scope = CS;
800 break;
801 }
802 if (isBodyLikeChildStmt(StmtOfUse, S)) {
803 // The declaration won't be seen outside of the statement, so we don't
804 // have to wrap the uses of any declared variables in if (@available).
805 // Therefore we can avoid setting Scope here.
806 break;
807 }
808 StmtOfUse = S;
809 }
810 const Stmt *LastStmtOfUse = nullptr;
811 if (isa<DeclStmt>(StmtOfUse) && Scope) {
812 for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) {
813 if (StmtUSEFinder::isContained(StmtStack.back(), D)) {
814 LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope);
815 break;
816 }
817 }
818 }
819
820 const SourceManager &SM = SemaRef.getSourceManager();
821 SourceLocation IfInsertionLoc =
822 SM.getExpansionLoc(StmtOfUse->getBeginLoc());
823 SourceLocation StmtEndLoc =
824 SM.getExpansionRange(
825 (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getEndLoc())
826 .getEnd();
827 if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc))
828 return;
829
830 StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM);
831 const char *ExtraIndentation = " ";
832 std::string FixItString;
833 llvm::raw_string_ostream FixItOS(FixItString);
834 FixItOS << "if (" << (SemaRef.getLangOpts().ObjC ? "@available"
835 : "__builtin_available")
836 << "("
837 << AvailabilityAttr::getPlatformNameSourceSpelling(
839 << " " << Introduced.getAsString() << ", *)) {\n"
840 << Indentation << ExtraIndentation;
841 FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str());
843 StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(),
844 /*SkipTrailingWhitespaceAndNewLine=*/false);
845 if (ElseInsertionLoc.isInvalid())
846 ElseInsertionLoc =
847 Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts());
848 FixItOS.str().clear();
849 FixItOS << "\n"
850 << Indentation << "} else {\n"
851 << Indentation << ExtraIndentation
852 << "// Fallback on earlier versions\n"
853 << Indentation << "}";
854 FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str());
855 }
856}
857
858bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) {
859 const Type *TyPtr = Ty.getTypePtr();
861
862 if (Range.isInvalid())
863 return true;
864
865 if (const auto *TT = dyn_cast<TagType>(TyPtr)) {
866 TagDecl *TD = TT->getDecl();
867 DiagnoseDeclAvailability(TD, Range);
868
869 } else if (const auto *TD = dyn_cast<TypedefType>(TyPtr)) {
870 TypedefNameDecl *D = TD->getDecl();
871 DiagnoseDeclAvailability(D, Range);
872
873 } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) {
874 if (NamedDecl *D = ObjCO->getInterface())
875 DiagnoseDeclAvailability(D, Range);
876 }
877
878 return true;
879}
880
881bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) {
882 VersionTuple CondVersion;
883 if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) {
884 CondVersion = E->getVersion();
885
886 // If we're using the '*' case here or if this check is redundant, then we
887 // use the enclosing version to check both branches.
888 if (CondVersion.empty() || CondVersion <= AvailabilityStack.back())
889 return TraverseStmt(If->getThen()) && TraverseStmt(If->getElse());
890 } else {
891 // This isn't an availability checking 'if', we can just continue.
892 return Base::TraverseIfStmt(If);
893 }
894
895 AvailabilityStack.push_back(CondVersion);
896 bool ShouldContinue = TraverseStmt(If->getThen());
897 AvailabilityStack.pop_back();
898
899 return ShouldContinue && TraverseStmt(If->getElse());
900}
901
902} // end anonymous namespace
903
905 Stmt *Body = nullptr;
906
907 if (auto *FD = D->getAsFunction()) {
908 // FIXME: We only examine the pattern decl for availability violations now,
909 // but we should also examine instantiated templates.
910 if (FD->isTemplateInstantiation())
911 return;
912
913 Body = FD->getBody();
914
915 if (auto *CD = dyn_cast<CXXConstructorDecl>(FD))
916 for (const CXXCtorInitializer *CI : CD->inits())
917 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(CI->getInit());
918
919 } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D))
920 Body = MD->getBody();
921 else if (auto *BD = dyn_cast<BlockDecl>(D))
922 Body = BD->getBody();
923
924 assert(Body && "Need a body here!");
925
926 DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body);
927}
928
930 if (FunctionScopes.empty())
931 return nullptr;
932
933 // Conservatively search the entire current function scope context for
934 // availability violations. This ensures we always correctly analyze nested
935 // classes, blocks, lambdas, etc. that may or may not be inside if(@available)
936 // checks themselves.
937 return FunctionScopes.front();
938}
939
942 const ObjCInterfaceDecl *UnknownObjCClass,
943 bool ObjCPropertyAccess,
944 bool AvoidPartialAvailabilityChecks,
945 ObjCInterfaceDecl *ClassReceiver) {
946 std::string Message;
948 const NamedDecl* OffendingDecl;
949 // See if this declaration is unavailable, deprecated, or partial.
950 std::tie(Result, OffendingDecl) =
951 ShouldDiagnoseAvailabilityOfDecl(*this, D, &Message, ClassReceiver);
952 if (Result == AR_Available)
953 return;
954
956 if (AvoidPartialAvailabilityChecks)
957 return;
958
959 // We need to know the @available context in the current function to
960 // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that
961 // when we're done parsing the current function.
963 Context->HasPotentialAvailabilityViolations = true;
964 return;
965 }
966 }
967
968 const ObjCPropertyDecl *ObjCPDecl = nullptr;
969 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
970 if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) {
971 AvailabilityResult PDeclResult = PD->getAvailability(nullptr);
972 if (PDeclResult == Result)
973 ObjCPDecl = PD;
974 }
975 }
976
977 EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Locs,
978 UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess);
979}
NodeId Parent
Definition: ASTDiff.cpp:191
#define SM(sm)
Definition: Cuda.cpp:80
Defines the classes clang::DelayedDiagnostic and clang::AccessedEntity.
Defines the clang::Preprocessor interface.
static std::pair< AvailabilityResult, const NamedDecl * > ShouldDiagnoseAvailabilityOfDecl(Sema &S, const NamedDecl *D, std::string *Message, ObjCInterfaceDecl *ClassReceiver)
The diagnostic we should emit for D, and the declaration that originated it, or AR_Available.
static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, VersionTuple DeclVersion, Decl *Ctx, const NamedDecl *OffendingDecl)
whether we should emit a diagnostic for K and DeclVersion in the context of Ctx.
static std::optional< AttributeInsertion > createAttributeInsertion(const NamedDecl *D, const SourceManager &SM, const LangOptions &LangOpts)
Returns a source location in which it's appropriate to insert a new attribute for the given declarati...
static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR, const NamedDecl *ReferringDecl, const NamedDecl *OffendingDecl, StringRef Message, ArrayRef< SourceLocation > Locs, const ObjCInterfaceDecl *UnknownObjCClass, const ObjCPropertyDecl *ObjCProperty, bool ObjCPropertyAccess)
static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, Decl *Ctx, const NamedDecl *ReferringDecl, const NamedDecl *OffendingDecl, StringRef Message, ArrayRef< SourceLocation > Locs, const ObjCInterfaceDecl *UnknownObjCClass, const ObjCPropertyDecl *ObjCProperty, bool ObjCPropertyAccess)
Actually emit an availability diagnostic for a reference to an unavailable decl.
static bool shouldDiagnoseAvailabilityByDefault(const ASTContext &Context, const VersionTuple &DeploymentVersion, const VersionTuple &DeclVersion)
static NamedDecl * findEnclosingDeclToAnnotate(Decl *OrigCtx)
static std::optional< unsigned > tryParseObjCMethodName(StringRef Name, SmallVectorImpl< StringRef > &SlotNames, const LangOptions &LangOpts)
Tries to parse a string as ObjC method name.
static const AvailabilityAttr * getAttrForPlatform(ASTContext &Context, const Decl *D)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
const LangOptions & getLangOpts() const
Definition: ASTContext.h:761
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:743
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2259
CaseStmt - Represent a case statement.
Definition: Stmt.h:1622
Stmt * getSubStmt()
Definition: Stmt.h:1739
Represents a character-granular source range.
static CharSourceRange getCharRange(SourceRange R)
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1429
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1409
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1242
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.cpp:601
ValueDecl * getDecl()
Definition: Expr.h:1310
SourceLocation getEndLoc() const LLVM_READONLY
Definition: Expr.cpp:606
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration,...
Definition: DeclBase.h:1029
SourceLocation getEndLoc() const LLVM_READONLY
Definition: DeclBase.h:428
T * getAttr() const
Definition: DeclBase.h:556
AvailabilityResult getAvailability(std::string *Message=nullptr, VersionTuple EnclosingVersion=VersionTuple(), StringRef *RealizedPlatform=nullptr) const
Determine the availability of the given declaration.
Definition: DeclBase.cpp:637
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition: DeclBase.cpp:228
SourceLocation getLocation() const
Definition: DeclBase.h:432
DeclContext * getDeclContext()
Definition: DeclBase.h:441
attr_range attrs() const
Definition: DeclBase.h:519
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:424
VersionTuple getVersionIntroduced() const
Retrieve the version of the target platform in which this declaration was introduced.
Definition: DeclBase.cpp:689
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition: Diagnostic.h:134
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:97
IfStmt - This represents an if/then/else.
Definition: Stmt.h:1959
Stmt * getThen()
Definition: Stmt.h:2048
Expr * getCond()
Definition: Stmt.h:2036
Stmt * getElse()
Definition: Stmt.h:2057
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:83
static SourceLocation findLocationAfterToken(SourceLocation loc, tok::TokenKind TKind, const SourceManager &SM, const LangOptions &LangOpts, bool SkipTrailingWhitespaceAndNewLine)
Checks that the given token is the first token that occurs after the given location (this excludes co...
Definition: Lexer.cpp:1310
static StringRef getIndentationForLine(SourceLocation Loc, const SourceManager &SM)
Returns the leading whitespace for line that corresponds to the given location Loc.
Definition: Lexer.cpp:1110
static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &LangOpts)
Computes the source location just past the end of the token at this source location.
Definition: Lexer.cpp:802
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3195
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3274
SourceLocation getEndLoc() const LLVM_READONLY
Definition: Expr.cpp:1830
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.cpp:1816
This represents a decl that may have a name.
Definition: Decl.h:247
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:313
NamedDecl * getMostRecentDecl()
Definition: Decl.h:473
A runtime availability query.
Definition: ExprObjC.h:1685
SourceLocation getBeginLoc() const
Definition: ExprObjC.h:1702
VersionTuple getVersion() const
Definition: ExprObjC.h:1708
Represents an ObjC class declaration.
Definition: DeclObjC.h:1147
ObjCMethodDecl * lookupInstanceMethod(Selector Sel) const
Lookup an instance method for a given selector.
Definition: DeclObjC.h:1834
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:942
QualType getClassReceiver() const
Returns the type of a class message send, or NULL if the message is not a class message.
Definition: ExprObjC.h:1269
const ObjCMethodDecl * getMethodDecl() const
Definition: ExprObjC.h:1346
SourceLocation getEndLoc() const LLVM_READONLY
Definition: ExprObjC.h:1442
SourceLocation getSelectorStartLoc() const
Definition: ExprObjC.h:1409
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:138
ObjCInterfaceDecl * getInterface() const
Gets the interface declaration for this object type, if the base type really is an interface.
Definition: Type.h:6374
Represents one property declaration in an Objective-C interface.
Definition: DeclObjC.h:729
static ObjCPropertyDecl * findPropertyDecl(const DeclContext *DC, const IdentifierInfo *propertyID, ObjCPropertyQueryKind queryKind)
Lookup a property by name in the specified DeclContext.
Definition: DeclObjC.cpp:178
ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC property.
Definition: ExprObjC.h:614
bool isMacroDefined(StringRef Id)
StringRef getImmediateMacroName(SourceLocation Loc)
Retrieve the name of the immediate macro expansion.
A (possibly-)qualified type.
Definition: Type.h:736
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:803
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
Smart pointer class that efficiently represents Objective-C method names.
StringRef getNameForSlot(unsigned argIndex) const
Retrieve the name at a given position in the selector.
unsigned getNumArgs() const
bool shouldDelayDiagnostics()
Determines whether diagnostics should be delayed.
Definition: Sema.h:976
void add(const sema::DelayedDiagnostic &diag)
Adds a delayed diagnostic.
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:356
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: Sema.cpp:1897
std::unique_ptr< NSAPI > NSAPIObj
Caches identifiers/selectors for NSFoundation APIs.
Definition: Sema.h:1195
SmallVector< sema::FunctionScopeInfo *, 4 > FunctionScopes
Stack containing information about each of the nested function, block, and method scopes that are cur...
Definition: Sema.h:818
Preprocessor & getPreprocessor() const
Definition: Sema.h:1691
class clang::Sema::DelayedDiagnostics DelayedDiagnostics
ASTContext & Context
Definition: Sema.h:407
ASTContext & getASTContext() const
Definition: Sema.h:1692
void DiagnoseUnguardedAvailabilityViolations(Decl *FD)
Issue any -Wunguarded-availability warnings in FD.
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Calls Lexer::getLocForEndOfToken()
Definition: Sema.cpp:58
const LangOptions & getLangOpts() const
Definition: Sema.h:1685
DeclContext * getCurLexicalContext() const
Definition: Sema.h:14033
SourceManager & getSourceManager() const
Definition: Sema.h:1690
sema::FunctionScopeInfo * getCurFunctionAvailabilityContext()
Retrieve the current function, if any, that should be analyzed for potential availability violations.
void DiagnoseAvailabilityOfDecl(NamedDecl *D, ArrayRef< SourceLocation > Locs, const ObjCInterfaceDecl *UnknownObjCClass, bool ObjCPropertyAccess, bool AvoidPartialAvailabilityChecks=false, ObjCInterfaceDecl *ClassReceiver=nullptr)
void handleDelayedAvailabilityCheck(sema::DelayedDiagnostic &DD, Decl *Ctx)
Encodes a location in the source.
This class handles loading and caching of source files into memory.
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition: Stmt.h:72
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:337
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3477
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1204
StringRef getPlatformName() const
Retrieve the name of the platform as it is used in the availability attribute.
Definition: TargetInfo.h:1559
VersionTuple getPlatformMinVersion() const
Retrieve the minimum desired version of the platform, to which the program should be compiled.
Definition: TargetInfo.h:1563
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:59
SourceLocation getEndLoc() const
Get the end source location.
Definition: TypeLoc.cpp:235
SourceLocation getBeginLoc() const
Get the begin source location.
Definition: TypeLoc.cpp:192
const Type * getTypePtr() const
Definition: TypeLoc.h:137
The base class of the type hierarchy.
Definition: Type.h:1597
const ObjCObjectType * getAsObjCInterfaceType() const
Definition: Type.cpp:1792
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3327
A diagnostic message which has been conditionally emitted pending the complete parsing of the current...
static DelayedDiagnostic makeAvailability(AvailabilityResult AR, ArrayRef< SourceLocation > Locs, const NamedDecl *ReferringDecl, const NamedDecl *OffendingDecl, const ObjCInterfaceDecl *UnknownObjCClass, const ObjCPropertyDecl *ObjCProperty, StringRef Msg, bool ObjCPropertyAccess)
const ObjCInterfaceDecl * getUnknownObjCClass() const
const NamedDecl * getAvailabilityOffendingDecl() const
const ObjCPropertyDecl * getObjCProperty() const
StringRef getAvailabilityMessage() const
ArrayRef< SourceLocation > getAvailabilitySelectorLocs() const
AvailabilityResult getAvailabilityResult() const
const NamedDecl * getAvailabilityReferringDecl() const
Retains information about a function, method, or block that is currently being parsed.
Definition: ScopeInfo.h:102
Defines the clang::TargetInfo interface.
LLVM_READONLY bool isValidAsciiIdentifier(StringRef S, bool AllowDollar=false)
Return true if this is a valid ASCII identifier.
Definition: CharInfo.h:228
@ C
Languages that the frontend can parse and compile.
@ Result
The result type of a method or function.
AvailabilityResult
Captures the result of checking the availability of a declaration.
Definition: DeclBase.h:69
@ AR_NotYetIntroduced
Definition: DeclBase.h:71
@ AR_Available
Definition: DeclBase.h:70
@ AR_Deprecated
Definition: DeclBase.h:72
@ AR_Unavailable
Definition: DeclBase.h:73