function_decl.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. // ARGS: compile --phase=lower --dump-llvm-ir --exclude-dump-file-prefix=%{core_package_dir} --target=x86_64-unknown-linux-gnu %s
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/interop/cpp/function_decl.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/function_decl.carbon
  12. // ============================================================================
  13. // function_decl
  14. // ============================================================================
  15. // --- function_decl.h
  16. void foo();
  17. // --- import_function_decl.carbon
  18. library "[[@TEST_NAME]]";
  19. import Cpp library "function_decl.h";
  20. fn MyF() {
  21. Cpp.foo();
  22. }
  23. // ============================================================================
  24. // inline_function_decl
  25. // ============================================================================
  26. // --- inline_function_decl.h
  27. inline void foo() {}
  28. // --- import_inline_function_decl.carbon
  29. library "[[@TEST_NAME]]";
  30. import Cpp library "inline_function_decl.h";
  31. fn MyF() {
  32. Cpp.foo();
  33. }
  34. // ============================================================================
  35. // inline_used_function_decl
  36. // ============================================================================
  37. // --- inline_used_function_decl.h
  38. inline __attribute__((used)) void foo() {}
  39. // --- import_inline_used_function_decl.carbon
  40. library "[[@TEST_NAME]]";
  41. import Cpp library "inline_used_function_decl.h";
  42. fn MyF() {
  43. Cpp.foo();
  44. }
  45. // ============================================================================
  46. // multiple_inline_function_decls
  47. // ============================================================================
  48. // --- multiple_inline_function_decls.h
  49. inline void foo1() {}
  50. inline void foo2() {}
  51. inline void foo3() {}
  52. // --- import_multiple_inline_function_decls.carbon
  53. library "[[@TEST_NAME]]";
  54. import Cpp library "multiple_inline_function_decls.h";
  55. fn MyF() {
  56. Cpp.foo1();
  57. Cpp.foo2();
  58. Cpp.foo3();
  59. }
  60. // ============================================================================
  61. // inline_recursive_function_decl
  62. // ============================================================================
  63. // --- inline_recursive_function_decl.h
  64. inline void foo1() {}
  65. inline void foo2() { foo1(); }
  66. // --- todo_import_inline_recursive_function_decl.carbon
  67. library "[[@TEST_NAME]]";
  68. import Cpp library "inline_recursive_function_decl.h";
  69. fn MyF() {
  70. // TODO: This should generate the definition of the inline function `foo1()`.
  71. Cpp.foo2();
  72. }
  73. // CHECK:STDOUT: ; ModuleID = 'import_function_decl.carbon'
  74. // CHECK:STDOUT: source_filename = "import_function_decl.carbon"
  75. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  76. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !8 {
  79. // CHECK:STDOUT: entry:
  80. // CHECK:STDOUT: call void @_Z3foov(), !dbg !11
  81. // CHECK:STDOUT: ret void, !dbg !12
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: declare void @_Z3foov()
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  87. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  88. // CHECK:STDOUT: !llvm.ident = !{!7}
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  91. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  92. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  93. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  94. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  95. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  96. // CHECK:STDOUT: !6 = !DIFile(filename: "import_function_decl.carbon", directory: "")
  97. // CHECK:STDOUT: !7 = !{!"clang version 21.0.0git"}
  98. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !9, spFlags: DISPFlagDefinition, unit: !5)
  99. // CHECK:STDOUT: !9 = !DISubroutineType(types: !10)
  100. // CHECK:STDOUT: !10 = !{}
  101. // CHECK:STDOUT: !11 = !DILocation(line: 7, column: 3, scope: !8)
  102. // CHECK:STDOUT: !12 = !DILocation(line: 6, column: 1, scope: !8)
  103. // CHECK:STDOUT: ; ModuleID = 'import_inline_function_decl.carbon'
  104. // CHECK:STDOUT: source_filename = "import_inline_function_decl.carbon"
  105. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  106. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: $_Z3foov = comdat any
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !8 {
  111. // CHECK:STDOUT: entry:
  112. // CHECK:STDOUT: call void @_Z3foov(), !dbg !11
  113. // CHECK:STDOUT: ret void, !dbg !12
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  117. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z3foov() #0 comdat {
  118. // CHECK:STDOUT: entry:
  119. // CHECK:STDOUT: ret void
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: attributes #0 = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  125. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  126. // CHECK:STDOUT: !llvm.ident = !{!7}
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  129. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  130. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  131. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  132. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  133. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  134. // CHECK:STDOUT: !6 = !DIFile(filename: "import_inline_function_decl.carbon", directory: "")
  135. // CHECK:STDOUT: !7 = !{!"clang version 21.0.0git"}
  136. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !9, spFlags: DISPFlagDefinition, unit: !5)
  137. // CHECK:STDOUT: !9 = !DISubroutineType(types: !10)
  138. // CHECK:STDOUT: !10 = !{}
  139. // CHECK:STDOUT: !11 = !DILocation(line: 7, column: 3, scope: !8)
  140. // CHECK:STDOUT: !12 = !DILocation(line: 6, column: 1, scope: !8)
  141. // CHECK:STDOUT: ; ModuleID = 'import_inline_used_function_decl.carbon'
  142. // CHECK:STDOUT: source_filename = "import_inline_used_function_decl.carbon"
  143. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  144. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: $_Z3foov = comdat any
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: @llvm.compiler.used = appending global [1 x ptr] [ptr @_Z3foov], section "llvm.metadata"
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !8 {
  151. // CHECK:STDOUT: entry:
  152. // CHECK:STDOUT: call void @_Z3foov(), !dbg !11
  153. // CHECK:STDOUT: ret void, !dbg !12
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  157. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z3foov() #0 comdat {
  158. // CHECK:STDOUT: entry:
  159. // CHECK:STDOUT: ret void
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: ; uselistorder directives
  163. // CHECK:STDOUT: uselistorder ptr @_Z3foov, { 1, 0 }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: attributes #0 = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  168. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  169. // CHECK:STDOUT: !llvm.ident = !{!7}
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  172. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  173. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  174. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  175. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  176. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  177. // CHECK:STDOUT: !6 = !DIFile(filename: "import_inline_used_function_decl.carbon", directory: "")
  178. // CHECK:STDOUT: !7 = !{!"clang version 21.0.0git"}
  179. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !9, spFlags: DISPFlagDefinition, unit: !5)
  180. // CHECK:STDOUT: !9 = !DISubroutineType(types: !10)
  181. // CHECK:STDOUT: !10 = !{}
  182. // CHECK:STDOUT: !11 = !DILocation(line: 7, column: 3, scope: !8)
  183. // CHECK:STDOUT: !12 = !DILocation(line: 6, column: 1, scope: !8)
  184. // CHECK:STDOUT: ; ModuleID = 'import_multiple_inline_function_decls.carbon'
  185. // CHECK:STDOUT: source_filename = "import_multiple_inline_function_decls.carbon"
  186. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  187. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: $_Z4foo1v = comdat any
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: $_Z4foo2v = comdat any
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: $_Z4foo3v = comdat any
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !8 {
  196. // CHECK:STDOUT: entry:
  197. // CHECK:STDOUT: call void @_Z4foo1v(), !dbg !11
  198. // CHECK:STDOUT: call void @_Z4foo2v(), !dbg !12
  199. // CHECK:STDOUT: call void @_Z4foo3v(), !dbg !13
  200. // CHECK:STDOUT: ret void, !dbg !14
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  204. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z4foo1v() #0 comdat {
  205. // CHECK:STDOUT: entry:
  206. // CHECK:STDOUT: ret void
  207. // CHECK:STDOUT: }
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  210. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z4foo2v() #0 comdat {
  211. // CHECK:STDOUT: entry:
  212. // CHECK:STDOUT: ret void
  213. // CHECK:STDOUT: }
  214. // CHECK:STDOUT:
  215. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline nounwind optnone
  216. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z4foo3v() #0 comdat {
  217. // CHECK:STDOUT: entry:
  218. // CHECK:STDOUT: ret void
  219. // CHECK:STDOUT: }
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: attributes #0 = { mustprogress noinline nounwind optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  222. // CHECK:STDOUT:
  223. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  224. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  225. // CHECK:STDOUT: !llvm.ident = !{!7}
  226. // CHECK:STDOUT:
  227. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  228. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  229. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  230. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  231. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  232. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  233. // CHECK:STDOUT: !6 = !DIFile(filename: "import_multiple_inline_function_decls.carbon", directory: "")
  234. // CHECK:STDOUT: !7 = !{!"clang version 21.0.0git"}
  235. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !9, spFlags: DISPFlagDefinition, unit: !5)
  236. // CHECK:STDOUT: !9 = !DISubroutineType(types: !10)
  237. // CHECK:STDOUT: !10 = !{}
  238. // CHECK:STDOUT: !11 = !DILocation(line: 7, column: 3, scope: !8)
  239. // CHECK:STDOUT: !12 = !DILocation(line: 8, column: 3, scope: !8)
  240. // CHECK:STDOUT: !13 = !DILocation(line: 9, column: 3, scope: !8)
  241. // CHECK:STDOUT: !14 = !DILocation(line: 6, column: 1, scope: !8)
  242. // CHECK:STDOUT: ; ModuleID = 'todo_import_inline_recursive_function_decl.carbon'
  243. // CHECK:STDOUT: source_filename = "todo_import_inline_recursive_function_decl.carbon"
  244. // CHECK:STDOUT: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
  245. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: $_Z4foo2v = comdat any
  248. // CHECK:STDOUT:
  249. // CHECK:STDOUT: define void @_CMyF.Main() !dbg !8 {
  250. // CHECK:STDOUT: entry:
  251. // CHECK:STDOUT: call void @_Z4foo2v(), !dbg !11
  252. // CHECK:STDOUT: ret void, !dbg !12
  253. // CHECK:STDOUT: }
  254. // CHECK:STDOUT:
  255. // CHECK:STDOUT: ; Function Attrs: mustprogress noinline optnone
  256. // CHECK:STDOUT: define linkonce_odr dso_local void @_Z4foo2v() #0 comdat {
  257. // CHECK:STDOUT: entry:
  258. // CHECK:STDOUT: call void @_Z4foo1v()
  259. // CHECK:STDOUT: ret void
  260. // CHECK:STDOUT: }
  261. // CHECK:STDOUT:
  262. // CHECK:STDOUT: declare void @_Z4foo1v() #1
  263. // CHECK:STDOUT:
  264. // CHECK:STDOUT: attributes #0 = { mustprogress noinline optnone "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  265. // CHECK:STDOUT: attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="0" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  266. // CHECK:STDOUT:
  267. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4}
  268. // CHECK:STDOUT: !llvm.dbg.cu = !{!5}
  269. // CHECK:STDOUT: !llvm.ident = !{!7}
  270. // CHECK:STDOUT:
  271. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  272. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  273. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  274. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  275. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  276. // CHECK:STDOUT: !5 = distinct !DICompileUnit(language: DW_LANG_C, file: !6, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  277. // CHECK:STDOUT: !6 = !DIFile(filename: "todo_import_inline_recursive_function_decl.carbon", directory: "")
  278. // CHECK:STDOUT: !7 = !{!"clang version 21.0.0git"}
  279. // CHECK:STDOUT: !8 = distinct !DISubprogram(name: "MyF", linkageName: "_CMyF.Main", scope: null, file: !6, line: 6, type: !9, spFlags: DISPFlagDefinition, unit: !5)
  280. // CHECK:STDOUT: !9 = !DISubroutineType(types: !10)
  281. // CHECK:STDOUT: !10 = !{}
  282. // CHECK:STDOUT: !11 = !DILocation(line: 8, column: 3, scope: !8)
  283. // CHECK:STDOUT: !12 = !DILocation(line: 6, column: 1, scope: !8)