static-init.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
  2. // CHECK: @_ZZ1hvE1i = internal global i32 0, align 4
  3. // CHECK: @base_req = global [4 x i8] c"foo\00", align 1
  4. // CHECK: @_ZZN5test1L6getvarEiE3var = internal constant [4 x i32] [i32 1, i32 0, i32 2, i32 4], align 16
  5. // CHECK: @_ZZ2h2vE1i = linkonce_odr global i32 0
  6. // CHECK: @_ZGVZ2h2vE1i = linkonce_odr global i64 0
  7. struct A {
  8. A();
  9. ~A();
  10. };
  11. void f() {
  12. // CHECK: load atomic i8* bitcast (i64* @_ZGVZ1fvE1a to i8*) acquire, align 1
  13. // CHECK: call i32 @__cxa_guard_acquire
  14. // CHECK: call void @_ZN1AC1Ev
  15. // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1AD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @_ZZ1fvE1a, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*))
  16. // CHECK: call void @__cxa_guard_release
  17. static A a;
  18. }
  19. void g() {
  20. // CHECK: call noalias i8* @_Znwm(i64 1)
  21. // CHECK: call void @_ZN1AC1Ev(
  22. static A& a = *new A;
  23. }
  24. int a();
  25. void h() {
  26. static const int i = a();
  27. }
  28. inline void h2() {
  29. static int i = a();
  30. }
  31. void h3() {
  32. h2();
  33. }
  34. // PR6980: this shouldn't crash
  35. namespace test0 {
  36. struct A { A(); };
  37. __attribute__((noreturn)) int throw_exception();
  38. void test() {
  39. throw_exception();
  40. static A r;
  41. }
  42. }
  43. namespace test1 {
  44. // CHECK: define internal i32 @_ZN5test1L6getvarEi(
  45. static inline int getvar(int index) {
  46. static const int var[] = { 1, 0, 2, 4 };
  47. return var[index];
  48. }
  49. void test() { (void) getvar(2); }
  50. }
  51. // Make sure we emit the initializer correctly for the following:
  52. char base_req[] = { "foo" };
  53. namespace union_static_local {
  54. // CHECK: define internal void @_ZZN18union_static_local4testEvEN1c4mainEv
  55. // CHECK: call void @_ZN18union_static_local1fEPNS_1xE(%"union.union_static_local::x"* bitcast ({ [2 x i8*] }* @_ZZN18union_static_local4testEvE3foo to %"union.union_static_local::x"*))
  56. union x { long double y; const char *x[2]; };
  57. void f(union x*);
  58. void test() {
  59. static union x foo = { .x = { "a", "b" } };
  60. struct c {
  61. static void main() {
  62. f(&foo);
  63. }
  64. };
  65. c::main();
  66. }
  67. }