[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
Sat May 14 15:50:03 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL269581: [clang-tidy] TypeTraits - Type is not expensive to copy when it has a deleted… (authored by flx).

Changed prior to commit:
  http://reviews.llvm.org/D20170?vs=57286&id=57287#toc

Repository:
  rL LLVM

http://reviews.llvm.org/D20170

Files:
  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

Index: clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp
===================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.cpp
+++ clang-tools-extra/trunk/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 {
 
 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,
Index: clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.h
===================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.h
+++ clang-tools-extra/trunk/clang-tidy/utils/TypeTraits.h
@@ -19,7 +19,8 @@
 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);
Index: clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp
===================================================================
--- clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp
+++ clang-tools-extra/trunk/test/clang-tidy/performance-unnecessary-value-param.cpp
@@ -23,6 +23,13 @@
   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 @@
   NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;
   // CHECK-FIXES: NegativeDeletedMethod& operator=(NegativeDeletedMethod N) = delete;
 };
+
+void NegativeMoveOnlyTypePassedByValue(MoveOnlyType M) {
+  M.constMethod();
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20170.57287.patch
Type: text/x-patch
Size: 3393 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160514/7cd2a60a/attachment.bin>


More information about the cfe-commits mailing list