Skip to content

Commit f194acb

Browse files
authored
Implement system header lookup for interop import (#6128)
Using the toolchain-tasks suggested syntax of: ``` import Cpp header "<system_header.h>"; ```
1 parent e3293a4 commit f194acb

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

toolchain/check/cpp/import.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,13 @@ static auto GenerateCppIncludesHeaderCode(
103103
GenerateLineMarker(context, code_stream,
104104
context.tokens().GetLineNumber(
105105
context.parse_tree().node_token(import.node_id)));
106-
code_stream << "#include \""
107-
<< FormatEscaped(
108-
context.string_literal_values().Get(import.library_id))
109-
<< "\"\n";
106+
auto name = context.string_literal_values().Get(import.library_id);
107+
if (name.starts_with('<') && name.ends_with('>')) {
108+
code_stream << "#include <"
109+
<< FormatEscaped(name.drop_front().drop_back()) << ">\n";
110+
} else {
111+
code_stream << "#include \"" << FormatEscaped(name) << "\"\n";
112+
}
110113
}
111114
}
112115

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ impl library "[[@TEST_NAME]]";
7979
import Cpp library "header.h";
8080
//@dump-sem-ir-end
8181

82+
83+
// --- /usr/include/system_header.h
84+
85+
auto system_function() -> void;
86+
87+
// --- system_header.carbon
88+
89+
library "[[@TEST_NAME]]";
90+
91+
//@dump-sem-ir-begin
92+
import Cpp library "<system_header.h>";
93+
//@dump-sem-ir-end
94+
95+
fn F() {
96+
//@dump-sem-ir-begin
97+
Cpp.system_function();
98+
//@dump-sem-ir-end
99+
}
100+
82101
// CHECK:STDOUT: --- cpp_in_inner_namespace.carbon
83102
// CHECK:STDOUT:
84103
// CHECK:STDOUT: imports {
@@ -120,3 +139,36 @@ import Cpp library "header.h";
120139
// CHECK:STDOUT: imports {
121140
// CHECK:STDOUT: }
122141
// CHECK:STDOUT:
142+
// CHECK:STDOUT: --- system_header.carbon
143+
// CHECK:STDOUT:
144+
// CHECK:STDOUT: constants {
145+
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
146+
// CHECK:STDOUT: %.98d: type = cpp_overload_set_type @system_function [concrete]
147+
// CHECK:STDOUT: %empty_struct: %.98d = struct_value () [concrete]
148+
// CHECK:STDOUT: %system_function.type: type = fn_type @system_function [concrete]
149+
// CHECK:STDOUT: %system_function: %system_function.type = struct_value () [concrete]
150+
// CHECK:STDOUT: }
151+
// CHECK:STDOUT:
152+
// CHECK:STDOUT: imports {
153+
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
154+
// CHECK:STDOUT: .system_function = %.769
155+
// CHECK:STDOUT: import Cpp//...
156+
// CHECK:STDOUT: }
157+
// CHECK:STDOUT: %.769: %.98d = cpp_overload_set_value @system_function [concrete = constants.%empty_struct]
158+
// CHECK:STDOUT: %system_function.decl: %system_function.type = fn_decl @system_function [concrete = constants.%system_function] {} {}
159+
// CHECK:STDOUT: }
160+
// CHECK:STDOUT:
161+
// CHECK:STDOUT: file {
162+
// CHECK:STDOUT: %Cpp.import_cpp = import_cpp {
163+
// CHECK:STDOUT: import Cpp "<system_header.h>"
164+
// CHECK:STDOUT: }
165+
// CHECK:STDOUT: }
166+
// CHECK:STDOUT:
167+
// CHECK:STDOUT: fn @F() {
168+
// CHECK:STDOUT: !entry:
169+
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
170+
// CHECK:STDOUT: %system_function.ref: %.98d = name_ref system_function, imports.%.769 [concrete = constants.%empty_struct]
171+
// CHECK:STDOUT: %system_function.call: init %empty_tuple.type = call imports.%system_function.decl()
172+
// CHECK:STDOUT: <elided>
173+
// CHECK:STDOUT: }
174+
// CHECK:STDOUT:

0 commit comments

Comments
 (0)