[clang-tools-extra] r307130 - [clang-tidy] Add "MakeSmartPtrFunction" option to modernize-make-shared/unique checks.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 5 00:49:00 PDT 2017


Author: hokein
Date: Wed Jul  5 00:49:00 2017
New Revision: 307130

URL: http://llvm.org/viewvc/llvm-project?rev=307130&view=rev
Log:
[clang-tidy] Add "MakeSmartPtrFunction" option to modernize-make-shared/unique checks.

Reviewers: alexfh, aaron.ballman

Reviewed By: alexfh

Subscribers: JDevlieghere, Eugene.Zelenko, xazax.hun, cfe-commits

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

Added:
    clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/
    clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/shared_ptr.h
    clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/unique_ptr.h
    clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared-header.cpp
    clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-header.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp
    clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h
    clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-shared.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst
    clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared.cpp
    clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp

Modified: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp?rev=307130&r1=307129&r2=307130&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp Wed Jul  5 00:49:00 2017
@@ -8,7 +8,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "MakeSharedCheck.h"
+#include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/Lexer.h"
+#include "clang/Lex/Preprocessor.h"
 
 using namespace clang::ast_matchers;
 
@@ -17,6 +19,9 @@ namespace tidy {
 namespace modernize {
 
 namespace {
+
+constexpr char StdMemoryHeader[] = "memory";
+
 std::string GetNewExprName(const CXXNewExpr *NewExpr,
                            const SourceManager &SM,
                            const LangOptions &Lang) {
@@ -29,6 +34,7 @@ std::string GetNewExprName(const CXXNewE
   }
   return WrittenName.str();
 }
+
 } // namespace
 
 const char MakeSmartPtrCheck::PointerType[] = "pointerType";
@@ -37,9 +43,28 @@ const char MakeSmartPtrCheck::ResetCall[
 const char MakeSmartPtrCheck::NewExpression[] = "newExpression";
 
 MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
-                                     std::string makeSmartPtrFunctionName)
+                                     StringRef MakeSmartPtrFunctionName)
     : ClangTidyCheck(Name, Context),
-      makeSmartPtrFunctionName(std::move(makeSmartPtrFunctionName)) {}
+      IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
+          Options.get("IncludeStyle", "llvm"))),
+      MakeSmartPtrFunctionHeader(
+          Options.get("MakeSmartPtrFunctionHeader", StdMemoryHeader)),
+      MakeSmartPtrFunctionName(
+          Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)) {}
+
+void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IncludeStyle", IncludeStyle);
+  Options.store(Opts, "MakeSmartPtrFunctionHeader", MakeSmartPtrFunctionHeader);
+  Options.store(Opts, "MakeSmartPtrFunction", MakeSmartPtrFunctionName);
+}
+
+void MakeSmartPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) {
+  if (getLangOpts().CPlusPlus11) {
+    Inserter.reset(new utils::IncludeInserter(
+        Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
+    Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
+  }
+}
 
 void MakeSmartPtrCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus11)
@@ -107,7 +132,7 @@ void MakeSmartPtrCheck::checkConstruct(S
     return;
 
   auto Diag = diag(ConstructCallStart, "use %0 instead")
-              << makeSmartPtrFunctionName;
+              << MakeSmartPtrFunctionName;
 
   // Find the location of the template's left angle.
   size_t LAngle = ExprStr.find("<");
@@ -125,7 +150,7 @@ void MakeSmartPtrCheck::checkConstruct(S
 
   Diag << FixItHint::CreateReplacement(
       CharSourceRange::getCharRange(ConstructCallStart, ConstructCallEnd),
-      makeSmartPtrFunctionName);
+      MakeSmartPtrFunctionName);
 
   // If the smart_ptr is built with brace enclosed direct initialization, use
   // parenthesis instead.
@@ -142,6 +167,7 @@ void MakeSmartPtrCheck::checkConstruct(S
   }
 
   replaceNew(Diag, New, SM);
+  insertHeader(Diag, SM.getFileID(ConstructCallStart));
 }
 
 void MakeSmartPtrCheck::checkReset(SourceManager &SM,
@@ -155,11 +181,11 @@ void MakeSmartPtrCheck::checkReset(Sourc
       Lexer::getLocForEndOfToken(Expr->getLocEnd(), 0, SM, getLangOpts());
 
   auto Diag = diag(ResetCallStart, "use %0 instead")
-              << makeSmartPtrFunctionName;
+              << MakeSmartPtrFunctionName;
 
   Diag << FixItHint::CreateReplacement(
       CharSourceRange::getCharRange(OperatorLoc, ExprEnd),
-      (llvm::Twine(" = ") + makeSmartPtrFunctionName + "<" +
+      (llvm::Twine(" = ") + MakeSmartPtrFunctionName + "<" +
        GetNewExprName(New, SM, getLangOpts()) + ">")
           .str());
 
@@ -167,6 +193,7 @@ void MakeSmartPtrCheck::checkReset(Sourc
     Diag << FixItHint::CreateInsertion(ExprStart, "*");
 
   replaceNew(Diag, New, SM);
+  insertHeader(Diag, SM.getFileID(OperatorLoc));
 }
 
 void MakeSmartPtrCheck::replaceNew(DiagnosticBuilder &Diag,
@@ -243,6 +270,17 @@ void MakeSmartPtrCheck::replaceNew(Diagn
   }
 }
 
+void MakeSmartPtrCheck::insertHeader(DiagnosticBuilder &Diag, FileID FD) {
+  if (MakeSmartPtrFunctionHeader.empty()) {
+    return;
+  }
+  if (auto IncludeFixit = Inserter->CreateIncludeInsertion(
+          FD, MakeSmartPtrFunctionHeader,
+          /*IsAngled=*/MakeSmartPtrFunctionHeader == StdMemoryHeader)) {
+    Diag << *IncludeFixit;
+  }
+}
+
 } // namespace modernize
 } // namespace tidy
 } // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h?rev=307130&r1=307129&r2=307130&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h Wed Jul  5 00:49:00 2017
@@ -11,6 +11,7 @@
 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MAKE_SMART_PTR_H
 
 #include "../ClangTidy.h"
+#include "../utils/IncludeInserter.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchersInternal.h"
 #include "llvm/ADT/StringRef.h"
@@ -24,9 +25,11 @@ namespace modernize {
 class MakeSmartPtrCheck : public ClangTidyCheck {
 public:
   MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context,
-                    std::string makeSmartPtrFunctionName);
+                    StringRef MakeSmartPtrFunctionName);
   void registerMatchers(ast_matchers::MatchFinder *Finder) final;
+  void registerPPCallbacks(clang::CompilerInstance &Compiler) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
 
 protected:
   using SmartPtrTypeMatcher = ast_matchers::internal::BindableMatcher<QualType>;
@@ -43,14 +46,19 @@ protected:
   static const char NewExpression[];
 
 private:
-  std::string makeSmartPtrFunctionName;
+  std::unique_ptr<utils::IncludeInserter> Inserter;
+  const utils::IncludeSorter::IncludeStyle IncludeStyle;
+  const std::string MakeSmartPtrFunctionHeader;
+  const std::string MakeSmartPtrFunctionName;
 
   void checkConstruct(SourceManager &SM, const CXXConstructExpr *Construct,
                       const QualType *Type, const CXXNewExpr *New);
   void checkReset(SourceManager &SM, const CXXMemberCallExpr *Member,
                   const CXXNewExpr *New);
+
   void replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New,
                   SourceManager &SM);
