| 1 |
//===--- UseEqualsDeleteCheck.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 |
|
| 9 |
#include "UseEqualsDeleteCheck.h" |
| 10 |
#include "clang/AST/ASTContext.h" |
| 11 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 |
#include "clang/Lex/Lexer.h" |
| 13 |
|
| 14 |
using namespace clang::ast_matchers; |
| 15 |
|
| 16 |
namespace clang::tidy::modernize { |
| 17 |
|
| 18 |
static const char SpecialFunction[] = "SpecialFunction"; |
| 19 |
static const char DeletedNotPublic[] = "DeletedNotPublic"; |
| 20 |
|
| 21 |
void UseEqualsDeleteCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 22 |
Options.store(Opts, "IgnoreMacros", IgnoreMacros); |
| 23 |
} |
| 24 |
|
| 25 |
void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) { |
| 26 |
auto PrivateSpecialFn = cxxMethodDecl( |
| 27 |
isPrivate(), |
| 28 |
anyOf(cxxConstructorDecl(anyOf(isDefaultConstructor(), |
| 29 |
isCopyConstructor(), isMoveConstructor())), |
| 30 |
cxxMethodDecl( |
| 31 |
anyOf(isCopyAssignmentOperator(), isMoveAssignmentOperator())), |
| 32 |
cxxDestructorDecl())); |
| 33 |
|
| 34 |
Finder->addMatcher( |
| 35 |
cxxMethodDecl( |
| 36 |
PrivateSpecialFn, |
| 37 |
unless(anyOf(hasAnyBody(stmt()), isDefaulted(), isDeleted(), |
| 38 |
ast_matchers::isTemplateInstantiation(), |
| 39 |
// Ensure that all methods except private special member |
| 40 |
// functions are defined. |
| 41 |
hasParent(cxxRecordDecl(hasMethod(unless( |
| 42 |
anyOf(PrivateSpecialFn, hasAnyBody(stmt()), isPure(), |
| 43 |
isDefaulted(), isDeleted())))))))) |
| 44 |
.bind(SpecialFunction), |
| 45 |
this); |
| 46 |
|
| 47 |
Finder->addMatcher( |
| 48 |
cxxMethodDecl(isDeleted(), unless(isPublic())).bind(DeletedNotPublic), |
| 49 |
this); |
| 50 |
} |
| 51 |
|
| 52 |
void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) { |
| 53 |
if (const auto *Func = |
| 54 |
Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) { |
| 55 |
SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
| 56 |
Func->getEndLoc(), 0, *Result.SourceManager, getLangOpts()); |
| 57 |
|
| 58 |
if (Func->getLocation().isMacroID() && IgnoreMacros) |
| 59 |
return; |
| 60 |
// FIXME: Improve FixItHint to make the method public. |
| 61 |
diag(Func->getLocation(), |
| 62 |
"use '= delete' to prohibit calling of a special member function") |
| 63 |
<< FixItHint::CreateInsertion(EndLoc, " = delete"); |
| 64 |
} else if (const auto *Func = |
| 65 |
Result.Nodes.getNodeAs<CXXMethodDecl>(DeletedNotPublic)) { |
| 66 |
// Ignore this warning in macros, since it's extremely noisy in code using |
| 67 |
// DISALLOW_COPY_AND_ASSIGN-style macros and there's no easy way to |
| 68 |
// automatically fix the warning when macros are in play. |
| 69 |
if (Func->getLocation().isMacroID() && IgnoreMacros) |
| 70 |
return; |
| 71 |
// FIXME: Add FixItHint to make the method public. |
| 72 |
diag(Func->getLocation(), "deleted member function should be public"); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
} // namespace clang::tidy::modernize |