Skip to content

Commit 2862a0a

Browse files
move PrintAsClang macro definitions into a separate file
1 parent 1b3256d commit 2862a0a

File tree

2 files changed

+266
-198
lines changed

2 files changed

+266
-198
lines changed

lib/PrintAsClang/ClangMacros.def

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
//===--- ClangMacros.def - Macros emitted by PrintAsClang ----=--*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the database of macros emitted into compatibility headers
14+
// generated by PrintAsClang.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
// CLANG_MACRO(NAME, ARGS, VALUE)
19+
// A simple name/value macro that does not depend on a condition.
20+
#ifndef CLANG_MACRO
21+
#define CLANG_MACRO(NAME, ARGS, VALUE)
22+
#endif
23+
24+
// CLANG_MACRO_BODY(NAME, BODY)
25+
// A macro conditionalized on the name, but that defines a helper macro or
26+
// other kind of additional definitions other than the macro itself.
27+
#ifndef CLANG_MACRO_BODY
28+
#define CLANG_MACRO_BODY(NAME, BODY)
29+
#endif
30+
31+
// CLANG_MACRO_DEFINED(NAME)
32+
// Used to signal that a macro is defined by another CLANG_MACRO_BODY call.
33+
// This is included so that these macros can be included in the generated
34+
// `compatibility_symbols` file.
35+
#ifndef CLANG_MACRO_DEFINED
36+
#define CLANG_MACRO_DEFINED(NAME)
37+
#endif
38+
39+
// CLANG_MACRO_ALTERNATIVE(NAME, ARGS, CONDITION, VALUE, ALTERNATIVE)
40+
// A simple name/value macro that evaluates to the given value when the given
41+
// condition is true, or to the alternative value when it is false.
42+
#ifndef CLANG_MACRO_ALTERNATIVE
43+
#define CLANG_MACRO_ALTERNATIVE(NAME, ARGS, CONDITION, VALUE, ALTERNATIVE)
44+
#endif
45+
46+
// CLANG_MACRO_CONDITIONAL(NAME, ARGS, CONDITION, VALUE)
47+
// A simple name/value macro that only evaluates to the given value when the
48+
// given condition is true. Otherwise the macro will evaluate to nothing.
49+
#ifndef CLANG_MACRO_CONDITIONAL
50+
#define CLANG_MACRO_CONDITIONAL(NAME, ARGS, CONDITION, VALUE) \
51+
CLANG_MACRO_ALTERNATIVE(NAME, ARGS, CONDITION, VALUE, )
52+
#endif
53+
54+
// CLANG_MACRO_OBJC(NAME, ARGS, VALUE)
55+
// A simple name/value macro that is only defined when being compiled as
56+
// Objective-C.
57+
#ifndef CLANG_MACRO_OBJC
58+
#define CLANG_MACRO_OBJC(NAME, ARGS, VALUE)
59+
#endif
60+
61+
// CLANG_MACRO_CXX(NAME, ARGS, VALUE, ALTERNATIVE)
62+
// A simple name/value macro that evaluates to the given value when being
63+
// compiled as C++, or as the alternative when not.
64+
#ifndef CLANG_MACRO_CXX
65+
#define CLANG_MACRO_CXX(NAME, ARGS, VALUE, ALTERNATIVE)
66+
#endif
67+
68+
// CLANG_MACRO_CXX_BODY(NAME, BODY)
69+
// A complex macro conditionalized on whether the source is being compiled as
70+
// C++.
71+
#ifndef CLANG_MACRO_CXX_BODY
72+
#define CLANG_MACRO_CXX_BODY(NAME, BODY)
73+
#endif
74+
75+
CLANG_MACRO_BODY(SWIFT_PASTE, \
76+
"# define SWIFT_PASTE_HELPER(x, y) x##y\n" \
77+
"# define SWIFT_PASTE(x, y) SWIFT_PASTE_HELPER(x, y)")
78+
CLANG_MACRO_DEFINED(SWIFT_PASTE_HELPER)
79+
80+
CLANG_MACRO(SWIFT_METATYPE, (X), Class)
81+
82+
CLANG_MACRO_CONDITIONAL(SWIFT_CLASS_PROPERTY, \
83+
(...), \
84+
__has_feature(objc_class_property), \
85+
__VA_ARGS__)
86+
87+
CLANG_MACRO_CONDITIONAL(SWIFT_RUNTIME_NAME, \
88+
(X), \
89+
__has_attribute(objc_runtime_name), \
90+
__attribute__((objc_runtime_name(X))))
91+
92+
CLANG_MACRO_CONDITIONAL(SWIFT_COMPILE_NAME, \
93+
(X), \
94+
__has_attribute(swift_name), \
95+
__attribute__((swift_name(X))))
96+
97+
CLANG_MACRO_CONDITIONAL(SWIFT_METHOD_FAMILY, \
98+
(X), \
99+
__has_attribute(objc_method_family),
100+
__attribute__((objc_method_family(X))))
101+
102+
CLANG_MACRO_CONDITIONAL(SWIFT_NOESCAPE, , \
103+
__has_attribute(noescape), \
104+
__attribute__((noescape)))
105+
106+
CLANG_MACRO_CONDITIONAL(SWIFT_RELEASES_ARGUMENT, , \
107+
__has_attribute(ns_consumed), \
108+
__attribute__((ns_consumed)))
109+
110+
CLANG_MACRO_CONDITIONAL(SWIFT_WARN_UNUSED_RESULT, , \
111+
__has_attribute(warn_unused_result), \
112+
__attribute__((warn_unused_result)))
113+
114+
// While this would ordinarily work with CLANG_MACRO_CONDITIONAL, this name is
115+
// defined in the compiler source itself, and is expanded by the time the macro
116+
// is printed into the resulting header
117+
CLANG_MACRO_BODY(SWIFT_NORETURN, \
118+
"# if __has_attribute(noreturn)\n" \
119+
"# define SWIFT_NORETURN __attribute((noreturn))\n" \
120+
"# else\n" \
121+
"# define SWIFT_NORETURN\n" \
122+
"# endif")
123+
124+
CLANG_MACRO(SWIFT_CLASS_EXTRA, , )
125+
CLANG_MACRO(SWIFT_PROTOCOL_EXTRA, , )
126+
CLANG_MACRO(SWIFT_ENUM_EXTRA, , )
127+
128+
CLANG_MACRO_BODY(SWIFT_CLASS, \
129+
"# if __has_attribute(objc_subclassing_restricted)\n" \
130+
"# define SWIFT_CLASS(SWIFT_NAME) " \
131+
"SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) " \
132+
"SWIFT_CLASS_EXTRA\n" \
133+
"# define SWIFT_CLASS_NAMED(SWIFT_NAME) " \
134+
"__attribute((objc_subclassing_restricted)) SWIFT_COMPILE_NAME(SWIFT_NAME) " \
135+
"SWIFT_CLASS_EXTRA\n" \
136+
"# else\n" \
137+
"# define SWIFT_CLASS(SWIFT_NAME) " \
138+
"SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA\n" \
139+
"# define SWIFT_CLASS_NAMED(SWIFT_NAME) " \
140+
"SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA\n" \
141+
"# endif")
142+
CLANG_MACRO_DEFINED(SWIFT_CLASS_NAMED)
143+
144+
CLANG_MACRO_BODY(SWIFT_RESILIENT_CLASS, \
145+
"# if __has_attribute(objc_class_stub)\n" \
146+
"# define SWIFT_RESILIENT_CLASS(SWIFT_NAME) " \
147+
"SWIFT_CLASS(SWIFT_NAME) __attribute__((objc_class_stub))\n" \
148+
"# define SWIFT_RESILIENT_CLASS_NAMED(SWIFT_NAME) " \
149+
"__attribute__((objc_class_stub)) SWIFT_CLASS_NAMED(SWIFT_NAME)\n" \
150+
"# else\n" \
151+
"# define SWIFT_RESILIENT_CLASS(SWIFT_NAME) SWIFT_CLASS(SWIFT_NAME)\n" \
152+
"# define SWIFT_RESILIENT_CLASS_NAMED(SWIFT_NAME) SWIFT_CLASS_NAMED(SWIFT_NAME)\n" \
153+
"# endif")
154+
CLANG_MACRO_DEFINED(SWIFT_RESILIENT_CLASS_NAMED)
155+
156+
CLANG_MACRO_BODY(SWIFT_PROTOCOL, \
157+
"# define SWIFT_PROTOCOL(SWIFT_NAME) " \
158+
"SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA\n" \
159+
"# define SWIFT_PROTOCOL_NAMED(SWIFT_NAME) " \
160+
"SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA")
161+
CLANG_MACRO_DEFINED(SWIFT_PROTOCOL_NAMED)
162+
163+
CLANG_MACRO(SWIFT_EXTENSION, (M), SWIFT_PASTE(M##_Swift_, __LINE__))
164+
165+
CLANG_MACRO_CONDITIONAL(OBJC_DESIGNATED_INITIALIZER, , \
166+
__has_attribute(objc_designated_initializer), \
167+
__attribute__((objc_designated_initializer)))
168+
169+
CLANG_MACRO_CONDITIONAL(SWIFT_ENUM_ATTR, (_extensibility), \
170+
__has_attribute(enum_extensibility), \
171+
__attribute__((enum_extensibility(_extensibility))))
172+
173+
CLANG_MACRO_BODY(SWIFT_ENUM, \
174+
"# define SWIFT_ENUM(_type, _name, _extensibility) " \
175+
"enum _name : _type _name; " \
176+
"enum SWIFT_ENUM_ATTR(_extensibility) SWIFT_ENUM_EXTRA _name: _type\n" \
177+
"# if __has_feature(generalized_swift_name)\n" \
178+
"# define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME, _extensibility) " \
179+
"enum _name : _type _name SWIFT_COMPILE_NAME(SWIFT_NAME); " \
180+
"enum SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_ENUM_ATTR(_extensibility) " \
181+
"SWIFT_ENUM_EXTRA _name : _type\n" \
182+
"# else\n" \
183+
"# define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME, _extensibility) " \
184+
"SWIFT_ENUM(_type, _name, _extensibility)\n" \
185+
"# endif")
186+
187+
CLANG_MACRO(SWIFT_UNAVAILABLE, , __attribute__((unavailable)))
188+
CLANG_MACRO(SWIFT_UNAVAILABLE_MSG, (msg), __attribute__((unavailable(msg))))
189+
190+
CLANG_MACRO(SWIFT_AVAILABILITY, (plat, ...), __attribute__((availability(plat, __VA_ARGS__))))
191+
192+
// SWIFT_WEAK_IMPORT is also defined within the compiler source, so use CLANG_MACRO_BODY to avoid
193+
// being expanded before being emitted into the header
194+
CLANG_MACRO_BODY(SWIFT_WEAK_IMPORT, "# define SWIFT_WEAK_IMPORT __attribute__((weak_import))")
195+
196+
CLANG_MACRO(SWIFT_DEPRECATED, , __attribute__((deprecated)))
197+
CLANG_MACRO(SWIFT_DEPRECATED_MSG, (...), __attribute__((deprecated(__VA_ARGS__))))
198+
199+
CLANG_MACRO_ALTERNATIVE(SWIFT_DEPRECATED_OBJC, (Msg), \
200+
__has_feature(attribute_diagnost_if_objc), \
201+
__attribute__((diagnose_if(1, Msg, "warning"))), \
202+
SWIFT_DEPRECATED_MSG(Msg))
203+
204+
CLANG_MACRO_OBJC(IBSegueAction, , )
205+
206+
CLANG_MACRO_BODY(SWIFT_EXTERN, \
207+
"# if defined(__cplusplus)\n" \
208+
"# define SWIFT_EXTERN extern \"C\"\n" \
209+
"# else\n" \
210+
"# define SWIFT_EXTERN extern\n"
211+
"# endif")
212+
213+
CLANG_MACRO(SWIFT_CALL, , __attribute__((swiftcall)))
214+
215+
CLANG_MACRO_CXX(SWIFT_NOEXCEPT, , noexcept, )
216+
217+
CLANG_MACRO_CXX_BODY(SWIFT_CXX_INT_DEFINED, \
218+
"#if !defined(SWIFT_CXX_INT_DEFINED)\n" \
219+
"#define SWIFT_CXX_INT_DEFINED\n" \
220+
"namespace swift {\n" \
221+
"using Int = ptrdiff_t;\n" \
222+
"using UInt = size_t;\n" \
223+
"}\n" \
224+
"#endif")
225+
226+
#undef CLANG_MACRO
227+
#undef CLANG_MACRO_BODY
228+
#undef CLANG_MACRO_DEFINED
229+
#undef CLANG_MACRO_ALTERNATIVE
230+
#undef CLANG_MACRO_CONDITIONAL
231+
#undef CLANG_MACRO_OBJC
232+
#undef CLANG_MACRO_CXX
233+
#undef CLANG_MACRO_CXX_BODY

0 commit comments

Comments
 (0)