Piotr Padlewski | ce18ade | 2016-05-02 16:56:39 +0000 | [diff] [blame] | 1 | //===--- MakeSmartPtrCheck.h - clang-tidy------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MAKE_SMART_PTR_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MAKE_SMART_PTR_H |
| 12 | |
| 13 | #include "../ClangTidy.h" |
Haojian Wu | 040c0f9 | 2017-07-05 07:49:00 +0000 | [diff] [blame] | 14 | #include "../utils/IncludeInserter.h" |
Eugene Zelenko | a19859d | 2016-05-03 01:13:27 +0000 | [diff] [blame] | 15 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 16 | #include "clang/ASTMatchers/ASTMatchersInternal.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
Piotr Padlewski | ce18ade | 2016-05-02 16:56:39 +0000 | [diff] [blame] | 18 | #include <string> |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace tidy { |
| 22 | namespace modernize { |
| 23 | |
| 24 | /// Base class for MakeSharedCheck and MakeUniqueCheck. |
| 25 | class MakeSmartPtrCheck : public ClangTidyCheck { |
| 26 | public: |
| 27 | MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context, |
Haojian Wu | 040c0f9 | 2017-07-05 07:49:00 +0000 | [diff] [blame] | 28 | StringRef MakeSmartPtrFunctionName); |
Eugene Zelenko | a19859d | 2016-05-03 01:13:27 +0000 | [diff] [blame] | 29 | void registerMatchers(ast_matchers::MatchFinder *Finder) final; |
Haojian Wu | 040c0f9 | 2017-07-05 07:49:00 +0000 | [diff] [blame] | 30 | void registerPPCallbacks(clang::CompilerInstance &Compiler) override; |
Eugene Zelenko | a19859d | 2016-05-03 01:13:27 +0000 | [diff] [blame] | 31 | void check(const ast_matchers::MatchFinder::MatchResult &Result) final; |
Haojian Wu | 040c0f9 | 2017-07-05 07:49:00 +0000 | [diff] [blame] | 32 | void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
Piotr Padlewski | ce18ade | 2016-05-02 16:56:39 +0000 | [diff] [blame] | 33 | |
| 34 | protected: |
| 35 | using SmartPtrTypeMatcher = ast_matchers::internal::BindableMatcher<QualType>; |
| 36 | |
| 37 | /// Returns matcher that match with different smart pointer types. |
| 38 | /// |
| 39 | /// Requires to bind pointer type (qualType) with PointerType string declared |
| 40 | /// in this class. |
| 41 | virtual SmartPtrTypeMatcher getSmartPointerTypeMatcher() const = 0; |
| 42 | |
| 43 | static const char PointerType[]; |
| 44 | static const char ConstructorCall[]; |
Malcolm Parsons | 54c5a54 | 2016-10-31 15:48:01 +0000 | [diff] [blame] | 45 | static const char ResetCall[]; |
Piotr Padlewski | ce18ade | 2016-05-02 16:56:39 +0000 | [diff] [blame] | 46 | static const char NewExpression[]; |
| 47 | |
| 48 | private: |
Haojian Wu | 040c0f9 | 2017-07-05 07:49:00 +0000 | [diff] [blame] | 49 | std::unique_ptr<utils::IncludeInserter> Inserter; |
| 50 | const utils::IncludeSorter::IncludeStyle IncludeStyle; |
| 51 | const std::string MakeSmartPtrFunctionHeader; |
| 52 | const std::string MakeSmartPtrFunctionName; |
Haojian Wu | 115b707 | 2017-08-04 11:18:00 +0000 | [diff] [blame] | 53 | const bool IgnoreMacros; |
Malcolm Parsons | 54c5a54 | 2016-10-31 15:48:01 +0000 | [diff] [blame] | 54 | |
| 55 | void checkConstruct(SourceManager &SM, const CXXConstructExpr *Construct, |
| 56 | const QualType *Type, const CXXNewExpr *New); |
| 57 | void checkReset(SourceManager &SM, const CXXMemberCallExpr *Member, |
| 58 | const CXXNewExpr *New); |
Haojian Wu | 040c0f9 | 2017-07-05 07:49:00 +0000 | [diff] [blame] | 59 | |
Haojian Wu | 4b956cb | 2017-08-17 10:14:52 +0000 | [diff] [blame] | 60 | /// Returns true when the fixes for replacing CXXNewExpr are generated. |
| 61 | bool replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New, |
Haojian Wu | 7663deb | 2017-06-27 16:25:05 +0000 | [diff] [blame] | 62 | SourceManager &SM); |
Haojian Wu | 040c0f9 | 2017-07-05 07:49:00 +0000 | [diff] [blame] | 63 | void insertHeader(DiagnosticBuilder &Diag, FileID FD); |
Piotr Padlewski | ce18ade | 2016-05-02 16:56:39 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // namespace modernize |
| 67 | } // namespace tidy |
| 68 | } // namespace clang |
| 69 | |
| 70 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MAKE_SMART_PTR_H |