Skip to content

Commit b5d86fd

Browse files
authored
Properly dump SemIR for inline C++ imports (#6001)
Dumping SemIR crashed on inline C++ imports and this outputs `import Cpp inline` instead. Followup of #5904.
1 parent 8adb357 commit b5d86fd

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

toolchain/check/testdata/interop/cpp/inline.carbon

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
library "[[@TEST_NAME]]";
1616

17+
//@dump-sem-ir-begin
1718
import Cpp inline '''
1819
1920
// A C++ function.
2021
inline void func() {}
2122
2223
''';
24+
//@dump-sem-ir-end
2325

2426
fn Run() {
2527
//@dump-sem-ir-begin
@@ -31,12 +33,14 @@ fn Run() {
3133

3234
library "[[@TEST_NAME]]";
3335

36+
//@dump-sem-ir-begin
3437
import Cpp inline '''c++
3538
3639
// A C++ function.
3740
inline void another_func() {}
3841
3942
''';
43+
//@dump-sem-ir-end
4044

4145
fn Run() {
4246
//@dump-sem-ir-begin
@@ -60,6 +64,12 @@ fn Run() {
6064
// CHECK:STDOUT: %func.decl: %func.type = fn_decl @func [concrete = constants.%func] {} {}
6165
// CHECK:STDOUT: }
6266
// CHECK:STDOUT:
67+
// CHECK:STDOUT: file {
68+
// CHECK:STDOUT: %Cpp.import_cpp = import_cpp {
69+
// CHECK:STDOUT: import Cpp inline
70+
// CHECK:STDOUT: }
71+
// CHECK:STDOUT: }
72+
// CHECK:STDOUT:
6373
// CHECK:STDOUT: fn @Run() {
6474
// CHECK:STDOUT: !entry:
6575
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
@@ -84,6 +94,12 @@ fn Run() {
8494
// CHECK:STDOUT: %another_func.decl: %another_func.type = fn_decl @another_func [concrete = constants.%another_func] {} {}
8595
// CHECK:STDOUT: }
8696
// CHECK:STDOUT:
97+
// CHECK:STDOUT: file {
98+
// CHECK:STDOUT: %Cpp.import_cpp = import_cpp {
99+
// CHECK:STDOUT: import Cpp inline
100+
// CHECK:STDOUT: }
101+
// CHECK:STDOUT: }
102+
// CHECK:STDOUT:
87103
// CHECK:STDOUT: fn @Run() {
88104
// CHECK:STDOUT: !entry:
89105
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]

toolchain/sem_ir/formatter.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,10 +1210,16 @@ auto Formatter::FormatImportCppDeclRhs() -> void {
12101210
OpenBrace();
12111211
for (ImportCpp import_cpp : sem_ir_->import_cpps().values()) {
12121212
Indent();
1213-
out_ << "import Cpp \""
1214-
<< FormatEscaped(
1215-
sem_ir_->string_literal_values().Get(import_cpp.library_id))
1216-
<< "\"\n";
1213+
out_ << "import Cpp ";
1214+
if (import_cpp.library_id.has_value()) {
1215+
out_ << "\""
1216+
<< FormatEscaped(
1217+
sem_ir_->string_literal_values().Get(import_cpp.library_id))
1218+
<< "\"";
1219+
} else {
1220+
out_ << "inline";
1221+
}
1222+
out_ << "\n";
12171223
}
12181224
CloseBrace();
12191225
}

0 commit comments

Comments
 (0)