[PATCH] D23343: [clang-tidy] modernize-make-{smart_ptr} private ctor bugfix

Piotr Padlewski via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 9 21:58:00 PDT 2016


Prazek created this revision.
Prazek added reviewers: alexfh, aaron.ballman, hokein.
Prazek added a subscriber: cfe-commits.

Bugfix for 27321. When the constructor of stored pointer
type is private then it is invalid to change it to
make_shared or make_unique.

https://reviews.llvm.org/D23343

Files:
  clang-tidy/modernize/MakeSmartPtrCheck.cpp
  test/clang-tidy/modernize-make-shared.cpp
  test/clang-tidy/modernize-make-unique.cpp

Index: test/clang-tidy/modernize-make-unique.cpp
===================================================================
--- test/clang-tidy/modernize-make-unique.cpp
+++ test/clang-tidy/modernize-make-unique.cpp
@@ -103,6 +103,17 @@
   std::unique_ptr<int> Placement = std::unique_ptr<int>(new (PInt) int{3});
 }
 
+class Private {
+private:
+  Private(int z) {}
+
+public:
+  void create() {
+    auto ptr = std::unique_ptr<Private>(new Private(42));
+  }
+};
+
+
 void initialization(int T, Base b) {
   // Test different kinds of initialization of the pointee.
 
Index: test/clang-tidy/modernize-make-shared.cpp
===================================================================
--- test/clang-tidy/modernize-make-shared.cpp
+++ test/clang-tidy/modernize-make-shared.cpp
@@ -100,6 +100,18 @@
   std::shared_ptr<int> Placement = std::shared_ptr<int>(new (PInt) int{3});
 }
 
+class Private {
+private:
+  Private(int z) {}
+
+public:
+  void create() {
+    auto ptr = std::shared_ptr<Private>(new Private(42));
+  }
+
+  ~Private();
+};
+
 void initialization(int T, Base b) {
   // Test different kinds of initialization of the pointee.
 
Index: clang-tidy/modernize/MakeSmartPtrCheck.cpp
===================================================================
--- clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -29,13 +29,19 @@
   if (!getLangOpts().CPlusPlus11)
     return;
 
+  // It is possible to make smart ptr calling private ctor inside of a member
+  // function. Change to make_smart_ptr will be invalid.
+  auto CallsPrivateCtor = has(ignoringImpCasts(cxxConstructExpr(
+    hasDeclaration(decl(isPrivate())))));
+
   Finder->addMatcher(
       cxxBindTemporaryExpr(has(ignoringParenImpCasts(
           cxxConstructExpr(
               hasType(getSmartPointerTypeMatcher()), argumentCountIs(1),
               hasArgument(0,
                           cxxNewExpr(hasType(pointsTo(qualType(hasCanonicalType(
-                                         equalsBoundNode(PointerType))))))
+                                         equalsBoundNode(PointerType))))),
+                                     unless(CallsPrivateCtor))
                               .bind(NewExpression)))
               .bind(ConstructorCall)))),
       this);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23343.67458.patch
Type: text/x-patch
Size: 2290 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160810/99c127c0/attachment.bin>


More information about the cfe-commits mailing list