clang-tools  12.0.0git
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/Lex/Lexer.h"
13 #include "clang/Tooling/FixIt.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace performance {
20 
21 void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) {
22  Finder->addMatcher(
23  cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")),
24  unless(isImplicit()), unless(isDeleted()))
25  .bind("decl"),
26  this);
27 }
28 
29 void NoexceptMoveConstructorCheck::check(
30  const MatchFinder::MatchResult &Result) {
31  if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) {
32  StringRef MethodType = "assignment operator";
33  if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) {
34  if (!Ctor->isMoveConstructor())
35  return;
36  MethodType = "constructor";
37  } else if (!Decl->isMoveAssignmentOperator()) {
38  return;
39  }
40 
41  const auto *ProtoType = Decl->getType()->getAs<FunctionProtoType>();
42 
43  if (isUnresolvedExceptionSpec(ProtoType->getExceptionSpecType()))
44  return;
45 
46  if (!isNoexceptExceptionSpec(ProtoType->getExceptionSpecType())) {
47  auto Diag =
48  diag(Decl->getLocation(), "move %0s should be marked noexcept")
49  << MethodType;
50  // Add FixIt hints.
51  SourceManager &SM = *Result.SourceManager;
52  assert(Decl->getNumParams() > 0);
53  SourceLocation NoexceptLoc = Decl->getParamDecl(Decl->getNumParams() - 1)
54  ->getSourceRange()
55  .getEnd();
56  if (NoexceptLoc.isValid())
57  NoexceptLoc = Lexer::findLocationAfterToken(
58  NoexceptLoc, tok::r_paren, SM, Result.Context->getLangOpts(), true);
59  if (NoexceptLoc.isValid())
60  Diag << FixItHint::CreateInsertion(NoexceptLoc, " noexcept ");
61  return;
62  }
63 
64  // Don't complain about nothrow(false), but complain on nothrow(expr)
65  // where expr evaluates to false.
66  if (ProtoType->canThrow() == CT_Can) {
67  Expr *E = ProtoType->getNoexceptExpr();
68  E = E->IgnoreImplicit();
69  if (!isa<CXXBoolLiteralExpr>(E)) {
70  diag(E->getExprLoc(),
71  "noexcept specifier on the move %0 evaluates to 'false'")
72  << MethodType;
73  }
74  }
75  }
76 }
77 
78 } // namespace performance
79 } // namespace tidy
80 } // namespace clang
const FunctionDecl * Decl
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const Expr * E