diff --git a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h --- a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h +++ b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNIQUEPTRRESETRELEASECHECK_H #include "../ClangTidyCheck.h" +#include "../utils/IncludeInserter.h" namespace clang { namespace tidy { @@ -28,8 +29,7 @@ /// be `std::unique_ptr*`. class UniqueptrResetReleaseCheck : public ClangTidyCheck { public: - UniqueptrResetReleaseCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + UniqueptrResetReleaseCheck(StringRef Name, ClangTidyContext *Context); bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { // Only register the matchers for C++11; the functionality currently does @@ -37,8 +37,14 @@ // provide any benefit to other languages, despite being benign. return LangOpts.CPlusPlus11; } + void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, + Preprocessor *ModuleExpanderPP) override; void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + void storeOptions(ClangTidyOptions::OptionMap &Opts) override; + +private: + utils::IncludeInserter Inserter; }; } // namespace misc diff --git a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp --- a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp @@ -16,6 +16,22 @@ namespace tidy { namespace misc { +UniqueptrResetReleaseCheck::UniqueptrResetReleaseCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context), + Inserter(Options.getLocalOrGlobal("IncludeStyle", + utils::IncludeSorter::IS_LLVM)) {} + +void UniqueptrResetReleaseCheck::storeOptions( + ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "IncludeStyle", Inserter.getStyle()); +} + +void UniqueptrResetReleaseCheck::registerPPCallbacks( + const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { + Inserter.registerPreprocessor(PP); +} + void UniqueptrResetReleaseCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxMemberCallExpr( @@ -103,12 +119,15 @@ StringRef AssignmentText = " = "; StringRef TrailingText = ""; + bool NeedsUtilityInclude = false; if (ReleaseMember->isArrow()) { AssignmentText = " = std::move(*"; TrailingText = ")"; + NeedsUtilityInclude = true; } else if (!Right->isRValue()) { AssignmentText = " = std::move("; TrailingText = ")"; + NeedsUtilityInclude = true; } auto D = diag(ResetMember->getExprLoc(), @@ -123,8 +142,11 @@ CharSourceRange::getTokenRange(ReleaseMember->getOperatorLoc(), ResetCall->getEndLoc()), TrailingText); + if (NeedsUtilityInclude) + D << Inserter.createIncludeInsertion( + Result.SourceManager->getFileID(ResetMember->getBeginLoc()), + ""); } - } // namespace misc } // namespace tidy } // namespace clang diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst b/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst --- a/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst @@ -14,3 +14,11 @@ If ``y`` is already rvalue, ``std::move()`` is not added. ``x`` and ``y`` can also be ``std::unique_ptr*``. + +Options +------- + +.. option:: IncludeStyle + + A string specifying which include-style is used, `llvm` or `google`. Default + is `llvm`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp @@ -1,5 +1,7 @@ // RUN: %check_clang_tidy %s misc-uniqueptr-reset-release %t +// CHECK-FIXES: #include + namespace std { template