xref: /llvm-project/clang/test/SemaCXX/warn-unused-filescoped.cpp (revision 9bb54b2aa006e3bf5df5eb8672075dd589fb9ba5)
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__anonf5343a620211::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__anonf5343a620211::VS73     virtual void vm() { }
74   };
75 
76   struct SVS : public VS {
vm__anonf5343a620211::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   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
162 }
163 
164 namespace unused_nested {
165   class outer {
166     void func1();
167     struct {
func2unused_nested::outer::__anonf5343a620708168       void func2() {
169       }
170     } x;
171   };
172 }
173 
174 namespace unused {
175   struct {
funcunused::__anonf5343a620808176     void func() { // expected-warning {{unused member function 'func'}}
177     }
178   } x; // expected-warning {{unused variable 'x'}}
179 }
180 
181 namespace test6 {
182   typedef struct { // expected-warning {{add a tag name}}
183     void bar(); // expected-note {{}}
184   } A; // expected-note {{}}
185 
186   typedef struct {
187     void bar();  // expected-warning {{unused member function 'bar'}}
188   } *B;
189 
190   struct C {
191     void bar();
192   };
193 }
194 
195 namespace pr14776 {
196   namespace {
197     struct X {};
198   }
199   X a = X(); // expected-warning {{unused variable 'a'}}
200   auto b = X(); // expected-warning {{unused variable 'b'}}
201 }
202 
203 namespace UndefinedInternalStaticMember {
204   namespace {
205     struct X {
206       static const unsigned x = 3;
207       int y[x];
208     };
209   }
210 }
211 
212 namespace test8 {
213 static void func();
bar()214 void bar() { void func() __attribute__((used)); }
func()215 static void func() {}
216 }
217 
218 namespace test9 {
219 template <typename T>
completeRedeclChainForTemplateSpecialization()220 static void completeRedeclChainForTemplateSpecialization() {} // expected-warning {{unused function template 'completeRedeclChainForTemplateSpecialization'}}
221 }
222 
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
229 }
230 
231 namespace pr19713 {
232 #if __cplusplus >= 201103L
233   // FIXME: We should warn on both of these.
constexpr3()234 static constexpr int constexpr3() { return 1; } // expected-warning {{unused function 'constexpr3'}}
constexpr4()235 constexpr int constexpr4() { return 2; }
236 #endif
237 }
238 
239 #endif
240