clang-tools  10.0.0
NoexceptMoveConstructorCheck.cpp
Go to the documentation of this file.
1 //===--- NoexceptMoveConstructorCheck.cpp - clang-tidy---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Tooling/FixIt.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace performance {
19 
20 void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) {
21  // Only register the matchers for C++11; the functionality currently does not
22  // provide any benefit to other languages, despite being benign.
23  if (!getLangOpts().CPlusPlus11)
24  return;
25 
26  Finder->addMatcher(
27  cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")),
28  unless(isImplicit()), unless(isDeleted()))
29  .bind("decl"),
30  this);
31 }
32 
33 void NoexceptMoveConstructorCheck::check(
34  const MatchFinder::MatchResult &Result) {
35  if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) {
36  StringRef MethodType = "assignment operator";
37  if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) {
38  if (!Ctor->isMoveConstructor())
39  return;
40  MethodType = "constructor";
41  } else if (!Decl->isMoveAssignmentOperator()) {
42  return;
43  }
44 
45  const auto *ProtoType = Decl->getType()->getAs<FunctionProtoType>();
46 
47  if (isUnresolvedExceptionSpec(ProtoType->getExceptionSpecType()))
48  return;
49 
50  if (!isNoexceptExceptionSpec(ProtoType->getExceptionSpecType())) {
51  auto Diag =
52  diag(Decl->getLocation(), "move %0s should be marked noexcept")
53  << MethodType;
54  // Add FixIt hints.
55  SourceManager &SM = *Result.SourceManager;
56  assert(Decl->getNumParams() > 0);
57  SourceLocation NoexceptLoc = Decl->getParamDecl(Decl->getNumParams() - 1)
58  ->getSourceRange()
59  .getEnd();
60  if (NoexceptLoc.isValid())
61  NoexceptLoc = Lexer::findLocationAfterToken(
62  NoexceptLoc, tok::r_paren, SM, Result.Context->getLangOpts(), true);
63  if (NoexceptLoc.isValid())
64  Diag << FixItHint::CreateInsertion(NoexceptLoc, " noexcept ");
65  return;
66  }
67 
68  // Don't complain about nothrow(false), but complain on nothrow(expr)
69  // where expr evaluates to false.
70  if (ProtoType->canThrow() == CT_Can) {
71  Expr *E = ProtoType->getNoexceptExpr();
72  E = E->IgnoreImplicit();
73  if (!isa<CXXBoolLiteralExpr>(E)) {
74  diag(E->getExprLoc(),
75  "noexcept specifier on the move %0 evaluates to 'false'")
76  << MethodType;
77  }
78  }
79  }
80 }
81 
82 } // namespace performance
83 } // namespace tidy
84 } // namespace clang
const FunctionDecl * Decl
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const Expr * E