| 1 | //===--- UnusedParametersCheck.cpp - clang-tidy----------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "UnusedParametersCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/AST/ASTLambda.h" |
| 12 | #include "clang/AST/Attr.h" |
| 13 | #include "clang/AST/Decl.h" |
| 14 | #include "clang/AST/RecursiveASTVisitor.h" |
| 15 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Lex/Lexer.h" |
| 18 | #include <unordered_map> |
| 19 | #include <unordered_set> |
| 20 | |
| 21 | using namespace clang::ast_matchers; |
| 22 | |
| 23 | namespace clang::tidy::misc { |
| 24 | |
| 25 | namespace { |
| 26 | bool isOverrideMethod(const FunctionDecl *Function) { |
| 27 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Function)) |
| 28 | return MD->size_overridden_methods() > 0 || MD->hasAttr<OverrideAttr>(); |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | bool hasAttrAfterParam(const SourceManager *SourceManager, |
| 33 | const ParmVarDecl *Param) { |
| 34 | for (const auto *Attr : Param->attrs()) { |
| 35 | if (SourceManager->isBeforeInTranslationUnit(Param->getLocation(), |
| 36 | Attr->getLocation())) { |
| 37 | return true; |
| 38 | } |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | } // namespace |
| 43 | |
| 44 | void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) { |
| 45 | Finder->addMatcher(functionDecl(isDefinition(), hasBody(stmt()), |
| 46 | hasAnyParameter(decl()), |
| 47 | unless(hasAttr(attr::Kind::Naked))) |
| 48 | .bind("function" ), |
| 49 | this); |
| 50 | } |
| 51 | |
| 52 | template <typename T> |
| 53 | static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, |
| 54 | const T *PrevNode, const T *Node, |
| 55 | const T *NextNode) { |
| 56 | if (NextNode) |
| 57 | return CharSourceRange::getCharRange(Node->getBeginLoc(), |
| 58 | NextNode->getBeginLoc()); |
| 59 | |
| 60 | if (PrevNode) |
| 61 | return CharSourceRange::getTokenRange( |
| 62 | Lexer::getLocForEndOfToken(Loc: PrevNode->getEndLoc(), Offset: 0, |
| 63 | SM: *Result.SourceManager, |
| 64 | LangOpts: Result.Context->getLangOpts()), |
| 65 | Node->getEndLoc()); |
| 66 | |
| 67 | return CharSourceRange::getTokenRange(Node->getSourceRange()); |
| 68 | } |
| 69 | |
| 70 | static FixItHint removeParameter(const MatchFinder::MatchResult &Result, |
| 71 | const FunctionDecl *Function, unsigned Index) { |
| 72 | return FixItHint::CreateRemoval(RemoveRange: removeNode( |
| 73 | Result, PrevNode: Index > 0 ? Function->getParamDecl(i: Index - 1) : nullptr, |
| 74 | Node: Function->getParamDecl(i: Index), |
| 75 | NextNode: Index + 1 < Function->getNumParams() ? Function->getParamDecl(i: Index + 1) |
| 76 | : nullptr)); |
| 77 | } |
| 78 | |
| 79 | static FixItHint removeArgument(const MatchFinder::MatchResult &Result, |
| 80 | const CallExpr *Call, unsigned Index) { |
| 81 | return FixItHint::CreateRemoval(RemoveRange: removeNode( |
| 82 | Result, PrevNode: Index > 0 ? Call->getArg(Arg: Index - 1) : nullptr, |
| 83 | Node: Call->getArg(Arg: Index), |
| 84 | NextNode: Index + 1 < Call->getNumArgs() ? Call->getArg(Arg: Index + 1) : nullptr)); |
| 85 | } |
| 86 | |
| 87 | class UnusedParametersCheck::IndexerVisitor |
| 88 | : public RecursiveASTVisitor<IndexerVisitor> { |
| 89 | public: |
| 90 | IndexerVisitor(ASTContext &Ctx) { TraverseAST(AST&: Ctx); } |
| 91 | |
| 92 | const std::unordered_set<const CallExpr *> & |
| 93 | getFnCalls(const FunctionDecl *Fn) { |
| 94 | return Index[Fn->getCanonicalDecl()].Calls; |
| 95 | } |
| 96 | |
| 97 | const std::unordered_set<const DeclRefExpr *> & |
| 98 | getOtherRefs(const FunctionDecl *Fn) { |
| 99 | return Index[Fn->getCanonicalDecl()].OtherRefs; |
| 100 | } |
| 101 | |
| 102 | bool shouldTraversePostOrder() const { return true; } |
| 103 | |
| 104 | bool WalkUpFromDeclRefExpr(DeclRefExpr *DeclRef) { |
| 105 | if (const auto *Fn = dyn_cast<FunctionDecl>(Val: DeclRef->getDecl())) { |
| 106 | Fn = Fn->getCanonicalDecl(); |
| 107 | Index[Fn].OtherRefs.insert(x: DeclRef); |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | bool WalkUpFromCallExpr(CallExpr *Call) { |
| 113 | if (const auto *Fn = |
| 114 | dyn_cast_or_null<FunctionDecl>(Val: Call->getCalleeDecl())) { |
| 115 | Fn = Fn->getCanonicalDecl(); |
| 116 | if (const auto *Ref = |
| 117 | dyn_cast<DeclRefExpr>(Val: Call->getCallee()->IgnoreImplicit())) { |
| 118 | Index[Fn].OtherRefs.erase(x: Ref); |
| 119 | } |
| 120 | Index[Fn].Calls.insert(x: Call); |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | struct IndexEntry { |
| 127 | std::unordered_set<const CallExpr *> Calls; |
| 128 | std::unordered_set<const DeclRefExpr *> OtherRefs; |
| 129 | }; |
| 130 | |
| 131 | std::unordered_map<const FunctionDecl *, IndexEntry> Index; |
| 132 | }; |
| 133 | |
| 134 | UnusedParametersCheck::~UnusedParametersCheck() = default; |
| 135 | |
| 136 | UnusedParametersCheck::UnusedParametersCheck(StringRef Name, |
| 137 | ClangTidyContext *Context) |
| 138 | : ClangTidyCheck(Name, Context), |
| 139 | StrictMode(Options.getLocalOrGlobal(LocalName: "StrictMode" , Default: false)), |
| 140 | IgnoreVirtual(Options.get(LocalName: "IgnoreVirtual" , Default: false)) {} |
| 141 | |
| 142 | void UnusedParametersCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 143 | Options.store(Options&: Opts, LocalName: "StrictMode" , Value: StrictMode); |
| 144 | Options.store(Options&: Opts, LocalName: "IgnoreVirtual" , Value: IgnoreVirtual); |
| 145 | } |
| 146 | |
| 147 | void UnusedParametersCheck::warnOnUnusedParameter( |
| 148 | const MatchFinder::MatchResult &Result, const FunctionDecl *Function, |
| 149 | unsigned ParamIndex) { |
| 150 | const auto *Param = Function->getParamDecl(i: ParamIndex); |
| 151 | // Don't bother to diagnose invalid parameters as being unused. |
| 152 | if (Param->isInvalidDecl()) |
| 153 | return; |
| 154 | auto MyDiag = diag(Param->getLocation(), "parameter %0 is unused" ) << Param; |
| 155 | |
| 156 | if (!Indexer) { |
| 157 | Indexer = std::make_unique<IndexerVisitor>(args&: *Result.Context); |
| 158 | } |
| 159 | |
| 160 | // Cannot remove parameter for non-local functions. |
| 161 | if (Function->isExternallyVisible() || |
| 162 | !Result.SourceManager->isInMainFile(Loc: Function->getLocation()) || |
| 163 | !Indexer->getOtherRefs(Fn: Function).empty() || isOverrideMethod(Function) || |
| 164 | isLambdaCallOperator(Function)) { |
| 165 | |
| 166 | // It is illegal to omit parameter name here in C code, so early-out. |
| 167 | if (!Result.Context->getLangOpts().CPlusPlus) |
| 168 | return; |
| 169 | |
| 170 | SourceRange RemovalRange(Param->getLocation()); |
| 171 | // Note: We always add a space before the '/*' to not accidentally create |
| 172 | // a '*/*' for pointer types, which doesn't start a comment. clang-format |
| 173 | // will clean this up afterwards. |
| 174 | MyDiag << FixItHint::CreateReplacement( |
| 175 | RemovalRange, (Twine(" /*" ) + Param->getName() + "*/" ).str()); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | // Fix all redeclarations. |
| 180 | for (const FunctionDecl *FD : Function->redecls()) |
| 181 | if (FD->param_size()) |
| 182 | MyDiag << removeParameter(Result, FD, ParamIndex); |
| 183 | |
| 184 | // Fix all call sites. |
| 185 | for (const CallExpr *Call : Indexer->getFnCalls(Fn: Function)) |
| 186 | if (ParamIndex < Call->getNumArgs()) // See PR38055 for example. |
| 187 | MyDiag << removeArgument(Result, Call, Index: ParamIndex); |
| 188 | } |
| 189 | |
| 190 | void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) { |
| 191 | const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>(ID: "function" ); |
| 192 | if (!Function->hasWrittenPrototype() || Function->isTemplateInstantiation()) |
| 193 | return; |
| 194 | if (const auto *Method = dyn_cast<CXXMethodDecl>(Val: Function)) { |
| 195 | if (IgnoreVirtual && Method->isVirtual()) |
| 196 | return; |
| 197 | if (Method->isLambdaStaticInvoker()) |
| 198 | return; |
| 199 | } |
| 200 | for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) { |
| 201 | const auto *Param = Function->getParamDecl(i: I); |
| 202 | if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() || |
| 203 | Param->hasAttr<UnusedAttr>()) |
| 204 | continue; |
| 205 | if (hasAttrAfterParam(SourceManager: Result.SourceManager, Param)) { |
| 206 | // Due to how grammar works, attributes would be wrongly applied to the |
| 207 | // type if we remove the preceding parameter name. |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | // In non-strict mode ignore function definitions with empty bodies |
| 212 | // (constructor initializer counts for non-empty body). |
| 213 | if (StrictMode || !Function->getBody()->children().empty() || |
| 214 | (isa<CXXConstructorDecl>(Val: Function) && |
| 215 | cast<CXXConstructorDecl>(Val: Function)->getNumCtorInitializers() > 0)) |
| 216 | warnOnUnusedParameter(Result, Function, ParamIndex: I); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } // namespace clang::tidy::misc |
| 221 | |