11 #include "../utils/OptionsUtils.h" 20 return Node.hasExplicitTemplateArgs();
23 const auto DefaultContainersWithPushBack =
24 "::std::vector; ::std::list; ::std::deque";
25 const auto DefaultSmartPointers =
26 "::std::shared_ptr; ::std::unique_ptr; ::std::auto_ptr; ::std::weak_ptr";
27 const auto DefaultTupleTypes =
"::std::pair; ::std::tuple";
28 const auto DefaultTupleMakeFunctions =
"::std::make_pair; ::std::make_tuple";
33 IgnoreImplicitConstructors(Options.get(
"IgnoreImplicitConstructors", 0)),
35 "ContainersWithPushBack", DefaultContainersWithPushBack))),
37 Options.get(
"SmartPointers", DefaultSmartPointers))),
39 Options.get(
"TupleTypes", DefaultTupleTypes))),
41 Options.get(
"TupleMakeFunctions", DefaultTupleMakeFunctions))) {}
55 auto CallPushBack = cxxMemberCallExpr(
56 hasDeclaration(functionDecl(hasName(
"push_back"))),
57 on(hasType(cxxRecordDecl(hasAnyName(SmallVector<StringRef, 5>(
58 ContainersWithPushBack.begin(), ContainersWithPushBack.end()))))));
64 auto IsCtorOfSmartPtr = hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(
65 SmallVector<StringRef, 5>(SmartPointers.begin(), SmartPointers.end())))));
68 auto BitFieldAsArgument = hasAnyArgument(
69 ignoringImplicit(memberExpr(hasDeclaration(fieldDecl(isBitField())))));
72 auto InitializerListAsArgument = hasAnyArgument(
73 ignoringImplicit(cxxConstructExpr(isListInitialization())));
76 auto NewExprAsArgument = hasAnyArgument(ignoringImplicit(cxxNewExpr()));
78 auto ConstructingDerived =
79 hasParent(implicitCastExpr(hasCastKind(CastKind::CK_DerivedToBase)));
82 auto IsPrivateCtor = hasDeclaration(cxxConstructorDecl(isPrivate()));
84 auto HasInitList = anyOf(has(ignoringImplicit(initListExpr())),
85 has(cxxStdInitializerListExpr()));
89 auto SoughtConstructExpr =
91 unless(anyOf(IsCtorOfSmartPtr, HasInitList, BitFieldAsArgument,
92 InitializerListAsArgument, NewExprAsArgument,
93 ConstructingDerived, IsPrivateCtor)))
95 auto HasConstructExpr = has(ignoringImplicit(SoughtConstructExpr));
97 auto MakeTuple = ignoringImplicit(
99 callee(expr(ignoringImplicit(declRefExpr(
100 unless(hasExplicitTemplateArgs()),
101 to(functionDecl(hasAnyName(SmallVector<StringRef, 2>(
102 TupleMakeFunctions.begin(), TupleMakeFunctions.end())))))))))
107 auto MakeTupleCtor = ignoringImplicit(cxxConstructExpr(
108 has(materializeTemporaryExpr(MakeTuple)),
109 hasDeclaration(cxxConstructorDecl(ofClass(hasAnyName(
110 SmallVector<StringRef, 2>(TupleTypes.begin(), TupleTypes.end())))))));
112 auto SoughtParam = materializeTemporaryExpr(
113 anyOf(has(MakeTuple), has(MakeTupleCtor),
114 HasConstructExpr, has(cxxFunctionalCastExpr(HasConstructExpr))));
116 Finder->addMatcher(cxxMemberCallExpr(CallPushBack, has(SoughtParam),
117 unless(isInTemplateInstantiation()))
123 const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"call");
124 const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>(
"ctor");
125 const auto *MakeCall = Result.Nodes.getNodeAs<CallExpr>(
"make");
126 assert((CtorCall || MakeCall) &&
"No push_back parameter matched");
128 if (IgnoreImplicitConstructors && CtorCall && CtorCall->getNumArgs() >= 1 &&
129 CtorCall->getArg(0)->getSourceRange() == CtorCall->getSourceRange())
132 const auto FunctionNameSourceRange = CharSourceRange::getCharRange(
133 Call->getExprLoc(), Call->getArg(0)->getExprLoc());
135 auto Diag =
diag(Call->getExprLoc(),
"use emplace_back instead of push_back");
137 if (FunctionNameSourceRange.getBegin().isMacroID())
140 const auto *EmplacePrefix = MakeCall ?
"emplace_back" :
"emplace_back(";
141 Diag << FixItHint::CreateReplacement(FunctionNameSourceRange, EmplacePrefix);
143 const SourceRange CallParensRange =
144 MakeCall ? SourceRange(MakeCall->getCallee()->getLocEnd(),
145 MakeCall->getRParenLoc())
146 : CtorCall->getParenOrBraceRange();
149 if (CallParensRange.getBegin().isInvalid())
152 const SourceLocation ExprBegin =
153 MakeCall ? MakeCall->getExprLoc() : CtorCall->getExprLoc();
156 const auto ParamCallSourceRange =
157 CharSourceRange::getTokenRange(ExprBegin, CallParensRange.getBegin());
159 Diag << FixItHint::CreateRemoval(ParamCallSourceRange)
160 << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
161 CallParensRange.getEnd(), CallParensRange.getEnd()));
AST_MATCHER(BinaryOperator, isAssignmentOperator)
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
LangOptions getLangOpts() const
Returns the language options from the context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Base class for all clang-tidy checks.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.