1 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-template -Wunused-member-function -Wno-unused-local-typedefs \
2 // RUN: -Wno-c++11-extensions -Wno-c++14-extensions -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-template -Wunused-member-function -Wno-unused-local-typedefs -std=c++14 %s
4
5 #ifdef HEADER
6
headerstatic()7 static void headerstatic() {} // expected-warning{{unused function 'headerstatic'}}
headerstaticinline()8 static inline void headerstaticinline() {}
9
10 namespace {
headeranon()11 void headeranon() {} // expected-warning{{unused function 'headeranon'}}
headerinlineanon()12 inline void headerinlineanon() {}
13 }
14
15 namespace test7
16 {
17 template<typename T>
foo(T)18 static inline void foo(T) { }
19
20 // This should not emit an unused-function warning since it inherits
21 // the static storage type from the base template.
22 template<>
foo(int)23 inline void foo(int) { }
24
25 // Partial specialization
26 template<typename T, typename U>
bar(T,U)27 static inline void bar(T, U) { }
28
29 template<typename U>
bar(int,U)30 inline void bar(int, U) { }
31
32 template<>
bar(int,int)33 inline void bar(int, int) { }
34 };
35
36 namespace pr19713 {
37 #if __cplusplus >= 201103L
constexpr1()38 static constexpr int constexpr1() { return 1; }
constexpr2()39 constexpr int constexpr2() { return 2; }
40 #endif
41 }
42
43 #else
44 #define HEADER
45 #include "warn-unused-filescoped.cpp"
46
47 static void f1(); // expected-warning{{unused function 'f1'}}
48
49 namespace {
50 void f2(); // expected-warning{{unused function 'f2'}}
51
f3()52 void f3() {} // expected-warning{{unused function 'f3'}}
53
54 struct S {
m1__anondb9101a90211::S55 void m1() {} // expected-warning{{unused member function 'm1'}}
56 void m2(); // expected-warning{{unused member function 'm2'}}
57 void m3();
58 S(const S &);
59 void operator=(const S &);
60 };
61
62 template <typename T>
63 struct TS {
64 void m();
65 };
m()66 template <> void TS<int>::m() {} // expected-warning{{unused member function 'm'}}
67
68 template <typename T>
tf()69 void tf() {} // expected-warning{{unused function template 'tf'}}
tf()70 template <> void tf<int>() {} // expected-warning{{unused function 'tf<int>'}}
71
72 struct VS {
vm__anondb9101a90211::VS73 virtual void vm() { }
74 };
75
76 struct SVS : public VS {
vm__anondb9101a90211::SVS77 void vm() { }
78 };
79 }
80
m3()81 void S::m3() {} // expected-warning{{unused member function 'm3'}}
82
f4()83 static inline void f4() {} // expected-warning{{unused function 'f4'}}
84 const unsigned int cx = 0; // expected-warning{{unused variable 'cx'}}
85 const unsigned int cy = 0;
f5()86 int f5() { return cy; }
87
88 static int x1; // expected-warning{{unused variable 'x1'}}
89
90 namespace {
91 int x2; // expected-warning{{unused variable 'x2'}}
92
93 struct S2 {
94 static int x; // expected-warning{{unused variable 'x'}}
95 };
96
97 template <typename T>
98 struct TS2 {
99 static int x;
100 };
101 template <> int TS2<int>::x; // expected-warning{{unused variable 'x'}}
102
103 template <typename T, typename U> int vt = 0; // expected-warning {{unused variable template 'vt'}}
104 template <typename T> int vt<T, void> = 0;
105 template <> int vt<void, void> = 0; // expected-warning {{unused variable 'vt<void, void>'}}
106 }
107
108 namespace PR8841 {
109 // Ensure that friends of class templates are considered to have a dependent
110 // context and not marked unused.
111 namespace {
112 template <typename T> struct X {
operator ==(const X &,const X &)113 friend bool operator==(const X&, const X&) { return false; }
114 };
115 }
template_test(X<T> x)116 template <typename T> void template_test(X<T> x) {
117 (void)(x == x);
118 }
test()119 void test() {
120 X<int> x;
121 template_test(x);
122 }
123 }
124
125 namespace test4 {
126 namespace { struct A {}; }
127
128 void test(A a); // expected-warning {{unused function 'test'}}
129 extern "C" void test4(A a);
130 }
131
132 namespace rdar8733476 {
foo()133 static void foo() {} // expected-warning {{function 'foo' is not needed and will not be emitted}}
foo_t()134 template <typename T> static void foo_t() {} // expected-warning {{unused function template 'foo_t'}}
foo_t()135 template <> void foo_t<int>() {} // expected-warning {{function 'foo_t<int>' is not needed and will not be emitted}}
136
137 template <int>
bar()138 void bar() {
139 foo();
140 foo_t<int>();
141 foo_t<void>();
142 }
143 }
144
145 namespace test5 {
146 static int n = 0;
147 static int &r = n;
148 int f(int &);
149 int k = f(r);
150
151 // FIXME: We should produce warnings for both of these.
152 static const int m = n;
153 int x = sizeof(m);
154 static const double d = 0.0; // expected-warning{{variable 'd' is not needed and will not be emitted}}
155 int y = sizeof(d);
156
157 namespace {
158 // FIXME: Should be "unused variable template 'var_t'" instead.
159 template <typename T> const double var_t = 0; // expected-warning {{unused variable 'var_t'}}
160 template <> const double var_t<int> = 0; // expected-warning {{variable 'var_t<int>' is not needed and will not be emitted}}
161 int z = sizeof(var_t<int>); // expected-warning {{unused variable 'z'}}
162 } // namespace
163 }
164
165 namespace unused_nested {
166 class outer {
167 void func1();
168 struct {
func2unused_nested::outer::__anondb9101a90708169 void func2() {
170 }
171 } x;
172 };
173 }
174
175 namespace unused {
176 struct {
funcunused::__anondb9101a90808177 void func() { // expected-warning {{unused member function 'func'}}
178 }
179 } x; // expected-warning {{unused variable 'x'}}
180 }
181
182 namespace test6 {
183 typedef struct { // expected-warning {{add a tag name}}
184 void bar(); // expected-note {{}}
185 } A; // expected-note {{}}
186
187 typedef struct {
188 void bar(); // expected-warning {{unused member function 'bar'}}
189 } *B;
190
191 struct C {
192 void bar();
193 };
194 }
195
196 namespace pr14776 {
197 namespace {
198 struct X {};
199 }
200 X a = X(); // expected-warning {{unused variable 'a'}}
201 auto b = X(); // expected-warning {{unused variable 'b'}}
202 }
203
204 namespace UndefinedInternalStaticMember {
205 namespace {
206 struct X {
207 static const unsigned x = 3;
208 int y[x];
209 };
210 }
211 }
212
213 namespace test8 {
214 static void func();
bar()215 void bar() { void func() __attribute__((used)); }
func()216 static void func() {}
217 }
218
219 namespace test9 {
220 template <typename T>
completeRedeclChainForTemplateSpecialization()221 static void completeRedeclChainForTemplateSpecialization() {} // expected-warning {{unused function template 'completeRedeclChainForTemplateSpecialization'}}
222 }
223
224 namespace test10 {
225 #if __cplusplus >= 201103L
226 // FIXME: Warn on template definitions with no instantiations?
227 template<class T>
228 constexpr T pi = T(3.14);
229 #endif
230 }
231
232 namespace pr19713 {
233 #if __cplusplus >= 201103L
234 // FIXME: We should warn on both of these.
constexpr3()235 static constexpr int constexpr3() { return 1; } // expected-warning {{unused function 'constexpr3'}}
constexpr4()236 constexpr int constexpr4() { return 2; }
237 #endif
238 }
239
240 #endif
241