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