[clang-tools-extra] 27dfcd9 - [clang-tidy] Add <utility> include to misc-uniqueptr-reset-release

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 12 15:32:25 PDT 2021


Author: Nathan James
Date: 2021-04-12T23:32:15+01:00
New Revision: 27dfcd978edc94b4b46218b12a3dfbcdc47ae4c8

URL: https://github.com/llvm/llvm-project/commit/27dfcd978edc94b4b46218b12a3dfbcdc47ae4c8
DIFF: https://github.com/llvm/llvm-project/commit/27dfcd978edc94b4b46218b12a3dfbcdc47ae4c8.diff

LOG: [clang-tidy] Add <utility> include to misc-uniqueptr-reset-release

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

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp
index dffb03f76a3d9..3d5c86493ed1e 100644
--- a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp
@@ -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

diff  --git a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.h
index 8a085322e209f..6744de2be56b8 100644
--- 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 @@ 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 @@ class UniqueptrResetReleaseCheck : public ClangTidyCheck {
     // 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/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst b/clang-tools-extra/docs/clang-tidy/checks/misc-uniqueptr-reset-release.rst
index 858fbe78fdfaa..8d48fa192e69f 100644
--- 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 @@ 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`.

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
index befd2a0576d2d..629f55a96f3b8 100644
--- 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 <utility>
+
 namespace std {
 
 template <typename T>


        


More information about the cfe-commits mailing list