Add -Wunused-local-typedef, a warning that finds unused local typedefs.
The warning warns on TypedefNameDecls -- typedefs and C++11 using aliases --
that are !isReferenced(). Since the isReferenced() bit on TypedefNameDecls
wasn't used for anything before this warning it wasn't always set correctly,
so this patch also adds a few missing MarkAnyDeclReferenced() calls in
various places for TypedefNameDecls.
This is made a bit complicated due to local typedefs possibly being used only
after their local scope has closed. Consider:
template <class T>
void template_fun(T t) {
typename T::Foo s3foo; // YYY
(void)s3foo;
}
void template_fun_user() {
struct Local {
typedef int Foo; // XXX
} p;
template_fun(p);
}
Here the typedef in XXX is only used at end-of-translation unit, when YYY in
template_fun() gets instantiated. To handle this, typedefs that are unused when
their scope exits are added to a set of potentially unused typedefs, and that
set gets checked at end-of-TU. Typedefs that are still unused at that point then
get warned on. There's also serialization code for this set, so that the
warning works with precompiled headers and modules. For modules, the warning
is emitted when the module is built, for precompiled headers each time the
header gets used.
Finally, consider a function using C++14 auto return types to return a local
type defined in a header:
auto f() {
struct S { typedef int a; };
return S();
}
Here, the typedef escapes its local scope and could be used by only some
translation units including the header. To not warn on this, add a
RecursiveASTVisitor that marks all delcs on local types returned from auto
functions as referenced. (Except if it's a function with internal linkage, or
the decls are private and the local type has no friends -- in these cases, it
_is_ safe to warn.)
Several of the included testcases (most of the interesting ones) were provided
by Richard Smith.
(gcc's spelling -Wunused-local-typedefs is supported as an alias for this
warning.)
llvm-svn: 217298
2014-09-06 09:25:55 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -Wno-unused-local-typedefs -Wno-c++11-extensions -std=c++98 %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -Wunused -Wunused-member-function -Wno-unused-local-typedefs -std=c++11 %s
|
2010-08-15 09:15:20 +08:00
|
|
|
|
2013-09-10 11:05:56 +08:00
|
|
|
#ifdef HEADER
|
|
|
|
|
|
|
|
static void headerstatic() {} // expected-warning{{unused}}
|
|
|
|
static inline void headerstaticinline() {}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void headeranon() {} // expected-warning{{unused}}
|
|
|
|
inline void headerinlineanon() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2010-08-15 09:15:20 +08:00
|
|
|
static void f1(); // expected-warning{{unused}}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void f2(); // expected-warning{{unused}}
|
|
|
|
|
|
|
|
void f3() { } // expected-warning{{unused}}
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
void m1() { } // expected-warning{{unused}}
|
|
|
|
void m2(); // expected-warning{{unused}}
|
|
|
|
void m3();
|
2010-08-15 18:17:33 +08:00
|
|
|
S(const S&);
|
|
|
|
void operator=(const S&);
|
2010-08-15 09:15:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct TS {
|
|
|
|
void m();
|
|
|
|
};
|
|
|
|
template <> void TS<int>::m() { } // expected-warning{{unused}}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void tf() { }
|
|
|
|
template <> void tf<int>() { } // expected-warning{{unused}}
|
|
|
|
|
|
|
|
struct VS {
|
|
|
|
virtual void vm() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SVS : public VS {
|
|
|
|
void vm() { }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void S::m3() { } // expected-warning{{unused}}
|
|
|
|
|
2013-09-10 11:05:56 +08:00
|
|
|
static inline void f4() { } // expected-warning{{unused}}
|
|
|
|
const unsigned int cx = 0; // expected-warning{{unused}}
|
|
|
|
const unsigned int cy = 0;
|
|
|
|
int f5() { return cy; }
|
2010-08-15 09:15:20 +08:00
|
|
|
|
|
|
|
static int x1; // expected-warning{{unused}}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int x2; // expected-warning{{unused}}
|
|
|
|
|
|
|
|
struct S2 {
|
|
|
|
static int x; // expected-warning{{unused}}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct TS2 {
|
|
|
|
static int x;
|
|
|
|
};
|
|
|
|
template <> int TS2<int>::x; // expected-warning{{unused}}
|
|
|
|
}
|
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 {}; }
|
|
|
|
|
|
|
|
void test(A a); // expected-warning {{unused function}}
|
|
|
|
extern "C" void test4(A a);
|
|
|
|
}
|
2011-04-20 03:51:10 +08:00
|
|
|
|
|
|
|
namespace rdar8733476 {
|
|
|
|
static void foo() { } // expected-warning {{not needed and will not be emitted}}
|
|
|
|
|
|
|
|
template <int>
|
|
|
|
void bar() {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
}
|
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);
|
2013-09-10 11:05:56 +08:00
|
|
|
static const double d = 0.0; // expected-warning{{not needed and will not be emitted}}
|
2012-10-28 12:47:21 +08:00
|
|
|
int y = sizeof(d);
|
|
|
|
}
|
2012-11-14 09:52:05 +08:00
|
|
|
|
|
|
|
namespace unused_nested {
|
|
|
|
class outer {
|
|
|
|
void func1();
|
|
|
|
struct {
|
|
|
|
void func2() {
|
|
|
|
}
|
|
|
|
} x;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace unused {
|
|
|
|
struct {
|
|
|
|
void func() { // expected-warning {{unused member function}}
|
|
|
|
}
|
|
|
|
} x; // expected-warning {{unused variable}}
|
|
|
|
}
|
2012-12-31 05:42:26 +08:00
|
|
|
|
|
|
|
namespace test6 {
|
|
|
|
typedef struct {
|
|
|
|
void bar();
|
|
|
|
} A;
|
|
|
|
|
|
|
|
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() {}
|
|
|
|
}
|
|
|
|
|
2014-05-12 05:25:24 +08:00
|
|
|
namespace pr19713 {
|
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
// FIXME: We should warn on both of these.
|
|
|
|
static constexpr int constexpr3() { return 1; } // expected-warning {{unused}}
|
|
|
|
constexpr int constexpr4() { return 2; }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:05:56 +08:00
|
|
|
#endif
|