clang-tools  3.9.0
UnconventionalAssignOperatorCheck.cpp
Go to the documentation of this file.
1 //===--- UnconventionalAssignOperatorCheck.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 
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace misc {
19 
20 void UnconventionalAssignOperatorCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
21  // Only register the matchers for C++; the functionality currently does not
22  // provide any benefit to other languages, despite being benign.
23  if (!getLangOpts().CPlusPlus)
24  return;
25 
26  const auto HasGoodReturnType = cxxMethodDecl(returns(
27  lValueReferenceType(pointee(unless(isConstQualified()),
28  hasDeclaration(equalsBoundNode("class"))))));
29 
30  const auto IsSelf = qualType(
31  anyOf(hasDeclaration(equalsBoundNode("class")),
32  referenceType(pointee(hasDeclaration(equalsBoundNode("class"))))));
33  const auto IsAssign =
34  cxxMethodDecl(unless(anyOf(isDeleted(), isPrivate(), isImplicit())),
35  hasName("operator="), ofClass(recordDecl().bind("class")))
36  .bind("method");
37  const auto IsSelfAssign =
38  cxxMethodDecl(IsAssign, hasParameter(0, parmVarDecl(hasType(IsSelf))))
39  .bind("method");
40 
41  Finder->addMatcher(
42  cxxMethodDecl(IsAssign, unless(HasGoodReturnType)).bind("ReturnType"),
43  this);
44 
45  const auto BadSelf = referenceType(
46  anyOf(lValueReferenceType(pointee(unless(isConstQualified()))),
47  rValueReferenceType(pointee(isConstQualified()))));
48 
49  Finder->addMatcher(
50  cxxMethodDecl(IsSelfAssign,
51  hasParameter(0, parmVarDecl(hasType(BadSelf))))
52  .bind("ArgumentType"),
53  this);
54 
55  Finder->addMatcher(
56  cxxMethodDecl(IsSelfAssign, anyOf(isConst(), isVirtual())).bind("cv"),
57  this);
58 
59  const auto IsBadReturnStatement = returnStmt(unless(has(ignoringParenImpCasts(
60  unaryOperator(hasOperatorName("*"), hasUnaryOperand(cxxThisExpr()))))));
61  const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
62 
63  Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign))
64  .bind("returnStmt"),
65  this);
66 }
67 
68 void UnconventionalAssignOperatorCheck::check(const MatchFinder::MatchResult &Result) {
69  if (const auto *RetStmt = Result.Nodes.getNodeAs<ReturnStmt>("returnStmt")) {
70  diag(RetStmt->getLocStart(), "operator=() should always return '*this'");
71  } else {
72  static const char *const Messages[][2] = {
73  {"ReturnType", "operator=() should return '%0&'"},
74  {"ArgumentType", "operator=() should take '%0 const&', '%0&&' or '%0'"},
75  {"cv", "operator=() should not be marked '%1'"}};
76 
77  const auto *Method = Result.Nodes.getNodeAs<CXXMethodDecl>("method");
78  for (const auto &Message : Messages) {
79  if (Result.Nodes.getNodeAs<Decl>(Message[0]))
80  diag(Method->getLocStart(), Message[1])
81  << Method->getParent()->getName()
82  << (Method->isConst() ? "const" : "virtual");
83  }
84  }
85 }
86 
87 } // namespace misc
88 } // namespace tidy
89 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
const NamedDecl * Result
Definition: USRFinder.cpp:137