[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-unused-filescoped.cpp
blobbe8d350855c0781b16e69221d82321ace4a6effd
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
5 #ifdef HEADER
7 static void headerstatic() {} // expected-warning{{unused function 'headerstatic'}}
8 static inline void headerstaticinline() {}
10 namespace {
11 void headeranon() {} // expected-warning{{unused function 'headeranon'}}
12 inline void headerinlineanon() {}
15 namespace test7
17 template<typename T>
18 static inline void foo(T) { }
20 // This should not emit an unused-function warning since it inherits
21 // the static storage type from the base template.
22 template<>
23 inline void foo(int) { }
25 // Partial specialization
26 template<typename T, typename U>
27 static inline void bar(T, U) { }
29 template<typename U>
30 inline void bar(int, U) { }
32 template<>
33 inline void bar(int, int) { }
36 namespace pr19713 {
37 #if __cplusplus >= 201103L
38 static constexpr int constexpr1() { return 1; }
39 constexpr int constexpr2() { return 2; }
40 #endif
43 #else
44 #define HEADER
45 #include "warn-unused-filescoped.cpp"
47 static void f1(); // expected-warning{{unused function 'f1'}}
49 namespace {
50 void f2(); // expected-warning{{unused function 'f2'}}
52 void f3() {} // expected-warning{{unused function 'f3'}}
54 struct S {
55 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 &);
62 template <typename T>
63 struct TS {
64 void m();
66 template <> void TS<int>::m() {} // expected-warning{{unused member function 'm'}}
68 template <typename T>
69 void tf() {} // expected-warning{{unused function template 'tf'}}
70 template <> void tf<int>() {} // expected-warning{{unused function 'tf<int>'}}
72 struct VS {
73 virtual void vm() { }
76 struct SVS : public VS {
77 void vm() { }
81 void S::m3() {} // expected-warning{{unused member function 'm3'}}
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;
86 int f5() { return cy; }
88 static int x1; // expected-warning{{unused variable 'x1'}}
90 namespace {
91 int x2; // expected-warning{{unused variable 'x2'}}
93 struct S2 {
94 static int x; // expected-warning{{unused variable 'x'}}
97 template <typename T>
98 struct TS2 {
99 static int x;
101 template <> int TS2<int>::x; // expected-warning{{unused variable 'x'}}
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>'}}
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 {
113 friend bool operator==(const X&, const X&) { return false; }
116 template <typename T> void template_test(X<T> x) {
117 (void)(x == x);
119 void test() {
120 X<int> x;
121 template_test(x);
125 namespace test4 {
126 namespace { struct A {}; }
128 void test(A a); // expected-warning {{unused function 'test'}}
129 extern "C" void test4(A a);
132 namespace rdar8733476 {
133 static void foo() {} // expected-warning {{function 'foo' is not needed and will not be emitted}}
134 template <typename T> static void foo_t() {} // expected-warning {{unused function template 'foo_t'}}
135 template <> void foo_t<int>() {} // expected-warning {{function 'foo_t<int>' is not needed and will not be emitted}}
137 template <int>
138 void bar() {
139 foo();
140 foo_t<int>();
141 foo_t<void>();
145 namespace test5 {
146 static int n = 0;
147 static int &r = n;
148 int f(int &);
149 int k = f(r);
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);
157 namespace {
158 template <typename T> const double var_t = 0; // expected-warning {{unused variable template 'var_t'}}
159 template <> const double var_t<int> = 0; // expected-warning {{variable 'var_t<int>' is not needed and will not be emitted}}
160 int z = sizeof(var_t<int>); // expected-warning {{unused variable 'z'}}
161 } // namespace
164 namespace unused_nested {
165 class outer {
166 void func1();
167 struct {
168 void func2() {
170 } x;
174 namespace unused {
175 struct {
176 void func() { // expected-warning {{unused member function 'func'}}
178 } x; // expected-warning {{unused variable 'x'}}
181 namespace test6 {
182 typedef struct { // expected-warning {{add a tag name}}
183 void bar(); // expected-note {{}}
184 } A; // expected-note {{}}
186 typedef struct {
187 void bar(); // expected-warning {{unused member function 'bar'}}
188 } *B;
190 struct C {
191 void bar();
195 namespace pr14776 {
196 namespace {
197 struct X {};
199 X a = X(); // expected-warning {{unused variable 'a'}}
200 auto b = X(); // expected-warning {{unused variable 'b'}}
203 namespace UndefinedInternalStaticMember {
204 namespace {
205 struct X {
206 static const unsigned x = 3;
207 int y[x];
212 namespace test8 {
213 static void func();
214 void bar() { void func() __attribute__((used)); }
215 static void func() {}
218 namespace test9 {
219 template <typename T>
220 static void completeRedeclChainForTemplateSpecialization() {} // expected-warning {{unused function template 'completeRedeclChainForTemplateSpecialization'}}
223 namespace test10 {
224 #if __cplusplus >= 201103L
225 // FIXME: Warn on template definitions with no instantiations?
226 template<class T>
227 constexpr T pi = T(3.14);
228 #endif
231 namespace pr19713 {
232 #if __cplusplus >= 201103L
233 // FIXME: We should warn on both of these.
234 static constexpr int constexpr3() { return 1; } // expected-warning {{unused function 'constexpr3'}}
235 constexpr int constexpr4() { return 2; }
236 #endif
239 #endif