blob: ffdc813beeb8b9ee8f47d3a4ae1f9d2881b2cec9 [file] [log] [blame]
Benjamin Kramer14d42d92014-07-15 16:47:09 +00001//===--- 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"
Chandler Carruth3cbd71c2015-01-14 11:24:38 +000011#include "clang/AST/ASTContext.h"
Benjamin Kramer14d42d92014-07-15 16:47:09 +000012#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
Benjamin Kramer14d42d92014-07-15 16:47:09 +000014
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
19namespace readability {
20
21void NamedParameterCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
Benjamin Kramer6bcbe6e2014-09-03 13:30:28 +000022 Finder->addMatcher(functionDecl(unless(isInstantiated())).bind("decl"), this);
Benjamin Kramer14d42d92014-07-15 16:47:09 +000023}
24
25void NamedParameterCheck::check(const MatchFinder::MatchResult &Result) {
26 const SourceManager &SM = *Result.SourceManager;
27 const auto *Function = Result.Nodes.getNodeAs<FunctionDecl>("decl");
28 SmallVector<std::pair<const FunctionDecl *, unsigned>, 4> UnnamedParams;
29
30 // Ignore implicitly generated members.
31 if (Function->isImplicit())
32 return;
33
Benjamin Kramer610ba532014-08-04 09:33:58 +000034 // Ignore declarations without a definition if we're not dealing with an
35 // overriden method.
36 const FunctionDecl *Definition = nullptr;
Benjamin Kramere59cd6e2014-08-29 08:58:35 +000037 if ((!Function->isDefined(Definition) || Function->isDefaulted() ||
38 Function->isDeleted()) &&
Benjamin Kramer610ba532014-08-04 09:33:58 +000039 (!isa<CXXMethodDecl>(Function) ||
40 cast<CXXMethodDecl>(Function)->size_overridden_methods() == 0))
41 return;
42
Benjamin Kramer14d42d92014-07-15 16:47:09 +000043 // TODO: Handle overloads.
44 // TODO: We could check that all redeclarations use the same name for
45 // arguments in the same position.
46 for (unsigned I = 0, E = Function->getNumParams(); I != E; ++I) {
47 const ParmVarDecl *Parm = Function->getParamDecl(I);
Alexander Kornienko272397b2015-11-06 00:19:21 +000048 if (Parm->isImplicit())
49 continue;
Benjamin Kramer14d42d92014-07-15 16:47:09 +000050 // Look for unnamed parameters.
51 if (!Parm->getName().empty())
52 continue;
53
Benjamin Kramerfec67052014-09-01 09:11:48 +000054 // Don't warn on the dummy argument on post-inc and post-dec operators.
55 if ((Function->getOverloadedOperator() == OO_PlusPlus ||
56 Function->getOverloadedOperator() == OO_MinusMinus) &&
57 Parm->getType()->isSpecificBuiltinType(BuiltinType::Int))
58 continue;
59
Benjamin Kramer14d42d92014-07-15 16:47:09 +000060 // Sanity check the source locations.
61 if (!Parm->getLocation().isValid() || Parm->getLocation().isMacroID() ||
62 !SM.isWrittenInSameFile(Parm->getLocStart(), Parm->getLocation()))
63 continue;
64
Alexander Kornienkof5a57c92014-09-25 08:16:24 +000065 // Skip gmock testing::Unused parameters.
66 if (auto Typedef = Parm->getType()->getAs<clang::TypedefType>())
67 if (Typedef->getDecl()->getQualifiedNameAsString() == "testing::Unused")
68 continue;
69
Alexander Kornienkoe20ce072014-11-05 11:08:39 +000070 // Skip std::nullptr_t.
71 if (Parm->getType().getCanonicalType()->isNullPtrType())
72 continue;
73
Benjamin Kramer14d42d92014-07-15 16:47:09 +000074 // Look for comments. We explicitly want to allow idioms like
75 // void foo(int /*unused*/)
76 const char *Begin = SM.getCharacterData(Parm->getLocStart());
77 const char *End = SM.getCharacterData(Parm->getLocation());
78 StringRef Data(Begin, End - Begin);
79 if (Data.find("/*") != StringRef::npos)
80 continue;
81
82 UnnamedParams.push_back(std::make_pair(Function, I));
83 }
84
85 // Emit only one warning per function but fixits for all unnamed parameters.
86 if (!UnnamedParams.empty()) {
87 const ParmVarDecl *FirstParm =
88 UnnamedParams.front().first->getParamDecl(UnnamedParams.front().second);
89 auto D = diag(FirstParm->getLocation(),
90 "all parameters should be named in a function");
91
92 for (auto P : UnnamedParams) {
Benjamin Kramer78cd5462014-08-04 09:42:18 +000093 // Fallback to an unused marker.
94 StringRef NewName = "unused";
95
Benjamin Kramer14d42d92014-07-15 16:47:09 +000096 // If the method is overridden, try to copy the name from the base method
97 // into the overrider.
Benjamin Kramer14d42d92014-07-15 16:47:09 +000098 const auto *M = dyn_cast<CXXMethodDecl>(P.first);
99 if (M && M->size_overridden_methods() > 0) {
100 const ParmVarDecl *OtherParm =
101 (*M->begin_overridden_methods())->getParamDecl(P.second);
Benjamin Kramer610ba532014-08-04 09:33:58 +0000102 StringRef Name = OtherParm->getName();
103 if (!Name.empty())
104 NewName = Name;
Benjamin Kramer14d42d92014-07-15 16:47:09 +0000105 }
106
Benjamin Kramer610ba532014-08-04 09:33:58 +0000107 // If the definition has a named parameter use that name.
108 if (Definition) {
109 const ParmVarDecl *DefParm = Definition->getParamDecl(P.second);
110 StringRef Name = DefParm->getName();
111 if (!Name.empty())
112 NewName = Name;
113 }
114
115 // Now insert the comment. Note that getLocation() points to the place
116 // where the name would be, this allows us to also get complex cases like
117 // function pointers right.
118 const ParmVarDecl *Parm = P.first->getParamDecl(P.second);
119 D << FixItHint::CreateInsertion(Parm->getLocation(),
120 " /*" + NewName.str() + "*/");
Benjamin Kramer14d42d92014-07-15 16:47:09 +0000121 }
122 }
123}
124
125} // namespace readability
Benjamin Kramer14d42d92014-07-15 16:47:09 +0000126} // namespace tidy
127} // namespace clang