<div dir="ltr">I think you also need to add the doc for the option in <span style="font-size:12.8px">modernize-use-emplace.rst.</span></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Aug 10, 2017 at 2:19 PM, Alexander Kornienko via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: alexfh<br>
Date: Thu Aug 10 05:19:05 2017<br>
New Revision: 310584<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=310584&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=310584&view=rev</a><br>
Log:<br>
[clang-tidy] Add modernize-use-emplace.<wbr>IgnoreImplicitConstructors option<br>
<br>
Added:<br>
clang-tools-extra/trunk/test/<wbr>clang-tidy/modernize-use-<wbr>emplace-ignore-implicit-<wbr>constructors.cpp<br>
Modified:<br>
clang-tools-extra/trunk/clang-<wbr>tidy/modernize/<wbr>UseEmplaceCheck.cpp<br>
clang-tools-extra/trunk/clang-<wbr>tidy/modernize/<wbr>UseEmplaceCheck.h<br>
<br>
Modified: clang-tools-extra/trunk/clang-<wbr>tidy/modernize/<wbr>UseEmplaceCheck.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseEmplaceCheck.cpp?rev=310584&r1=310583&r2=310584&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/clang-tools-extra/<wbr>trunk/clang-tidy/modernize/<wbr>UseEmplaceCheck.cpp?rev=<wbr>310584&r1=310583&r2=310584&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- clang-tools-extra/trunk/clang-<wbr>tidy/modernize/<wbr>UseEmplaceCheck.cpp (original)<br>
+++ clang-tools-extra/trunk/clang-<wbr>tidy/modernize/<wbr>UseEmplaceCheck.cpp Thu Aug 10 05:19:05 2017<br>
@@ -30,6 +30,7 @@ const auto DefaultTupleMakeFunctions = "<br>
<br>
UseEmplaceCheck::<wbr>UseEmplaceCheck(StringRef Name, ClangTidyContext *Context)<br>
: ClangTidyCheck(Name, Context),<br>
+ IgnoreImplicitConstructors(<wbr>Options.get("<wbr>IgnoreImplicitConstructors", 0)),<br>
ContainersWithPushBack(utils::<wbr>options::parseStringList(<wbr>Options.get(<br>
"ContainersWithPushBack", DefaultContainersWithPushBack)<wbr>)),<br>
SmartPointers(utils::options::<wbr>parseStringList(<br>
@@ -120,9 +121,13 @@ void UseEmplaceCheck::<wbr>registerMatchers(M<br>
<br>
void UseEmplaceCheck::check(const MatchFinder::MatchResult &Result) {<br>
const auto *Call = Result.Nodes.getNodeAs<<wbr>CXXMemberCallExpr>("call");<br>
- const auto *InnerCtorCall = Result.Nodes.getNodeAs<<wbr>CXXConstructExpr>("ctor");<br>
+ const auto *CtorCall = Result.Nodes.getNodeAs<<wbr>CXXConstructExpr>("ctor");<br>
const auto *MakeCall = Result.Nodes.getNodeAs<<wbr>CallExpr>("make");<br>
- assert((InnerCtorCall || MakeCall) && "No push_back parameter matched");<br>
+ assert((CtorCall || MakeCall) && "No push_back parameter matched");<br>
+<br>
+ if (IgnoreImplicitConstructors && CtorCall && CtorCall->getNumArgs() >= 1 &&<br>
+ CtorCall->getArg(0)-><wbr>getSourceRange() == CtorCall->getSourceRange())<br>
+ return;<br>
<br>
const auto FunctionNameSourceRange = CharSourceRange::getCharRange(<br>
Call->getExprLoc(), Call->getArg(0)->getExprLoc())<wbr>;<br>
@@ -138,14 +143,14 @@ void UseEmplaceCheck::check(const MatchF<br>
const SourceRange CallParensRange =<br>
MakeCall ? SourceRange(MakeCall-><wbr>getCallee()->getLocEnd(),<br>
MakeCall->getRParenLoc())<br>
- : InnerCtorCall-><wbr>getParenOrBraceRange();<br>
+ : CtorCall-><wbr>getParenOrBraceRange();<br>
<br>
// Finish if there is no explicit constructor call.<br>
if (CallParensRange.getBegin().<wbr>isInvalid())<br>
return;<br>
<br>
const SourceLocation ExprBegin =<br>
- MakeCall ? MakeCall->getExprLoc() : InnerCtorCall->getExprLoc();<br>
+ MakeCall ? MakeCall->getExprLoc() : CtorCall->getExprLoc();<br>
<br>
// Range for constructor name and opening brace.<br>
const auto ParamCallSourceRange =<br>
<br>
Modified: clang-tools-extra/trunk/clang-<wbr>tidy/modernize/<wbr>UseEmplaceCheck.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseEmplaceCheck.h?rev=310584&r1=310583&r2=310584&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/clang-tools-extra/<wbr>trunk/clang-tidy/modernize/<wbr>UseEmplaceCheck.h?rev=310584&<wbr>r1=310583&r2=310584&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- clang-tools-extra/trunk/clang-<wbr>tidy/modernize/<wbr>UseEmplaceCheck.h (original)<br>
+++ clang-tools-extra/trunk/clang-<wbr>tidy/modernize/<wbr>UseEmplaceCheck.h Thu Aug 10 05:19:05 2017<br>
@@ -33,10 +33,11 @@ public:<br>
void storeOptions(ClangTidyOptions:<wbr>:OptionMap &Opts) override;<br>
<br>
private:<br>
- std::vector<std::string> ContainersWithPushBack;<br>
- std::vector<std::string> SmartPointers;<br>
- std::vector<std::string> TupleTypes;<br>
- std::vector<std::string> TupleMakeFunctions;<br>
+ const bool IgnoreImplicitConstructors;<br>
+ const std::vector<std::string> ContainersWithPushBack;<br>
+ const std::vector<std::string> SmartPointers;<br>
+ const std::vector<std::string> TupleTypes;<br>
+ const std::vector<std::string> TupleMakeFunctions;<br>
};<br>
<br>
} // namespace modernize<br>
<br>
Added: clang-tools-extra/trunk/test/<wbr>clang-tidy/modernize-use-<wbr>emplace-ignore-implicit-<wbr>constructors.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-emplace-ignore-implicit-constructors.cpp?rev=310584&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/clang-tools-extra/<wbr>trunk/test/clang-tidy/<wbr>modernize-use-emplace-ignore-<wbr>implicit-constructors.cpp?rev=<wbr>310584&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- clang-tools-extra/trunk/test/<wbr>clang-tidy/modernize-use-<wbr>emplace-ignore-implicit-<wbr>constructors.cpp (added)<br>
+++ clang-tools-extra/trunk/test/<wbr>clang-tidy/modernize-use-<wbr>emplace-ignore-implicit-<wbr>constructors.cpp Thu Aug 10 05:19:05 2017<br>
@@ -0,0 +1,123 @@<br>
+// RUN: %check_clang_tidy %s modernize-use-emplace %t -- \<br>
+// RUN: -config="{CheckOptions: \<br>
+// RUN: [{key: modernize-use-emplace.<wbr>IgnoreImplicitConstructors, \<br>
+// RUN: value: 1}] \<br>
+// RUN: }" -- -std=c++11<br>
+<br>
+namespace std {<br>
+template <typename><br>
+class initializer_list<br>
+{<br>
+public:<br>
+ initializer_list() noexcept {}<br>
+};<br>
+<br>
+template <typename T><br>
+class vector {<br>
+public:<br>
+ vector() = default;<br>
+ vector(initializer_list<T>) {}<br>
+<br>
+ void push_back(const T &) {}<br>
+ void push_back(T &&) {}<br>
+<br>
+ template <typename... Args><br>
+ void emplace_back(Args &&... args){};<br>
+ ~vector();<br>
+};<br>
+<br>
+} // namespace std<br>
+<br>
+void testInts() {<br>
+ std::vector<int> v;<br>
+ v.push_back(42);<br>
+ v.push_back(int(42));<br>
+ v.push_back(int{42});<br>
+ v.push_back(42.0);<br>
+ int z;<br>
+ v.push_back(z);<br>
+}<br>
+<br>
+struct Something {<br>
+ Something(int a, int b = 41) {}<br>
+ Something() {}<br>
+ void push_back(Something);<br>
+ int getInt() { return 42; }<br>
+};<br>
+<br>
+struct Convertable {<br>
+ operator Something() { return Something{}; }<br>
+};<br>
+<br>
+struct Zoz {<br>
+ Zoz(Something, int = 42) {}<br>
+};<br>
+<br>
+Zoz getZoz(Something s) { return Zoz(s); }<br>
+<br>
+void test_Something() {<br>
+ std::vector<Something> v;<br>
+<br>
+ v.push_back(Something(1, 2));<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back instead of push_back [modernize-use-emplace]<br>
+ // CHECK-FIXES: v.emplace_back(1, 2);<br>
+<br>
+ v.push_back(Something{1, 2});<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back<br>
+ // CHECK-FIXES: v.emplace_back(1, 2);<br>
+<br>
+ v.push_back(Something());<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back<br>
+ // CHECK-FIXES: v.emplace_back();<br>
+<br>
+ v.push_back(Something{});<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back<br>
+ // CHECK-FIXES: v.emplace_back();<br>
+<br>
+ Something Different;<br>
+ v.push_back(Something(<wbr>Different.getInt(), 42));<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back<br>
+ // CHECK-FIXES: v.emplace_back(Different.<wbr>getInt(), 42);<br>
+<br>
+ v.push_back(Different.getInt()<wbr>);<br>
+ v.push_back(42);<br>
+<br>
+ Something temporary(42, 42);<br>
+ temporary.push_back(temporary)<wbr>;<br>
+ v.push_back(temporary);<br>
+<br>
+ v.push_back(Convertable());<br>
+ v.push_back(Convertable{});<br>
+ Convertable s;<br>
+ v.push_back(s);<br>
+}<br>
+<br>
+template <typename ElemType><br>
+void dependOnElem() {<br>
+ std::vector<ElemType> v;<br>
+ v.push_back(ElemType(42));<br>
+}<br>
+<br>
+template <typename ContainerType><br>
+void dependOnContainer() {<br>
+ ContainerType v;<br>
+ v.push_back(Something(42));<br>
+}<br>
+<br>
+void callDependent() {<br>
+ dependOnElem<Something>();<br>
+ dependOnContainer<std::vector<<wbr>Something>>();<br>
+}<br>
+<br>
+void test2() {<br>
+ std::vector<Zoz> v;<br>
+ v.push_back(Zoz(Something(21, 37)));<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back<br>
+ // CHECK-FIXES: v.emplace_back(Something(21, 37));<br>
+<br>
+ v.push_back(Zoz(Something(21, 37), 42));<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use emplace_back<br>
+ // CHECK-FIXES: v.emplace_back(Something(21, 37), 42);<br>
+<br>
+ v.push_back(getZoz(Something(<wbr>1, 2)));<br>
+}<br>
<br>
<br>
______________________________<wbr>_________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>