blob: 4a06dc8d6bb62eecbb646d46e723ccc3739c8a3e [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"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13#include "clang/AST/ASTContext.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
19namespace readability {
20
21void 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
31void 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 Kramer610ba532014-08-04 09:33:58 +000040 // Ignore declarations without a definition if we're not dealing with an
41 // overriden method.
42 const FunctionDecl *Definition = nullptr;
Benjamin Kramere59cd6e2014-08-29 08:58:35 +000043 if ((!Function->isDefined(Definition) || Function->isDefaulted() ||
44 Function->isDeleted()) &&
Benjamin Kramer610ba532014-08-04 09:33:58 +000045 (!isa<CXXMethodDecl>(Function) ||
46 cast<CXXMethodDecl>(Function)->size_overridden_methods() == 0))
47 return;
48
Benjamin Kramer14d42d92014-07-15 16:47:09 +000049 // 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
Benjamin Kramerfec67052014-09-01 09:11:48 +000058 // Don't warn on the dummy argument on post-inc and post-dec operators.
59 if ((Function->getOverloadedOperator() == OO_PlusPlus ||
60 Function->getOverloadedOperator() == OO_MinusMinus) &&
61 Parm->getType()->isSpecificBuiltinType(BuiltinType::Int))
62 continue;
63
Benjamin Kramer14d42d92014-07-15 16:47:09 +000064 // Sanity check the source locations.
65 if (!Parm->getLocation().isValid() || Parm->getLocation().isMacroID() ||
66 !SM.isWrittenInSameFile(Parm->getLocStart(), Parm->getLocation()))
67 continue;
68
69 // Look for comments. We explicitly want to allow idioms like
70 // void foo(int /*unused*/)
71 const char *Begin = SM.getCharacterData(Parm->getLocStart());
72 const char *End = SM.getCharacterData(Parm->getLocation());
73 StringRef Data(Begin, End - Begin);
74 if (Data.find("/*") != StringRef::npos)
75 continue;
76
77 UnnamedParams.push_back(std::make_pair(Function, I));
78 }
79
80 // Emit only one warning per function but fixits for all unnamed parameters.
81 if (!UnnamedParams.empty()) {
82 const ParmVarDecl *FirstParm =
83 UnnamedParams.front().first->getParamDecl(UnnamedParams.front().second);
84 auto D = diag(FirstParm->getLocation(),
85 "all parameters should be named in a function");
86
87 for (auto P : UnnamedParams) {
Benjamin Kramer78cd5462014-08-04 09:42:18 +000088 // Fallback to an unused marker.
89 StringRef NewName = "unused";
90
Benjamin Kramer14d42d92014-07-15 16:47:09 +000091 // If the method is overridden, try to copy the name from the base method
92 // into the overrider.
Benjamin Kramer14d42d92014-07-15 16:47:09 +000093 const auto *M = dyn_cast<CXXMethodDecl>(P.first);
94 if (M && M->size_overridden_methods() > 0) {
95 const ParmVarDecl *OtherParm =
96 (*M->begin_overridden_methods())->getParamDecl(P.second);
Benjamin Kramer610ba532014-08-04 09:33:58 +000097 StringRef Name = OtherParm->getName();
98 if (!Name.empty())
99 NewName = Name;
Benjamin Kramer14d42d92014-07-15 16:47:09 +0000100 }
101
Benjamin Kramer610ba532014-08-04 09:33:58 +0000102 // If the definition has a named parameter use that name.
103 if (Definition) {
104 const ParmVarDecl *DefParm = Definition->getParamDecl(P.second);
105 StringRef Name = DefParm->getName();
106 if (!Name.empty())
107 NewName = Name;
108 }
109
110 // Now insert the comment. Note that getLocation() points to the place
111 // where the name would be, this allows us to also get complex cases like
112 // function pointers right.
113 const ParmVarDecl *Parm = P.first->getParamDecl(P.second);
114 D << FixItHint::CreateInsertion(Parm->getLocation(),
115 " /*" + NewName.str() + "*/");
Benjamin Kramer14d42d92014-07-15 16:47:09 +0000116 }
117 }
118}
119
120} // namespace readability
121} // namespace tidy
122} // namespace clang