[clang-tidy] Add <utility> include to misc-uniqueptr-reset-release
authorNathan James <n.james93@hotmail.co.uk>
Mon, 12 Apr 2021 22:32:12 +0000 (23:32 +0100)
committerNathan James <n.james93@hotmail.co.uk>
Mon, 12 Apr 2021 22:32:15 +0000 (23:32 +0100)
This is the only remaining check that creates `std::move` includes but doesn't add a `<utility>` include.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D97683

clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp
clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h
clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst
clang-tools-extra/test/clang-tidy/checkers/misc-uniqueptr-reset-release.cpp

index dffb03f..3d5c864 100644 (file)
@@ -16,6 +16,22 @@ namespace clang {
 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 @@ void UniqueptrResetReleaseCheck::check(const MatchFinder::MatchResult &Result) {
 
   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 @@ void UniqueptrResetReleaseCheck::check(const MatchFinder::MatchResult &Result) {
            CharSourceRange::getTokenRange(ReleaseMember->getOperatorLoc(),
                                           ResetCall->getEndLoc()),
            TrailingText);
+  if (NeedsUtilityInclude)
+    D << Inserter.createIncludeInsertion(
+        Result.SourceManager->getFileID(ResetMember->getBeginLoc()),
+        "<utility>");
 }
-
 } // namespace misc
 } // namespace tidy
 } // namespace clang
index 8a08532..6744de2 100644 (file)
@@ -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 @@ namespace misc {
 /// be `std::unique_ptr<Foo>*`.
 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 @@ public:
     // 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
index 858fbe7..8d48fa1 100644 (file)
@@ -14,3 +14,11 @@ Example:
 
 If ``y`` is already rvalue, ``std::move()`` is not added. ``x`` and ``y`` can
 also be ``std::unique_ptr<Foo>*``.
+
+Options
+-------
+
+.. option:: IncludeStyle
+
+   A string specifying which include-style is used, `llvm` or `google`. Default
+   is `llvm`.
index befd2a0..629f55a 100644 (file)
@@ -1,5 +1,7 @@
 // RUN: %check_clang_tidy %s misc-uniqueptr-reset-release %t
 
+// CHECK-FIXES: #include <utility>
+
 namespace std {
 
 template <typename T>