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