Daniel Jasper | 9c6df88 | 2015-07-20 01:06:44 +0000 | [diff] [blame] | 1 | //===--- UnusedParametersCheck.cpp - clang-tidy----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "UnusedParametersCheck.h" |
| 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Samuel Benzaquen | b439627 | 2015-11-11 18:40:36 +0000 | [diff] [blame] | 13 | #include "clang/Lex/Lexer.h" |
Daniel Jasper | 9c6df88 | 2015-07-20 01:06:44 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
| 19 | |
| 20 | void UnusedParametersCheck::registerMatchers(MatchFinder *Finder) { |
Daniel Jasper | 9fe55a32 | 2015-07-27 13:46:37 +0000 | [diff] [blame] | 21 | Finder->addMatcher(functionDecl().bind("function"), this); |
Daniel Jasper | 9c6df88 | 2015-07-20 01:06:44 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Samuel Benzaquen | b439627 | 2015-11-11 18:40:36 +0000 | [diff] [blame] | 24 | template <typename T> |
| 25 | static CharSourceRange removeNode(const MatchFinder::MatchResult &Result, |
| 26 | const T *PrevNode, const T *Node, |
| 27 | const T *NextNode) { |
| 28 | if (NextNode) |
| 29 | return CharSourceRange::getCharRange(Node->getLocStart(), |
| 30 | NextNode->getLocStart()); |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 31 | |
Samuel Benzaquen | b439627 | 2015-11-11 18:40:36 +0000 | [diff] [blame] | 32 | if (PrevNode) |
| 33 | return CharSourceRange::getTokenRange( |
| 34 | Lexer::getLocForEndOfToken(PrevNode->getLocEnd(), 0, |
| 35 | *Result.SourceManager, |
| 36 | Result.Context->getLangOpts()), |
| 37 | Node->getLocEnd()); |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 38 | |
Samuel Benzaquen | b439627 | 2015-11-11 18:40:36 +0000 | [diff] [blame] | 39 | return CharSourceRange::getTokenRange(Node->getSourceRange()); |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Samuel Benzaquen | b439627 | 2015-11-11 18:40:36 +0000 | [diff] [blame] | 42 | static FixItHint removeParameter(const MatchFinder::MatchResult &Result, |
| 43 | const FunctionDecl *Function, unsigned Index) { |
| 44 | return FixItHint::CreateRemoval(removeNode( |
| 45 | Result, Index > 0 ? Function->getParamDecl(Index - 1) : nullptr, |
| 46 | Function->getParamDecl(Index), |
| 47 | Index + 1 < Function->getNumParams() ? Function->getParamDecl(Index + 1) |
| 48 | : nullptr)); |
| 49 | } |
| 50 | |
| 51 | static FixItHint removeArgument(const MatchFinder::MatchResult &Result, |
| 52 | const CallExpr *Call, unsigned Index) { |
| 53 | return FixItHint::CreateRemoval(removeNode( |
| 54 | Result, Index > 0 ? Call->getArg(Index - 1) : nullptr, |
| 55 | Call->getArg(Index), |
| 56 | Index + 1 < Call->getNumArgs() ? Call->getArg(Index + 1) : nullptr)); |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Daniel Jasper | 9fe55a32 | 2015-07-27 13:46:37 +0000 | [diff] [blame] | 59 | void UnusedParametersCheck::warnOnUnusedParameter( |
| 60 | const MatchFinder::MatchResult &Result, const FunctionDecl *Function, |
| 61 | unsigned ParamIndex) { |
| 62 | const auto *Param = Function->getParamDecl(ParamIndex); |
Daniel Jasper | 9c6df88 | 2015-07-20 01:06:44 +0000 | [diff] [blame] | 63 | auto MyDiag = diag(Param->getLocation(), "parameter '%0' is unused") |
| 64 | << Param->getName(); |
| 65 | |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 66 | auto UsedByRef = [&] { |
| 67 | return !ast_matchers::match( |
| 68 | decl(hasDescendant( |
| 69 | declRefExpr(to(equalsNode(Function)), |
| 70 | unless(hasAncestor( |
| 71 | callExpr(callee(equalsNode(Function)))))))), |
| 72 | *Result.Context->getTranslationUnitDecl(), *Result.Context) |
| 73 | .empty(); |
| 74 | }; |
| 75 | |
| 76 | // Comment out parameter name for non-local functions. |
Daniel Jasper | 542482e | 2015-07-28 13:19:12 +0000 | [diff] [blame] | 77 | if (Function->isExternallyVisible() || |
| 78 | !Result.SourceManager->isInMainFile(Function->getLocation()) || |
| 79 | UsedByRef()) { |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 80 | SourceRange RemovalRange(Param->getLocation(), Param->getLocEnd()); |
Daniel Jasper | 718029b | 2015-07-28 10:39:25 +0000 | [diff] [blame] | 81 | // Note: We always add a space before the '/*' to not accidentally create a |
| 82 | // '*/*' for pointer types, which doesn't start a comment. clang-format will |
| 83 | // clean this up afterwards. |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 84 | MyDiag << FixItHint::CreateReplacement( |
| 85 | RemovalRange, (Twine(" /*") + Param->getName() + "*/").str()); |
| 86 | return; |
| 87 | } |
| 88 | |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 89 | // Fix all redeclarations. |
| 90 | for (const FunctionDecl *FD : Function->redecls()) |
Daniel Jasper | ea223a7 | 2015-08-14 13:39:57 +0000 | [diff] [blame] | 91 | if (FD->param_size()) |
Samuel Benzaquen | b439627 | 2015-11-11 18:40:36 +0000 | [diff] [blame] | 92 | MyDiag << removeParameter(Result, FD, ParamIndex); |
Daniel Jasper | 82efabb | 2015-07-20 03:42:38 +0000 | [diff] [blame] | 93 | |
| 94 | // Fix all call sites. |
| 95 | auto CallMatches = ast_matchers::match( |
| 96 | decl(forEachDescendant( |
| 97 | callExpr(callee(functionDecl(equalsNode(Function)))).bind("x"))), |
| 98 | *Result.Context->getTranslationUnitDecl(), *Result.Context); |
| 99 | for (const auto &Match : CallMatches) |
Samuel Benzaquen | b439627 | 2015-11-11 18:40:36 +0000 | [diff] [blame] | 100 | MyDiag << removeArgument(Result, Match.getNodeAs<CallExpr>("x"), |
| 101 | ParamIndex); |
Daniel Jasper | 9c6df88 | 2015-07-20 01:06:44 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Daniel Jasper | 9fe55a32 | 2015-07-27 13:46:37 +0000 | [diff] [blame] | 104 | void UnusedParametersCheck::check(const MatchFinder::MatchResult &Result) { |
| 105 | const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("function"); |
Daniel Jasper | b3a74c6 | 2015-08-10 15:45:46 +0000 | [diff] [blame] | 106 | if (!Function->doesThisDeclarationHaveABody() || |
Daniel Jasper | 3a6e81c | 2016-02-03 11:33:18 +0000 | [diff] [blame^] | 107 | !Function->hasWrittenPrototype() || |
| 108 | Function->isTemplateInstantiation()) |
Daniel Jasper | 9fe55a32 | 2015-07-27 13:46:37 +0000 | [diff] [blame] | 109 | return; |
Daniel Jasper | f631191 | 2015-09-22 09:20:20 +0000 | [diff] [blame] | 110 | if (const auto *Method = dyn_cast<CXXMethodDecl>(Function)) |
| 111 | if (Method->isLambdaStaticInvoker()) |
| 112 | return; |
Daniel Jasper | 9fe55a32 | 2015-07-27 13:46:37 +0000 | [diff] [blame] | 113 | for (unsigned i = 0, e = Function->getNumParams(); i != e; ++i) { |
| 114 | const auto *Param = Function->getParamDecl(i); |
| 115 | if (Param->isUsed() || Param->isReferenced() || !Param->getDeclName() || |
| 116 | Param->hasAttr<UnusedAttr>()) |
| 117 | continue; |
| 118 | warnOnUnusedParameter(Result, Function, i); |
| 119 | } |
| 120 | } |
| 121 | |
Daniel Jasper | 9c6df88 | 2015-07-20 01:06:44 +0000 | [diff] [blame] | 122 | } // namespace tidy |
| 123 | } // namespace clang |