CleanupCtadCheck.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #include "CleanupCtadCheck.h"
  9. #include "../utils/TransformerClangTidyCheck.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/ASTMatchers/ASTMatchFinder.h"
  12. #include "clang/ASTMatchers/ASTMatchers.h"
  13. #include "clang/Tooling/Transformer/RangeSelector.h"
  14. #include "clang/Tooling/Transformer/RewriteRule.h"
  15. #include "clang/Tooling/Transformer/Stencil.h"
  16. #include "llvm/ADT/StringRef.h"
  17. using namespace ::clang::ast_matchers;
  18. using namespace ::clang::transformer;
  19. namespace clang::tidy::abseil {
  20. RewriteRuleWith<std::string> CleanupCtadCheckImpl() {
  21. auto warning_message = cat("prefer absl::Cleanup's class template argument "
  22. "deduction pattern in C++17 and higher");
  23. return makeRule(
  24. declStmt(hasSingleDecl(varDecl(
  25. hasType(autoType()), hasTypeLoc(typeLoc().bind("auto_type_loc")),
  26. hasInitializer(hasDescendant(
  27. callExpr(callee(functionDecl(hasName("absl::MakeCleanup"))),
  28. argumentCountIs(1))
  29. .bind("make_cleanup_call")))))),
  30. {changeTo(node("auto_type_loc"), cat("absl::Cleanup")),
  31. changeTo(node("make_cleanup_call"), cat(callArgs("make_cleanup_call")))},
  32. warning_message);
  33. }
  34. CleanupCtadCheck::CleanupCtadCheck(StringRef Name, ClangTidyContext *Context)
  35. : utils::TransformerClangTidyCheck(CleanupCtadCheckImpl(), Name, Context) {}
  36. } // namespace clang::tidy::abseil