Skip to content

Commit 2cb0df4

Browse files
authored
Add C++ interop inline function tests (#5406)
This shows that `inline` is ignored and the function definition is not generated. Demo: ```c++ // hello_world.h inline void hello_world() {} ``` ```carbon // main.carbon library "Main"; import Cpp library "hello_world.h"; fn Run() -> i32 { Cpp.hello_world(); return 0; } ``` ```shell $ bazel-bin/toolchain/carbon compile main.carbon $ bazel-bin/toolchain/carbon link main.o --output=demo ld.lld: error: undefined symbol: hello_world() >>> referenced by main.carbon:8 >>> main.o:(main) error: linker command failed with exit code 1 (use -v to see invocation) ``` Part of #5405.
1 parent f8443ae commit 2cb0df4

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
//
5+
// AUTOUPDATE
6+
// TIP: To test this file alone, run:
7+
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/no_prelude/function_decl_inline.carbon
8+
// TIP: To dump output, run:
9+
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/no_prelude/function_decl_inline.carbon
10+
11+
// ============================================================================
12+
// inline_function_decl_with_definition
13+
// ============================================================================
14+
15+
// --- inline_function_decl_with_definition.h
16+
17+
inline void foo() {}
18+
19+
// --- import_inline_function_decl_with_definition.carbon
20+
21+
library "[[@TEST_NAME]]";
22+
23+
import Cpp library "inline_function_decl_with_definition.h";
24+
25+
fn MyF() {
26+
Cpp.foo();
27+
}
28+
29+
// ============================================================================
30+
// inline_function_decl_without_definition
31+
// ============================================================================
32+
33+
// --- inline_function_decl_without_definition.h
34+
35+
inline void foo();
36+
37+
// --- todo_fail_import_inline_function_decl_without_definition.carbon
38+
39+
library "[[@TEST_NAME]]";
40+
41+
import Cpp library "inline_function_decl_without_definition.h";
42+
43+
fn MyF() {
44+
// TODO: Error on using an inline function without definition.
45+
Cpp.foo();
46+
47+
// Don't error on repeated calls.
48+
Cpp.foo();
49+
}
50+
51+
// CHECK:STDOUT: --- import_inline_function_decl_with_definition.carbon
52+
// CHECK:STDOUT:
53+
// CHECK:STDOUT: constants {
54+
// CHECK:STDOUT: %MyF.type: type = fn_type @MyF [concrete]
55+
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
56+
// CHECK:STDOUT: %MyF: %MyF.type = struct_value () [concrete]
57+
// CHECK:STDOUT: %foo.type: type = fn_type @foo [concrete]
58+
// CHECK:STDOUT: %foo: %foo.type = struct_value () [concrete]
59+
// CHECK:STDOUT: }
60+
// CHECK:STDOUT:
61+
// CHECK:STDOUT: imports {
62+
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
63+
// CHECK:STDOUT: .foo = @MyF.%foo.decl
64+
// CHECK:STDOUT: import Cpp//...
65+
// CHECK:STDOUT: }
66+
// CHECK:STDOUT: }
67+
// CHECK:STDOUT:
68+
// CHECK:STDOUT: file {
69+
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
70+
// CHECK:STDOUT: .Cpp = imports.%Cpp
71+
// CHECK:STDOUT: .MyF = %MyF.decl
72+
// CHECK:STDOUT: }
73+
// CHECK:STDOUT: %Cpp.import_cpp = import_cpp {
74+
// CHECK:STDOUT: import Cpp "inline_function_decl_with_definition.h"
75+
// CHECK:STDOUT: }
76+
// CHECK:STDOUT: %MyF.decl: %MyF.type = fn_decl @MyF [concrete = constants.%MyF] {} {}
77+
// CHECK:STDOUT: }
78+
// CHECK:STDOUT:
79+
// CHECK:STDOUT: fn @MyF() {
80+
// CHECK:STDOUT: !entry:
81+
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
82+
// CHECK:STDOUT: %foo.decl: %foo.type = fn_decl @foo [concrete = constants.%foo] {} {}
83+
// CHECK:STDOUT: %foo.ref: %foo.type = name_ref foo, %foo.decl [concrete = constants.%foo]
84+
// CHECK:STDOUT: %foo.call: init %empty_tuple.type = call %foo.ref()
85+
// CHECK:STDOUT: return
86+
// CHECK:STDOUT: }
87+
// CHECK:STDOUT:
88+
// CHECK:STDOUT: fn @foo();
89+
// CHECK:STDOUT:
90+
// CHECK:STDOUT: --- todo_fail_import_inline_function_decl_without_definition.carbon
91+
// CHECK:STDOUT:
92+
// CHECK:STDOUT: constants {
93+
// CHECK:STDOUT: %MyF.type: type = fn_type @MyF [concrete]
94+
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
95+
// CHECK:STDOUT: %MyF: %MyF.type = struct_value () [concrete]
96+
// CHECK:STDOUT: %foo.type: type = fn_type @foo [concrete]
97+
// CHECK:STDOUT: %foo: %foo.type = struct_value () [concrete]
98+
// CHECK:STDOUT: }
99+
// CHECK:STDOUT:
100+
// CHECK:STDOUT: imports {
101+
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
102+
// CHECK:STDOUT: .foo = @MyF.%foo.decl
103+
// CHECK:STDOUT: import Cpp//...
104+
// CHECK:STDOUT: }
105+
// CHECK:STDOUT: }
106+
// CHECK:STDOUT:
107+
// CHECK:STDOUT: file {
108+
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
109+
// CHECK:STDOUT: .Cpp = imports.%Cpp
110+
// CHECK:STDOUT: .MyF = %MyF.decl
111+
// CHECK:STDOUT: }
112+
// CHECK:STDOUT: %Cpp.import_cpp = import_cpp {
113+
// CHECK:STDOUT: import Cpp "inline_function_decl_without_definition.h"
114+
// CHECK:STDOUT: }
115+
// CHECK:STDOUT: %MyF.decl: %MyF.type = fn_decl @MyF [concrete = constants.%MyF] {} {}
116+
// CHECK:STDOUT: }
117+
// CHECK:STDOUT:
118+
// CHECK:STDOUT: fn @MyF() {
119+
// CHECK:STDOUT: !entry:
120+
// CHECK:STDOUT: %Cpp.ref.loc8: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
121+
// CHECK:STDOUT: %foo.decl: %foo.type = fn_decl @foo [concrete = constants.%foo] {} {}
122+
// CHECK:STDOUT: %foo.ref.loc8: %foo.type = name_ref foo, %foo.decl [concrete = constants.%foo]
123+
// CHECK:STDOUT: %foo.call.loc8: init %empty_tuple.type = call %foo.ref.loc8()
124+
// CHECK:STDOUT: %Cpp.ref.loc11: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
125+
// CHECK:STDOUT: %foo.ref.loc11: %foo.type = name_ref foo, %foo.decl [concrete = constants.%foo]
126+
// CHECK:STDOUT: %foo.call.loc11: init %empty_tuple.type = call %foo.ref.loc11()
127+
// CHECK:STDOUT: return
128+
// CHECK:STDOUT: }
129+
// CHECK:STDOUT:
130+
// CHECK:STDOUT: fn @foo();
131+
// CHECK:STDOUT:

toolchain/lower/testdata/interop/cpp/function_decl.carbon

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
// TIP: To dump output, run:
99
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/function_decl.carbon
1010

11+
// ============================================================================
12+
// function_decl
13+
// ============================================================================
14+
1115
// --- function_decl.h
1216

1317
void foo();
@@ -22,6 +26,25 @@ fn MyF() {
2226
Cpp.foo();
2327
}
2428

29+
// ============================================================================
30+
// inline_function_decl
31+
// ============================================================================
32+
33+
// --- inline_function_decl.h
34+
35+
inline void foo() {}
36+
37+
// --- todo_import_inline_function_decl.carbon
38+
39+
library "[[@TEST_NAME]]";
40+
41+
import Cpp library "inline_function_decl.h";
42+
43+
fn MyF() {
44+
// TODO: This should generate the definition of the inline function `foo()`.
45+
Cpp.foo();
46+
}
47+
2548
// CHECK:STDOUT: ; ModuleID = 'import_function_decl.carbon'
2649
// CHECK:STDOUT: source_filename = "import_function_decl.carbon"
2750
// CHECK:STDOUT:
@@ -45,3 +68,26 @@ fn MyF() {
4568
// CHECK:STDOUT: !6 = !{}
4669
// CHECK:STDOUT: !7 = !DILocation(line: 7, column: 3, scope: !4)
4770
// CHECK:STDOUT: !8 = !DILocation(line: 6, column: 1, scope: !4)
71+
// CHECK:STDOUT: ; ModuleID = 'todo_import_inline_function_decl.carbon'
72+
// CHECK:STDOUT: source_filename = "todo_import_inline_function_decl.carbon"
73+
// CHECK:STDOUT:
74+
// CHECK:STDOUT: define void @_CMyF.Main() !dbg !4 {
75+
// CHECK:STDOUT: entry:
76+
// CHECK:STDOUT: call void @_Z3foov(), !dbg !7
77+
// CHECK:STDOUT: ret void, !dbg !8
78+
// CHECK:STDOUT: }
79+
// CHECK:STDOUT:
80+
// CHECK:STDOUT: declare void @_Z3foov()
81+
// CHECK:STDOUT:
82+
// CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
83+
// CHECK:STDOUT: !llvm.dbg.cu = !{!2}
84+
// CHECK:STDOUT:
85+
// CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
86+
// CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
87+
// CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
88+
// CHECK:STDOUT: !3 = !DIFile(filename: "todo_import_inline_function_decl.carbon", directory: "")
89+
// CHECK:STDOUT: !4 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !3, line: 6, type: !5, spFlags: DISPFlagDefinition, unit: !2)
90+
// CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
91+
// CHECK:STDOUT: !6 = !{}
92+
// CHECK:STDOUT: !7 = !DILocation(line: 8, column: 3, scope: !4)
93+
// CHECK:STDOUT: !8 = !DILocation(line: 6, column: 1, scope: !4)

0 commit comments

Comments
 (0)