diff options
author | Haojian Wu <hokein@google.com> | 2019-06-03 08:14:15 +0000 |
---|---|---|
committer | Haojian Wu <hokein@google.com> | 2019-06-03 08:14:15 +0000 |
commit | ceb0cc54f9d647c2fed3ae878ba91b95f9740777 (patch) | |
tree | 4b6b8a6de1ab4454a458ffd54a38245ffe76b586 | |
parent | 404a679e1d0c19bf504776fd10aaca411462da5e (diff) | |
download | bcm5719-llvm-ceb0cc54f9d647c2fed3ae878ba91b95f9740777.tar.gz bcm5719-llvm-ceb0cc54f9d647c2fed3ae878ba91b95f9740777.zip |
[clang-tidy] Fix make-unique check to work in C++17 mode.
Summary:
Previously, we intended to omit the check fix to the case when constructor has
any braced-init-list argument. But the HasListInitializedArgument was not
correct to handle all cases (Foo(Bar{1, 2}) will return false in C++14
mode).
This patch fixes it, corrects the tests, and makes the check to run at C++17 mode.
Reviewers: gribozavr
Subscribers: xazax.hun, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D62736
llvm-svn: 362361
-rw-r--r-- | clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp | 15 | ||||
-rw-r--r-- | clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp | 12 |
2 files changed, 18 insertions, 9 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp index 179a09745e8..5fbc7be3749 100644 --- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -298,11 +298,20 @@ bool MakeSmartPtrCheck::replaceNew(DiagnosticBuilder &Diag, return true; // Check whether we implicitly construct a class from a // std::initializer_list. - if (const auto *ImplicitCE = dyn_cast<CXXConstructExpr>(Arg)) { - if (ImplicitCE->isStdInitListInitialization()) + if (const auto *CEArg = dyn_cast<CXXConstructExpr>(Arg)) { + // Strip the elidable move constructor, it is present in the AST for + // C++11/14, e.g. Foo(Bar{1, 2}), the move constructor is around the + // init-list constructor. + if (CEArg->isElidable()) { + if (const auto *TempExp = CEArg->getArg(0)) { + if (const auto *UnwrappedCE = + dyn_cast<CXXConstructExpr>(TempExp->IgnoreImplicit())) + CEArg = UnwrappedCE; + } + } + if (CEArg->isStdInitListInitialization()) return true; } - return false; } return false; }; diff --git a/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp b/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp index 3b7a3de43ac..2920596e67c 100644 --- a/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp +++ b/clang-tools-extra/test/clang-tidy/modernize-make-unique.cpp @@ -1,5 +1,5 @@ -// RUN: %check_clang_tidy -std=c++14 %s modernize-make-unique %t -- -- -I %S/Inputs/modernize-smart-ptr -// FIXME: Fix the checker to work in C++17 mode. +// RUN: %check_clang_tidy -std=c++14,c++17 %s modernize-make-unique %t -- -- -I %S/Inputs/modernize-smart-ptr +// FIXME: Fix the test code in C++2a mode. #include "unique_ptr.h" #include "initializer_list.h" @@ -455,10 +455,10 @@ void initialization(int T, Base b) { std::unique_ptr<J> PJ2 = std::unique_ptr<J>(new J(E{1, 2}, 1)); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead - // CHECK-FIXES: std::unique_ptr<J> PJ2 = std::make_unique<J>(E{1, 2}, 1); + // CHECK-FIXES: std::unique_ptr<J> PJ2 = std::unique_ptr<J>(new J(E{1, 2}, 1)); PJ2.reset(new J(E{1, 2}, 1)); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead - // CHECK-FIXES: PJ2 = std::make_unique<J>(E{1, 2}, 1); + // CHECK-FIXES: PJ2.reset(new J(E{1, 2}, 1)); std::unique_ptr<J> PJ3 = std::unique_ptr<J>(new J{ {1, 2}, 1 }); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead @@ -469,10 +469,10 @@ void initialization(int T, Base b) { std::unique_ptr<J> PJ4 = std::unique_ptr<J>(new J{E{1, 2}, 1}); // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead - // CHECK-FIXES: std::unique_ptr<J> PJ4 = std::make_unique<J>(E{1, 2}, 1); + // CHECK-FIXES: std::unique_ptr<J> PJ4 = std::unique_ptr<J>(new J{E{1, 2}, 1}); PJ4.reset(new J{E{1, 2}, 1}); // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead - // CHECK-FIXES: PJ4 = std::make_unique<J>(E{1, 2}, 1); + // CHECK-FIXES: PJ4.reset(new J{E{1, 2}, 1}); std::unique_ptr<Foo> FF = std::unique_ptr<Foo>(new Foo()); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: |