xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/linkage.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // This is an IR generation test because the calculation of visibility
2f4a2713aSLionel Sambuc // during IR gen will cause linkage to be implicitly recomputed and
3f4a2713aSLionel Sambuc // compared against the earlier cached value.  If we had a way of
4f4a2713aSLionel Sambuc // testing linkage directly in Sema, that would be better.
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc // RUN: %clang_cc1 -Werror -triple x86_64-apple-darwin10 -emit-llvm %s -o - | FileCheck %s
7f4a2713aSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc // CHECK: @_ZZN5test61A3fooEvE3bar = linkonce_odr global i32 0, align 4
9*0a6a1f1dSLionel Sambuc 
10f4a2713aSLionel Sambuc // PR8926
11f4a2713aSLionel Sambuc namespace test0 {
12f4a2713aSLionel Sambuc   typedef struct {
footest0::__anona8b8e979010813f4a2713aSLionel Sambuc     void *foo() { return 0; }
14f4a2713aSLionel Sambuc   } A;
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc   // CHECK: define linkonce_odr i8* @_ZN5test01A3fooEv(
17f4a2713aSLionel Sambuc 
test(A * a)18f4a2713aSLionel Sambuc   void test(A *a) {
19f4a2713aSLionel Sambuc     a->foo();
20f4a2713aSLionel Sambuc   }
21f4a2713aSLionel Sambuc }
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc namespace test1 {
24f4a2713aSLionel Sambuc   typedef struct {
footest1::__anona8b8e979020825f4a2713aSLionel Sambuc     template <unsigned n> void *foo() { return 0; }
26f4a2713aSLionel Sambuc 
footest1::__anona8b8e979020827f4a2713aSLionel Sambuc     void foo() {
28f4a2713aSLionel Sambuc       foo<0>();
29f4a2713aSLionel Sambuc     }
30f4a2713aSLionel Sambuc   } A;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc   // CHECK: define linkonce_odr void @_ZN5test11A3fooEv(
33f4a2713aSLionel Sambuc   // another at the end
34f4a2713aSLionel Sambuc 
test(A * a)35f4a2713aSLionel Sambuc   void test(A *a) {
36f4a2713aSLionel Sambuc     a->foo();
37f4a2713aSLionel Sambuc   }
38f4a2713aSLionel Sambuc }
39f4a2713aSLionel Sambuc 
40f4a2713aSLionel Sambuc namespace test2 {
41f4a2713aSLionel Sambuc   typedef struct {
42f4a2713aSLionel Sambuc     template <unsigned n> struct B {
footest2::__anona8b8e9790308::B43f4a2713aSLionel Sambuc       void *foo() { return 0; }
44f4a2713aSLionel Sambuc     };
45f4a2713aSLionel Sambuc 
footest2::__anona8b8e979030846f4a2713aSLionel Sambuc     void foo(B<0> *b) {
47f4a2713aSLionel Sambuc       b->foo();
48f4a2713aSLionel Sambuc     }
49f4a2713aSLionel Sambuc   } A;
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   // CHECK: define linkonce_odr void @_ZN5test21A3fooEPNS0_1BILj0EEE(
52f4a2713aSLionel Sambuc 
test(A * a)53f4a2713aSLionel Sambuc   void test(A *a) {
54f4a2713aSLionel Sambuc     a->foo(0);
55f4a2713aSLionel Sambuc   }
56f4a2713aSLionel Sambuc }
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc namespace test3 {
59f4a2713aSLionel Sambuc   namespace { struct A {}; }
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc   // CHECK: define internal void @_ZN5test34testENS_12_GLOBAL__N_11AE(
test(A a)62f4a2713aSLionel Sambuc   void test(A a) {}
force()63f4a2713aSLionel Sambuc   void force() { test(A()); }
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc   // CHECK: define void @test3(
test3(A a)66f4a2713aSLionel Sambuc   extern "C" void test3(A a) {}
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc namespace {
70f4a2713aSLionel Sambuc   // CHECK: define void @test4(
test4(void)71f4a2713aSLionel Sambuc   extern "C" void test4(void) {}
72f4a2713aSLionel Sambuc }
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc // PR9316: Ensure that even non-namespace-scope function declarations in
75f4a2713aSLionel Sambuc // a C declaration context respect that over the anonymous namespace.
76f4a2713aSLionel Sambuc extern "C" {
77f4a2713aSLionel Sambuc   namespace {
78f4a2713aSLionel Sambuc     struct X {
f__anona8b8e9790611::X79f4a2713aSLionel Sambuc       int f() {
80f4a2713aSLionel Sambuc         extern int g();
81f4a2713aSLionel Sambuc         extern int a;
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc         // Test both for mangling in the code generation and warnings from use
84f4a2713aSLionel Sambuc         // of internal, undefined names via -Werror.
85f4a2713aSLionel Sambuc         // CHECK: call i32 @g(
86f4a2713aSLionel Sambuc         // CHECK: load i32* @a,
87f4a2713aSLionel Sambuc         return g() + a;
88f4a2713aSLionel Sambuc       }
89f4a2713aSLionel Sambuc     };
90f4a2713aSLionel Sambuc   }
91f4a2713aSLionel Sambuc   // Force the above function to be emitted by codegen.
test(X & x)92f4a2713aSLionel Sambuc   int test(X& x) {
93f4a2713aSLionel Sambuc     return x.f();
94f4a2713aSLionel Sambuc   }
95f4a2713aSLionel Sambuc }
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc // CHECK: define linkonce_odr i8* @_ZN5test21A1BILj0EE3fooEv(
98f4a2713aSLionel Sambuc // CHECK: define linkonce_odr i8* @_ZN5test11A3fooILj0EEEPvv(
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc namespace test5 {
101f4a2713aSLionel Sambuc   struct foo {
102f4a2713aSLionel Sambuc   };
103f4a2713aSLionel Sambuc   extern "C" {
104f4a2713aSLionel Sambuc     const foo bar[]  = {
105f4a2713aSLionel Sambuc     };
106f4a2713aSLionel Sambuc   }
107f4a2713aSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc // Test that we don't compute linkage too hastily before we're done
110*0a6a1f1dSLionel Sambuc // processing a record decl.  rdar://15928125
111*0a6a1f1dSLionel Sambuc namespace test6 {
112*0a6a1f1dSLionel Sambuc   typedef struct {
footest6::__anona8b8e9790708113*0a6a1f1dSLionel Sambuc     int foo() {
114*0a6a1f1dSLionel Sambuc       // Tested at top of file.
115*0a6a1f1dSLionel Sambuc       static int bar = 0;
116*0a6a1f1dSLionel Sambuc       return bar++;
117*0a6a1f1dSLionel Sambuc     }
118*0a6a1f1dSLionel Sambuc   } A;
119*0a6a1f1dSLionel Sambuc 
test()120*0a6a1f1dSLionel Sambuc   void test() {
121*0a6a1f1dSLionel Sambuc     A a;
122*0a6a1f1dSLionel Sambuc     a.foo();
123*0a6a1f1dSLionel Sambuc   }
124*0a6a1f1dSLionel Sambuc }
125