+  void insertHeader(DiagnosticBuilder &Diag, FileID FD);
 };
 
 } // namespace modernize

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-shared.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-shared.rst?rev=307130&r1=307129&r2=307130&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-shared.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-shared.rst Wed Jul  5 00:49:00 2017
@@ -25,3 +25,21 @@ expression, and replaces it with a call
   // becomes
 
   my_ptr = std::make_shared<MyPair>(1, 2);
+
+Options
+-------
+
+.. option:: MakeSmartPtrFunction
+
+   A string specifying the name of make-shared-ptr function. Default is
+   `std::make_shared`.
+
+.. option:: MakeSmartPtrFunctionHeader
+
+   A string specifying the corresponding header of make-shared-ptr function.
+   Default is `memory`.
+
+.. option:: IncludeStyle
+
+   A string specifying which include-style is used, `llvm` or `google`. Default
+   is `llvm`.

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst?rev=307130&r1=307129&r2=307130&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst Wed Jul  5 00:49:00 2017
@@ -25,3 +25,21 @@ expression, and replaces it with a call
   // becomes
 
   my_ptr = std::make_unique<MyPair>(1, 2);
+
+Options
+-------
+
+.. option:: MakeSmartPtrFunction
+
+   A string specifying the name of make-unique-ptr function. Default is
+   `std::make_unique`.
+
+.. option:: MakeSmartPtrFunctionHeader
+
+   A string specifying the corresponding header of make-unique-ptr function.
+   Default is `memory`.
+
+.. option:: IncludeStyle
+
+   A string specifying which include-style is used, `llvm` or `google`. Default
+   is `llvm`.

Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/shared_ptr.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/shared_ptr.h?rev=307130&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/shared_ptr.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/shared_ptr.h Wed Jul  5 00:49:00 2017
@@ -0,0 +1,24 @@
+namespace std {
+
+template <typename type>
+class shared_ptr {
+public:
+  shared_ptr();
+  shared_ptr(type *ptr);
+  shared_ptr(const shared_ptr<type> &t) {}
+  shared_ptr(shared_ptr<type> &&t) {}
+  ~shared_ptr();
+  type &operator*() { return *ptr; }
+  type *operator->() { return ptr; }
+  type *release();
+  void reset();
+  void reset(type *pt);
+  shared_ptr &operator=(shared_ptr &&);
+  template <typename T>
+  shared_ptr &operator=(shared_ptr<T> &&);
+
+private:
+  type *ptr;
+};
+
+}  // namespace std

