2015-07-20 09:06:44 +08:00
|
|
|
//===--- UnusedParametersCheck.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 "UnusedParametersCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2015-11-12 02:40:36 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2015-07-20 09:06:44 +08:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2016-05-03 02:00:29 +08:00
|
|
|
namespace misc {
|
|
|
|
|
2016-04-01 15:57:30 +08:00
|
|
|
namespace {
|
|
|
|
bool isOverrideMethod(const FunctionDecl *Function) {
|
|
|
|
if (const auto *MD = dyn_cast<CXXMethodDecl>(Function))
|
|
|
|
return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // namespace
|
2015-07-20 09:06:44 +08:00
|
|
|
|
|
|
|
void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
|
2015-07-27 21:46:37 +08:00
|
|
|
Finder->addMatcher(functionDecl().bind("function"), this);
|
2015-07-20 09:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-11-12 02:40:36 +08:00
|
|
|
template <typename T>
|
|
|
|
static CharSourceRange removeNode(const MatchFinder::MatchResult &Result,
|
|
|
|
const T *PrevNode, const T *Node,
|
|
|
|
const T *NextNode) {
|
|
|
|
if (NextNode)
|
|
|
|
return CharSourceRange::getCharRange(Node->getLocStart(),
|
|
|
|
NextNode->getLocStart());
|
|
|
|
|
|
|
|
if (PrevNode)
|
|
|
|
return CharSourceRange::getTokenRange(
|
|
|
|
Lexer::getLocForEndOfToken(PrevNode->getLocEnd(), 0,
|
|
|
|
*Result.SourceManager,
|
|
|
|
Result.Context->getLangOpts()),
|
|
|
|
Node->getLocEnd());
|
|
|
|
|
|
|
|
return CharSourceRange::getTokenRange(Node->getSourceRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
static FixItHint removeParameter(const MatchFinder::MatchResult &Result,
|
|
|
|
const FunctionDecl *Function, unsigned Index) {
|
|
|
|
return FixItHint::CreateRemoval(removeNode(
|
|
|
|
Result, Index > 0 ? Function->getParamDecl(Index - 1) : nullptr,
|
|
|
|
Function->getParamDecl(Index),
|
|
|
|
Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
|
|
|
|
: nullptr));
|
2015-07-20 11:42:38 +08:00
|
|
|
}
|
|
|
|
|
2015-11-12 02:40:36 +08:00
|
|
|
static FixItHint removeArgument(const MatchFinder::MatchResult &Result,
|
|
|
|
const CallExpr *Call, unsigned Index) {
|
|
|
|
return FixItHint::CreateRemoval(removeNode(
|
|
|
|
Result, Index > 0 ? Call->getArg(Index - 1) : nullptr,
|
|
|
|
Call->getArg(Index),
|
|
|
|
Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) : nullptr));
|
2015-07-20 11:42:38 +08:00
|
|
|
}
|
|
|
|
|
2015-07-27 21:46:37 +08:00
|
|
|
void UnusedParametersCheck::warnOnUnusedParameter(
|
|
|
|
const MatchFinder::MatchResult &Result, const FunctionDecl *Function,
|
|
|
|
unsigned ParamIndex) {
|
|
|
|
const auto *Param = Function->getParamDecl(ParamIndex);
|
2016-04-07 22:55:25 +08:00
|
|
|
auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused") << Param;
|
2015-07-20 09:06:44 +08:00
|
|
|
|
2016-04-01 15:57:30 +08:00
|
|
|
auto DeclRefExpr =
|
|
|
|
declRefExpr(to(equalsNode(Function)),
|
|
|
|
unless(hasAncestor(callExpr(callee(equalsNode(Function))))));
|
2015-07-20 11:42:38 +08:00
|
|
|
|
|
|
|
// Comment out parameter name for non-local functions.
|
2015-07-28 21:19:12 +08:00
|
|
|
if (Function->isExternallyVisible() ||
|
|
|
|
!Result.SourceManager->isInMainFile(Function->getLocation()) ||
|
2016-04-01 15:57:30 +08:00
|
|
|
!ast_matchers::match(DeclRefExpr, *Result.Context).empty() ||
|
|
|
|
isOverrideMethod(Function)) {
|
2015-07-20 11:42:38 +08:00
|
|
|
SourceRange RemovalRange(Param->getLocation(), Param->getLocEnd());
|
2015-07-28 18:39:25 +08:00
|
|
|
// Note: We always add a space before the '/*' to not accidentally create a
|
|
|
|
// '*/*' for pointer types, which doesn't start a comment. clang-format will
|
|
|
|
// clean this up afterwards.
|
2015-07-20 11:42:38 +08:00
|
|
|
MyDiag << FixItHint::CreateReplacement(
|
|
|
|
RemovalRange, (Twine(" /*") + Param->getName() + "*/").str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix all redeclarations.
|
|
|
|
for (const FunctionDecl *FD : Function->redecls())
|
2015-08-14 21:39:57 +08:00
|
|
|
if (FD->param_size())
|
2015-11-12 02:40:36 +08:00
|
|
|
MyDiag << removeParameter(Result, FD, ParamIndex);
|
2015-07-20 11:42:38 +08:00
|
|
|
|
|
|
|
// Fix all call sites.
|
|
|
|
auto CallMatches = ast_matchers::match(
|
|
|
|
decl(forEachDescendant(
|
|
|
|
callExpr(callee(functionDecl(equalsNode(Function)))).bind("x"))),
|
|
|
|
*Result.Context->getTranslationUnitDecl(), *Result.Context);
|
|
|
|
for (const auto &Match : CallMatches)
|
2015-11-12 02:40:36 +08:00
|
|
|
MyDiag << removeArgument(Result, Match.getNodeAs<CallExpr>("x"),
|
|
|
|
ParamIndex);
|
2015-07-20 09:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-07-27 21:46:37 +08:00
|
|
|
void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function");
|
2015-08-10 23:45:46 +08:00
|
|
|
if (!Function->doesThisDeclarationHaveABody() ||
|
2016-02-03 19:33:18 +08:00
|
|
|
!Function->hasWrittenPrototype() ||
|
|
|
|
Function->isTemplateInstantiation())
|
2015-07-27 21:46:37 +08:00
|
|
|
return;
|
2015-09-22 17:20:20 +08:00
|
|
|
if (const auto *Method = dyn_cast<CXXMethodDecl>(Function))
|
|
|
|
if (Method->isLambdaStaticInvoker())
|
|
|
|
return;
|
2015-07-27 21:46:37 +08:00
|
|
|
for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
|
|
|
|
const auto *Param = Function->getParamDecl(i);
|
|
|
|
if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
|
|
|
|
Param->hasAttr<UnusedAttr>())
|
|
|
|
continue;
|
|
|
|
warnOnUnusedParameter(Result, Function, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 02:00:29 +08:00
|
|
|
} // namespace misc
|
2015-07-20 09:06:44 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|