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