[PATCH] D32767: [clang-tidy] Fix PR32896: detect initializer lists in modernize-use-empalce
Jakub Kuderski via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed May 3 12:17:37 PDT 2017
kuhar updated this revision to Diff 97704.
kuhar added a comment.
I switched from a narrowing matcher to a "normal" node matcher.
Repository:
rL LLVM
https://reviews.llvm.org/D32767
Files:
clang-tidy/modernize/UseEmplaceCheck.cpp
test/clang-tidy/modernize-use-emplace.cpp
Index: test/clang-tidy/modernize-use-emplace.cpp
===================================================================
--- test/clang-tidy/modernize-use-emplace.cpp
+++ test/clang-tidy/modernize-use-emplace.cpp
@@ -4,9 +4,19 @@
// RUN: value: '::std::vector; ::std::list; ::std::deque; llvm::LikeASmallVector'}]}" -- -std=c++11
namespace std {
+template <typename>
+class initializer_list
+{
+public:
+ initializer_list() noexcept {}
+};
+
template <typename T>
class vector {
public:
+ vector() = default;
+ vector(initializer_list<T>) {}
+
void push_back(const T &) {}
void push_back(T &&) {}
@@ -455,3 +465,16 @@
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back
// CHECK-FIXES: v.emplace_back(42);
}
+
+void testInitializerList() {
+ std::vector<std::vector<int>> v;
+ v.push_back(std::vector<int>({1}));
+ // Test against the bug reported in PR32896.
+
+ v.push_back({{2}});
+
+ using PairIntVector = std::pair<int, std::vector<int>>;
+ std::vector<PairIntVector> x;
+ x.push_back(PairIntVector(3, {4}));
+ x.push_back({5, {6}});
+}
Index: clang-tidy/modernize/UseEmplaceCheck.cpp
===================================================================
--- clang-tidy/modernize/UseEmplaceCheck.cpp
+++ clang-tidy/modernize/UseEmplaceCheck.cpp
@@ -20,6 +20,12 @@
return Node.hasExplicitTemplateArgs();
}
+namespace impl {
+// FIXME: This matcher should be replaced by a matcher from ASTMatcher.h
+const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt,
+ CXXStdInitializerListExpr> cxxStdInitializerListExpr;
+} // namespace impl
+
const auto DefaultContainersWithPushBack =
"::std::vector; ::std::list; ::std::deque";
const auto DefaultSmartPointers =
@@ -74,7 +80,11 @@
// emplace_back can't access private constructor.
auto IsPrivateCtor = hasDeclaration(cxxConstructorDecl(isPrivate()));
- auto HasInitList = has(ignoringImplicit(initListExpr()));
+ auto HasInitList = anyOf(has(ignoringImplicit(initListExpr())),
+ has(impl::cxxStdInitializerListExpr()));
+ // FIXME: Replace internal C++ initializer list matcher with one from
+ // ASTMatchers.h
+
// FIXME: Discard 0/NULL (as nullptr), static inline const data members,
// overloaded functions and template names.
auto SoughtConstructExpr =
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32767.97704.patch
Type: text/x-patch
Size: 2325 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170503/ac46ae03/attachment.bin>
More information about the cfe-commits
mailing list