Added: clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/unique_ptr.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/unique_ptr.h?rev=307130&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/unique_ptr.h (added)
+++ clang-tools-extra/trunk/test/clang-tidy/Inputs/modernize-smart-ptr/unique_ptr.h Wed Jul  5 00:49:00 2017
@@ -0,0 +1,28 @@
+namespace std {
+
+template <typename T>
+class default_delete {};
+
+template <typename type, typename Deleter = std::default_delete<type>>
+class unique_ptr {
+public:
+  unique_ptr();
+  unique_ptr(type *ptr);
+  unique_ptr(const unique_ptr<type> &t) = delete;
+  unique_ptr(unique_ptr<type> &&t);
+  ~unique_ptr();
+  type &operator*() { return *ptr; }
+  type *operator->() { return ptr; }
+  type *release();
+  void reset();
+  void reset(type *pt);
+  void reset(type pt);
+  unique_ptr &operator=(unique_ptr &&);
+  template <typename T>
+  unique_ptr &operator=(unique_ptr<T> &&);
+
+private:
+  type *ptr;
+};
+
+}  // namespace std

Added: clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared-header.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared-header.cpp?rev=307130&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared-header.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared-header.cpp Wed Jul  5 00:49:00 2017
@@ -0,0 +1,17 @@
+// RUN: %check_clang_tidy %s modernize-make-shared %t -- \
+// RUN:   -config="{CheckOptions: \
+// RUN:     [{key: modernize-make-shared.MakeSmartPtrFunction, \
+// RUN:       value: 'my::MakeShared'}, \
+// RUN:      {key: modernize-make-shared.MakeSmartPtrFunctionHeader, \
+// RUN:       value: 'make_shared_util.h'} \
+// RUN:     ]}" \
+// RUN:   -- -std=c++11 -I%S/Inputs/modernize-smart-ptr
+
+#include "shared_ptr.h"
+// CHECK-FIXES: #include "make_shared_util.h"
+
+void f() {
+  std::shared_ptr<int> P1 = std::shared_ptr<int>(new int());
+  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use my::MakeShared instead
+  // CHECK-FIXES: std::shared_ptr<int> P1 = my::MakeShared<int>();
+}

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared.cpp?rev=307130&r1=307129&r2=307130&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-shared.cpp Wed Jul  5 00:49:00 2017
@@ -1,28 +1,8 @@
-// RUN: %check_clang_tidy %s modernize-make-shared %t
+// RUN: %check_clang_tidy %s modernize-make-shared %t -- -- -std=c++11 \
+// RUN:   -I%S/Inputs/modernize-smart-ptr
 
