11 #include "clang/AST/ASTContext.h" 12 #include "clang/AST/RecursiveASTVisitor.h" 13 #include "clang/ASTMatchers/ASTMatchFinder.h" 14 #include "clang/Lex/Lexer.h" 15 #include <unordered_set> 25 if (
const auto *MD = dyn_cast<CXXMethodDecl>(Function))
26 return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>();
31 void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) {
33 functionDecl(isDefinition(), hasBody(stmt()), hasAnyParameter(decl()))
39 static CharSourceRange
removeNode(
const MatchFinder::MatchResult &Result,
40 const T *PrevNode,
const T *Node,
43 return CharSourceRange::getCharRange(Node->getLocStart(),
44 NextNode->getLocStart());
47 return CharSourceRange::getTokenRange(
48 Lexer::getLocForEndOfToken(PrevNode->getLocEnd(), 0,
49 *Result.SourceManager,
50 Result.Context->getLangOpts()),
53 return CharSourceRange::getTokenRange(Node->getSourceRange());
57 const FunctionDecl *Function,
unsigned Index) {
59 Result, Index > 0 ? Function->getParamDecl(Index - 1) :
nullptr,
60 Function->getParamDecl(Index),
61 Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1)
66 const CallExpr *Call,
unsigned Index) {
68 Result, Index > 0 ? Call->getArg(Index - 1) :
nullptr,
70 Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) :
nullptr));
74 :
public RecursiveASTVisitor<IndexerVisitor> {
78 const std::unordered_set<const CallExpr *> &
80 return Index[Fn->getCanonicalDecl()].Calls;
83 const std::unordered_set<const DeclRefExpr *> &
85 return Index[Fn->getCanonicalDecl()].OtherRefs;
91 if (
const auto *Fn = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
92 Fn = Fn->getCanonicalDecl();
93 Index[Fn].OtherRefs.insert(DeclRef);
100 dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
101 Fn = Fn->getCanonicalDecl();
102 if (
const auto *Ref =
103 dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit())) {
104 Index[Fn].OtherRefs.erase(Ref);
106 Index[Fn].Calls.insert(Call);
113 std::unordered_set<const CallExpr *> Calls;
114 std::unordered_set<const DeclRefExpr *> OtherRefs;
117 std::unordered_map<const FunctionDecl *, IndexEntry> Index;
120 UnusedParametersCheck::~UnusedParametersCheck() =
default;
122 UnusedParametersCheck::UnusedParametersCheck(StringRef
Name,
126 void UnusedParametersCheck::warnOnUnusedParameter(
127 const MatchFinder::MatchResult &Result,
const FunctionDecl *Function,
128 unsigned ParamIndex) {
129 const auto *Param = Function->getParamDecl(ParamIndex);
130 auto MyDiag =
diag(Param->getLocation(),
"parameter %0 is unused") << Param;
133 Indexer = llvm::make_unique<IndexerVisitor>(
134 Result.Context->getTranslationUnitDecl());
138 if (Function->isExternallyVisible() ||
139 !Result.SourceManager->isInMainFile(Function->getLocation()) ||
141 SourceRange RemovalRange(Param->getLocation());
145 MyDiag << FixItHint::CreateReplacement(
146 RemovalRange, (Twine(
" /*") + Param->getName() +
"*/").str());
151 for (
const FunctionDecl *FD : Function->redecls())
152 if (FD->param_size())
156 for (
const auto *Call : Indexer->getFnCalls(Function))
161 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
162 if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation())
164 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function))
165 if (Method->isLambdaStaticInvoker())
167 for (
unsigned i = 0, e = Function->getNumParams(); i != e; ++i) {
168 const auto *Param = Function->getParamDecl(i);
169 if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() ||
170 Param->hasAttr<UnusedAttr>())
172 warnOnUnusedParameter(Result, Function, i);
static bool isOverrideMethod(const CXXMethodDecl *MD)
Finds out if the given method overrides some method.
static FixItHint removeArgument(const MatchFinder::MatchResult &Result, const CallExpr *Call, unsigned Index)
bool shouldTraversePostOrder() const
Base class for all clang-tidy checks.
const std::unordered_set< const CallExpr * > & getFnCalls(const FunctionDecl *Fn)
bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef)
IndexerVisitor(TranslationUnitDecl *Top)
const std::unordered_set< const DeclRefExpr * > & getOtherRefs(const FunctionDecl *Fn)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, const T *PrevNode, const T *Node, const T *NextNode)
bool WalkUpFromCallExpr(CallExpr *Call)
const DeclRefExpr * DeclRef
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
static FixItHint removeParameter(const MatchFinder::MatchResult &Result, const FunctionDecl *Function, unsigned Index)