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

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

Reviewers: hokein, aaron.ballman, ilya-biryukov, alexfh

Reviewed By: hokein, aaron.ballman, alexfh

Subscribers: Quuxplusone, xazax.hun, cfe-commits

Tags: #clang-tools-extra

Patch by Frederic Tingaud!

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

llvm-svn: 328101
This commit is contained in:
Alexander Kornienko 2018-03-21 14:39:24 +00:00
parent 766338ad7f
commit 670c6315ac
8 changed files with 45 additions and 5 deletions

View File

@ -61,8 +61,13 @@ void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}
bool MakeSmartPtrCheck::isLanguageVersionSupported(
const LangOptions &LangOpts) const {
return LangOpts.CPlusPlus11;
}
void MakeSmartPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) {
if (getLangOpts().CPlusPlus11) {
if (isLanguageVersionSupported(getLangOpts())) {
Inserter.reset(new utils::IncludeInserter(
Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle));
Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks());
@ -70,7 +75,7 @@ void MakeSmartPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) {
}
void MakeSmartPtrCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
if (!getLangOpts().CPlusPlus11)
if (!isLanguageVersionSupported(getLangOpts()))
return;
// Calling make_smart_ptr from within a member function of a type with a

View File

@ -40,6 +40,9 @@ protected:
/// in this class.
virtual SmartPtrTypeMatcher getSmartPointerTypeMatcher() const = 0;
/// Returns whether the C++ version is compatible with current check.
virtual bool isLanguageVersionSupported(const LangOptions &LangOpts) const;
static const char PointerType[];
static const char ConstructorCall[];
static const char ResetCall[];

View File

@ -17,7 +17,8 @@ namespace modernize {
MakeUniqueCheck::MakeUniqueCheck(StringRef Name,
clang::tidy::ClangTidyContext *Context)
: MakeSmartPtrCheck(Name, Context, "std::make_unique") {}
: MakeSmartPtrCheck(Name, Context, "std::make_unique"),
RequireCPlusPlus14(Options.get("MakeSmartPtrFunction", "").empty()) {}
MakeUniqueCheck::SmartPtrTypeMatcher
MakeUniqueCheck::getSmartPointerTypeMatcher() const {
@ -36,6 +37,11 @@ MakeUniqueCheck::getSmartPointerTypeMatcher() const {
equalsBoundNode(PointerType))))))))))))))));
}
bool MakeUniqueCheck::isLanguageVersionSupported(
const LangOptions &LangOpts) const {
return RequireCPlusPlus14 ? LangOpts.CPlusPlus14 : LangOpts.CPlusPlus11;
}
} // namespace modernize
} // namespace tidy
} // namespace clang

View File

@ -31,6 +31,11 @@ public:
protected:
SmartPtrTypeMatcher getSmartPointerTypeMatcher() const override;
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
private:
const bool RequireCPlusPlus14;
};
} // namespace modernize

View File

@ -0,0 +1,10 @@
// RUN: %check_clang_tidy %s modernize-make-unique %t -- -- -std=c++11 \
// RUN: -I%S/Inputs/modernize-smart-ptr
#include "unique_ptr.h"
// CHECK-FIXES: #include "unique_ptr.h"
void f() {
auto my_ptr = std::unique_ptr<int>(new int(1));
// CHECK-FIXES: auto my_ptr = std::unique_ptr<int>(new int(1));
}

View File

@ -0,0 +1,11 @@
// RUN: %check_clang_tidy %s modernize-make-unique %t -- -- -std=c++14 \
// RUN: -I%S/Inputs/modernize-smart-ptr
#include "unique_ptr.h"
// CHECK-FIXES: #include <memory>
void f() {
auto my_ptr = std::unique_ptr<int>(new int(1));
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead
// CHECK-FIXES: auto my_ptr = std::make_unique<int>(1);
}

View File

@ -1,6 +1,6 @@
// RUN: %check_clang_tidy %s modernize-make-unique %t -- \
// RUN: -config="{CheckOptions: [{key: modernize-make-unique.IgnoreMacros, value: 0}]}" \
// RUN: -- -std=c++11 -I%S/Inputs/modernize-smart-ptr
// RUN: -- -std=c++14 -I%S/Inputs/modernize-smart-ptr
#include "unique_ptr.h"

View File

@ -1,4 +1,4 @@
// RUN: %check_clang_tidy %s modernize-make-unique %t -- -- -std=c++11 \
// RUN: %check_clang_tidy %s modernize-make-unique %t -- -- -std=c++14 \
// RUN: -I%S/Inputs/modernize-smart-ptr
#include "unique_ptr.h"