[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 22:07:26 PDT 2016


Prazek updated this revision to Diff 67460.
Prazek added a comment.

I hate it when arc do this thing. When origin/master is not the trunk...


https://reviews.llvm.org/D23343

Files:
  clang-tidy/modernize/MakeSmartPtrCheck.cpp
  docs/ReleaseNotes.rst
  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,16 @@
   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: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -75,6 +75,13 @@
   This check warns about the performance overhead arising from concatenating
   strings using the ``operator+``, instead of ``operator+=``.
 
+- Bugfix for `modernize-make-unique
+  <http://clang.llvm.org/extra/clang-tidy/checks/modernize-make-unique.html>`_
+  and `modernize-make-shared
+  <http://clang.llvm.org/extra/clang-tidy/checks/modernize-make-shared.html>`_
+  checks
+  It is invalid to use ``make_{smart_ptr}`` when the called constructor is
+  private.
 
 Improvements to include-fixer
 -----------------------------
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.67460.patch
Type: text/x-patch
Size: 2990 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160810/949487dd/attachment.bin>


More information about the cfe-commits mailing list