10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
22ast_matchers::internal::BindableMatcher<Stmt>
23handleFrom(
const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle,
24 const ast_matchers::internal::Matcher<Expr> &Arg) {
26 anyOf(cxxConstructExpr(hasDeclaration(cxxMethodDecl(ofClass(IsAHandle))),
28 cxxMemberCallExpr(hasType(hasUnqualifiedDesugaredType(recordType(
29 hasDeclaration(cxxRecordDecl(IsAHandle))))),
30 callee(memberExpr(member(cxxConversionDecl()))),
34ast_matchers::internal::Matcher<Stmt> handleFromTemporaryValue(
35 const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
37 const auto TemporaryExpr = anyOf(
38 cxxBindTemporaryExpr(),
39 cxxFunctionalCastExpr(
40 hasCastKind(CK_ConstructorConversion),
41 hasSourceExpression(ignoringParenImpCasts(cxxBindTemporaryExpr()))));
45 const auto TemporaryTernary = conditionalOperator(
46 hasTrueExpression(ignoringParenImpCasts(TemporaryExpr)),
47 hasFalseExpression(ignoringParenImpCasts(TemporaryExpr)));
49 return handleFrom(IsAHandle, anyOf(TemporaryExpr, TemporaryTernary));
52ast_matchers::internal::Matcher<RecordDecl> isASequence() {
53 return hasAnyName(
"::std::deque",
"::std::forward_list",
"::std::list",
57ast_matchers::internal::Matcher<RecordDecl> isASet() {
58 return hasAnyName(
"::std::set",
"::std::multiset",
"::std::unordered_set",
59 "::std::unordered_multiset");
62ast_matchers::internal::Matcher<RecordDecl> isAMap() {
63 return hasAnyName(
"::std::map",
"::std::multimap",
"::std::unordered_map",
64 "::std::unordered_multimap");
67ast_matchers::internal::BindableMatcher<Stmt> makeContainerMatcher(
68 const ast_matchers::internal::Matcher<RecordDecl> &IsAHandle) {
77 ignoringParenImpCasts(handleFromTemporaryValue(IsAHandle))),
81 callee(functionDecl(hasAnyName(
"assign",
"push_back",
"resize"))),
82 on(expr(hasType(hasUnqualifiedDesugaredType(
83 recordType(hasDeclaration(recordDecl(isASequence())))))))),
85 cxxMemberCallExpr(callee(functionDecl(hasName(
"insert"))),
86 on(expr(hasType(hasUnqualifiedDesugaredType(
87 recordType(hasDeclaration(recordDecl(
88 anyOf(isASequence(), isASet()))))))))),
90 cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(isAMap()))),
91 hasOverloadedOperatorName(
"[]"))));
99 HandleClasses(utils::options::parseStringList(Options.get(
101 "std::basic_string_view;std::experimental::basic_string_view"))),
102 IsAHandle(cxxRecordDecl(hasAnyName(HandleClasses)).bind(
"handle")) {}
109void DanglingHandleCheck::registerMatchersForVariables(MatchFinder *Finder) {
110 const auto ConvertedHandle = handleFromTemporaryValue(IsAHandle);
114 varDecl(hasType(hasUnqualifiedDesugaredType(
115 recordType(hasDeclaration(cxxRecordDecl(IsAHandle))))),
116 unless(parmVarDecl()),
118 exprWithCleanups(ignoringElidableConstructorCall(has(
119 ignoringParenImpCasts(ConvertedHandle))))
126 cxxOperatorCallExpr(callee(cxxMethodDecl(ofClass(IsAHandle))),
127 hasOverloadedOperatorName(
"="),
128 hasArgument(1, ConvertedHandle))
134 traverse(TK_AsIs, makeContainerMatcher(IsAHandle).bind(
"bad_stmt")),
138void DanglingHandleCheck::registerMatchersForReturn(MatchFinder *Finder) {
147 has(ignoringImplicit(ignoringElidableConstructorCall(
148 ignoringImplicit(handleFrom(
150 declRefExpr(to(varDecl(
152 hasAutomaticStorageDuration(),
154 anyOf(hasType(arrayType()),
155 hasType(hasUnqualifiedDesugaredType(
156 recordType(hasDeclaration(recordDecl(
157 unless(IsAHandle))))))))))))))),
159 unless(hasAncestor(lambdaExpr())))
166 returnStmt(has(exprWithCleanups(ignoringElidableConstructorCall(
167 has(ignoringParenImpCasts(
168 handleFromTemporaryValue(IsAHandle)))))))
174 registerMatchersForVariables(Finder);
175 registerMatchersForReturn(Finder);
179 auto *Handle = Result.Nodes.getNodeAs<CXXRecordDecl>(
"handle");
180 diag(Result.Nodes.getNodeAs<Stmt>(
"bad_stmt")->getBeginLoc(),
181 "%0 outlives its value")
182 << Handle->getQualifiedNameAsString();
llvm::SmallString< 256U > Name
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
DanglingHandleCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap