xref: /llvm-project/clang/test/SemaCXX/undefined-internal.cpp (revision 15dd404c467551a783ec7c9341b285403499a93a)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 
3 // Make sure we don't produce invalid IR.
4 // RUN: %clang_cc1 -emit-llvm-only %s
5 
6 namespace test1 {
7   static void foo(); // expected-warning {{function 'test1::foo' has internal linkage but is not defined}}
8   template <class T> static void bar(); // expected-warning {{function 'test1::bar<int>' has internal linkage but is not defined}}
9 
10   void test() {
11     foo(); // expected-note {{used here}}
12     bar<int>(); // expected-note {{used here}}
13   }
14 }
15 
16 namespace test2 {
17   namespace {
18     void foo(); // expected-warning {{function 'test2::<anonymous namespace>::foo' has internal linkage but is not defined}}
19     extern int var; // expected-warning {{variable 'test2::<anonymous namespace>::var' has internal linkage but is not defined}}
20     template <class T> void bar(); // expected-warning {{function 'test2::<anonymous namespace>::bar<int>' has internal linkage but is not defined}}
21   }
22   void test() {
23     foo(); // expected-note {{used here}}
24     var = 0; // expected-note {{used here}}
25     bar<int>(); // expected-note {{used here}}
26   }
27 }
28 
29 namespace test3 {
30   namespace {
31     void foo();
32     extern int var;
33     template <class T> void bar();
34   }
35 
36   void test() {
37     foo();
38     var = 0;
39     bar<int>();
40   }
41 
42   namespace {
43     void foo() {}
44     int var = 0;
45     template <class T> void bar() {}
46   }
47 }
48 
49 namespace test4 {
50   namespace {
51     struct A {
52       A(); // expected-warning {{function 'test4::<anonymous namespace>::A::A' has internal linkage but is not defined}}
53       ~A();// expected-warning {{function 'test4::<anonymous namespace>::A::~A' has internal linkage but is not defined}}
54       virtual void foo(); // expected-warning {{function 'test4::<anonymous namespace>::A::foo' has internal linkage but is not defined}}
55       virtual void bar() = 0;
56       virtual void baz(); // expected-warning {{function 'test4::<anonymous namespace>::A::baz' has internal linkage but is not defined}}
57     };
58   }
59 
60   void test(A &a) {
61     a.foo(); // expected-note {{used here}}
62     a.bar();
63     a.baz(); // expected-note {{used here}}
64   }
65 
66   struct Test : A {
67     Test() {} // expected-note 2 {{used here}}
68   };
69 }
70 
71 // rdar://problem/9014651
72 namespace test5 {
73   namespace {
74     struct A {};
75   }
76 
77   template <class N> struct B {
78     static int var; // expected-warning {{variable 'test5::B<test5::<anonymous>::A>::var' has internal linkage but is not defined}}
79     static void foo(); // expected-warning {{function 'test5::B<test5::<anonymous>::A>::foo' has internal linkage but is not defined}}
80   };
81 
82   void test() {
83     B<A>::var = 0; // expected-note {{used here}}
84     B<A>::foo(); // expected-note {{used here}}
85   }
86 }
87 
88 namespace test6 {
89   template <class T> struct A {
90     static const int zero = 0;
91     static const int one = 1;
92     static const int two = 2;
93 
94     int value;
95 
96     A() : value(zero) {
97       value = one;
98     }
99   };
100 
101   namespace { struct Internal; }
102 
103   void test() {
104     A<Internal> a;
105     a.value = A<Internal>::two;
106   }
107 }
108