240 lines · cpp
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 %s3// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-template -Wunused-member-function -Wno-unused-local-typedefs -std=c++14 %s4 5#ifdef HEADER6 7static void headerstatic() {} // expected-warning{{unused function 'headerstatic'}}8static inline void headerstaticinline() {}9 10namespace {11void headeranon() {} // expected-warning{{unused function 'headeranon'}}12inline void headerinlineanon() {}13}14 15namespace test716{17 template<typename T>18 static inline void foo(T) { }19 20 // This should not emit an unused-function warning since it inherits21 // the static storage type from the base template.22 template<>23 inline void foo(int) { }24 25 // Partial specialization26 template<typename T, typename U>27 static inline void bar(T, U) { }28 29 template<typename U>30 inline void bar(int, U) { }31 32 template<>33 inline void bar(int, int) { }34};35 36namespace pr19713 {37#if __cplusplus >= 201103L38 static constexpr int constexpr1() { return 1; }39 constexpr int constexpr2() { return 2; }40#endif41}42 43#else44#define HEADER45#include "warn-unused-filescoped.cpp"46 47static void f1(); // expected-warning{{unused function 'f1'}}48 49namespace {50void f2(); // expected-warning{{unused function 'f2'}}51 52void f3() {} // expected-warning{{unused function 'f3'}}53 54struct 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 &);60};61 62 template <typename T>63 struct TS {64 void m();65 };66 template <> void TS<int>::m() {} // expected-warning{{unused member function 'm'}}67 68 template <typename T>69 void tf() {} // expected-warning{{unused function template 'tf'}}70 template <> void tf<int>() {} // expected-warning{{unused function 'tf<int>'}}71 72 struct VS {73 virtual void vm() { }74 };75 76 struct SVS : public VS {77 void vm() { }78 };79}80 81void S::m3() {} // expected-warning{{unused member function 'm3'}}82 83static inline void f4() {} // expected-warning{{unused function 'f4'}}84const unsigned int cx = 0; // expected-warning{{unused variable 'cx'}}85const unsigned int cy = 0;86int f5() { return cy; }87 88static int x1; // expected-warning{{unused variable 'x1'}}89 90namespace {91int x2; // expected-warning{{unused variable 'x2'}}92 93struct 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 108namespace PR8841 {109 // Ensure that friends of class templates are considered to have a dependent110 // context and not marked unused.111 namespace {112 template <typename T> struct X {113 friend bool operator==(const X&, const X&) { return false; }114 };115 }116 template <typename T> void template_test(X<T> x) {117 (void)(x == x);118 }119 void test() {120 X<int> x;121 template_test(x);122 }123}124 125namespace 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 132namespace rdar8733476 {133static void foo() {} // expected-warning {{function 'foo' is not needed and will not be emitted}}134template <typename T> static void foo_t() {} // expected-warning {{unused function template 'foo_t'}}135template <> void foo_t<int>() {} // expected-warning {{function 'foo_t<int>' is not needed and will not be emitted}}136 137template <int>138void bar() {139 foo();140 foo_t<int>();141 foo_t<void>();142}143}144 145namespace 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 } // namespace162}163 164namespace unused_nested {165 class outer {166 void func1();167 struct {168 void func2() {169 }170 } x;171 };172}173 174namespace unused {175 struct {176 void func() { // expected-warning {{unused member function 'func'}}177 }178 } x; // expected-warning {{unused variable 'x'}}179}180 181namespace 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 195namespace 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 203namespace UndefinedInternalStaticMember {204 namespace {205 struct X {206 static const unsigned x = 3;207 int y[x];208 };209 }210}211 212namespace test8 {213static void func();214void bar() { void func() __attribute__((used)); }215static void func() {}216}217 218namespace test9 {219template <typename T>220static void completeRedeclChainForTemplateSpecialization() {} // expected-warning {{unused function template 'completeRedeclChainForTemplateSpecialization'}}221}222 223namespace test10 {224#if __cplusplus >= 201103L225// FIXME: Warn on template definitions with no instantiations?226template<class T>227constexpr T pi = T(3.14);228#endif229}230 231namespace pr19713 {232#if __cplusplus >= 201103L233 // FIXME: We should warn on both of these.234static constexpr int constexpr3() { return 1; } // expected-warning {{unused function 'constexpr3'}}235constexpr int constexpr4() { return 2; }236#endif237}238 239#endif240