Benjamin Kramer | 14d42d9 | 2014-07-15 16:47:09 +0000 | [diff] [blame] | 1 | //===--- NamedParameterCheck.cpp - clang-tidy -------------------*- C++ -*-===// |
| 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 "NamedParameterCheck.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 13 | #include "clang/AST/ASTContext.h" |
| 14 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
| 19 | namespace readability { |
| 20 | |
| 21 | void NamedParameterCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { |
| 22 | Finder->addMatcher( |
| 23 | functionDecl( |
| 24 | unless(hasAncestor(decl( |
| 25 | anyOf(recordDecl(ast_matchers::isTemplateInstantiation()), |
| 26 | functionDecl(ast_matchers::isTemplateInstantiation())))))) |
| 27 | .bind("decl"), |
| 28 | this); |
| 29 | } |
| 30 | |
| 31 | void NamedParameterCheck::check(const MatchFinder::MatchResult &Result) { |
| 32 | const SourceManager &SM = *Result.SourceManager; |
| 33 | const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("decl"); |
| 34 | SmallVector<std::pair<const FunctionDecl *, unsigned>, 4> UnnamedParams; |
| 35 | |
| 36 | // Ignore implicitly generated members. |
| 37 | if (Function->isImplicit()) |
| 38 | return; |
| 39 | |
Benjamin Kramer | 610ba53 | 2014-08-04 09:33:58 +0000 | [diff] [blame] | 40 | // Ignore declarations without a definition if we're not dealing with an |
| 41 | // overriden method. |
| 42 | const FunctionDecl *Definition = nullptr; |
Benjamin Kramer | e59cd6e | 2014-08-29 08:58:35 +0000 | [diff] [blame] | 43 | if ((!Function->isDefined(Definition) || Function->isDefaulted() || |
| 44 | Function->isDeleted()) && |
Benjamin Kramer | 610ba53 | 2014-08-04 09:33:58 +0000 | [diff] [blame] | 45 | (!isa<CXXMethodDecl>(Function) || |
| 46 | cast<CXXMethodDecl>(Function)->size_overridden_methods() == 0)) |
| 47 | return; |
| 48 | |
Benjamin Kramer | 14d42d9 | 2014-07-15 16:47:09 +0000 | [diff] [blame] | 49 | // TODO: Handle overloads. |
| 50 | // TODO: We could check that all redeclarations use the same name for |
| 51 | // arguments in the same position. |
| 52 | for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) { |
| 53 | const ParmVarDecl *Parm = Function->getParamDecl(I); |
| 54 | // Look for unnamed parameters. |
| 55 | if (!Parm->getName().empty()) |
| 56 | continue; |
| 57 | |
| 58 | // Sanity check the source locations. |
| 59 | if (!Parm->getLocation().isValid() || Parm->getLocation().isMacroID() || |
| 60 | !SM.isWrittenInSameFile(Parm->getLocStart(), Parm->getLocation())) |
| 61 | continue; |
| 62 | |
| 63 | // Look for comments. We explicitly want to allow idioms like |
| 64 | // void foo(int /*unused*/) |
| 65 | const char *Begin = SM.getCharacterData(Parm->getLocStart()); |
| 66 | const char *End = SM.getCharacterData(Parm->getLocation()); |
| 67 | StringRef Data(Begin, End - Begin); |
| 68 | if (Data.find("/*") != StringRef::npos) |
| 69 | continue; |
| 70 | |
| 71 | UnnamedParams.push_back(std::make_pair(Function, I)); |
| 72 | } |
| 73 | |
| 74 | // Emit only one warning per function but fixits for all unnamed parameters. |
| 75 | if (!UnnamedParams.empty()) { |
| 76 | const ParmVarDecl *FirstParm = |
| 77 | UnnamedParams.front().first->getParamDecl(UnnamedParams.front().second); |
| 78 | auto D = diag(FirstParm->getLocation(), |
| 79 | "all parameters should be named in a function"); |
| 80 | |
| 81 | for (auto P : UnnamedParams) { |
Benjamin Kramer | 78cd546 | 2014-08-04 09:42:18 +0000 | [diff] [blame] | 82 | // Fallback to an unused marker. |
| 83 | StringRef NewName = "unused"; |
| 84 | |
Benjamin Kramer | 14d42d9 | 2014-07-15 16:47:09 +0000 | [diff] [blame] | 85 | // If the method is overridden, try to copy the name from the base method |
| 86 | // into the overrider. |
Benjamin Kramer | 14d42d9 | 2014-07-15 16:47:09 +0000 | [diff] [blame] | 87 | const auto *M = dyn_cast<CXXMethodDecl>(P.first); |
| 88 | if (M && M->size_overridden_methods() > 0) { |
| 89 | const ParmVarDecl *OtherParm = |
| 90 | (*M->begin_overridden_methods())->getParamDecl(P.second); |
Benjamin Kramer | 610ba53 | 2014-08-04 09:33:58 +0000 | [diff] [blame] | 91 | StringRef Name = OtherParm->getName(); |
| 92 | if (!Name.empty()) |
| 93 | NewName = Name; |
Benjamin Kramer | 14d42d9 | 2014-07-15 16:47:09 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Benjamin Kramer | 610ba53 | 2014-08-04 09:33:58 +0000 | [diff] [blame] | 96 | // If the definition has a named parameter use that name. |
| 97 | if (Definition) { |
| 98 | const ParmVarDecl *DefParm = Definition->getParamDecl(P.second); |
| 99 | StringRef Name = DefParm->getName(); |
| 100 | if (!Name.empty()) |
| 101 | NewName = Name; |
| 102 | } |
| 103 | |
| 104 | // Now insert the comment. Note that getLocation() points to the place |
| 105 | // where the name would be, this allows us to also get complex cases like |
| 106 | // function pointers right. |
| 107 | const ParmVarDecl *Parm = P.first->getParamDecl(P.second); |
| 108 | D << FixItHint::CreateInsertion(Parm->getLocation(), |
| 109 | " /*" + NewName.str() + "*/"); |
Benjamin Kramer | 14d42d9 | 2014-07-15 16:47:09 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } // namespace readability |
| 115 | } // namespace tidy |
| 116 | } // namespace clang |