Skip to content

[PrintAsObjC] Make sure the preprocessor checks work in a compiler different than clang #8918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2321,8 +2321,16 @@ class ModuleWriter {
M.getASTContext().LangOpts.EffectiveLanguageVersion) << "\n"
"#pragma clang diagnostic push\n"
"\n"
"#if defined(__has_include) && "
"__has_include(<swift/objc-prologue.h>)\n"
"#if !defined(__has_include)\n"
"# define __has_include(x) 0\n"
"#endif\n"
"#if !defined(__has_attribute)\n"
"# define __has_attribute(x) 0\n"
"#endif\n"
"#if !defined(__has_feature)\n"
"# define __has_feature(x) 0\n"
"#endif\n"
"#if __has_include(<swift/objc-prologue.h>)\n"
"# include <swift/objc-prologue.h>\n"
"#endif\n"
"\n"
Expand All @@ -2334,7 +2342,7 @@ class ModuleWriter {
"\n"
"#if !defined(SWIFT_TYPEDEFS)\n"
"# define SWIFT_TYPEDEFS 1\n"
"# if defined(__has_include) && __has_include(<uchar.h>)\n"
"# if __has_include(<uchar.h>)\n"
"# include <uchar.h>\n"
"# elif !defined(__cplusplus) || __cplusplus < 201103L\n"
"typedef uint_least16_t char16_t;\n"
Expand Down Expand Up @@ -2366,38 +2374,35 @@ class ModuleWriter {
"# endif\n"
"#endif\n"
"\n"
"#if defined(__has_attribute) && "
"__has_attribute(objc_runtime_name)\n"
"#if __has_attribute(objc_runtime_name)\n"
"# define SWIFT_RUNTIME_NAME(X) "
"__attribute__((objc_runtime_name(X)))\n"
"#else\n"
"# define SWIFT_RUNTIME_NAME(X)\n"
"#endif\n"
"#if defined(__has_attribute) && "
"__has_attribute(swift_name)\n"
"#if __has_attribute(swift_name)\n"
"# define SWIFT_COMPILE_NAME(X) "
"__attribute__((swift_name(X)))\n"
"#else\n"
"# define SWIFT_COMPILE_NAME(X)\n"
"#endif\n"
"#if defined(__has_attribute) && "
"__has_attribute(objc_method_family)\n"
"#if __has_attribute(objc_method_family)\n"
"# define SWIFT_METHOD_FAMILY(X) "
"__attribute__((objc_method_family(X)))\n"
"#else\n"
"# define SWIFT_METHOD_FAMILY(X)\n"
"#endif\n"
"#if defined(__has_attribute) && __has_attribute(noescape)\n"
"#if __has_attribute(noescape)\n"
"# define SWIFT_NOESCAPE __attribute__((noescape))\n"
"#else\n"
"# define SWIFT_NOESCAPE\n"
"#endif\n"
"#if defined(__has_attribute) && __has_attribute(warn_unused_result)\n"
"#if __has_attribute(warn_unused_result)\n"
"# define SWIFT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n"
"#else\n"
"# define SWIFT_WARN_UNUSED_RESULT\n"
"#endif\n"
"#if defined(__has_attribute) && __has_attribute(noreturn)\n"
"#if __has_attribute(noreturn)\n"
"# define SWIFT_NORETURN __attribute__((noreturn))\n"
"#else\n"
"# define SWIFT_NORETURN\n"
Expand All @@ -2412,8 +2417,7 @@ class ModuleWriter {
"# define SWIFT_ENUM_EXTRA\n"
"#endif\n"
"#if !defined(SWIFT_CLASS)\n"
"# if defined(__has_attribute) && "
"__has_attribute(objc_subclassing_restricted)\n"
"# if __has_attribute(objc_subclassing_restricted)\n"
"# define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) "
"__attribute__((objc_subclassing_restricted)) "
"SWIFT_CLASS_EXTRA\n"
Expand Down Expand Up @@ -2443,8 +2447,7 @@ class ModuleWriter {
"#endif\n"
"\n"
"#if !defined(OBJC_DESIGNATED_INITIALIZER)\n"
"# if defined(__has_attribute) && "
"__has_attribute(objc_designated_initializer)\n"
"# if __has_attribute(objc_designated_initializer)\n"
"# define OBJC_DESIGNATED_INITIALIZER "
"__attribute__((objc_designated_initializer))\n"
"# else\n"
Expand All @@ -2464,8 +2467,7 @@ class ModuleWriter {
"# define SWIFT_ENUM(_type, _name) "
"enum _name : _type _name; "
"enum SWIFT_ENUM_ATTR SWIFT_ENUM_EXTRA _name : _type\n"
"# if defined(__has_feature) && "
"__has_feature(generalized_swift_name)\n"
"# if __has_feature(generalized_swift_name)\n"
"# define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME) "
"enum _name : _type _name SWIFT_COMPILE_NAME(SWIFT_NAME); "
"enum SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_ENUM_ATTR "
Expand Down Expand Up @@ -2506,7 +2508,7 @@ class ModuleWriter {
}

void writeImports(raw_ostream &out) {
out << "#if defined(__has_feature) && __has_feature(modules)\n";
out << "#if __has_feature(modules)\n";

// Track printed names to handle overlay modules.
llvm::SmallPtrSet<Identifier, 8> seenImports;
Expand Down
6 changes: 5 additions & 1 deletion test/PrintAsObjC/empty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

// CHECK-NOT: @import Swift;

// CHECK-LABEL: #if !defined(__has_feature)
// CHECK-NEXT: # define __has_feature(x) 0
// CHECK-NEXT: #endif

// CHECK-LABEL: #include <objc/NSObject.h>
// CHECK: #include <stdint.h>
// CHECK: #include <stddef.h>
Expand All @@ -26,7 +30,7 @@
// CHECK: # define SWIFT_EXTENSION(M)
// CHECK: # define OBJC_DESIGNATED_INITIALIZER

// CHECK-LABEL: #if defined(__has_feature) && __has_feature(modules)
// CHECK-LABEL: #if __has_feature(modules)
// CHECK-NEXT: #endif


Expand Down
2 changes: 1 addition & 1 deletion test/PrintAsObjC/mixed-framework-fwd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// REQUIRES: objc_interop

// CHECK-LABEL: #if defined(__has_feature) && __has_feature(modules)
// CHECK-LABEL: #if __has_feature(modules)
// CHECK-NEXT: @import Foundation;
// CHECK-NEXT: #endif

Expand Down
2 changes: 1 addition & 1 deletion test/PrintAsObjC/mixed-framework.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// REQUIRES: objc_interop

// CHECK-LABEL: #if defined(__has_feature) && __has_feature(modules)
// CHECK-LABEL: #if __has_feature(modules)
// CHECK-NEXT: @import Foundation;
// CHECK-NEXT: #endif

Expand Down