clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

NoexceptMoveConstructorCheck.cpp
Go to the documentation of this file.
1 //===--- NoexceptMoveConstructorCheck.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 "NoexceptMoveConstructorCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18
19 void NoexceptMoveConstructorCheck::registerMatchers(MatchFinder *Finder) {
20 // Only register the matchers for C++11; the functionality currently does not
21 // provide any benefit to other languages, despite being benign.
22 if (!getLangOpts().CPlusPlus11)
23 return;
24
25 Finder->addMatcher(
26 cxxMethodDecl(anyOf(cxxConstructorDecl(), hasOverloadedOperatorName("=")),
27 unless(isImplicit()), unless(isDeleted()))
28 .bind("decl"),
29 this);
30 }
31
32 void NoexceptMoveConstructorCheck::check(
33 const MatchFinder::MatchResult &Result) {
34 if (const auto *Decl = Result.Nodes.getNodeAs<CXXMethodDecl>("decl")) {
35 StringRef MethodType = "assignment operator";
36 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(Decl)) {
37 if (!Ctor->isMoveConstructor())
38 return;
39 MethodType = "constructor";
40 } else if (!Decl->isMoveAssignmentOperator()) {
41 return;
42 }
43
44 const auto *ProtoType = Decl->getType()->getAs<FunctionProtoType>();
45 switch(ProtoType->getNoexceptSpec(*Result.Context)) {
46 case FunctionProtoType::NR_NoNoexcept:
47 diag(Decl->getLocation(), "move %0s should be marked noexcept")
48 << MethodType;
49 // FIXME: Add a fixit.
50 break;
51 case FunctionProtoType::NR_Throw:
52 // Don't complain about nothrow(false), but complain on nothrow(expr)
53 // where expr evaluates to false.
54 if (const Expr *E = ProtoType->getNoexceptExpr()) {
55 if (isa<CXXBoolLiteralExpr>(E))
56 break;
57 diag(E->getExprLoc(),
58 "noexcept specifier on the move %0 evaluates to 'false'")
59 << MethodType;
60 }
61 break;
62 case FunctionProtoType::NR_Nothrow:
63 case FunctionProtoType::NR_Dependent:
64 case FunctionProtoType::NR_BadNoexcept:
65 break;
66 }
67 }
68 }
69
70 } // namespace tidy
71 } // namespace clang
72
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
NoexceptMoveConstructorCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121