2016-06-21 23:23:27 +08:00
|
|
|
//===--- UseEmplaceCheck.cpp - clang-tidy----------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UseEmplaceCheck.h"
|
2016-07-29 10:10:23 +08:00
|
|
|
#include "../utils/OptionsUtils.h"
|
2016-06-21 23:23:27 +08:00
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace modernize {
|
|
|
|
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
namespace {
|
|
|
|
AST_MATCHER(DeclRefExpr, hasExplicitTemplateArgs) {
|
|
|
|
return Node.hasExplicitTemplateArgs();
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto DefaultContainersWithPushBack =
|
2016-07-29 10:10:23 +08:00
|
|
|
"::std::vector; ::std::list; ::std::deque";
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
const auto DefaultSmartPointers =
|
2016-07-29 10:10:23 +08:00
|
|
|
"::std::shared_ptr; ::std::unique_ptr; ::std::auto_ptr; ::std::weak_ptr";
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
const auto DefaultTupleTypes = "::std::pair; ::std::tuple";
|
|
|
|
const auto DefaultTupleMakeFunctions = "::std::make_pair; ::std::make_tuple";
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
} // namespace
|
2016-07-29 10:10:23 +08:00
|
|
|
|
|
|
|
UseEmplaceCheck::UseEmplaceCheck(StringRef Name, ClangTidyContext *Context)
|
|
|
|
: ClangTidyCheck(Name, Context),
|
2017-08-10 20:19:05 +08:00
|
|
|
IgnoreImplicitConstructors(Options.get("IgnoreImplicitConstructors", 0)),
|
2016-07-29 10:10:23 +08:00
|
|
|
ContainersWithPushBack(utils::options::parseStringList(Options.get(
|
|
|
|
"ContainersWithPushBack", DefaultContainersWithPushBack))),
|
|
|
|
SmartPointers(utils::options::parseStringList(
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
Options.get("SmartPointers", DefaultSmartPointers))),
|
|
|
|
TupleTypes(utils::options::parseStringList(
|
|
|
|
Options.get("TupleTypes", DefaultTupleTypes))),
|
|
|
|
TupleMakeFunctions(utils::options::parseStringList(
|
|
|
|
Options.get("TupleMakeFunctions", DefaultTupleMakeFunctions))) {}
|
2016-07-29 10:10:23 +08:00
|
|
|
|
2016-06-21 23:23:27 +08:00
|
|
|
void UseEmplaceCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
if (!getLangOpts().CPlusPlus11)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Bunch of functionality that could be easily added:
|
|
|
|
// + add handling of `push_front` for std::forward_list, std::list
|
|
|
|
// and std::deque.
|
|
|
|
// + add handling of `push` for std::stack, std::queue, std::priority_queue
|
|
|
|
// + add handling of `insert` for stl associative container, but be careful
|
|
|
|
// because this requires special treatment (it could cause performance
|
|
|
|
// regression)
|
|
|
|
// + match for emplace calls that should be replaced with insertion
|
2017-05-01 05:12:56 +08:00
|
|
|
auto CallPushBack = cxxMemberCallExpr(
|
2016-06-21 23:23:27 +08:00
|
|
|
hasDeclaration(functionDecl(hasName("push_back"))),
|
2016-07-29 10:10:23 +08:00
|
|
|
on(hasType(cxxRecordDecl(hasAnyName(SmallVector<StringRef, 5>(
|
|
|
|
ContainersWithPushBack.begin(), ContainersWithPushBack.end()))))));
|
2016-06-21 23:23:27 +08:00
|
|
|
|
|
|
|
// We can't replace push_backs of smart pointer because
|
|
|
|
// if emplacement fails (f.e. bad_alloc in vector) we will have leak of
|
|
|
|
// passed pointer because smart pointer won't be constructed
|
|
|
|
// (and destructed) as in push_back case.
|
2017-05-01 05:12:56 +08:00
|
|
|
auto IsCtorOfSmartPtr = hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(
|
2016-07-29 10:10:23 +08:00
|
|
|
SmallVector<StringRef, 5>(SmartPointers.begin(), SmartPointers.end())))));
|
2016-06-21 23:23:27 +08:00
|
|
|
|
|
|
|
// Bitfields binds only to consts and emplace_back take it by universal ref.
|
2017-05-01 05:12:56 +08:00
|
|
|
auto BitFieldAsArgument = hasAnyArgument(
|
2016-07-29 10:10:23 +08:00
|
|
|
ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField())))));
|
|
|
|
|
|
|
|
// Initializer list can't be passed to universal reference.
|
2017-05-01 05:12:56 +08:00
|
|
|
auto InitializerListAsArgument = hasAnyArgument(
|
2016-07-29 10:10:23 +08:00
|
|
|
ignoringImplicit(cxxConstructExpr(isListInitialization())));
|
2016-06-21 23:23:27 +08:00
|
|
|
|
|
|
|
// We could have leak of resource.
|
2017-05-01 05:12:56 +08:00
|
|
|
auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr()));
|
2016-07-29 10:10:23 +08:00
|
|
|
// We would call another constructor.
|
2017-05-01 05:12:56 +08:00
|
|
|
auto ConstructingDerived =
|
2016-06-21 23:23:27 +08:00
|
|
|
hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase)));
|
|
|
|
|
2016-07-29 10:10:23 +08:00
|
|
|
// emplace_back can't access private constructor.
|
2017-05-01 05:12:56 +08:00
|
|
|
auto IsPrivateCtor = hasDeclaration(cxxConstructorDecl(isPrivate()));
|
2016-07-29 10:10:23 +08:00
|
|
|
|
2017-05-06 04:35:30 +08:00
|
|
|
auto HasInitList = anyOf(has(ignoringImplicit(initListExpr())),
|
2017-05-06 07:00:37 +08:00
|
|
|
has(cxxStdInitializerListExpr()));
|
2017-05-06 04:35:30 +08:00
|
|
|
|
2016-07-29 10:10:23 +08:00
|
|
|
// FIXME: Discard 0/NULL (as nullptr), static inline const data members,
|
|
|
|
// overloaded functions and template names.
|
2017-05-01 05:12:56 +08:00
|
|
|
auto SoughtConstructExpr =
|
2016-06-21 23:23:27 +08:00
|
|
|
cxxConstructExpr(
|
2017-05-01 05:12:56 +08:00
|
|
|
unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument,
|
|
|
|
InitializerListAsArgument, NewExprAsArgument,
|
|
|
|
ConstructingDerived, IsPrivateCtor)))
|
2016-06-21 23:23:27 +08:00
|
|
|
.bind("ctor");
|
2017-05-01 05:12:56 +08:00
|
|
|
auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr));
|
2016-06-21 23:23:27 +08:00
|
|
|
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
auto MakeTuple = ignoringImplicit(
|
|
|
|
callExpr(
|
|
|
|
callee(expr(ignoringImplicit(declRefExpr(
|
|
|
|
unless(hasExplicitTemplateArgs()),
|
|
|
|
to(functionDecl(hasAnyName(SmallVector<StringRef, 2>(
|
|
|
|
TupleMakeFunctions.begin(), TupleMakeFunctions.end())))))))))
|
|
|
|
.bind("make"));
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
// make_something can return type convertible to container's element type.
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
// Allow the conversion only on containers of pairs.
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
auto MakeTupleCtor = ignoringImplicit(cxxConstructExpr(
|
|
|
|
has(materializeTemporaryExpr(MakeTuple)),
|
|
|
|
hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(
|
|
|
|
SmallVector<StringRef, 2>(TupleTypes.begin(), TupleTypes.end())))))));
|
2016-06-21 23:23:27 +08:00
|
|
|
|
2017-05-01 05:12:56 +08:00
|
|
|
auto SoughtParam = materializeTemporaryExpr(
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
anyOf(has(MakeTuple), has(MakeTupleCtor),
|
2017-05-01 05:12:56 +08:00
|
|
|
HasConstructExpr, has(cxxFunctionalCastExpr(HasConstructExpr))));
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
|
2017-05-01 05:12:56 +08:00
|
|
|
Finder->addMatcher(cxxMemberCallExpr(CallPushBack, has(SoughtParam),
|
2016-07-29 10:10:23 +08:00
|
|
|
unless(isInTemplateInstantiation()))
|
|
|
|
.bind("call"),
|
|
|
|
this);
|
2016-06-21 23:23:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UseEmplaceCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>("call");
|
2017-08-10 20:19:05 +08:00
|
|
|
const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>("ctor");
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
const auto *MakeCall = Result.Nodes.getNodeAs<CallExpr>("make");
|
2017-08-10 20:19:05 +08:00
|
|
|
assert((CtorCall || MakeCall) && "No push_back parameter matched");
|
|
|
|
|
|
|
|
if (IgnoreImplicitConstructors && CtorCall && CtorCall->getNumArgs() >= 1 &&
|
|
|
|
CtorCall->getArg(0)->getSourceRange() == CtorCall->getSourceRange())
|
|
|
|
return;
|
2016-06-21 23:23:27 +08:00
|
|
|
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
const auto FunctionNameSourceRange = CharSourceRange::getCharRange(
|
2016-06-21 23:23:27 +08:00
|
|
|
Call->getExprLoc(), Call->getArg(0)->getExprLoc());
|
|
|
|
|
|
|
|
auto Diag = diag(Call->getExprLoc(), "use emplace_back instead of push_back");
|
|
|
|
|
|
|
|
if (FunctionNameSourceRange.getBegin().isMacroID())
|
|
|
|
return;
|
|
|
|
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
const auto *EmplacePrefix = MakeCall ? "emplace_back" : "emplace_back(";
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
Diag << FixItHint::CreateReplacement(FunctionNameSourceRange, EmplacePrefix);
|
2016-06-21 23:23:27 +08:00
|
|
|
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
const SourceRange CallParensRange =
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
MakeCall ? SourceRange(MakeCall->getCallee()->getLocEnd(),
|
|
|
|
MakeCall->getRParenLoc())
|
2017-08-10 20:19:05 +08:00
|
|
|
: CtorCall->getParenOrBraceRange();
|
2016-06-21 23:23:27 +08:00
|
|
|
|
|
|
|
// Finish if there is no explicit constructor call.
|
|
|
|
if (CallParensRange.getBegin().isInvalid())
|
|
|
|
return;
|
|
|
|
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
const SourceLocation ExprBegin =
|
2017-08-10 20:19:05 +08:00
|
|
|
MakeCall ? MakeCall->getExprLoc() : CtorCall->getExprLoc();
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
|
2016-06-21 23:23:27 +08:00
|
|
|
// Range for constructor name and opening brace.
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
const auto ParamCallSourceRange =
|
|
|
|
CharSourceRange::getTokenRange(ExprBegin, CallParensRange.getBegin());
|
2016-06-21 23:23:27 +08:00
|
|
|
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
Diag << FixItHint::CreateRemoval(ParamCallSourceRange)
|
2016-07-29 10:10:23 +08:00
|
|
|
<< FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
|
[clang-tidy] modernize-use-emplace: remove unnecessary make_pair calls
Summary:
When there is a push_back with a call to make_pair, turn it into emplace_back and remove the unnecessary make_pair call.
Eg.
```
std::vector<std::pair<int, int>> v;
v.push_back(std::make_pair(1, 2)); // --> v.emplace_back(1, 2);
```
make_pair doesn't get removed when explicit template parameters are provided, because of potential problems with type conversions.
Reviewers: Prazek, aaron.ballman, hokein, alexfh
Reviewed By: Prazek, alexfh
Subscribers: JDevlieghere, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32395
llvm-svn: 301651
2017-04-29 00:25:45 +08:00
|
|
|
CallParensRange.getEnd(), CallParensRange.getEnd()));
|
2016-07-29 10:10:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UseEmplaceCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
|
|
|
|
Options.store(Opts, "ContainersWithPushBack",
|
|
|
|
utils::options::serializeStringList(ContainersWithPushBack));
|
|
|
|
Options.store(Opts, "SmartPointers",
|
|
|
|
utils::options::serializeStringList(SmartPointers));
|
[clang-tidy] modernize-use-emplace: Remove unnecessary make_tuple calls
Summary:
This patch makes modernize-use-emplace remove unnecessary make_ calls from push_back calls and turn them into emplace_back -- the same way make_pair calls are handled.
Custom make_ calls can be removed for custom tuple-like types -- two new options that control that are `TupleTypes` and `TupleMakeFunctions`. By default, the check removes calls to `std::make_pair` and `std::make_tuple`.
Eq.
```
std::vector<std::tuple<int, char, bool>> v;
v.push_back(std::make_tuple(1, 'A', true)); // --> v.emplace_back(1, 'A', true);
```
Reviewers: alexfh, aaron.ballman, Prazek, hokein
Reviewed By: Prazek
Subscribers: JDevlieghere, xazax.hun, JonasToth, cfe-commits
Tags: #clang-tools-extra
Differential Revision: https://reviews.llvm.org/D32690
llvm-svn: 303145
2017-05-16 14:32:38 +08:00
|
|
|
Options.store(Opts, "TupleTypes",
|
|
|
|
utils::options::serializeStringList(TupleTypes));
|
|
|
|
Options.store(Opts, "TupleMakeFunctions",
|
|
|
|
utils::options::serializeStringList(TupleMakeFunctions));
|
2016-06-21 23:23:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace modernize
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|