ReplaceAutoPtrCheck.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H
  10. #include "../ClangTidyCheck.h"
  11. #include "../utils/IncludeInserter.h"
  12. namespace clang::tidy::modernize {
  13. /// Transforms the deprecated `std::auto_ptr` into the C++11 `std::unique_ptr`.
  14. ///
  15. /// Note that both the `std::auto_ptr` type and the transfer of ownership are
  16. /// transformed. `std::auto_ptr` provides two ways to transfer the ownership,
  17. /// the copy-constructor and the assignment operator. Unlike most classes these
  18. /// operations do not 'copy' the resource but they 'steal' it.
  19. /// `std::unique_ptr` uses move semantics instead, which makes the intent of
  20. /// transferring the resource explicit. This difference between the two smart
  21. /// pointers requires wrapping the copy-ctor and assign-operator with
  22. /// `std::move()`.
  23. ///
  24. /// For example, given:
  25. ///
  26. /// \code
  27. /// std::auto_ptr<int> i, j;
  28. /// i = j;
  29. /// \endcode
  30. ///
  31. /// This code is transformed to:
  32. ///
  33. /// \code
  34. /// std::unique_ptr<in> i, j;
  35. /// i = std::move(j);
  36. /// \endcode
  37. class ReplaceAutoPtrCheck : public ClangTidyCheck {
  38. public:
  39. ReplaceAutoPtrCheck(StringRef Name, ClangTidyContext *Context);
  40. bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
  41. return LangOpts.CPlusPlus;
  42. }
  43. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  44. void registerMatchers(ast_matchers::MatchFinder *Finder) override;
  45. void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  46. Preprocessor *ModuleExpanderPP) override;
  47. void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
  48. private:
  49. utils::IncludeInserter Inserter;
  50. };
  51. } // namespace clang::tidy::modernize
  52. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_REPLACE_AUTO_PTR_H