Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 1 | //===--- ReplaceAutoPtrCheck.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_REPLACE_AUTO_PTR_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H |
| 12 | |
| 13 | #include "../ClangTidy.h" |
| 14 | #include "../utils/IncludeInserter.h" |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace modernize { |
| 19 | |
Alexander Kornienko | d96d89f | 2015-09-08 09:44:04 +0000 | [diff] [blame] | 20 | /// Transforms the deprecated `std::auto_ptr` into the C++11 `std::unique_ptr`. |
Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 21 | /// |
Alexander Kornienko | d96d89f | 2015-09-08 09:44:04 +0000 | [diff] [blame] | 22 | /// Note that both the `std::auto_ptr` type and the transfer of ownership are |
| 23 | /// transformed. `std::auto_ptr` provides two ways to transfer the ownership, |
Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 24 | /// the copy-constructor and the assignment operator. Unlike most classes these |
| 25 | /// operations do not 'copy' the resource but they 'steal' it. |
Alexander Kornienko | d96d89f | 2015-09-08 09:44:04 +0000 | [diff] [blame] | 26 | /// `std::unique_ptr` uses move semantics instead, which makes the intent of |
Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 27 | /// transferring the resource explicit. This difference between the two smart |
| 28 | /// pointers requeres to wrap the copy-ctor and assign-operator with |
Alexander Kornienko | d96d89f | 2015-09-08 09:44:04 +0000 | [diff] [blame] | 29 | /// `std::move()`. |
Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 30 | /// |
| 31 | /// For example, given: |
Alexander Kornienko | d96d89f | 2015-09-08 09:44:04 +0000 | [diff] [blame] | 32 | /// |
Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 33 | /// \code |
| 34 | /// std::auto_ptr<int> i, j; |
| 35 | /// i = j; |
| 36 | /// \endcode |
Alexander Kornienko | d96d89f | 2015-09-08 09:44:04 +0000 | [diff] [blame] | 37 | /// |
Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 38 | /// This code is transformed to: |
Alexander Kornienko | d96d89f | 2015-09-08 09:44:04 +0000 | [diff] [blame] | 39 | /// |
Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 40 | /// \code |
| 41 | /// std::unique_ptr<in> i, j; |
| 42 | /// i = std::move(j); |
| 43 | /// \endcode |
| 44 | class ReplaceAutoPtrCheck : public ClangTidyCheck { |
| 45 | public: |
| 46 | ReplaceAutoPtrCheck(StringRef Name, ClangTidyContext *Context); |
| 47 | void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
| 48 | void registerMatchers(ast_matchers::MatchFinder *Finder) override; |
| 49 | void registerPPCallbacks(CompilerInstance &Compiler) override; |
| 50 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override; |
| 51 | |
| 52 | private: |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 53 | std::unique_ptr<utils::IncludeInserter> Inserter; |
| 54 | const utils::IncludeSorter::IncludeStyle IncludeStyle; |
Angel Garcia Gomez | 1901671 | 2015-08-25 13:03:43 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | } // namespace modernize |
| 58 | } // namespace tidy |
| 59 | } // namespace clang |
| 60 | |
| 61 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H |