================
@@ -0,0 +1,475 @@
+//===--- UseSharedPtrArrayCheck.cpp - clang-tidy
--------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UseSharedPtrArrayCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/AST/Stmt.h"
+#include "clang/AST/TypeLoc.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+void UseSharedPtrArrayCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ cxxConstructExpr(
+
+ hasType(qualType(hasDeclaration(
+ classTemplateSpecializationDecl(hasName("::std::shared_ptr"))))),
+
+ argumentCountIs(2),
+
+ hasArgument(
+ 0, ignoringParenImpCasts(cxxNewExpr(isArray()).bind("newExpr"))),
+
+ anyOf(hasArgument(
+ 1, ignoringImplicit(
+ cxxConstructExpr(
+
hasType(qualType(hasCanonicalType(hasDeclaration(
+ classTemplateSpecializationDecl(
+ hasName("::std::default_delete")))))))
+ .bind("defaultDelete"))),
+
+ hasArgument(
+ 1, ignoringImplicit(lambdaExpr().bind("lambdaDeleter"))),
+
+ hasArgument(1, ignoringImplicit(declRefExpr(to(
+ functionDecl().bind("deleterFunction")))))))
+ .bind("sharedPtrCtor"),
+ this);
+}
+
+// Verifies that a callable body consists of exactly delete[] param; with only
+// one parameter. Used for both lambdas and free functions.
+static bool bodyIsArrayDeleteOfParam(const Stmt *Body,
----------------
zwuis wrote:
Try to make it an AST matcher, so that we can write matchers like
```cpp
functionDecl(
hasParameter(0, parmVarDecl().bind(...)),
hasBody(/* use `equalBoundNode` macher */))
```
. See clang/include/clang/ASTMatchers/ASTMatchersMacros.h and implementation of
other clang-tidy checks for creating matchers.
https://github.com/llvm/llvm-project/pull/199458
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits