2016-04-19 21:48:39 +08:00
|
|
|
//===--- UnusedUsingDeclsCheck.cpp - clang-tidy----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UnusedUsingDeclsCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace misc {
|
|
|
|
|
|
|
|
void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
|
2016-04-20 17:48:56 +08:00
|
|
|
auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
|
|
|
|
Finder->addMatcher(loc(recordType(DeclMatcher)), this);
|
|
|
|
Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
|
2016-05-09 21:37:12 +08:00
|
|
|
Finder->addMatcher(declRefExpr().bind("used"), this);
|
2016-05-18 19:49:34 +08:00
|
|
|
Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind("used"))),
|
|
|
|
this);
|
2016-04-19 21:48:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
|
|
|
|
// FIXME: Implement the correct behavior for using declarations with more
|
|
|
|
// than one shadow.
|
|
|
|
if (Using->shadow_size() != 1)
|
|
|
|
return;
|
2016-04-20 16:58:27 +08:00
|
|
|
const auto *TargetDecl =
|
|
|
|
Using->shadow_begin()->getTargetDecl()->getCanonicalDecl();
|
2016-04-19 21:48:39 +08:00
|
|
|
|
2016-05-12 18:00:49 +08:00
|
|
|
// Ignores using-declarations defined in macros.
|
|
|
|
if (TargetDecl->getLocation().isMacroID())
|
|
|
|
return;
|
|
|
|
|
2016-05-09 21:37:12 +08:00
|
|
|
// Ignores using-declarations defined in class definition.
|
|
|
|
if (isa<CXXRecordDecl>(TargetDecl->getDeclContext()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!isa<RecordDecl>(TargetDecl) && !isa<ClassTemplateDecl>(TargetDecl) &&
|
|
|
|
!isa<FunctionDecl>(TargetDecl) && !isa<VarDecl>(TargetDecl) &&
|
|
|
|
!isa<FunctionTemplateDecl>(TargetDecl))
|
2016-04-19 21:48:39 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
FoundDecls[TargetDecl] = Using;
|
|
|
|
FoundRanges[TargetDecl] = CharSourceRange::getCharRange(
|
|
|
|
Using->getLocStart(),
|
|
|
|
Lexer::findLocationAfterToken(
|
|
|
|
Using->getLocEnd(), tok::semi, *Result.SourceManager,
|
|
|
|
Result.Context->getLangOpts(),
|
|
|
|
/*SkipTrailingWhitespaceAndNewLine=*/true));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark using declarations as used by setting FoundDecls' value to zero. As
|
|
|
|
// the AST is walked in order, usages are only marked after a the
|
|
|
|
// corresponding using declaration has been found.
|
|
|
|
// FIXME: This currently doesn't look at whether the type reference is
|
|
|
|
// actually found with the help of the using declaration.
|
|
|
|
if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) {
|
2016-04-20 17:48:56 +08:00
|
|
|
if (const auto *Specialization =
|
|
|
|
dyn_cast<ClassTemplateSpecializationDecl>(Used))
|
|
|
|
Used = Specialization->getSpecializedTemplate();
|
2016-05-09 21:37:12 +08:00
|
|
|
removeFromFoundDecls(Used);
|
|
|
|
return;
|
2016-04-19 21:48:39 +08:00
|
|
|
}
|
2016-05-09 21:37:12 +08:00
|
|
|
|
|
|
|
if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) {
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
|
|
|
|
if (const auto *FDT = FD->getPrimaryTemplate())
|
|
|
|
removeFromFoundDecls(FDT);
|
|
|
|
else
|
|
|
|
removeFromFoundDecls(FD);
|
|
|
|
} else if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
|
|
|
|
removeFromFoundDecls(VD);
|
|
|
|
}
|
|
|
|
}
|
2016-05-18 19:49:34 +08:00
|
|
|
// Check the uninstantiated template function usage.
|
|
|
|
if (const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>("used")) {
|
|
|
|
for (const NamedDecl* ND : ULE->decls()) {
|
|
|
|
if (const auto *USD = dyn_cast<UsingShadowDecl>(ND))
|
|
|
|
removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl());
|
|
|
|
}
|
|
|
|
}
|
2016-05-09 21:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
|
|
|
|
auto I = FoundDecls.find(D->getCanonicalDecl());
|
|
|
|
if (I != FoundDecls.end())
|
|
|
|
I->second = nullptr;
|
2016-04-19 21:48:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
|
|
|
|
for (const auto &FoundDecl : FoundDecls) {
|
|
|
|
if (FoundDecl.second == nullptr)
|
|
|
|
continue;
|
|
|
|
diag(FoundDecl.second->getLocation(), "using decl %0 is unused")
|
|
|
|
<< FoundDecl.second
|
|
|
|
<< FixItHint::CreateRemoval(FoundRanges[FoundDecl.first]);
|
|
|
|
}
|
|
|
|
FoundDecls.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace misc
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|