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