clang_code_generator_callbacks.carbon 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/full.carbon
  6. // EXTRA-ARGS: --target=x86_64-linux-gnu
  7. //
  8. // AUTOUPDATE
  9. // TIP: To test this file alone, run:
  10. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/interop/cpp/clang_code_generator_callbacks.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/interop/cpp/clang_code_generator_callbacks.carbon
  13. library "[[@TEST_NAME]]";
  14. import Cpp inline '''
  15. // Clang uses an implementation of clang::ASTConsumer to implement code
  16. // generation, it has a variety of callbacks that may be called throughout the
  17. // process of parsing/semantically analyzing the source code. Below are test
  18. // cases based on those callbacks to ensure that C++ code compiled as part of
  19. // Carbon interop imports preserve Clang's behavior.
  20. struct t1 {
  21. virtual void f1();
  22. };
  23. // HandleVTable - Emit a definition for `t1`'s vtable.
  24. void t1::f1() { }
  25. // CompleteExternalDeclaration - Only be related to the BPF target.
  26. // CompleteTentativeDefinition - Only relevant to C code.
  27. // AssignInheritanceModel - Only relevant to ms inheritance attribute
  28. // HandleTagDeclRequiredDefinition - Only relevant to ms debug info (only
  29. // fails clang/test/DebugInfo/CXX/dllimport-base-class.cpp)
  30. // HandleTagDeclDefinition - updating previous IR when a definition for a type
  31. // is provided.
  32. struct S;
  33. extern S a[10];
  34. S(*b)[10] = &a;
  35. struct S {
  36. int x;
  37. };
  38. int f() { return a[3].x; }
  39. // without the callback, clang describes the type of 'a' as 10xi8, resulting in
  40. // this:
  41. // load i32, ptr getelementptr inbounds ([10 x i8], ptr @a, i64 0, i64 3), align 4
  42. // with the callback, the type gets updated and the result is:
  43. // load i32, ptr getelementptr inbounds ([10 x %struct.S], ptr @a, i64 0, i64 3), align 4
  44. // HandleInlineFunctionDefinition+EmitDeferredDecls - Emit a definition of
  45. // `func`.
  46. struct t2 {
  47. __attribute__((used)) void func() {}
  48. };
  49. // HandleCXXStaticMemberVarInstantiation - Emit a definition for
  50. // `t3<int>::i`.
  51. template<typename T>
  52. struct t3 {
  53. static int i;
  54. };
  55. template<typename T>
  56. int t3<T>::i;
  57. void f2() {
  58. t3<int>::i = 42;
  59. }
  60. ''';
  61. // CHECK:STDOUT: ; ModuleID = 'clang_code_generator_callbacks.carbon'
  62. // CHECK:STDOUT: source_filename = "clang_code_generator_callbacks.carbon"
  63. // 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"
  64. // CHECK:STDOUT: target triple = "x86_64-unknown-linux-gnu"
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: %struct.S = type { i32 }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: $_ZN2t24funcEv = comdat any
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: $_ZN2t3IiE1iE = comdat any
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: @llvm.compiler.used = appending global [1 x ptr] [ptr @_ZN2t24funcEv], section "llvm.metadata"
  73. // CHECK:STDOUT: @a = external global [10 x i8], align 1
  74. // CHECK:STDOUT: @b = dso_local global ptr @a, align 8
  75. // CHECK:STDOUT: @_ZN2t3IiE1iE = linkonce_odr dso_local global i32 0, comdat, align 4
  76. // CHECK:STDOUT: @_ZTV2t1 = dso_local unnamed_addr constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTI2t1, ptr @_ZN2t12f1Ev] }, align 8
  77. // CHECK:STDOUT: @_ZTI2t1 = dso_local constant { ptr, ptr } { ptr getelementptr inbounds (ptr, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 2), ptr @_ZTS2t1 }, align 8
  78. // CHECK:STDOUT: @_ZTVN10__cxxabiv117__class_type_infoE = external global [0 x ptr]
  79. // CHECK:STDOUT: @_ZTS2t1 = dso_local constant [4 x i8] c"2t1\00", align 1
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable
  82. // CHECK:STDOUT: define dso_local void @_ZN2t12f1Ev(ptr noundef nonnull align 8 dereferenceable(8) %this) unnamed_addr #0 align 2 {
  83. // CHECK:STDOUT: entry:
  84. // CHECK:STDOUT: %this.addr = alloca ptr, align 8
  85. // CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !12
  86. // CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8
  87. // CHECK:STDOUT: ret void
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable
  91. // CHECK:STDOUT: define linkonce_odr dso_local void @_ZN2t24funcEv(ptr noundef nonnull align 1 dereferenceable(1) %this) #0 comdat align 2 {
  92. // CHECK:STDOUT: entry:
  93. // CHECK:STDOUT: %this.addr = alloca ptr, align 8
  94. // CHECK:STDOUT: store ptr %this, ptr %this.addr, align 8, !tbaa !15
  95. // CHECK:STDOUT: %this1 = load ptr, ptr %this.addr, align 8
  96. // CHECK:STDOUT: ret void
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable
  100. // CHECK:STDOUT: define dso_local noundef i32 @_Z1fv() #0 {
  101. // CHECK:STDOUT: entry:
  102. // CHECK:STDOUT: %0 = load i32, ptr getelementptr inbounds ([10 x %struct.S], ptr @a, i64 0, i64 3), align 4, !tbaa !17
  103. // CHECK:STDOUT: ret i32 %0
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: ; Function Attrs: mustprogress nounwind uwtable
  107. // CHECK:STDOUT: define dso_local void @_Z2f2v() #0 {
  108. // CHECK:STDOUT: entry:
  109. // CHECK:STDOUT: store i32 42, ptr @_ZN2t3IiE1iE, align 4, !tbaa !8
  110. // CHECK:STDOUT: ret void
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: attributes #0 = { mustprogress nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1, !2, !3, !4, !5}
  116. // CHECK:STDOUT: !llvm.dbg.cu = !{!6}
  117. // CHECK:STDOUT: !llvm.errno.tbaa = !{!8}
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  120. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  121. // CHECK:STDOUT: !2 = !{i32 1, !"wchar_size", i32 4}
  122. // CHECK:STDOUT: !3 = !{i32 8, !"PIC Level", i32 0}
  123. // CHECK:STDOUT: !4 = !{i32 7, !"PIE Level", i32 2}
  124. // CHECK:STDOUT: !5 = !{i32 7, !"uwtable", i32 2}
  125. // CHECK:STDOUT: !6 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !7, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  126. // CHECK:STDOUT: !7 = !DIFile(filename: "clang_code_generator_callbacks.carbon", directory: "")
  127. // CHECK:STDOUT: !8 = !{!9, !9, i64 0}
  128. // CHECK:STDOUT: !9 = !{!"int", !10, i64 0}
  129. // CHECK:STDOUT: !10 = !{!"omnipotent char", !11, i64 0}
  130. // CHECK:STDOUT: !11 = !{!"Simple C++ TBAA"}
  131. // CHECK:STDOUT: !12 = !{!13, !13, i64 0}
  132. // CHECK:STDOUT: !13 = !{!"p1 _ZTS2t1", !14, i64 0}
  133. // CHECK:STDOUT: !14 = !{!"any pointer", !10, i64 0}
  134. // CHECK:STDOUT: !15 = !{!16, !16, i64 0}
  135. // CHECK:STDOUT: !16 = !{!"p1 _ZTS2t2", !14, i64 0}
  136. // CHECK:STDOUT: !17 = !{!18, !9, i64 0}
  137. // CHECK:STDOUT: !18 = !{!"_ZTS1S", !9, i64 0}