Skip to content

Commit 2483556

Browse files
authored
Merge pull request #8918 from akyrtzi/printasobjc-preprocessor-directives-checks
2 parents cb406c6 + 0efba5f commit 2483556

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,8 +2321,16 @@ class ModuleWriter {
23212321
M.getASTContext().LangOpts.EffectiveLanguageVersion) << "\n"
23222322
"#pragma clang diagnostic push\n"
23232323
"\n"
2324-
"#if defined(__has_include) && "
2325-
"__has_include(<swift/objc-prologue.h>)\n"
2324+
"#if !defined(__has_include)\n"
2325+
"# define __has_include(x) 0\n"
2326+
"#endif\n"
2327+
"#if !defined(__has_attribute)\n"
2328+
"# define __has_attribute(x) 0\n"
2329+
"#endif\n"
2330+
"#if !defined(__has_feature)\n"
2331+
"# define __has_feature(x) 0\n"
2332+
"#endif\n"
2333+
"#if __has_include(<swift/objc-prologue.h>)\n"
23262334
"# include <swift/objc-prologue.h>\n"
23272335
"#endif\n"
23282336
"\n"
@@ -2334,7 +2342,7 @@ class ModuleWriter {
23342342
"\n"
23352343
"#if !defined(SWIFT_TYPEDEFS)\n"
23362344
"# define SWIFT_TYPEDEFS 1\n"
2337-
"# if defined(__has_include) && __has_include(<uchar.h>)\n"
2345+
"# if __has_include(<uchar.h>)\n"
23382346
"# include <uchar.h>\n"
23392347
"# elif !defined(__cplusplus) || __cplusplus < 201103L\n"
23402348
"typedef uint_least16_t char16_t;\n"
@@ -2366,38 +2374,35 @@ class ModuleWriter {
23662374
"# endif\n"
23672375
"#endif\n"
23682376
"\n"
2369-
"#if defined(__has_attribute) && "
2370-
"__has_attribute(objc_runtime_name)\n"
2377+
"#if __has_attribute(objc_runtime_name)\n"
23712378
"# define SWIFT_RUNTIME_NAME(X) "
23722379
"__attribute__((objc_runtime_name(X)))\n"
23732380
"#else\n"
23742381
"# define SWIFT_RUNTIME_NAME(X)\n"
23752382
"#endif\n"
2376-
"#if defined(__has_attribute) && "
2377-
"__has_attribute(swift_name)\n"
2383+
"#if __has_attribute(swift_name)\n"
23782384
"# define SWIFT_COMPILE_NAME(X) "
23792385
"__attribute__((swift_name(X)))\n"
23802386
"#else\n"
23812387
"# define SWIFT_COMPILE_NAME(X)\n"
23822388
"#endif\n"
2383-
"#if defined(__has_attribute) && "
2384-
"__has_attribute(objc_method_family)\n"
2389+
"#if __has_attribute(objc_method_family)\n"
23852390
"# define SWIFT_METHOD_FAMILY(X) "
23862391
"__attribute__((objc_method_family(X)))\n"
23872392
"#else\n"
23882393
"# define SWIFT_METHOD_FAMILY(X)\n"
23892394
"#endif\n"
2390-
"#if defined(__has_attribute) && __has_attribute(noescape)\n"
2395+
"#if __has_attribute(noescape)\n"
23912396
"# define SWIFT_NOESCAPE __attribute__((noescape))\n"
23922397
"#else\n"
23932398
"# define SWIFT_NOESCAPE\n"
23942399
"#endif\n"
2395-
"#if defined(__has_attribute) && __has_attribute(warn_unused_result)\n"
2400+
"#if __has_attribute(warn_unused_result)\n"
23962401
"# define SWIFT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n"
23972402
"#else\n"
23982403
"# define SWIFT_WARN_UNUSED_RESULT\n"
23992404
"#endif\n"
2400-
"#if defined(__has_attribute) && __has_attribute(noreturn)\n"
2405+
"#if __has_attribute(noreturn)\n"
24012406
"# define SWIFT_NORETURN __attribute__((noreturn))\n"
24022407
"#else\n"
24032408
"# define SWIFT_NORETURN\n"
@@ -2412,8 +2417,7 @@ class ModuleWriter {
24122417
"# define SWIFT_ENUM_EXTRA\n"
24132418
"#endif\n"
24142419
"#if !defined(SWIFT_CLASS)\n"
2415-
"# if defined(__has_attribute) && "
2416-
"__has_attribute(objc_subclassing_restricted)\n"
2420+
"# if __has_attribute(objc_subclassing_restricted)\n"
24172421
"# define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) "
24182422
"__attribute__((objc_subclassing_restricted)) "
24192423
"SWIFT_CLASS_EXTRA\n"
@@ -2443,8 +2447,7 @@ class ModuleWriter {
24432447
"#endif\n"
24442448
"\n"
24452449
"#if !defined(OBJC_DESIGNATED_INITIALIZER)\n"
2446-
"# if defined(__has_attribute) && "
2447-
"__has_attribute(objc_designated_initializer)\n"
2450+
"# if __has_attribute(objc_designated_initializer)\n"
24482451
"# define OBJC_DESIGNATED_INITIALIZER "
24492452
"__attribute__((objc_designated_initializer))\n"
24502453
"# else\n"
@@ -2464,8 +2467,7 @@ class ModuleWriter {
24642467
"# define SWIFT_ENUM(_type, _name) "
24652468
"enum _name : _type _name; "
24662469
"enum SWIFT_ENUM_ATTR SWIFT_ENUM_EXTRA _name : _type\n"
2467-
"# if defined(__has_feature) && "
2468-
"__has_feature(generalized_swift_name)\n"
2470+
"# if __has_feature(generalized_swift_name)\n"
24692471
"# define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME) "
24702472
"enum _name : _type _name SWIFT_COMPILE_NAME(SWIFT_NAME); "
24712473
"enum SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_ENUM_ATTR "
@@ -2506,7 +2508,7 @@ class ModuleWriter {
25062508
}
25072509

25082510
void writeImports(raw_ostream &out) {
2509-
out << "#if defined(__has_feature) && __has_feature(modules)\n";
2511+
out << "#if __has_feature(modules)\n";
25102512

25112513
// Track printed names to handle overlay modules.
25122514
llvm::SmallPtrSet<Identifier, 8> seenImports;

test/PrintAsObjC/empty.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
// CHECK-NOT: @import Swift;
1515

16+
// CHECK-LABEL: #if !defined(__has_feature)
17+
// CHECK-NEXT: # define __has_feature(x) 0
18+
// CHECK-NEXT: #endif
19+
1620
// CHECK-LABEL: #include <objc/NSObject.h>
1721
// CHECK: #include <stdint.h>
1822
// CHECK: #include <stddef.h>
@@ -26,7 +30,7 @@
2630
// CHECK: # define SWIFT_EXTENSION(M)
2731
// CHECK: # define OBJC_DESIGNATED_INITIALIZER
2832

29-
// CHECK-LABEL: #if defined(__has_feature) && __has_feature(modules)
33+
// CHECK-LABEL: #if __has_feature(modules)
3034
// CHECK-NEXT: #endif
3135

3236

test/PrintAsObjC/mixed-framework-fwd.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
// REQUIRES: objc_interop
1919

20-
// CHECK-LABEL: #if defined(__has_feature) && __has_feature(modules)
20+
// CHECK-LABEL: #if __has_feature(modules)
2121
// CHECK-NEXT: @import Foundation;
2222
// CHECK-NEXT: #endif
2323

test/PrintAsObjC/mixed-framework.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// REQUIRES: objc_interop
1111

12-
// CHECK-LABEL: #if defined(__has_feature) && __has_feature(modules)
12+
// CHECK-LABEL: #if __has_feature(modules)
1313
// CHECK-NEXT: @import Foundation;
1414
// CHECK-NEXT: #endif
1515

0 commit comments

Comments
 (0)