Index: clang-tidy/modernize/MakeSmartPtrCheck.h =================================================================== --- clang-tidy/modernize/MakeSmartPtrCheck.h +++ clang-tidy/modernize/MakeSmartPtrCheck.h @@ -44,9 +44,6 @@ virtual bool isLanguageVersionSupported(const LangOptions &LangOpts) const; static const char PointerType[]; - static const char ConstructorCall[]; - static const char ResetCall[]; - static const char NewExpression[]; private: std::unique_ptr Inserter; @@ -55,14 +52,15 @@ const std::string MakeSmartPtrFunctionName; const bool IgnoreMacros; - void checkConstruct(SourceManager &SM, const CXXConstructExpr *Construct, - const QualType *Type, const CXXNewExpr *New); - void checkReset(SourceManager &SM, const CXXMemberCallExpr *Member, - const CXXNewExpr *New); + void checkConstruct(SourceManager &SM, ASTContext *Ctx, + const CXXConstructExpr *Construct, const QualType *Type, + const CXXNewExpr *New); + void checkReset(SourceManager &SM, ASTContext *Ctx, + const CXXMemberCallExpr *Member, const CXXNewExpr *New); /// Returns true when the fixes for replacing CXXNewExpr are generated. bool replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New, - SourceManager &SM); + SourceManager &SM, ASTContext *Ctx); void insertHeader(DiagnosticBuilder &Diag, FileID FD); }; Index: clang-tidy/modernize/MakeSmartPtrCheck.cpp =================================================================== --- clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -21,6 +21,9 @@ namespace { constexpr char StdMemoryHeader[] = "memory"; +constexpr char ConstructorCall[] = "constructorCall"; +constexpr char ResetCall[] = "resetCall"; +constexpr char NewExpression[] = "newExpression"; std::string GetNewExprName(const CXXNewExpr *NewExpr, const SourceManager &SM, @@ -30,7 +33,7 @@ NewExpr->getAllocatedTypeSourceInfo()->getTypeLoc().getSourceRange()), SM, Lang); if (NewExpr->isArray()) { - return WrittenName.str() + "[]"; + return (WrittenName + "[]").str(); } return WrittenName.str(); } @@ -38,9 +41,6 @@ } // namespace const char MakeSmartPtrCheck::PointerType[] = "pointerType"; -const char MakeSmartPtrCheck::ConstructorCall[] = "constructorCall"; -const char MakeSmartPtrCheck::ResetCall[] = "resetCall"; -const char MakeSmartPtrCheck::NewExpression[] = "newExpression"; MakeSmartPtrCheck::MakeSmartPtrCheck(StringRef Name, ClangTidyContext* Context, @@ -68,8 +68,8 @@ void MakeSmartPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) { if (isLanguageVersionSupported(getLangOpts())) { - Inserter.reset(new utils::IncludeInserter( - Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle)); + Inserter = llvm::make_unique( + Compiler.getSourceManager(), Compiler.getLangOpts(), IncludeStyle); Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); } } @@ -122,12 +122,12 @@ return; if (Construct) - checkConstruct(SM, Construct, Type, New); + checkConstruct(SM, Result.Context, Construct, Type, New); else if (Reset) - checkReset(SM, Reset, New); + checkReset(SM, Result.Context, Reset, New); } -void MakeSmartPtrCheck::checkConstruct(SourceManager &SM, +void MakeSmartPtrCheck::checkConstruct(SourceManager &SM, ASTContext *Ctx, const CXXConstructExpr *Construct, const QualType *Type, const CXXNewExpr *New) { @@ -154,7 +154,7 @@ return; } - if (!replaceNew(Diag, New, SM)) { + if (!replaceNew(Diag, New, SM, Ctx)) { return; } @@ -193,7 +193,7 @@ insertHeader(Diag, SM.getFileID(ConstructCallStart)); } -void MakeSmartPtrCheck::checkReset(SourceManager &SM, +void MakeSmartPtrCheck::checkReset(SourceManager &SM, ASTContext *Ctx, const CXXMemberCallExpr *Reset, const CXXNewExpr *New) { const auto *Expr = cast(Reset->getCallee()); @@ -224,7 +224,7 @@ return; } - if (!replaceNew(Diag, New, SM)) { + if (!replaceNew(Diag, New, SM, Ctx)) { return; } @@ -241,10 +241,20 @@ } bool MakeSmartPtrCheck::replaceNew(DiagnosticBuilder &Diag, - const CXXNewExpr *New, - SourceManager& SM) { - SourceLocation NewStart = New->getSourceRange().getBegin(); - SourceLocation NewEnd = New->getSourceRange().getEnd(); + const CXXNewExpr *New, SourceManager &SM, + ASTContext *Ctx) { + std::function SkipParensParents = + [&](const Expr *E) { + for (const auto &Node : Ctx->getParents(*E)) { + if (const Expr *Parent = Node.get()) + return SkipParensParents(Parent); + } + return E; + }; + + SourceRange NewRange = SkipParensParents(New)->getSourceRange(); + SourceLocation NewStart = NewRange.getBegin(); + SourceLocation NewEnd = NewRange.getEnd(); // Skip when the source location of the new expression is invalid. if (NewStart.isInvalid() || NewEnd.isInvalid()) Index: test/clang-tidy/modernize-make-shared.cpp =================================================================== --- test/clang-tidy/modernize-make-shared.cpp +++ test/clang-tidy/modernize-make-shared.cpp @@ -70,6 +70,18 @@ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_shared instead // CHECK-FIXES: auto P3 = std::make_shared(); + std::shared_ptr P4 = std::shared_ptr((new int)); + // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_shared instead [modernize-make-shared] + // CHECK-FIXES: std::shared_ptr P4 = std::make_shared(); + + P4.reset((((new int())))); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_shared instead [modernize-make-shared] + // CHECK-FIXES: P4 = std::make_shared(); + + P4 = std::shared_ptr(((new int))); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_shared instead [modernize-make-shared] + // CHECK-FIXES: P4 = std::make_shared(); + { // No std. using namespace std; Index: test/clang-tidy/modernize-make-unique.cpp =================================================================== --- test/clang-tidy/modernize-make-unique.cpp +++ test/clang-tidy/modernize-make-unique.cpp @@ -110,6 +110,19 @@ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead // CHECK-FIXES: auto P3 = std::make_unique(); + std::unique_ptr P4 = std::unique_ptr((new int)); + // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: std::unique_ptr P4 = std::make_unique(); + P4.reset((new int)); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P4 = std::make_unique(); + std::unique_ptr P5 = std::unique_ptr((((new int)))); + // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: std::unique_ptr P5 = std::make_unique(); + P5.reset(((((new int))))); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P5 = std::make_unique(); + { // No std. using namespace std;