Index: clang-tidy/modernize/MakeSmartPtrCheck.h =================================================================== --- clang-tidy/modernize/MakeSmartPtrCheck.h +++ 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-tidy/modernize/MakeSmartPtrCheck.cpp =================================================================== --- clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -50,12 +50,14 @@ MakeSmartPtrFunctionHeader( Options.get("MakeSmartPtrFunctionHeader", StdMemoryHeader)), MakeSmartPtrFunctionName( - Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)) {} + Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)), + IgnoreMacros(Options.get("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 +124,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 +141,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 +192,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: docs/clang-tidy/checks/modernize-make-unique.rst =================================================================== --- docs/clang-tidy/checks/modernize-make-unique.rst +++ 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: test/clang-tidy/modernize-make-unique.cpp =================================================================== --- test/clang-tidy/modernize-make-unique.cpp +++ 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