MakeSmartPtrCheck.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //===--- MakeSmartPtrCheck.h - clang-tidy------------------------*- C++ -*-===//
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MAKE_SMART_PTR_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MAKE_SMART_PTR_H
  10. #include "../ClangTidyCheck.h"
  11. #include "../utils/IncludeInserter.h"
  12. #include "clang/ASTMatchers/ASTMatchFinder.h"
  13. #include "clang/ASTMatchers/ASTMatchersInternal.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include <string>
  16. namespace clang::tidy::modernize {
  17. /// Base class for MakeSharedCheck and MakeUniqueCheck.
  18. class MakeSmartPtrCheck : public ClangTidyCheck {
  19. public:
  20. MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
  21. StringRef MakeSmartPtrFunctionName);
  22. void registerMatchers(ast_matchers::MatchFinder *Finder) final;
  23. void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  24. Preprocessor *ModuleExpanderPP) override;
  25. void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
  26. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  27. protected:
  28. using SmartPtrTypeMatcher = ast_matchers::internal::BindableMatcher<QualType>;
  29. /// Returns matcher that match with different smart pointer types.
  30. ///
  31. /// Requires to bind pointer type (qualType) with PointerType string declared
  32. /// in this class.
  33. virtual SmartPtrTypeMatcher getSmartPointerTypeMatcher() const = 0;
  34. /// Returns whether the C++ version is compatible with current check.
  35. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
  36. static const char PointerType[];
  37. private:
  38. utils::IncludeInserter Inserter;
  39. const StringRef MakeSmartPtrFunctionHeader;
  40. const StringRef MakeSmartPtrFunctionName;
  41. const bool IgnoreMacros;
  42. const bool IgnoreDefaultInitialization;
  43. void checkConstruct(SourceManager &SM, ASTContext *Ctx,
  44. const CXXConstructExpr *Construct, const QualType *Type,
  45. const CXXNewExpr *New);
  46. void checkReset(SourceManager &SM, ASTContext *Ctx,
  47. const CXXMemberCallExpr *Member, const CXXNewExpr *New);
  48. /// Returns true when the fixes for replacing CXXNewExpr are generated.
  49. bool replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New,
  50. SourceManager &SM, ASTContext *Ctx);
  51. void insertHeader(DiagnosticBuilder &Diag, FileID FD);
  52. };
  53. } // namespace clang::tidy::modernize
  54. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MAKE_SMART_PTR_H