2020-07-27 00:20:56 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-template -Wunused-member-function -Wno-unused-local-typedefs \
|
|
|
|
// RUN: -Wno-c++11-extensions -Wno-c++14-extensions -std=c++98 %s
|
2017-05-09 19:25:41 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-template -Wunused-member-function -Wno-unused-local-typedefs -std=c++14 %s
|
2010-08-15 09:15:20 +08:00
|
|
|
|
2013-09-10 11:05:56 +08:00
|
|
|
#ifdef HEADER
|
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
static void headerstatic() {} // expected-warning{{unused function 'headerstatic'}}
|
2013-09-10 11:05:56 +08:00
|
|
|
static inline void headerstaticinline() {}
|
|
|
|
|
|
|
|
namespace {
|
2020-07-27 00:20:56 +08:00
|
|
|
void headeranon() {} // expected-warning{{unused function 'headeranon'}}
|
|
|
|
inline void headerinlineanon() {}
|
2013-09-10 11:05:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace test7
|
|
|
|
{
|
|
|
|
template<typename T>
|
|
|
|
static inline void foo(T) { }
|
|
|
|
|
|
|
|
// This should not emit an unused-function warning since it inherits
|
|
|
|
// the static storage type from the base template.
|
|
|
|
template<>
|
|
|
|
inline void foo(int) { }
|
|
|
|
|
|
|
|
// Partial specialization
|
|
|
|
template<typename T, typename U>
|
|
|
|
static inline void bar(T, U) { }
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
inline void bar(int, U) { }
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline void bar(int, int) { }
|
|
|
|
};
|
|
|
|
|
2014-05-12 05:25:24 +08:00
|
|
|
namespace pr19713 {
|
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
static constexpr int constexpr1() { return 1; }
|
|
|
|
constexpr int constexpr2() { return 2; }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:05:56 +08:00
|
|
|
#else
|
|
|
|
#define HEADER
|
|
|
|
#include "warn-unused-filescoped.cpp"
|
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
static void f1(); // expected-warning{{unused function 'f1'}}
|
2010-08-15 09:15:20 +08:00
|
|
|
|
|
|
|
namespace {
|
2020-07-27 00:20:56 +08:00
|
|
|
void f2(); // expected-warning{{unused function 'f2'}}
|
2010-08-15 09:15:20 +08:00
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
void f3() {} // expected-warning{{unused function 'f3'}}
|
2010-08-15 09:15:20 +08:00
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
struct S {
|
|
|
|
void m1() {} // expected-warning{{unused member function 'm1'}}
|
|
|
|
void m2(); // expected-warning{{unused member function 'm2'}}
|
|
|
|
void m3();
|
|
|
|
S(const S &);
|
|
|
|
void operator=(const S &);
|
|
|
|
};
|
2010-08-15 09:15:20 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct TS {
|
|
|
|
void m();
|
|
|
|
};
|
2020-07-27 00:20:56 +08:00
|
|
|
template <> void TS<int>::m() {} // expected-warning{{unused member function 'm'}}
|
2010-08-15 09:15:20 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2020-07-27 00:20:56 +08:00
|
|
|
void tf() {} // expected-warning{{unused function template 'tf'}}
|
[clang] Pass the NamedDecl* instead of the DeclarationName into many diagnostics.
Background:
-----------
There are two related argument types which can be sent into a diagnostic to
display the name of an entity: DeclarationName (ak_declarationname) or
NamedDecl* (ak_nameddecl) (there is also ak_identifierinfo for
IdentifierInfo*, but we are not concerned with it here).
A DeclarationName in a diagnostic will just be streamed to the output,
which will directly result in a call to DeclarationName::print.
A NamedDecl* in a diagnostic will also ultimately result in a call to
DeclarationName::print, but with two customisation points along the way:
The first customisation point is NamedDecl::getNameForDiagnostic which is
overloaded by FunctionDecl, ClassTemplateSpecializationDecl and
VarTemplateSpecializationDecl to print the template arguments, if any.
The second customisation point is NamedDecl::printName. By default it just
streams the stored DeclarationName into the output but it can be customised
to provide a user-friendly name for an entity. It is currently overloaded by
DecompositionDecl and MSGuidDecl.
What this patch does:
---------------------
For many diagnostics a DeclarationName is used instead of the NamedDecl*.
This bypasses the two customisation points mentioned above. This patches fix
this for diagnostics in Sema.cpp, SemaCast.cpp, SemaChecking.cpp, SemaDecl.cpp,
SemaDeclAttr.cpp, SemaDecl.cpp, SemaOverload.cpp and SemaStmt.cpp.
I have only modified diagnostics where I could construct a test-case which
demonstrates that the change is appropriate (either with this patch or the next
one).
Reviewed By: erichkeane, aaron.ballman
Differential Revision: https://reviews.llvm.org/D84656
2020-07-28 06:22:21 +08:00
|
|
|
template <> void tf<int>() {} // expected-warning{{unused function 'tf<int>'}}
|
2020-07-27 00:20:56 +08:00
|
|
|
|
2010-08-15 09:15:20 +08:00
|
|
|
struct VS {
|
|
|
|
virtual void vm() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SVS : public VS {
|
|
|
|
void vm() { }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
void S::m3() {} // expected-warning{{unused member function 'm3'}}
|
2010-08-15 09:15:20 +08:00
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
static inline void f4() {} // expected-warning{{unused function 'f4'}}
|
|
|
|
const unsigned int cx = 0; // expected-warning{{unused variable 'cx'}}
|
2013-09-10 11:05:56 +08:00
|
|
|
const unsigned int cy = 0;
|
|
|
|
int f5() { return cy; }
|
2010-08-15 09:15:20 +08:00
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
static int x1; // expected-warning{{unused variable 'x1'}}
|
2010-08-15 09:15:20 +08:00
|
|
|
|
|
|
|
namespace {
|
2020-07-27 00:20:56 +08:00
|
|
|
int x2; // expected-warning{{unused variable 'x2'}}
|
|
|
|
|
|
|
|
struct S2 {
|
|
|
|
static int x; // expected-warning{{unused variable 'x'}}
|
|
|
|
};
|
2010-08-15 09:15:20 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct TS2 {
|
|
|
|
static int x;
|
|
|
|
};
|
2020-07-27 00:20:56 +08:00
|
|
|
template <> int TS2<int>::x; // expected-warning{{unused variable 'x'}}
|
|
|
|
|
|
|
|
template <typename T, typename U> int vt = 0; // expected-warning {{unused variable template 'vt'}}
|
|
|
|
template <typename T> int vt<T, void> = 0;
|
[clang] Pass the NamedDecl* instead of the DeclarationName into many diagnostics.
Background:
-----------
There are two related argument types which can be sent into a diagnostic to
display the name of an entity: DeclarationName (ak_declarationname) or
NamedDecl* (ak_nameddecl) (there is also ak_identifierinfo for
IdentifierInfo*, but we are not concerned with it here).
A DeclarationName in a diagnostic will just be streamed to the output,
which will directly result in a call to DeclarationName::print.
A NamedDecl* in a diagnostic will also ultimately result in a call to
DeclarationName::print, but with two customisation points along the way:
The first customisation point is NamedDecl::getNameForDiagnostic which is
overloaded by FunctionDecl, ClassTemplateSpecializationDecl and
VarTemplateSpecializationDecl to print the template arguments, if any.
The second customisation point is NamedDecl::printName. By default it just
streams the stored DeclarationName into the output but it can be customised
to provide a user-friendly name for an entity. It is currently overloaded by
DecompositionDecl and MSGuidDecl.
What this patch does:
---------------------
For many diagnostics a DeclarationName is used instead of the NamedDecl*.
This bypasses the two customisation points mentioned above. This patches fix
this for diagnostics in Sema.cpp, SemaCast.cpp, SemaChecking.cpp, SemaDecl.cpp,
SemaDeclAttr.cpp, SemaDecl.cpp, SemaOverload.cpp and SemaStmt.cpp.
I have only modified diagnostics where I could construct a test-case which
demonstrates that the change is appropriate (either with this patch or the next
one).
Reviewed By: erichkeane, aaron.ballman
Differential Revision: https://reviews.llvm.org/D84656
2020-07-28 06:22:21 +08:00
|
|
|
template <> int vt<void, void> = 0; // expected-warning {{unused variable 'vt<void, void>'}}
|
2010-08-15 09:15:20 +08:00
|
|
|
}
|
2011-01-04 03:27:19 +08:00
|
|
|
|
|
|
|
namespace PR8841 {
|
|
|
|
// Ensure that friends of class templates are considered to have a dependent
|
|
|
|
// context and not marked unused.
|
|
|
|
namespace {
|
|
|
|
template <typename T> struct X {
|
|
|
|
friend bool operator==(const X&, const X&) { return false; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
template <typename T> void template_test(X<T> x) {
|
|
|
|
(void)(x == x);
|
|
|
|
}
|
2011-02-10 14:50:24 +08:00
|
|
|
void test() {
|
|
|
|
X<int> x;
|
2011-01-04 03:27:19 +08:00
|
|
|
template_test(x);
|
|
|
|
}
|
|
|
|
}
|
2011-02-10 14:50:24 +08:00
|
|
|
|
|
|
|
namespace test4 {
|
|
|
|
namespace { struct A {}; }
|
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
void test(A a); // expected-warning {{unused function 'test'}}
|
2011-02-10 14:50:24 +08:00
|
|
|
extern "C" void test4(A a);
|
|
|
|
}
|
2011-04-20 03:51:10 +08:00
|
|
|
|
|
|
|
namespace rdar8733476 {
|
2020-07-27 00:20:56 +08:00
|
|
|
static void foo() {} // expected-warning {{function 'foo' is not needed and will not be emitted}}
|
|
|
|
template <typename T> static void foo_t() {} // expected-warning {{unused function template 'foo_t'}}
|
[clang] Pass the NamedDecl* instead of the DeclarationName into many diagnostics.
Background:
-----------
There are two related argument types which can be sent into a diagnostic to
display the name of an entity: DeclarationName (ak_declarationname) or
NamedDecl* (ak_nameddecl) (there is also ak_identifierinfo for
IdentifierInfo*, but we are not concerned with it here).
A DeclarationName in a diagnostic will just be streamed to the output,
which will directly result in a call to DeclarationName::print.
A NamedDecl* in a diagnostic will also ultimately result in a call to
DeclarationName::print, but with two customisation points along the way:
The first customisation point is NamedDecl::getNameForDiagnostic which is
overloaded by FunctionDecl, ClassTemplateSpecializationDecl and
VarTemplateSpecializationDecl to print the template arguments, if any.
The second customisation point is NamedDecl::printName. By default it just
streams the stored DeclarationName into the output but it can be customised
to provide a user-friendly name for an entity. It is currently overloaded by
DecompositionDecl and MSGuidDecl.
What this patch does:
---------------------
For many diagnostics a DeclarationName is used instead of the NamedDecl*.
This bypasses the two customisation points mentioned above. This patches fix
this for diagnostics in Sema.cpp, SemaCast.cpp, SemaChecking.cpp, SemaDecl.cpp,
SemaDeclAttr.cpp, SemaDecl.cpp, SemaOverload.cpp and SemaStmt.cpp.
I have only modified diagnostics where I could construct a test-case which
demonstrates that the change is appropriate (either with this patch or the next
one).
Reviewed By: erichkeane, aaron.ballman
Differential Revision: https://reviews.llvm.org/D84656
2020-07-28 06:22:21 +08:00
|
|
|
template <> void foo_t<int>() {} // expected-warning {{function 'foo_t<int>' is not needed and will not be emitted}}
|
2011-04-20 03:51:10 +08:00
|
|
|
|
2020-07-27 00:20:56 +08:00
|
|
|
template <int>
|
|
|
|
void bar() {
|
|
|
|
foo();
|
|
|
|
foo_t<int>();
|
|
|
|
foo_t<void>();
|
|
|
|
}
|
2011-04-20 03:51:10 +08:00
|
|
|
}
|
2012-10-28 12:47:21 +08:00
|
|
|
|
|
|
|
namespace test5 {
|
|
|
|
static int n = 0;
|
|
|
|
static int &r = n;
|
|
|
|
int f(int &);
|
|
|
|
int k = f(r);
|
|
|
|
|
2012-10-28 15:39:29 +08:00
|
|
|
// FIXME: We should produce warnings for both of these.
|
|
|
|
static const int m = n;
|
2012-10-28 12:47:21 +08:00
|
|
|
int x = sizeof(m);
|
2020-07-27 00:20:56 +08:00
|
|
|
static const double d = 0.0; // expected-warning{{variable 'd' is not needed and will not be emitted}}
|
2012-10-28 12:47:21 +08:00
|
|
|
int y = sizeof(d);
|
2020-07-27 00:20:56 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
// FIXME: Should be "unused variable template 'var_t'" instead.
|
|
|
|
template <typename T> const double var_t = 0; // expected-warning {{unused variable 'var_t'}}
|
[clang] Pass the NamedDecl* instead of the DeclarationName into many diagnostics.
Background:
-----------
There are two related argument types which can be sent into a diagnostic to
display the name of an entity: DeclarationName (ak_declarationname) or
NamedDecl* (ak_nameddecl) (there is also ak_identifierinfo for
IdentifierInfo*, but we are not concerned with it here).
A DeclarationName in a diagnostic will just be streamed to the output,
which will directly result in a call to DeclarationName::print.
A NamedDecl* in a diagnostic will also ultimately result in a call to
DeclarationName::print, but with two customisation points along the way:
The first customisation point is NamedDecl::getNameForDiagnostic which is
overloaded by FunctionDecl, ClassTemplateSpecializationDecl and
VarTemplateSpecializationDecl to print the template arguments, if any.
The second customisation point is NamedDecl::printName. By default it just
streams the stored DeclarationName into the output but it can be customised
to provide a user-friendly name for an entity. It is currently overloaded by
DecompositionDecl and MSGuidDecl.
What this patch does:
---------------------
For many diagnostics a DeclarationName is used instead of the NamedDecl*.
This bypasses the two customisation points mentioned above. This patches fix
this for diagnostics in Sema.cpp, SemaCast.cpp, SemaChecking.cpp, SemaDecl.cpp,
SemaDeclAttr.cpp, SemaDecl.cpp, SemaOverload.cpp and SemaStmt.cpp.
I have only modified diagnostics where I could construct a test-case which
demonstrates that the change is appropriate (either with this patch or the next
one).
Reviewed By: erichkeane, aaron.ballman
Differential Revision: https://reviews.llvm.org/D84656
2020-07-28 06:22:21 +08:00
|
|
|
template <> const double var_t<int> = 0; // expected-warning {{variable 'var_t<int>' is not needed and will not be emitted}}
|
2020-07-27 00:20:56 +08:00
|
|
|
int z = sizeof(var_t<int>); // expected-warning {{unused variable 'z'}}
|
|
|
|
} // namespace
|
2012-10-28 12:47:21 +08:00
|
|
|
}
|
2012-11-14 09:52:05 +08:00
|
|
|
|
|
|
|
namespace unused_nested {
|
|
|
|
class outer {
|
|
|
|
void func1();
|
|
|
|
struct {
|
|
|
|
void func2() {
|
|
|
|
}
|
|
|
|
} x;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace unused {
|
|
|
|
struct {
|
2020-07-27 00:20:56 +08:00
|
|
|
void func() { // expected-warning {{unused member function 'func'}}
|
2012-11-14 09:52:05 +08:00
|
|
|
}
|
2020-07-27 00:20:56 +08:00
|
|
|
} x; // expected-warning {{unused variable 'x'}}
|
2012-11-14 09:52:05 +08:00
|
|
|
}
|
2012-12-31 05:42:26 +08:00
|
|
|
|
|
|
|
namespace test6 {
|
2020-02-06 10:52:38 +08:00
|
|
|
typedef struct { // expected-warning {{add a tag name}}
|
|
|
|
void bar(); // expected-note {{}}
|
|
|
|
} A; // expected-note {{}}
|
2012-12-31 05:42:26 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void bar(); // expected-warning {{unused member function 'bar'}}
|
|
|
|
} *B;
|
|
|
|
|
|
|
|
struct C {
|
|
|
|
void bar();
|
|
|
|
};
|
|
|
|
}
|
2013-01-03 12:29:20 +08:00
|
|
|
|
|
|
|
namespace pr14776 {
|
|
|
|
namespace {
|
|
|
|
struct X {};
|
|
|
|
}
|
|
|
|
X a = X(); // expected-warning {{unused variable 'a'}}
|
|
|
|
auto b = X(); // expected-warning {{unused variable 'b'}}
|
|
|
|
}
|
2013-09-10 11:05:56 +08:00
|
|
|
|
2013-09-11 05:10:25 +08:00
|
|
|
namespace UndefinedInternalStaticMember {
|
|
|
|
namespace {
|
|
|
|
struct X {
|
|
|
|
static const unsigned x = 3;
|
|
|
|
int y[x];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 09:28:12 +08:00
|
|
|
namespace test8 {
|
|
|
|
static void func();
|
|
|
|
void bar() { void func() __attribute__((used)); }
|
|
|
|
static void func() {}
|
|
|
|
}
|
|
|
|
|
2017-05-09 19:25:41 +08:00
|
|
|
namespace test9 {
|
2020-07-27 00:20:56 +08:00
|
|
|
template <typename T>
|
|
|
|
static void completeRedeclChainForTemplateSpecialization() {} // expected-warning {{unused function template 'completeRedeclChainForTemplateSpecialization'}}
|
2017-05-09 19:25:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace test10 {
|
|
|
|
#if __cplusplus >= 201103L
|
2019-04-26 09:51:08 +08:00
|
|
|
// FIXME: Warn on template definitions with no instantiations?
|
2017-05-09 19:25:41 +08:00
|
|
|
template<class T>
|
2019-04-26 09:51:08 +08:00
|
|
|
constexpr T pi = T(3.14);
|
2017-05-09 19:25:41 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-12 05:25:24 +08:00
|
|
|
namespace pr19713 {
|
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
// FIXME: We should warn on both of these.
|
2020-07-27 00:20:56 +08:00
|
|
|
static constexpr int constexpr3() { return 1; } // expected-warning {{unused function 'constexpr3'}}
|
|
|
|
constexpr int constexpr4() { return 2; }
|
2014-05-12 05:25:24 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:05:56 +08:00
|
|
|
#endif
|