Fix a wrong check in misc-unused-using-decls

Summary:
We should check whether a UsingDecl is defined in macros or in class
definition, not TargetDecls of the UsingDecl.

Reviewers: alexfh

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D20666

llvm-svn: 271199
This commit is contained in:
Haojian Wu 2016-05-30 07:42:22 +00:00
parent 669b44e857
commit 3b9f4cf462
2 changed files with 17 additions and 13 deletions

View File

@ -18,20 +18,10 @@ namespace clang {
namespace tidy { namespace tidy {
namespace misc { namespace misc {
// A function that helps to tell whether a TargetDecl will be checked. // A function that helps to tell whether a TargetDecl in a UsingDecl will be
// We only check a TargetDecl if : // checked. Only variable, function, function template, class template and class
// * The corresponding UsingDecl is not defined in macros or in class // are considered.
// definitions.
// * Only variable, function and class types are considered.
static bool ShouldCheckDecl(const Decl *TargetDecl) { static bool ShouldCheckDecl(const Decl *TargetDecl) {
// Ignores using-declarations defined in macros.
if (TargetDecl->getLocation().isMacroID())
return false;
// Ignores using-declarations defined in class definition.
if (isa<CXXRecordDecl>(TargetDecl->getDeclContext()))
return false;
return isa<RecordDecl>(TargetDecl) || isa<ClassTemplateDecl>(TargetDecl) || return isa<RecordDecl>(TargetDecl) || isa<ClassTemplateDecl>(TargetDecl) ||
isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl) || isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl) ||
isa<FunctionTemplateDecl>(TargetDecl); isa<FunctionTemplateDecl>(TargetDecl);
@ -49,6 +39,14 @@ void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) { void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) { if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
// Ignores using-declarations defined in macros.
if (Using->getLocation().isMacroID())
return ;
// Ignores using-declarations defined in class definition.
if (isa<CXXRecordDecl>(Using->getDeclContext()))
return ;
UsingDeclContext Context(Using); UsingDeclContext Context(Using);
Context.UsingDeclRange = CharSourceRange::getCharRange( Context.UsingDeclRange = CharSourceRange::getCharRange(
Using->getLocStart(), Using->getLocStart(),

View File

@ -33,6 +33,7 @@ template <typename T> int UnusedTemplateFunc() { return 1; }
template <typename T> int UsedInTemplateFunc() { return 1; } template <typename T> int UsedInTemplateFunc() { return 1; }
void OverloadFunc(int); void OverloadFunc(int);
void OverloadFunc(double); void OverloadFunc(double);
int FuncUsedByUsingDeclInMacro() { return 1; }
class ostream { class ostream {
public: public:
@ -93,6 +94,11 @@ using n::OverloadFunc; // OverloadFunc
DEFINE_INT(test); DEFINE_INT(test);
#undef DEFIND_INT #undef DEFIND_INT
#define USING_FUNC \
using n::FuncUsedByUsingDeclInMacro;
USING_FUNC
#undef USING_FUNC
// ----- Usages ----- // ----- Usages -----
void f(B b); void f(B b);
void g() { void g() {