-namespace std {
-
-template <typename type>
-class shared_ptr {
-public:
-  shared_ptr();
-  shared_ptr(type *ptr);
-  shared_ptr(const shared_ptr<type> &t) {}
-  shared_ptr(shared_ptr<type> &&t) {}
-  ~shared_ptr();
-  type &operator*() { return *ptr; }
-  type *operator->() { return ptr; }
-  type *release();
-  void reset();
-  void reset(type *pt);
-  shared_ptr &operator=(shared_ptr &&);
-  template <typename T>
-  shared_ptr &operator=(shared_ptr<T> &&);
-
-private:
-  type *ptr;
-};
-}
+#include "shared_ptr.h"
+// CHECK-FIXES: #include <memory>
 
 struct Base {
   Base();

Added: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-header.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-header.cpp?rev=307130&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-header.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-header.cpp Wed Jul  5 00:49:00 2017
@@ -0,0 +1,17 @@
+// RUN: %check_clang_tidy %s modernize-make-unique %t -- \
+// RUN:   -config="{CheckOptions: \
+// RUN:     [{key: modernize-make-unique.MakeSmartPtrFunction, \
+// RUN:       value: 'my::MakeUnique'}, \
+// RUN:      {key: modernize-make-unique.MakeSmartPtrFunctionHeader, \
+// RUN:       value: 'make_unique_util.h'} \
+// RUN:     ]}" \
+// RUN:   -- -std=c++11 -I%S/Inputs/modernize-smart-ptr
+
+#include "unique_ptr.h"
+// CHECK-FIXES: #include "make_unique_util.h"
+
+void f() {
+  std::unique_ptr<int> P1 = std::unique_ptr<int>(new int());
+  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use my::MakeUnique instead
+  // CHECK-FIXES: std::unique_ptr<int> P1 = my::MakeUnique<int>();
+}

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp?rev=307130&r1=307129&r2=307130&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp Wed Jul  5 00:49:00 2017
@@ -1,32 +1,8 @@
-// RUN: %check_clang_tidy %s modernize-make-unique %t
+// RUN: %check_clang_tidy %s modernize-make-unique %t -- -- -std=c++11 \
+// RUN:   -I%S/Inputs/modernize-smart-ptr
 
-namespace std {
-
-template <typename T>
-class default_delete {};
-
-template <typename type, typename Deleter = std::default_delete<type>>
-class unique_ptr {
-public:
-  unique_ptr();
-  unique_ptr(type *ptr);
-  unique_ptr(const unique_ptr<type> &t) = delete;
-  unique_ptr(unique_ptr<type> &&t);
-  ~unique_ptr();
-  type &operator*() { return *ptr; }
-  type *operator->() { return ptr; }
-  type *release();
-  void reset();
-  void reset(type *pt);
-  void reset(type pt);
-  unique_ptr &operator=(unique_ptr &&);
-  template <typename T>
-  unique_ptr &operator=(unique_ptr<T> &&);
-
-private:
-  type *ptr;
-};
-}
+#include "unique_ptr.h"
+// CHECK-FIXES: #include <memory>
 
 struct Base {
   Base();




More information about the cfe-commits mailing list