Skip to content

Commit 23e56e3

Browse files
committed
[cxx-interop] add OBJC and cplusplus conditions for emitted header includes
This will allow us to generate a unified clang header.
1 parent 6b8108b commit 23e56e3

File tree

2 files changed

+53
-27
lines changed

2 files changed

+53
-27
lines changed

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "swift/PrintAsClang/PrintAsClang.h"
1414

1515
#include "ModuleContentsWriter.h"
16-
#include "OutputLanguageMode.h"
1716

1817
#include "swift/AST/ASTContext.h"
1918
#include "swift/AST/Module.h"
@@ -28,7 +27,28 @@
2827
using namespace swift;
2928

3029
static void writePrologue(raw_ostream &out, ASTContext &ctx,
31-
StringRef macroGuard, OutputLanguageMode Lang) {
30+
StringRef macroGuard) {
31+
auto emitCxxConditional = [&](llvm::function_ref<void()> cxxCase,
32+
llvm::function_ref<void()> cCase = {}) {
33+
out << "#if defined(__cplusplus)\n";
34+
cxxCase();
35+
if (cCase) {
36+
out << "#else\n";
37+
cCase();
38+
}
39+
out << "#endif\n";
40+
};
41+
auto emitObjCConditional = [&](llvm::function_ref<void()> objcCase,
42+
llvm::function_ref<void()> nonObjCCase = {}) {
43+
out << "#if defined(__OBJC__)\n";
44+
objcCase();
45+
if (nonObjCCase) {
46+
out << "#else\n";
47+
nonObjCCase();
48+
}
49+
out << "#endif\n";
50+
};
51+
3252
out << "// Generated by "
3353
<< version::getSwiftFullVersion(ctx.LangOpts.EffectiveLanguageVersion)
3454
<< "\n"
@@ -57,16 +77,18 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
5777
"#endif\n"
5878
"\n"
5979
"#pragma clang diagnostic ignored \"-Wauto-import\"\n";
60-
if (Lang == OutputLanguageMode::Cxx) {
61-
out << "#include <cstdint>\n"
62-
"#include <cstddef>\n"
63-
"#include <cstdbool>\n";
64-
} else {
65-
out << "#include <Foundation/Foundation.h>\n"
66-
"#include <stdint.h>\n"
67-
"#include <stddef.h>\n"
68-
"#include <stdbool.h>\n";
69-
}
80+
emitObjCConditional([&] { out << "#include <Foundation/Foundation.h>\n"; });
81+
emitCxxConditional(
82+
[&] {
83+
out << "#include <cstdint>\n"
84+
"#include <cstddef>\n"
85+
"#include <cstdbool>\n";
86+
},
87+
[&] {
88+
out << "#include <stdint.h>\n"
89+
"#include <stddef.h>\n"
90+
"#include <stdbool.h>\n";
91+
});
7092
out << "\n"
7193
"#if !defined(SWIFT_TYPEDEFS)\n"
7294
"# define SWIFT_TYPEDEFS 1\n"
@@ -254,30 +276,27 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
254276
"#else\n"
255277
"# define SWIFT_DEPRECATED_OBJC(Msg) SWIFT_DEPRECATED_MSG(Msg)\n"
256278
"#endif\n";
257-
if (Lang == OutputLanguageMode::ObjC) {
279+
emitObjCConditional([&] {
258280
out << "#if !defined(IBSegueAction)\n"
259281
"# define IBSegueAction\n"
260282
"#endif\n";
261-
}
283+
});
262284
out << "#if !defined(SWIFT_EXTERN)\n"
263285
"# if defined(__cplusplus)\n"
264286
"# define SWIFT_EXTERN extern \"C\"\n"
265287
"# else\n"
266288
"# define SWIFT_EXTERN extern\n"
267289
"# endif\n"
268290
"#endif\n";
269-
auto emitMacro = [&](StringRef name, StringRef value) {
291+
auto emitMacro = [&](StringRef name, StringRef value = "") {
270292
out << "#if !defined(" << name << ")\n";
271293
out << "# define " << name << " " << value << "\n";
272294
out << "#endif\n";
273295
};
274296
emitMacro("SWIFT_CALL", "__attribute__((swiftcall))");
275297
// SWIFT_NOEXCEPT applies 'noexcept' in C++ mode only.
276-
out << "#if defined(__cplusplus)\n";
277-
emitMacro("SWIFT_NOEXCEPT", "noexcept");
278-
out << "#else\n";
279-
emitMacro("SWIFT_NOEXCEPT", "");
280-
out << "#endif\n";
298+
emitCxxConditional([&] { emitMacro("SWIFT_NOEXCEPT", "noexcept"); },
299+
[&] { emitMacro("SWIFT_NOEXCEPT"); });
281300
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
282301
"need to add SIMD typedefs here if max elements is increased");
283302
}
@@ -431,8 +450,7 @@ bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
431450
std::string moduleContentsBuf;
432451
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
433452
printModuleContentsAsObjC(moduleContents, imports, *M);
434-
writePrologue(os, M->getASTContext(), computeMacroGuard(M),
435-
OutputLanguageMode::ObjC);
453+
writePrologue(os, M->getASTContext(), computeMacroGuard(M));
436454
writeImports(os, imports, *M, bridgingHeader);
437455
writePostImportPrologue(os, *M);
438456
os << moduleContents.str();
@@ -448,8 +466,7 @@ bool swift::printAsCXX(raw_ostream &os, ModuleDecl *M) {
448466
std::string moduleContentsBuf;
449467
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
450468
printModuleContentsAsCxx(moduleContents, imports, *M);
451-
writePrologue(os, M->getASTContext(), computeMacroGuard(M),
452-
OutputLanguageMode::Cxx);
469+
writePrologue(os, M->getASTContext(), computeMacroGuard(M));
453470
writePostImportPrologue(os, *M);
454471
os << moduleContents.str();
455472
writeEpilogue(os);

test/PrintAsCxx/empty.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,18 @@
2727
// CHECK-NEXT: # define __has_warning(x) 0
2828
// CHECK-NEXT: #endif
2929

30-
// CHECK-LABEL: #include <cstdint>
31-
// CHECK: #include <cstddef>
32-
// CHECK: #include <cstdbool>
30+
// CHECK-LABEL: #if defined(__OBJC__)
31+
// CHECK-NEXT: #include <Foundation/Foundation.h>
32+
// CHECK-NEXT: #endif
33+
// CHECK-NEXT: #if defined(__cplusplus)
34+
// CHECK-NEXT: #include <cstdint>
35+
// CHECK-NEXT: #include <cstddef>
36+
// CHECK-NEXT: #include <cstdbool>
37+
// CHECK-NEXT: #else
38+
// CHECK-NEXT: #include <stdint.h>
39+
// CHECK-NEXT: #include <stddef.h>
40+
// CHECK-NEXT: #include <stdbool.h>
41+
// CHECK-NEXT: #endif
3342

3443
// CHECK-LABEL: !defined(SWIFT_TYPEDEFS)
3544
// CHECK-NEXT: # define SWIFT_TYPEDEFS 1

0 commit comments

Comments
 (0)