1//===- Availability.cpp --------------------------------------------------===//
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 Availability information for Decls.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/Availability.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Attr.h"
16#include "clang/AST/Decl.h"
17#include "clang/Basic/TargetInfo.h"
18
19namespace {
20
21/// Represents the availability of a symbol across platforms.
22struct AvailabilitySet {
23 bool UnconditionallyDeprecated = false;
24 bool UnconditionallyUnavailable = false;
25
26 void insert(clang::AvailabilityInfo &&Availability) {
27 auto *Found = getForPlatform(Domain: Availability.Domain);
28 if (Found)
29 Found->mergeWith(Other: std::move(Availability));
30 else
31 Availabilities.emplace_back(Args: std::move(Availability));
32 }
33
34 clang::AvailabilityInfo *getForPlatform(llvm::StringRef Domain) {
35 auto *It = llvm::find_if(Range&: Availabilities,
36 P: [Domain](const clang::AvailabilityInfo &Info) {
37 return Domain.compare(RHS: Info.Domain) == 0;
38 });
39 return It == Availabilities.end() ? nullptr : It;
40 }
41
42private:
43 llvm::SmallVector<clang::AvailabilityInfo> Availabilities;
44};
45
46static void createInfoForDecl(const clang::Decl *Decl,
47 AvailabilitySet &Availabilities) {
48 // Collect availability attributes from all redeclarations.
49 for (const auto *RD : Decl->redecls()) {
50 for (const auto *A : RD->specific_attrs<clang::AvailabilityAttr>()) {
51 const auto *Eff = A->getEffectiveAttr();
52 Availabilities.insert(Availability: clang::AvailabilityInfo(
53 Eff->getPlatform()->getName(), Eff->getIntroduced(),
54 Eff->getDeprecated(), Eff->getObsoleted(), Eff->getUnavailable(),
55 false, false));
56 }
57
58 if (const auto *A = RD->getAttr<clang::UnavailableAttr>())
59 if (!A->isImplicit())
60 Availabilities.UnconditionallyUnavailable = true;
61
62 if (const auto *A = RD->getAttr<clang::DeprecatedAttr>())
63 if (!A->isImplicit())
64 Availabilities.UnconditionallyDeprecated = true;
65 }
66}
67
68} // namespace
69
70namespace clang {
71
72void AvailabilityInfo::mergeWith(AvailabilityInfo Other) {
73 if (isDefault() && Other.isDefault())
74 return;
75
76 if (Domain.empty())
77 Domain = Other.Domain;
78
79 UnconditionallyUnavailable |= Other.UnconditionallyUnavailable;
80 UnconditionallyDeprecated |= Other.UnconditionallyDeprecated;
81 Unavailable |= Other.Unavailable;
82
83 Introduced = std::max(a: Introduced, b: Other.Introduced);
84
85 // Default VersionTuple is 0.0.0 so if both are non default let's pick the
86 // smallest version number, otherwise select the one that is non-zero if there
87 // is one.
88 if (!Deprecated.empty() && !Other.Deprecated.empty())
89 Deprecated = std::min(a: Deprecated, b: Other.Deprecated);
90 else
91 Deprecated = std::max(a: Deprecated, b: Other.Deprecated);
92
93 if (!Obsoleted.empty() && !Other.Obsoleted.empty())
94 Obsoleted = std::min(a: Obsoleted, b: Other.Obsoleted);
95 else
96 Obsoleted = std::max(a: Obsoleted, b: Other.Obsoleted);
97}
98
99AvailabilityInfo AvailabilityInfo::createFromDecl(const Decl *D) {
100 AvailabilitySet Availabilities;
101 // Walk DeclContexts upwards starting from D to find the combined availability
102 // of the symbol.
103 for (const auto *Ctx = D; Ctx;
104 Ctx = llvm::cast_or_null<Decl>(Val: Ctx->getDeclContext()))
105 createInfoForDecl(Decl: Ctx, Availabilities);
106
107 if (auto *Avail = Availabilities.getForPlatform(
108 Domain: D->getASTContext().getTargetInfo().getPlatformName())) {
109 Avail->UnconditionallyDeprecated = Availabilities.UnconditionallyDeprecated;
110 Avail->UnconditionallyUnavailable =
111 Availabilities.UnconditionallyUnavailable;
112 return std::move(*Avail);
113 }
114
115 AvailabilityInfo Avail;
116 Avail.UnconditionallyDeprecated = Availabilities.UnconditionallyDeprecated;
117 Avail.UnconditionallyUnavailable = Availabilities.UnconditionallyUnavailable;
118 return Avail;
119}
120
121} // namespace clang
122