[PATCH] D20170: [clang-tidy] TypeTraits - Type is not expensive to copy when it has a deleted copy constructor.
Felix Berger via cfe-commits
cfe-commits at lists.llvm.org
Wed May 11 07:49:15 PDT 2016
flx created this revision.
flx added reviewers: alexfh, sbenza.
flx added a subscriber: cfe-commits.
flx set the repository for this revision to rL LLVM.
Repository:
rL LLVM
http://reviews.llvm.org/D20170
Files:
clang-tidy/utils/TypeTraits.cpp
test/clang-tidy/performance-unnecessary-value-param.cpp
Index: test/clang-tidy/performance-unnecessary-value-param.cpp
===================================================================
--- test/clang-tidy/performance-unnecessary-value-param.cpp
+++ test/clang-tidy/performance-unnecessary-value-param.cpp
@@ -23,6 +23,12 @@
SomewhatTrivial& operator=(const SomewhatTrivial&);
};
+struct MoveOnlyType {
+ MoveOnlyType(const MoveOnlyType&) = delete;
+ ~MoveOnlyType();
+ void constMethod() const;
+};
+
void positiveExpensiveConstValue(const ExpensiveToCopyType Obj);
// CHECK-FIXES: void positiveExpensiveConstValue(const ExpensiveToCopyType& Obj);
void positiveExpensiveConstValue(const ExpensiveToCopyType Obj) {
@@ -169,3 +175,7 @@
NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;
// CHECK-FIXES: NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;
};
+
+void NegativeMoveOnlyTypePassedByValue(MoveOnlyType M) {
+ M.constMethod();
+}
Index: clang-tidy/utils/TypeTraits.cpp
===================================================================
--- clang-tidy/utils/TypeTraits.cpp
+++ clang-tidy/utils/TypeTraits.cpp
@@ -10,26 +10,42 @@
#include "TypeTraits.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
namespace clang {
namespace tidy {
namespace utils {
namespace type_traits {
+using namespace ::clang::ast_matchers;
+
namespace {
bool classHasTrivialCopyAndDestroy(QualType Type) {
auto *Record = Type->getAsCXXRecordDecl();
return Record && Record->hasDefinition() &&
!Record->hasNonTrivialCopyConstructor() &&
!Record->hasNonTrivialDestructor();
}
+
+bool hasDeletedCopyConstructor(QualType Type, ASTContext &Context) {
+ auto *Record = Type->getAsCXXRecordDecl();
+ if (Record == nullptr || !Record->hasDefinition())
+ return false;
+ auto Matches = match(cxxRecordDecl(hasMethod(
+ cxxConstructorDecl(isCopyConstructor(), isDeleted())
+ .bind("constructor"))),
+ *Record, Context);
+ return !Matches.empty();
+}
+
} // namespace
llvm::Optional<bool> isExpensiveToCopy(QualType Type, ASTContext &Context) {
if (Type->isDependentType())
return llvm::None;
return !Type.isTriviallyCopyableType(Context) &&
- !classHasTrivialCopyAndDestroy(Type);
+ !classHasTrivialCopyAndDestroy(Type) &&
+ !hasDeletedCopyConstructor(Type, Context);
}
bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20170.56913.patch
Type: text/x-patch
Size: 2550 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160511/ac0c75f4/attachment.bin>
More information about the cfe-commits
mailing list