llvm-capstone/clang-tools-extra/clang-tidy/readability/DeletedDefaultCheck.cpp
Chandler Carruth 2946cd7010 Update the file headers across all of the LLVM projects in the monorepo
to reflect the new license.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351636
2019-01-19 08:50:56 +00:00

69 lines
2.7 KiB
C++

//===--- DeletedDefaultCheck.cpp - clang-tidy------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "DeletedDefaultCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace readability {
void DeletedDefaultCheck::registerMatchers(MatchFinder *Finder) {
// We match constructors/assignment operators that are:
// - explicitly marked '= default'
// - actually deleted
// - not in template instantiation.
// We bind the declaration to "method-decl" and also to "constructor" when
// it is a constructor.
Finder->addMatcher(
cxxMethodDecl(anyOf(cxxConstructorDecl().bind("constructor"),
isCopyAssignmentOperator(),
isMoveAssignmentOperator()),
isDefaulted(), unless(isImplicit()), isDeleted(),
unless(isInstantiated()))
.bind("method-decl"),
this);
}
void DeletedDefaultCheck::check(const MatchFinder::MatchResult &Result) {
const StringRef Message = "%0 is explicitly defaulted but implicitly "
"deleted, probably because %1; definition can "
"either be removed or explicitly deleted";
if (const auto *Constructor =
Result.Nodes.getNodeAs<CXXConstructorDecl>("constructor")) {
auto Diag = diag(Constructor->getBeginLoc(), Message);
if (Constructor->isDefaultConstructor()) {
Diag << "default constructor"
<< "a non-static data member or a base class is lacking a default "
"constructor";
} else if (Constructor->isCopyConstructor()) {
Diag << "copy constructor"
<< "a non-static data member or a base class is not copyable";
} else if (Constructor->isMoveConstructor()) {
Diag << "move constructor"
<< "a non-static data member or a base class is neither copyable "
"nor movable";
}
} else if (const auto *Assignment =
Result.Nodes.getNodeAs<CXXMethodDecl>("method-decl")) {
diag(Assignment->getBeginLoc(), Message)
<< (Assignment->isCopyAssignmentOperator() ? "copy assignment operator"
: "move assignment operator")
<< "a base class or a non-static data member is not assignable, e.g. "
"because the latter is marked 'const'";
}
}
} // namespace readability
} // namespace tidy
} // namespace clang