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