clang-tools  17.0.0git
MakeSmartPtrCheck.h
Go to the documentation of this file.
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.
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.
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 *Member, 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
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:114
clang::tidy::modernize::MakeSmartPtrCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: MakeSmartPtrCheck.cpp:69
clang::tidy::utils::IncludeInserter
Produces fixes to insert specified includes to source files, if not yet present.
Definition: IncludeInserter.h:55
clang::tidy::modernize::MakeSmartPtrCheck::getSmartPointerTypeMatcher
virtual SmartPtrTypeMatcher getSmartPointerTypeMatcher() const =0
Returns matcher that match with different smart pointer types.
Ctx
Context Ctx
Definition: TUScheduler.cpp:552
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:53
clang::tidy::modernize
Definition: AvoidBindCheck.cpp:32
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:68
clang::tidy::modernize::MakeSmartPtrCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: MakeSmartPtrCheck.cpp:55
Name
Token Name
Definition: MacroToEnumCheck.cpp:87
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:28
clang::tidy::modernize::MakeSmartPtrCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Returns whether the C++ version is compatible with current check.
Definition: MakeSmartPtrCheck.cpp:64
clang::tidy::modernize::MakeSmartPtrCheck
Base class for MakeSharedCheck and MakeUniqueCheck.
Definition: MakeSmartPtrCheck.h:22
clang::tidy::modernize::MakeSmartPtrCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) final
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: MakeSmartPtrCheck.cpp:110
clang::tidy::modernize::MakeSmartPtrCheck::MakeSmartPtrCheck
MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context, StringRef MakeSmartPtrFunctionName)
Definition: MakeSmartPtrCheck.cpp:41
LangOpts
const LangOptions * LangOpts
Definition: ExtractFunction.cpp:374
clang::tidy::modernize::MakeSmartPtrCheck::SmartPtrTypeMatcher
ast_matchers::internal::BindableMatcher< QualType > SmartPtrTypeMatcher
Definition: MakeSmartPtrCheck.h:33
clang::tidy::modernize::MakeSmartPtrCheck::PointerType
static const char PointerType[]
Definition: MakeSmartPtrCheck.h:44
clang::tidy::modernize::MakeSmartPtrCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) final
Override this to register AST matchers with Finder.
Definition: MakeSmartPtrCheck.cpp:75