Skip to content

Commit 1709b61

Browse files
authored
Merge pull request #29660 from nkcsgexi/macro-guard
[PrintAsObjc] add macro guard against potential recursive definitions
2 parents 8037f2f + fa659ed commit 1709b61

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626

2727
using namespace swift;
2828

29-
static void writePrologue(raw_ostream &out, ASTContext &ctx) {
29+
static void writePrologue(raw_ostream &out, ASTContext &ctx,
30+
StringRef macroGuard) {
3031
out << "// Generated by " << version::getSwiftFullVersion(
3132
ctx.LangOpts.EffectiveLanguageVersion) << "\n"
33+
// Guard against recursive definition.
34+
<< "#ifndef " << macroGuard << "\n"
35+
<< "#define " << macroGuard << "\n"
3236
"#pragma clang diagnostic push\n"
3337
"#pragma clang diagnostic ignored \"-Wgcc-compat\"\n"
3438
"\n"
@@ -373,7 +377,9 @@ static void writeEpilogue(raw_ostream &os) {
373377
"#if __has_attribute(external_source_symbol)\n"
374378
"# pragma clang attribute pop\n"
375379
"#endif\n"
376-
"#pragma clang diagnostic pop\n";
380+
"#pragma clang diagnostic pop\n"
381+
// For the macro guard against recursive definition
382+
"#endif\n";
377383
}
378384

379385
bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
@@ -385,8 +391,8 @@ bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
385391
std::string moduleContentsBuf;
386392
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
387393
printModuleContentsAsObjC(moduleContents, imports, *M, minRequiredAccess);
388-
389-
writePrologue(os, M->getASTContext());
394+
std::string macroGuard = (llvm::Twine(M->getNameStr().upper()) + "_SWIFT_H").str();
395+
writePrologue(os, M->getASTContext(), macroGuard);
390396
writeImports(os, imports, *M, bridgingHeader);
391397
writePostImportPrologue(os, *M);
392398
os << moduleContents.str();

test/PrintAsObjC/Inputs/comments-expected-output.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,4 @@ SWIFT_CLASS("_TtC8comments13UnorderedList")
428428
# pragma clang attribute pop
429429
#endif
430430
#pragma clang diagnostic pop
431+
#endif

test/PrintAsObjC/accessibility.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
// REQUIRES: objc_interop
2323

24+
// CHECK: #ifndef ACCESSIBILITY_SWIFT_H
25+
// CHECK-NEXT: #define ACCESSIBILITY_SWIFT_H
26+
2427
// CHECK-LABEL: @interface A_Public{{$}}
2528
// CHECK-INTERNAL-NEXT: init
2629
// CHECK-NEXT: @end

0 commit comments

Comments
 (0)