1 //===--- CleanupCtadCheck.cpp - clang-tidy --------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "CleanupCtadCheck.h"
10 #include "../utils/TransformerClangTidyCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 #include "clang/Tooling/Transformer/RangeSelector.h"
15 #include "clang/Tooling/Transformer/RewriteRule.h"
16 #include "clang/Tooling/Transformer/Stencil.h"
17 #include "llvm/ADT/StringRef.h"
19 using namespace ::clang::ast_matchers
;
20 using namespace ::clang::transformer
;
22 namespace clang::tidy::abseil
{
24 RewriteRuleWith
<std::string
> CleanupCtadCheckImpl() {
25 auto warning_message
= cat("prefer absl::Cleanup's class template argument "
26 "deduction pattern in C++17 and higher");
29 declStmt(hasSingleDecl(varDecl(
30 hasType(autoType()), hasTypeLoc(typeLoc().bind("auto_type_loc")),
31 hasInitializer(hasDescendant(
32 callExpr(callee(functionDecl(hasName("absl::MakeCleanup"))),
34 .bind("make_cleanup_call")))))),
35 {changeTo(node("auto_type_loc"), cat("absl::Cleanup")),
36 changeTo(node("make_cleanup_call"), cat(callArgs("make_cleanup_call")))},
40 CleanupCtadCheck::CleanupCtadCheck(StringRef Name
, ClangTidyContext
*Context
)
41 : utils::TransformerClangTidyCheck(CleanupCtadCheckImpl(), Name
, Context
) {}
43 } // namespace clang::tidy::abseil