clang-tools  14.0.0git
ReplaceAutoPtrCheck.h
Go to the documentation of this file.
1 //===--- ReplaceAutoPtrCheck.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_REPLACE_AUTO_PTR_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H
11 
12 #include "../ClangTidyCheck.h"
13 #include "../utils/IncludeInserter.h"
14 
15 namespace clang {
16 namespace tidy {
17 namespace modernize {
18 
19 /// Transforms the deprecated `std::auto_ptr` into the C++11 `std::unique_ptr`.
20 ///
21 /// Note that both the `std::auto_ptr` type and the transfer of ownership are
22 /// transformed. `std::auto_ptr` provides two ways to transfer the ownership,
23 /// the copy-constructor and the assignment operator. Unlike most classes these
24 /// operations do not 'copy' the resource but they 'steal' it.
25 /// `std::unique_ptr` uses move semantics instead, which makes the intent of
26 /// transferring the resource explicit. This difference between the two smart
27 /// pointers requires wrapping the copy-ctor and assign-operator with
28 /// `std::move()`.
29 ///
30 /// For example, given:
31 ///
32 /// \code
33 /// std::auto_ptr<int> i, j;
34 /// i = j;
35 /// \endcode
36 ///
37 /// This code is transformed to:
38 ///
39 /// \code
40 /// std::unique_ptr<in> i, j;
41 /// i = std::move(j);
42 /// \endcode
44 public:
45  ReplaceAutoPtrCheck(StringRef Name, ClangTidyContext *Context);
46  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
47  return LangOpts.CPlusPlus;
48  }
49  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
50  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
51  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
52  Preprocessor *ModuleExpanderPP) override;
53  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
54 
55 private:
56  utils::IncludeInserter Inserter;
57 };
58 
59 } // namespace modernize
60 } // namespace tidy
61 } // namespace clang
62 
63 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::utils::IncludeInserter
Produces fixes to insert specified includes to source files, if not yet present.
Definition: IncludeInserter.h:55
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::modernize::ReplaceAutoPtrCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: ReplaceAutoPtrCheck.cpp:97
clang::tidy::modernize::ReplaceAutoPtrCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
Definition: ReplaceAutoPtrCheck.h:46
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::modernize::ReplaceAutoPtrCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: ReplaceAutoPtrCheck.cpp:103
clang::tidy::modernize::ReplaceAutoPtrCheck::ReplaceAutoPtrCheck
ReplaceAutoPtrCheck(StringRef Name, ClangTidyContext *Context)
Definition: ReplaceAutoPtrCheck.cpp:40
clang::tidy::modernize::ReplaceAutoPtrCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: ReplaceAutoPtrCheck.cpp:50
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::tidy::modernize::ReplaceAutoPtrCheck
Transforms the deprecated std::auto_ptr into the C++11 std::unique_ptr.
Definition: ReplaceAutoPtrCheck.h:43
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::modernize::ReplaceAutoPtrCheck::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: ReplaceAutoPtrCheck.cpp:46