clang-tools  4.0.0
UseEqualsDefaultCheck.cpp
Go to the documentation of this file.
1 //===--- UseEqualsDefaultCheck.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 "UseEqualsDefaultCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace modernize {
20 
21 static const char SpecialFunction[] = "SpecialFunction";
22 
23 /// \brief Finds all the named non-static fields of \p Record.
24 static std::set<const FieldDecl *>
25 getAllNamedFields(const CXXRecordDecl *Record) {
26  std::set<const FieldDecl *> Result;
27  for (const auto *Field : Record->fields()) {
28  // Static data members are not in this range.
29  if (Field->isUnnamedBitfield())
30  continue;
31  Result.insert(Field);
32  }
33  return Result;
34 }
35 
36 /// \brief Returns the names of the direct bases of \p Record, both virtual and
37 /// non-virtual.
38 static std::set<const Type *> getAllDirectBases(const CXXRecordDecl *Record) {
39  std::set<const Type *> Result;
40  for (auto Base : Record->bases()) {
41  // CXXBaseSpecifier.
42  const auto *BaseType = Base.getTypeSourceInfo()->getType().getTypePtr();
43  Result.insert(BaseType);
44  }
45  return Result;
46 }
47 
48 /// \brief Returns a matcher that matches member expressions where the base is
49 /// the variable declared as \p Var and the accessed member is the one declared
50 /// as \p Field.
51 internal::Matcher<Expr> accessToFieldInVar(const FieldDecl *Field,
52  const ValueDecl *Var) {
53  return ignoringImpCasts(
54  memberExpr(hasObjectExpression(declRefExpr(to(varDecl(equalsNode(Var))))),
55  member(fieldDecl(equalsNode(Field)))));
56 }
57 
58 /// \brief Check that the given constructor has copy signature and that it
59 /// copy-initializes all its bases and members.
61  const CXXConstructorDecl *Ctor) {
62  // An explicitly-defaulted constructor cannot have default arguments.
63  if (Ctor->getMinRequiredArguments() != 1)
64  return false;
65 
66  const auto *Record = Ctor->getParent();
67  const auto *Param = Ctor->getParamDecl(0);
68 
69  // Base classes and members that have to be copied.
70  auto BasesToInit = getAllDirectBases(Record);
71  auto FieldsToInit = getAllNamedFields(Record);
72 
73  // Ensure that all the bases are copied.
74  for (const auto *Base : BasesToInit) {
75  // The initialization of a base class should be a call to a copy
76  // constructor of the base.
77  if (match(
78  cxxConstructorDecl(forEachConstructorInitializer(cxxCtorInitializer(
79  isBaseInitializer(),
80  withInitializer(cxxConstructExpr(allOf(
81  hasType(equalsNode(Base)),
82  hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
83  argumentCountIs(1),
84  hasArgument(
85  0, declRefExpr(to(varDecl(equalsNode(Param))))))))))),
86  *Ctor, *Context)
87  .empty())
88  return false;
89  }
90 
91  // Ensure that all the members are copied.
92  for (const auto *Field : FieldsToInit) {
93  auto AccessToFieldInParam = accessToFieldInVar(Field, Param);
94  // The initialization is a CXXConstructExpr for class types.
95  if (match(
96  cxxConstructorDecl(forEachConstructorInitializer(cxxCtorInitializer(
97  isMemberInitializer(), forField(equalsNode(Field)),
98  withInitializer(anyOf(
99  AccessToFieldInParam,
100  cxxConstructExpr(allOf(
101  hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
102  argumentCountIs(1),
103  hasArgument(0, AccessToFieldInParam)))))))),
104  *Ctor, *Context)
105  .empty())
106  return false;
107  }
108 
109  // Ensure that we don't do anything else, like initializing an indirect base.
110  return Ctor->getNumCtorInitializers() ==
111  BasesToInit.size() + FieldsToInit.size();
112 }
113 
114 /// \brief Checks that the given method is an overloading of the assignment
115 /// operator, has copy signature, returns a reference to "*this" and copies
116 /// all its members and subobjects.
118  const CXXMethodDecl *Operator) {
119  const auto *Record = Operator->getParent();
120  const auto *Param = Operator->getParamDecl(0);
121 
122  // Base classes and members that have to be copied.
123  auto BasesToInit = getAllDirectBases(Record);
124  auto FieldsToInit = getAllNamedFields(Record);
125 
126  const auto *Compound = cast<CompoundStmt>(Operator->getBody());
127 
128  // The assignment operator definition has to end with the following return
129  // statement:
130  // return *this;
131  if (Compound->body_empty() ||
132  match(returnStmt(has(ignoringParenImpCasts(unaryOperator(
133  hasOperatorName("*"), hasUnaryOperand(cxxThisExpr()))))),
134  *Compound->body_back(), *Context)
135  .empty())
136  return false;
137 
138  // Ensure that all the bases are copied.
139  for (const auto *Base : BasesToInit) {
140  // Assignment operator of a base class:
141  // Base::operator=(Other);
142  //
143  // Clang translates this into:
144  // ((Base*)this)->operator=((Base)Other);
145  //
146  // So we are looking for a member call that fulfills:
147  if (match(compoundStmt(has(ignoringParenImpCasts(cxxMemberCallExpr(allOf(
148  // - The object is an implicit cast of 'this' to a pointer to
149  // a base class.
150  onImplicitObjectArgument(
151  implicitCastExpr(hasImplicitDestinationType(
152  pointsTo(type(equalsNode(Base)))),
153  hasSourceExpression(cxxThisExpr()))),
154  // - The called method is the operator=.
155  callee(cxxMethodDecl(isCopyAssignmentOperator())),
156  // - The argument is (an implicit cast to a Base of) the
157  // argument taken by "Operator".
158  argumentCountIs(1),
159  hasArgument(0,
160  declRefExpr(to(varDecl(equalsNode(Param)))))))))),
161  *Compound, *Context)
162  .empty())
163  return false;
164  }
165 
166  // Ensure that all the members are copied.
167  for (const auto *Field : FieldsToInit) {
168  // The assignment of data members:
169  // Field = Other.Field;
170  // Is a BinaryOperator in non-class types, and a CXXOperatorCallExpr
171  // otherwise.
172  auto LHS = memberExpr(hasObjectExpression(cxxThisExpr()),
173  member(fieldDecl(equalsNode(Field))));
174  auto RHS = accessToFieldInVar(Field, Param);
175  if (match(
176  compoundStmt(has(ignoringParenImpCasts(stmt(anyOf(
177  binaryOperator(hasOperatorName("="), hasLHS(LHS), hasRHS(RHS)),
178  cxxOperatorCallExpr(hasOverloadedOperatorName("="),
179  argumentCountIs(2), hasArgument(0, LHS),
180  hasArgument(1, RHS))))))),
181  *Compound, *Context)
182  .empty())
183  return false;
184  }
185 
186  // Ensure that we don't do anything else.
187  return Compound->size() == BasesToInit.size() + FieldsToInit.size() + 1;
188 }
189 
190 /// \brief Returns false if the body has any non-whitespace character.
191 static bool bodyEmpty(const ASTContext *Context, const CompoundStmt *Body) {
192  bool Invalid = false;
193  StringRef Text = Lexer::getSourceText(
194  CharSourceRange::getCharRange(Body->getLBracLoc().getLocWithOffset(1),
195  Body->getRBracLoc()),
196  Context->getSourceManager(), Context->getLangOpts(), &Invalid);
197  return !Invalid && std::strspn(Text.data(), " \t\r\n") == Text.size();
198 }
199 
200 void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) {
201  if (getLangOpts().CPlusPlus) {
202  // Destructor.
203  Finder->addMatcher(cxxDestructorDecl(isDefinition()).bind(SpecialFunction),
204  this);
205  Finder->addMatcher(
206  cxxConstructorDecl(
207  isDefinition(),
208  anyOf(
209  // Default constructor.
210  allOf(unless(hasAnyConstructorInitializer(isWritten())),
211  parameterCountIs(0)),
212  // Copy constructor.
213  allOf(isCopyConstructor(),
214  // Discard constructors that can be used as a copy
215  // constructor because all the other arguments have
216  // default values.
217  parameterCountIs(1))))
218  .bind(SpecialFunction),
219  this);
220  // Copy-assignment operator.
221  Finder->addMatcher(
222  cxxMethodDecl(isDefinition(), isCopyAssignmentOperator(),
223  // isCopyAssignmentOperator() allows the parameter to be
224  // passed by value, and in this case it cannot be
225  // defaulted.
226  hasParameter(0, hasType(lValueReferenceType())))
227  .bind(SpecialFunction),
228  this);
229  }
230 }
231 
232 void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
233  std::string SpecialFunctionName;
234 
235  // Both CXXConstructorDecl and CXXDestructorDecl inherit from CXXMethodDecl.
236  const auto *SpecialFunctionDecl =
237  Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction);
238 
239  // Discard explicitly deleted/defaulted special member functions and those
240  // that are not user-provided (automatically generated).
241  if (SpecialFunctionDecl->isDeleted() ||
242  SpecialFunctionDecl->isExplicitlyDefaulted() ||
243  SpecialFunctionDecl->isLateTemplateParsed() ||
244  SpecialFunctionDecl->isTemplateInstantiation() ||
245  !SpecialFunctionDecl->isUserProvided() || !SpecialFunctionDecl->hasBody())
246  return;
247 
248  const auto *Body = dyn_cast<CompoundStmt>(SpecialFunctionDecl->getBody());
249  if (!Body)
250  return;
251 
252  // If there is code inside the body, don't warn.
253  if (!SpecialFunctionDecl->isCopyAssignmentOperator() && !Body->body_empty())
254  return;
255 
256  // If there are comments inside the body, don't do the change.
257  bool ApplyFix = SpecialFunctionDecl->isCopyAssignmentOperator() ||
258  bodyEmpty(Result.Context, Body);
259 
260  std::vector<FixItHint> RemoveInitializers;
261 
262  if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(SpecialFunctionDecl)) {
263  if (Ctor->getNumParams() == 0) {
264  SpecialFunctionName = "default constructor";
265  } else {
266  if (!isCopyConstructorAndCanBeDefaulted(Result.Context, Ctor))
267  return;
268  SpecialFunctionName = "copy constructor";
269  // If there are constructor initializers, they must be removed.
270  for (const auto *Init : Ctor->inits()) {
271  RemoveInitializers.emplace_back(
272  FixItHint::CreateRemoval(Init->getSourceRange()));
273  }
274  }
275  } else if (isa<CXXDestructorDecl>(SpecialFunctionDecl)) {
276  SpecialFunctionName = "destructor";
277  } else {
278  if (!isCopyAssignmentAndCanBeDefaulted(Result.Context, SpecialFunctionDecl))
279  return;
280  SpecialFunctionName = "copy-assignment operator";
281  }
282 
283  // The location of the body is more useful inside a macro as spelling and
284  // expansion locations are reported.
285  SourceLocation Location = SpecialFunctionDecl->getLocation();
286  if (Location.isMacroID())
287  Location = Body->getLocStart();
288 
289  auto Diag = diag(Location, "use '= default' to define a trivial " +
290  SpecialFunctionName);
291 
292  if (ApplyFix)
293  Diag << FixItHint::CreateReplacement(Body->getSourceRange(), "= default;")
294  << RemoveInitializers;
295 }
296 
297 } // namespace modernize
298 } // namespace tidy
299 } // namespace clang
static bool isCopyAssignmentAndCanBeDefaulted(ASTContext *Context, const CXXMethodDecl *Operator)
Checks that the given method is an overloading of the assignment operator, has copy signature...
static const char SpecialFunction[]
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:262
static bool bodyEmpty(const ASTContext *Context, const CompoundStmt *Body)
Returns false if the body has any non-whitespace character.
internal::Matcher< Expr > accessToFieldInVar(const FieldDecl *Field, const ValueDecl *Var)
Returns a matcher that matches member expressions where the base is the variable declared as Var and ...
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
static bool isCopyConstructorAndCanBeDefaulted(ASTContext *Context, const CXXConstructorDecl *Ctor)
Check that the given constructor has copy signature and that it copy-initializes all its bases and me...
static std::set< const Type * > getAllDirectBases(const CXXRecordDecl *Record)
Returns the names of the direct bases of Record, both virtual and non-virtual.
static std::set< const FieldDecl * > getAllNamedFields(const CXXRecordDecl *Record)
Finds all the named non-static fields of Record.
const NamedDecl * Result
Definition: USRFinder.cpp:162