Skip to content

Commit 682eef2

Browse files
committed
Expose macros for availabilty domain definitions.
Also, replace feature-availability.h with availability_domain.h to better align the header/module name with the name of the compiler feature. Resolves rdar://157689733.
1 parent 6aa67af commit 682eef2

File tree

12 files changed

+85
-43
lines changed

12 files changed

+85
-43
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,8 @@ ASTContext::getFeatureAvailInfo(Decl *D) const {
927927
auto *Attr = VD->getAttr<AvailabilityDomainAttr>();
928928
if (!Attr)
929929
return {};
930+
if (!VD->getInit())
931+
return {};
930932
StringRef Name = Attr->getName()->getName();
931933
auto *Init = cast<InitListExpr>(VD->getInit());
932934
Expr::EvalResult Result;

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(core_files
3737
unwind.h
3838
varargs.h
3939
feature-availability.h
40+
availability_domain.h
4041
)
4142

4243
set(arm_common_files
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*===---- availability_domain.h - Availability Domain -----------------------===
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+
10+
#ifndef __AVAILABILITY_DOMAIN_H
11+
#define __AVAILABILITY_DOMAIN_H
12+
13+
#include <stdint.h>
14+
15+
#define __AVAILABILITY_DOMAIN_ENABLED 0
16+
#define __AVAILABILITY_DOMAIN_DISABLED 1
17+
#define __AVAILABILITY_DOMAIN_DYNAMIC 2
18+
19+
/// Describes the fields of a Clang availability domain. This struct is an
20+
/// implementation detail of the compiler and is subject to change so don't
21+
/// reference `__AvailabilityDomain` directly. Instead, use the provided macros:
22+
///
23+
/// CLANG_DYNAMIC_AVAILABILITY_DOMAIN(MyDomain, query);
24+
///
25+
struct __AvailabilityDomain {
26+
/// The state of the domain (AVAILABLE, UNAVAILABLE, DYNAMIC, etc.).
27+
intptr_t state;
28+
/// An optional function pointer to call to query the availability of a domain
29+
/// at runtime. This should only be non-null for domains in the DYNAMIC state.
30+
int (*const runtimeQuery)(void);
31+
};
32+
33+
#define CLANG_DYNAMIC_AVAILABILITY_DOMAIN(domain, query) \
34+
static struct __AvailabilityDomain domain __attribute__(( \
35+
availability_domain(domain))) = {__AVAILABILITY_DOMAIN_DYNAMIC, query}
36+
37+
#define CLANG_ENABLED_AVAILABILITY_DOMAIN(domain) \
38+
static struct __AvailabilityDomain domain __attribute__(( \
39+
availability_domain(domain))) = {__AVAILABILITY_DOMAIN_ENABLED, 0}
40+
41+
#define CLANG_DISABLED_AVAILABILITY_DOMAIN(domain) \
42+
static struct __AvailabilityDomain domain __attribute__(( \
43+
availability_domain(domain))) = {__AVAILABILITY_DOMAIN_DISABLED, 0}
44+
45+
#define CLANG_ALWAYS_ENABLED_AVAILABILITY_DOMAIN(domain) \
46+
static struct __AvailabilityDomain domain __attribute__(( \
47+
availability_domain(domain))) = {__AVAILABILITY_DOMAIN_ENABLED, 0}
48+
49+
#define CLANG_ALWAYS_DISABLED_AVAILABILITY_DOMAIN(domain) \
50+
static struct __AvailabilityDomain domain __attribute__(( \
51+
availability_domain(domain))) = {__AVAILABILITY_DOMAIN_DISABLED, 0}
52+
53+
#endif /* __AVAILABILITY_DOMAIN_H */

clang/lib/Headers/feature-availability.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,8 @@
1010
#ifndef __FEATURE_AVAILABILITY_H
1111
#define __FEATURE_AVAILABILITY_H
1212

13-
#include <stdint.h>
13+
// feature-availability.h is deprecated - use availability_domain.h instead.
1414

15-
/// The possible availability domain states. These values are hardcoded in the
16-
/// compiler and reproduced here for convenience when defining domains.
17-
18-
#define __AVAILABILITY_DOMAIN_ENABLED 0
19-
#define __AVAILABILITY_DOMAIN_DISABLED 1
20-
#define __AVAILABILITY_DOMAIN_DYNAMIC 2
21-
22-
/// A struct describing availability domain definitions. This struct definition
23-
/// is just a convenience to ensure that a header defining an availability
24-
/// domain can define it with the arguments that Clang expects at parse time.
25-
struct __AvailabilityDomain {
26-
/// The state of the domain (AVAILABLE, UNAVAILABLE, DYNAMIC, etc.).
27-
intptr_t state;
28-
/// An optional function pointer to call to query the availability of a domain
29-
/// at runtime. This should only be non-null for domains in the DYNAMIC state.
30-
int (*const runtimeQuery)(void);
31-
};
15+
#include <availability_domain.h>
3216

3317
#endif /* __FEATURE_AVAILABILITY_H */

clang/lib/Headers/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ module feature_availability {
342342
export *
343343
}
344344

345+
module availability_domain {
346+
header "availability_domain.h"
347+
export *
348+
}
349+
345350
/* TO_UPSTREAM(BoundsSafety) ON */
346351
module ptrcheck {
347352
header "ptrcheck.h"

clang/test/CodeGen/feature-availability.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
#ifndef HEADER
1616
#define HEADER
1717

18-
#include <feature-availability.h>
18+
#include <availability_domain.h>
1919

2020
#define AVAIL 0
2121

2222
#ifdef USE_DOMAIN
2323
// DOMAIN: @g3 = extern_weak global i32, align 4
2424

25-
static struct __AvailabilityDomain feature1 __attribute__((availability_domain(feature1))) = {__AVAILABILITY_DOMAIN_ENABLED, 0};
26-
static struct __AvailabilityDomain feature2 __attribute__((availability_domain(feature2))) = {__AVAILABILITY_DOMAIN_DISABLED, 0};
25+
CLANG_ENABLED_AVAILABILITY_DOMAIN(feature1);
26+
CLANG_DISABLED_AVAILABILITY_DOMAIN(feature2);
2727
#endif
2828

2929
__attribute__((availability(domain:feature1, AVAIL))) int func0(void);
@@ -94,7 +94,7 @@ void test3(struct S0 *s0) {
9494
// DOMAIN-NEXT: ret void
9595

9696
int pred1(void);
97-
static struct __AvailabilityDomain feature3 __attribute__((availability_domain(feature3))) = {__AVAILABILITY_DOMAIN_DYNAMIC, pred1};
97+
CLANG_DYNAMIC_AVAILABILITY_DOMAIN(feature3, pred1);
9898
__attribute__((availability(domain:feature3, AVAIL))) int func3(void);
9999
__attribute__((availability(domain:feature3, AVAIL))) extern int g3;
100100

clang/test/CodeGenObjC/feature-availability.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// RUN: %clang_cc1 -triple arm64-apple-macosx -fblocks -ffeature-availability=feature1:on -ffeature-availability=feature2:off -ffeature-availability=feature3:on -emit-llvm -o - %s | FileCheck %s
22
// RUN: %clang_cc1 -triple arm64-apple-macosx -fblocks -emit-llvm -o - -DUSE_DOMAIN %s | FileCheck %s
33

4-
#include <feature-availability.h>
4+
#include <availability_domain.h>
55

66
#define AVAIL 0
77

88
#ifdef USE_DOMAIN
9-
int pred1(void);
10-
11-
static struct __AvailabilityDomain feature1 __attribute__((availability_domain(feature1))) = {__AVAILABILITY_DOMAIN_ENABLED, 0};
12-
static struct __AvailabilityDomain feature2 __attribute__((availability_domain(feature2))) = {__AVAILABILITY_DOMAIN_DISABLED, 0};
13-
static struct __AvailabilityDomain feature3 __attribute__((availability_domain(feature3))) = {__AVAILABILITY_DOMAIN_ENABLED, 0};
9+
CLANG_ENABLED_AVAILABILITY_DOMAIN(feature1);
10+
CLANG_DISABLED_AVAILABILITY_DOMAIN(feature2);
11+
CLANG_ENABLED_AVAILABILITY_DOMAIN(feature3);
1412
#endif
1513

1614
// CHECK: @"OBJC_IVAR_$_C0._prop0" = hidden global i32 4, section "__DATA, __objc_ivar", align 4
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef FEATURE1_H
22
#define FEATURE1_H
3-
#include <feature-availability.h>
3+
#include <availability_domain.h>
44

5-
static struct __AvailabilityDomain feature1 __attribute__((availability_domain(feature1))) = {__AVAILABILITY_DOMAIN_ENABLED, 0};
5+
CLANG_ENABLED_AVAILABILITY_DOMAIN(feature1);
66

77
#endif
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef FEATURE2_H
22
#define FEATURE2_H
3-
#include <feature-availability.h>
3+
#include <availability_domain.h>
44
#include "feature1.h"
55

6-
static struct __AvailabilityDomain feature2 __attribute__((availability_domain(feature2))) = {__AVAILABILITY_DOMAIN_DISABLED, 0};
6+
CLANG_DISABLED_AVAILABILITY_DOMAIN(feature2);
77

88
#endif

clang/test/Modules/feature-availability.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
// RUN: %clang_cc1 -triple arm64-apple-macosx -fmodules -fmodule-file=%t/feature2.pcm -I %S/Inputs/feature-availability -emit-llvm -o - %s | FileCheck %s
44
// RUN: %clang_cc1 -triple arm64-apple-macosx -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs/feature-availability -Werror=non-modular-include-in-module -emit-llvm -o - %s | FileCheck %s
55

6-
#include <feature-availability.h>
6+
#include <availability_domain.h>
77
#include "feature2.h"
88

99
#define AVAIL 0
1010
#define UNAVAIL 1
1111

1212
int pred1(void);
13-
static struct __AvailabilityDomain feature3 __attribute__((availability_domain(feature3))) = {__AVAILABILITY_DOMAIN_DYNAMIC, pred1};
13+
CLANG_DYNAMIC_AVAILABILITY_DOMAIN(feature3, pred1);
1414

1515
void func0(void);
1616
__attribute__((availability(domain:feature1, AVAIL))) void func1(void);

0 commit comments

Comments
 (0)