[clang-tools-extra] r322822 - [clang-tidy] Don't generate fix for argument constructed from std::initializer_list.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 18 00:58:18 PST 2018


Author: hokein
Date: Thu Jan 18 00:58:18 2018
New Revision: 322822

URL: http://llvm.org/viewvc/llvm-project?rev=322822&view=rev
Log:
[clang-tidy] Don't generate fix for argument constructed from std::initializer_list.

Summary:
A follow-up fix of rL311652.

The previous `vector` in our test is different with `std::vector`, so
The check still generates fixes for std::vector (`auto p =
std::unique_ptr<Foo>(new Foo({1,2,3}))`) in real world, the patch makes the
vector behavior in test align with std::vector (both AST nodes are the same now).

Reviewers: ilya-biryukov, alexfh

Reviewed By: ilya-biryukov

Subscribers: klimek, xazax.hun, cfe-commits

Differential Revision: https://reviews.llvm.org/D41852

Modified:
    clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/initializer_list.h
    clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp

Modified: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp?rev=322822&r1=322821&r2=322822&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp Thu Jan 18 00:58:18 2018
@@ -281,12 +281,25 @@ bool MakeSmartPtrCheck::replaceNew(Diagn
         if (isa<CXXStdInitializerListExpr>(Arg)) {
           return false;
         }
+        // Check whether we construct a class from a std::initializer_list.
+        // If so, we won't generate the fixes.
+        auto IsStdInitListInitConstructExpr = [](const Expr* E) {
+          assert(E);
+          if (const auto *ImplicitCE = dyn_cast<CXXConstructExpr>(E)) {
+            if (ImplicitCE->isStdInitListInitialization())
+              return true;
+          }
+          return false;
+        };
         // Check the implicit conversion from the std::initializer_list type to
         // a class type.
-        if (const auto *ImplicitCE = dyn_cast<CXXConstructExpr>(Arg)) {
-          if (ImplicitCE->isStdInitListInitialization()) {
+        if (IsStdInitListInitConstructExpr(Arg))
+          return false;
+        // The Arg can be a CXXBindTemporaryExpr, checking its underlying
+        // construct expr.
+        if (const auto * CTE = dyn_cast<CXXBindTemporaryExpr>(Arg)) {
+          if (IsStdInitListInitConstructExpr(CTE->getSubExpr()))
             return false;
-          }
         }
       }
     }

Modified: clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/initializer_list.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/initializer_list.h?rev=322822&r1=322821&r2=322822&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/initializer_list.h (original)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/initializer_list.h Thu Jan 18 00:58:18 2018
@@ -27,5 +27,6 @@ template <class _E>
 class vector {
  public:
   vector(initializer_list<_E> init);
+  ~vector();
 };
 } // namespace std

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp?rev=322822&r1=322821&r2=322822&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp Thu Jan 18 00:58:18 2018
@@ -25,6 +25,11 @@ struct DPair {
   int a, b;
 };
 
+template<typename T>
+struct MyVector {
+  MyVector(std::initializer_list<T>);
+};
+
 struct Empty {};
 
 struct E {
@@ -45,6 +50,7 @@ struct G {
 
 struct H {
   H(std::vector<int>);
+  H(MyVector<int>, int);
 };
 
 struct I {
@@ -331,6 +337,13 @@ void initialization(int T, Base b) {
   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
   // CHECK-FIXES: PH1.reset(new H({1, 2, 3}));
 
+  std::unique_ptr<H> PH2 = std::unique_ptr<H>(new H({1, 2, 3}, 1));
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
+  // CHECK-FIXES: std::unique_ptr<H> PH2 = std::unique_ptr<H>(new H({1, 2, 3}, 1));
+  PH2.reset(new H({1, 2, 3}, 1));
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
+  // CHECK-FIXES: PH2.reset(new H({1, 2, 3}, 1));
+
   std::unique_ptr<I> PI1 = std::unique_ptr<I>(new I(G({1, 2, 3})));
   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
   // CHECK-FIXES: std::unique_ptr<I> PI1 = std::make_unique<I>(G({1, 2, 3}));




More information about the cfe-commits mailing list