Index: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h +++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h @@ -50,6 +50,7 @@ const utils::IncludeSorter::IncludeStyle IncludeStyle; const std::string MakeSmartPtrFunctionHeader; const std::string MakeSmartPtrFunctionName; + const bool IgnoreMacros; void checkConstruct(SourceManager &SM, const CXXConstructExpr *Construct, const QualType *Type, const CXXNewExpr *New); Index: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -42,7 +42,8 @@ const char MakeSmartPtrCheck::ResetCall[] = "resetCall"; const char MakeSmartPtrCheck::NewExpression[] = "newExpression"; -MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext *Context, +MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, + ClangTidyContext* Context, StringRef MakeSmartPtrFunctionName) : ClangTidyCheck(Name, Context), IncludeStyle(utils::IncludeSorter::parseIncludeStyle( @@ -50,12 +51,14 @@ MakeSmartPtrFunctionHeader( Options.get("MakeSmartPtrFunctionHeader", StdMemoryHeader)), MakeSmartPtrFunctionName( - Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)) {} + Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)), + IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {} void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "IncludeStyle", IncludeStyle); Options.store(Opts, "MakeSmartPtrFunctionHeader", MakeSmartPtrFunctionHeader); Options.store(Opts, "MakeSmartPtrFunction", MakeSmartPtrFunctionName); + Options.store(Opts, "IgnoreMacros", IgnoreMacros); } void MakeSmartPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) { @@ -122,6 +125,11 @@ const QualType *Type, const CXXNewExpr *New) { SourceLocation ConstructCallStart = Construct->getExprLoc(); + bool InMacro = ConstructCallStart.isMacroID(); + + if (InMacro && IgnoreMacros) { + return; + } bool Invalid = false; StringRef ExprStr = Lexer::getSourceText( @@ -134,6 +142,11 @@ auto Diag = diag(ConstructCallStart, "use %0 instead") << MakeSmartPtrFunctionName; + // Disable the fix in macros. + if (InMacro) { + return; + } + // Find the location of the template's left angle. size_t LAngle = ExprStr.find("<"); SourceLocation ConstructCallEnd; @@ -180,9 +193,20 @@ SourceLocation ExprEnd = Lexer::getLocForEndOfToken(Expr->getLocEnd(), 0, SM, getLangOpts()); + bool InMacro = ExprStart.isMacroID(); + + if (InMacro && IgnoreMacros) { + return; + } + auto Diag = diag(ResetCallStart, "use %0 instead") << MakeSmartPtrFunctionName; + // Disable the fix in macros. + if (InMacro) { + return; + } + Diag << FixItHint::CreateReplacement( CharSourceRange::getCharRange(OperatorLoc, ExprEnd), (llvm::Twine(" = ") + MakeSmartPtrFunctionName + "<" + Index: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst =================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst +++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-make-unique.rst @@ -43,3 +43,8 @@ A string specifying which include-style is used, `llvm` or `google`. Default is `llvm`. + +.. option:: IgnoreMacros + + If set to non-zero, the check will not give warnings inside macros. Default + is `1`. Index: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-macros.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-macros.cpp +++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique-macros.cpp @@ -0,0 +1,28 @@ +// 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 + +#include "unique_ptr.h" + +class Foo {}; +class Bar {}; +#define DEFINE(...) __VA_ARGS__ +// CHECK-FIXES: {{^}}#define DEFINE(...) __VA_ARGS__{{$}} +template +void g2(std::unique_ptr *t) { + DEFINE( + // CHECK-FIXES: {{^ *}}DEFINE({{$}} + auto p = std::unique_ptr(new Foo); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead + // CHECK-FIXES: {{^ *}}auto p = std::unique_ptr(new Foo);{{$}} + t->reset(new Foo); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead + // CHECK-FIXES: {{^ *}}t->reset(new Foo);{{$}} + ); + // CHECK-FIXES: {{^ *}});{{$}} +} +void macro() { + std::unique_ptr *t; + g2(t); +} +#undef DEFINE Index: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp +++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp @@ -404,3 +404,14 @@ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead // CHECK-FIXES: *Q = std::make_unique(); } + +#define DEFINE(...) __VA_ARGS__ +template +void g2(std::unique_ptr *t) { + DEFINE(auto p = std::unique_ptr(new Foo); t->reset(new Foo);); +} +void macro() { + std::unique_ptr *t; + g2(t); +} +#undef DEFINE