File: namespace.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,245,028 kB
  • sloc: cpp: 7,619,726; ansic: 1,434,018; asm: 1,058,748; python: 252,740; f90: 94,671; objc: 70,685; lisp: 42,813; pascal: 18,401; sh: 8,601; ml: 5,111; perl: 4,720; makefile: 3,675; awk: 3,523; javascript: 2,409; xml: 892; fortran: 770
file content (103 lines) | stat: -rw-r--r-- 2,614 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | FileCheck %s

// Test anonymous namespace.
namespace {
  int g1 = 1;

  void f1(void) {}
}


// Test named namespace.
namespace test {
  int g2 = 2;
  void f2(void);

  // Test nested namespace.
  namespace test2 {
    int g3 = 3;
    void f3(void);
  }
}

// CHECK-DAG: cir.global "private" internal dso_local @_ZN12_GLOBAL__N_12g1E = #cir.int<1> : !s32i
// CHECK-DAG: cir.global external @_ZN4test2g2E = #cir.int<2> : !s32i
// CHECK-DAG: cir.global external @_ZN4test5test22g3E = #cir.int<3> : !s32i
// CHECK-DAG: cir.func{{.*}} @_ZN12_GLOBAL__N_12f1Ev()
// CHECK-DAG: cir.func{{.*}} @_ZN4test2f2Ev()
// CHECK-DAG: cir.func{{.*}} @_ZN4test5test22f3Ev()

using namespace test;

// Test global function.
int f4(void) {
    f1();
    f2();
    test2::f3();
    return g1 + g2 + test2::g3;
}

// The namespace gets added during name mangling, so this is wrong but expected.
// CHECK: cir.func{{.*}} @_Z2f4v()
// CHECK:   cir.call @_ZN12_GLOBAL__N_12f1Ev()
// CHECK:   cir.call @_ZN4test2f2Ev()
// CHECK:   cir.call @_ZN4test5test22f3Ev()
// CHECK:   %[[G1_ADDR:.*]] = cir.get_global @_ZN12_GLOBAL__N_12g1E : !cir.ptr<!s32i>
// CHECK:   %[[G1_VAL:.*]] = cir.load{{.*}} %[[G1_ADDR]] : !cir.ptr<!s32i>, !s32i
// CHECK:   %[[G2_ADDR:.*]] = cir.get_global @_ZN4test2g2E : !cir.ptr<!s32i>
// CHECK:   %[[G2_VAL:.*]] = cir.load{{.*}} %[[G2_ADDR]] : !cir.ptr<!s32i>, !s32i
// CHECK:   %[[SUM:.*]] = cir.binop(add, %[[G1_VAL]], %[[G2_VAL]]) nsw : !s32i
// CHECK:   %[[G3_ADDR:.*]] = cir.get_global @_ZN4test5test22g3E : !cir.ptr<!s32i>
// CHECK:   %[[G3_VAL:.*]] = cir.load{{.*}} %[[G3_ADDR]] : !cir.ptr<!s32i>, !s32i
// CHECK:   %[[SUM2:.*]] = cir.binop(add, %[[SUM]], %[[G3_VAL]]) nsw : !s32i

using test2::f3;
using test2::g3;

int f5() {
  f3();
  return g3;
}

// CHECK: cir.func{{.*}} @_Z2f5v()
// CHECK:   cir.call @_ZN4test5test22f3Ev()
// CHECK:   %[[G3_ADDR:.*]] = cir.get_global @_ZN4test5test22g3E : !cir.ptr<!s32i>
// CHECK:   %[[G3_VAL:.*]] = cir.load{{.*}} %[[G3_ADDR]] : !cir.ptr<!s32i>, !s32i

namespace test3 {
  struct S {
    int a;
  } s;
}

using test3::s;

int f6() {
  return s.a;
}

// CHECK: cir.func{{.*}} @_Z2f6v()
// CHECK:   cir.get_global @_ZN5test31sE : !cir.ptr<!rec_test33A3AS>
// CHECK:   cir.get_member %{{.*}}[0] {name = "a"}

int shadowedFunc() {
  return 3;
}

namespace shadow {
  using ::shadowedFunc;
}

void f7() {
  shadow::shadowedFunc();
}

// CHECK: cir.func{{.*}} @_Z2f7v()

namespace test_alias = test;

int f8() {
  return test_alias::g2;
}

// CHECK: cir.func{{.*}} @_Z2f8v()