1//===--- CleanupCtadCheck.cpp - clang-tidy --------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
18
19using namespace ::clang::ast_matchers;
20using namespace ::clang::transformer;
21
22namespace clang::tidy::abseil {
23
24RewriteRuleWith<std::string> CleanupCtadCheckImpl() {
25 auto warning_message = cat(Parts: "prefer absl::Cleanup's class template argument "
26 "deduction pattern in C++17 and higher");
27
28 return makeRule(
29 M: declStmt(hasSingleDecl(InnerMatcher: varDecl(
30 hasType(InnerMatcher: autoType()), hasTypeLoc(Inner: typeLoc().bind(ID: "auto_type_loc")),
31 hasInitializer(InnerMatcher: hasDescendant(
32 callExpr(callee(InnerMatcher: functionDecl(hasName(Name: "absl::MakeCleanup"))),
33 argumentCountIs(N: 1))
34 .bind(ID: "make_cleanup_call")))))),
35 Edits: {changeTo(Target: node(ID: "auto_type_loc"), Replacement: cat(Parts: "absl::Cleanup")),
36 changeTo(Target: node(ID: "make_cleanup_call"), Replacement: cat(Parts: callArgs(ID: "make_cleanup_call")))},
37 Metadata: warning_message);
38}
39
40CleanupCtadCheck::CleanupCtadCheck(StringRef Name, ClangTidyContext *Context)
41 : utils::TransformerClangTidyCheck(CleanupCtadCheckImpl(), Name, Context) {}
42
43} // namespace clang::tidy::abseil
44

source code of clang-tools-extra/clang-tidy/abseil/CleanupCtadCheck.cpp