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