[clang-tools-extra] r269581 - [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
Sat May 14 15:43:51 PDT 2016
Author: flx
Date: Sat May 14 17:43:50 2016
New Revision: 269581
URL: http://llvm.org/viewvc/llvm-project?rev=269581&view=rev
Log:
[clang-tidy] TypeTraits - Type is not expensive to copy when it has a deleted copy constructor.
Reviewers: alexfh, sbenza
Subscribers: etienneb, aaron.ballman, cfe-commits
Differential Revision: http://reviews.llvm.org/D20170
Modified:
clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp
clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.h
clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp
Modified: clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp?rev=269581&r1=269580&r2=269581&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp Sat May 14 17:43:50 2016
@@ -10,6 +10,7 @@
#include "TypeTraits.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
namespace clang {
namespace tidy {
@@ -17,19 +18,34 @@ namespace utils {
namespace type_traits {
namespace {
+
bool classHasTrivialCopyAndDestroy(QualType Type) {
auto *Record = Type->getAsCXXRecordDecl();
return Record && Record->hasDefinition() &&
!Record->hasNonTrivialCopyConstructor() &&
!Record->hasNonTrivialDestructor();
}
+
+bool hasDeletedCopyConstructor(QualType Type) {
+ auto *Record = Type->getAsCXXRecordDecl();
+ if (!Record || !Record->hasDefinition())
+ return false;
+ for (const auto *Constructor : Record->ctors()) {
+ if (Constructor->isCopyConstructor() && Constructor->isDeleted())
+ return true;
+ }
+ return false;
+}
+
} // namespace
-llvm::Optional<bool> isExpensiveToCopy(QualType Type, ASTContext &Context) {
+llvm::Optional<bool> isExpensiveToCopy(QualType Type,
+ const ASTContext &Context) {
if (Type->isDependentType())
return llvm::None;
return !Type.isTriviallyCopyableType(Context) &&
- !classHasTrivialCopyAndDestroy(Type);
+ !classHasTrivialCopyAndDestroy(Type) &&
+ !hasDeletedCopyConstructor(Type);
}
bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl,
Modified: clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.h?rev=269581&r1=269580&r2=269581&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.h (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.h Sat May 14 17:43:50 2016
@@ -19,7 +19,8 @@ namespace utils {
namespace type_traits {
// \brief Returns true If \c Type is expensive to copy.
-llvm::Optional<bool> isExpensiveToCopy(QualType Type, ASTContext &Context);
+llvm::Optional<bool> isExpensiveToCopy(QualType Type,
+ const ASTContext &Context);
// \brief Returns true If \c Type is trivially default constructible.
bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context);
Modified: clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp?rev=269581&r1=269580&r2=269581&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp Sat May 14 17:43:50 2016
@@ -23,6 +23,13 @@ class SomewhatTrivial {
SomewhatTrivial& operator=(const SomewhatTrivial&);
};
+struct MoveOnlyType {
+ MoveOnlyType(const MoveOnlyType &) = delete;
+ MoveOnlyType(MoveOnlyType &&) = default;
+ ~MoveOnlyType();
+ void constMethod() const;
+};
+
void positiveExpensiveConstValue(const ExpensiveToCopyType Obj);
// CHECK-FIXES: void positiveExpensiveConstValue(const ExpensiveToCopyType& Obj);
void positiveExpensiveConstValue(const ExpensiveToCopyType Obj) {
@@ -169,3 +176,7 @@ struct NegativeDeletedMethod {
NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;
// CHECK-FIXES: NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;
};
+
+void NegativeMoveOnlyTypePassedByValue(MoveOnlyType M) {
+ M.constMethod();
+}
More information about the cfe-commits
mailing list