const-init.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // RUN: not %clang_cc1 -verify -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck %s
  2. // CHECK: @a = global i32 10
  3. int a = 10;
  4. // CHECK: @ar = constant i32* @a
  5. int &ar = a;
  6. void f();
  7. // CHECK: @fr = constant void ()* @_Z1fv
  8. void (&fr)() = f;
  9. struct S { int& a; };
  10. // CHECK: @s = global %struct.S { i32* @a }
  11. S s = { a };
  12. // PR5581
  13. namespace PR5581 {
  14. class C {
  15. public:
  16. enum { e0, e1 };
  17. unsigned f;
  18. };
  19. // CHECK: @_ZN6PR55812g0E = global %"class.PR5581::C" { i32 1 }
  20. C g0 = { C::e1 };
  21. }
  22. namespace test2 {
  23. struct A {
  24. static const double d = 1.0;
  25. static const float f = d / 2;
  26. static int g();
  27. } a;
  28. // CHECK: @_ZN5test22t0E = global double {{1\.0+e\+0+}}, align 8
  29. // CHECK: @_ZN5test22t1E = global [2 x double] [double {{1\.0+e\+0+}}, double {{5\.0+e-0*}}1], align 16
  30. // CHECK: @_ZN5test22t2E = global double* @_ZN5test21A1d
  31. // CHECK: @_ZN5test22t3E = global {{.*}} @_ZN5test21A1g
  32. double t0 = A::d;
  33. double t1[] = { A::d, A::f };
  34. const double *t2 = &a.d;
  35. int (*t3)() = &a.g;
  36. }
  37. // We don't expect to fold this in the frontend, but make sure it doesn't crash.
  38. // CHECK: @PR9558 = global float 0.000000e+0
  39. float PR9558 = reinterpret_cast<const float&>("asd");
  40. // An initialized const automatic variable cannot be promoted to a constant
  41. // global if it has a mutable member.
  42. struct MutableMember {
  43. mutable int n;
  44. };
  45. int writeToMutable() {
  46. // CHECK-NOT: {{.*}}MM{{.*}} = {{.*}}constant
  47. const MutableMember MM = { 0 };
  48. return ++MM.n;
  49. }
  50. // Make sure we don't try to fold this in the frontend; the backend can't
  51. // handle it.
  52. // CHECK: @PR11705 = global i128 0
  53. __int128_t PR11705 = (__int128_t)&PR11705;
  54. // Make sure we don't try to fold this either.
  55. // CHECK: @_ZZ23UnfoldableAddrLabelDiffvE1x = internal global i128 0
  56. void UnfoldableAddrLabelDiff() { static __int128_t x = (long)&&a-(long)&&b; a:b:return;}
  57. // But make sure we do fold this.
  58. // CHECK: @_ZZ21FoldableAddrLabelDiffvE1x = internal global i64 sub (i64 ptrtoint (i8* blockaddress(@_Z21FoldableAddrLabelDiffv
  59. void FoldableAddrLabelDiff() { static long x = (long)&&a-(long)&&b; a:b:return;}
  60. // CHECK: @i = constant i32* bitcast (float* @PR9558 to i32*)
  61. int &i = reinterpret_cast<int&>(PR9558);
  62. int arr[2];
  63. // CHECK: @pastEnd = constant i32* bitcast (i8* getelementptr (i8* bitcast ([2 x i32]* @arr to i8*), i64 8) to i32*)
  64. int &pastEnd = arr[2];