1 //===--- UseEqualsDeleteCheck.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 "UseEqualsDeleteCheck.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 static const char DeletedNotPublic[] = "DeletedNotPublic"; 23 24 void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) { 25 auto PrivateSpecialFn = cxxMethodDecl( 26 isPrivate(), 27 anyOf(cxxConstructorDecl(anyOf(isDefaultConstructor(), 28 isCopyConstructor(), isMoveConstructor())), 29 cxxMethodDecl( 30 anyOf(isCopyAssignmentOperator(), isMoveAssignmentOperator())), 31 cxxDestructorDecl())); 32 33 Finder->addMatcher( 34 cxxMethodDecl( 35 PrivateSpecialFn, 36 unless(anyOf(hasBody(stmt()), isDefaulted(), isDeleted(), 37 ast_matchers::isTemplateInstantiation(), 38 // Ensure that all methods except private special member 39 // functions are defined. 40 hasParent(cxxRecordDecl(hasMethod(unless( 41 anyOf(PrivateSpecialFn, hasBody(stmt()), isPure(), 42 isDefaulted(), isDeleted())))))))) 43 .bind(SpecialFunction), 44 this); 45 46 Finder->addMatcher( 47 cxxMethodDecl(isDeleted(), unless(isPublic())).bind(DeletedNotPublic), 48 this); 49 } 50 51 void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) { 52 if (const auto *Func = 53 Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) { 54 SourceLocation EndLoc = Lexer::getLocForEndOfToken( 55 Func->getLocEnd(), 0, *Result.SourceManager, getLangOpts()); 56 57 // FIXME: Improve FixItHint to make method public 58 diag(Func->getLocation(), 59 "use '= delete' to prohibit calling of a special member function") 60 << FixItHint::CreateInsertion(EndLoc, " = delete"); 61 } else if (const auto *Func = 62 Result.Nodes.getNodeAs<CXXMethodDecl>(DeletedNotPublic)) { 63 // FIXME: Add FixItHint to make method public 64 diag(Func->getLocation(), "deleted member function should be public"); 65 } 66 } 67 68 } // namespace modernize 69 } // namespace tidy 70 } // namespace clang 71