26
26
27
27
using namespace swift ;
28
28
29
- static void writePrologue (raw_ostream &out, ASTContext &ctx) {
29
+ static void writePrologue (raw_ostream &out, ASTContext &ctx,
30
+ StringRef macroGuard) {
30
31
out << " // Generated by " << version::getSwiftFullVersion (
31
32
ctx.LangOpts .EffectiveLanguageVersion ) << " \n "
33
+ // Guard against recursive definition.
34
+ << " #ifndef " << macroGuard << " \n "
35
+ << " #define " << macroGuard << " \n "
32
36
" #pragma clang diagnostic push\n "
33
37
" #pragma clang diagnostic ignored \" -Wgcc-compat\"\n "
34
38
" \n "
@@ -373,7 +377,9 @@ static void writeEpilogue(raw_ostream &os) {
373
377
" #if __has_attribute(external_source_symbol)\n "
374
378
" # pragma clang attribute pop\n "
375
379
" #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 " ;
377
383
}
378
384
379
385
bool swift::printAsObjC (raw_ostream &os, ModuleDecl *M,
@@ -385,8 +391,8 @@ bool swift::printAsObjC(raw_ostream &os, ModuleDecl *M,
385
391
std::string moduleContentsBuf;
386
392
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
387
393
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 );
390
396
writeImports (os, imports, *M, bridgingHeader);
391
397
writePostImportPrologue (os, *M);
392
398
os << moduleContents.str ();
0 commit comments