Skip to content

Commit ec192f6

Browse files
authored
Merge pull request #65753 from hyp/5.9/f4f6f7a408d0c521dead9912f1fa9572612ee2c6
[5.9][interop][SwiftToCxx] avoid importing C++ stdlib in C++ section of ge…
2 parents d7183fa + fff11bf commit ec192f6

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,19 @@ static void writePostImportPrologue(raw_ostream &os, ModuleDecl &M) {
542542
"#endif\n\n";
543543
}
544544

545+
static void writeObjCEpilogue(raw_ostream &os) {
546+
// Pop out of `external_source_symbol` attribute
547+
// before emitting the C++ section as the C++ section
548+
// might include other files in it.
549+
os << "#if __has_attribute(external_source_symbol)\n"
550+
"# pragma clang attribute pop\n"
551+
"#endif\n";
552+
}
553+
545554
static void writeEpilogue(raw_ostream &os) {
546-
os <<
547-
"#if __has_attribute(external_source_symbol)\n"
548-
"# pragma clang attribute pop\n"
549-
"#endif\n"
550-
"#pragma clang diagnostic pop\n"
551-
// For the macro guard against recursive definition
552-
"#endif\n";
555+
os << "#pragma clang diagnostic pop\n"
556+
// For the macro guard against recursive definition
557+
"#endif\n";
553558
}
554559

555560
static std::string computeMacroGuard(const ModuleDecl *M) {
@@ -577,6 +582,7 @@ bool swift::printAsClangHeader(raw_ostream &os, ModuleDecl *M,
577582
});
578583
writePostImportPrologue(os, *M);
579584
emitObjCConditional(os, [&] { os << objcModuleContents.str(); });
585+
writeObjCEpilogue(os);
580586
emitCxxConditional(os, [&] {
581587
// FIXME: Expose Swift with @expose by default.
582588
bool enableCxx = frontendOpts.ClangHeaderExposedDecls.has_value() ||
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
// RUN: %target-swift-frontend -typecheck %t/print-string.swift -typecheck -module-name Stringer -enable-experimental-cxx-interop -emit-clang-header-path %t/Stringer.h
4+
5+
// Ensure: we don't hit any spurios warnings instantiating
6+
// C++ standard library templates because of the generated header.
7+
8+
// RUN: %target-interop-build-clangxx -std=gnu++20 -fsyntax-only -c %t/test-stdlib.cpp -I %t -Wall -Werror -Werror=ignored-attributes -Wno-error=unused-command-line-argument
9+
10+
//--- print-string.swift
11+
12+
public func printString(_ s: String) {
13+
}
14+
15+
//--- test-stdlib.cpp
16+
17+
#include "Stringer.h"
18+
#include <iostream>
19+
20+
int main() {
21+
// Ensure that template instantiations inside C++
22+
// standard library in this call don't cause any new diagnostics
23+
// because of the generated header.
24+
std::cout << "test invoke std::cout\n" << std::endl;
25+
26+
auto _ = swift::String::init();
27+
return 0;
28+
}

test/PrintAsCxx/empty.swift

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

9393
// CHECK-LABEL: #if defined(__OBJC__)
9494
// CHECK-NEXT: #endif
95+
// CHECK-NEXT: #if __has_attribute(external_source_symbol)
96+
// CHECK-NEXT: # pragma clang attribute pop
97+
// CHECK-NEXT: #endif
9598
// CHECK-NEXT: #if defined(__cplusplus)
9699
// CHECK-NEXT: #pragma clang diagnostic push
97100
// CHECK-NEXT: #pragma clang diagnostic ignored "-Wnon-modular-include-in-framework-module"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,11 @@ SWIFT_CLASS("_TtC8comments13UnorderedList")
442442
- (void)f0;
443443
@end
444444

445-
#endif
446-
#if defined(__cplusplus)
447445
#endif
448446
#if __has_attribute(external_source_symbol)
449447
# pragma clang attribute pop
450448
#endif
449+
#if defined(__cplusplus)
450+
#endif
451451
#pragma clang diagnostic pop
452452
#endif

0 commit comments

Comments
 (0)