[PATCH] D43766: [clang-tidy][modernize-make-unique] Checks c++14 flag before using std::make_unique

Frederic Tingaud via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 26 09:02:08 PST 2018


ftingaud created this revision.
ftingaud added reviewers: hokein, Prazek.
ftingaud added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, xazax.hun.

For a c++11 code, the clang-tidy rule "modernize-make-unique" should return immediately, as std::make_unique is not supported.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D43766

Files:
  clang-tidy/modernize/MakeSharedCheck.cpp
  clang-tidy/modernize/MakeSharedCheck.h
  clang-tidy/modernize/MakeSmartPtrCheck.cpp
  clang-tidy/modernize/MakeSmartPtrCheck.h
  clang-tidy/modernize/MakeUniqueCheck.cpp
  clang-tidy/modernize/MakeUniqueCheck.h


Index: clang-tidy/modernize/MakeUniqueCheck.h
===================================================================
--- clang-tidy/modernize/MakeUniqueCheck.h
+++ clang-tidy/modernize/MakeUniqueCheck.h
@@ -31,6 +31,8 @@
 
 protected:
   SmartPtrTypeMatcher getSmartPointerTypeMatcher() const override;
+
+  bool isVersionSupported(const clang::LangOptions &LangOpts) const override;
 };
 
 } // namespace modernize
Index: clang-tidy/modernize/MakeUniqueCheck.cpp
===================================================================
--- clang-tidy/modernize/MakeUniqueCheck.cpp
+++ clang-tidy/modernize/MakeUniqueCheck.cpp
@@ -36,6 +36,10 @@
                                     equalsBoundNode(PointerType))))))))))))))));
 }
 
+bool MakeUniqueCheck::isVersionSupported(const clang::LangOptions &LangOpts) const {
+   return LangOpts.CPlusPlus14;
+}
+
 } // namespace modernize
 } // namespace tidy
 } // namespace clang
Index: clang-tidy/modernize/MakeSmartPtrCheck.h
===================================================================
--- clang-tidy/modernize/MakeSmartPtrCheck.h
+++ clang-tidy/modernize/MakeSmartPtrCheck.h
@@ -40,6 +40,9 @@
   /// in this class.
   virtual SmartPtrTypeMatcher getSmartPointerTypeMatcher() const = 0;
 
+  /// Returns whether the C++ version is compatible with current check.
+  virtual bool isVersionSupported(const clang::LangOptions &LangOpts) const = 0;
+
   static const char PointerType[];
   static const char ConstructorCall[];
   static const char ResetCall[];
Index: clang-tidy/modernize/MakeSmartPtrCheck.cpp
===================================================================
--- clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -62,15 +62,15 @@
 }
 
 void MakeSmartPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) {
-  if (getLangOpts().CPlusPlus11) {
+  if (isVersionSupported(getLangOpts())) {
     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)
+  if (!isVersionSupported(getLangOpts()))
     return;
 
   // Calling make_smart_ptr from within a member function of a type with a
Index: clang-tidy/modernize/MakeSharedCheck.h
===================================================================
--- clang-tidy/modernize/MakeSharedCheck.h
+++ clang-tidy/modernize/MakeSharedCheck.h
@@ -34,6 +34,8 @@
 
 protected:
   SmartPtrTypeMatcher getSmartPointerTypeMatcher() const override;
+
+  bool isVersionSupported(const clang::LangOptions &LangOpts) const override;
 };
 
 } // namespace modernize
Index: clang-tidy/modernize/MakeSharedCheck.cpp
===================================================================
--- clang-tidy/modernize/MakeSharedCheck.cpp
+++ clang-tidy/modernize/MakeSharedCheck.cpp
@@ -27,6 +27,10 @@
                                      qualType().bind(PointerType)))))))));
 }
 
+bool MakeSharedCheck::isVersionSupported(const clang::LangOptions &LangOpts) const {
+   return LangOpts.CPlusPlus11;
+}
+
 } // namespace modernize
 } // namespace tidy
 } // namespace clang


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43766.135912.patch
Type: text/x-patch
Size: 3271 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180226/ddefe1d2/attachment-0001.bin>


More information about the cfe-commits mailing list