hokein updated this revision to Diff 104113. hokein added a comment. Fix code style.
https://reviews.llvm.org/D34674 Files: clang-tidy/modernize/MakeSmartPtrCheck.cpp clang-tidy/modernize/MakeSmartPtrCheck.h test/clang-tidy/modernize-make-unique.cpp
Index: test/clang-tidy/modernize-make-unique.cpp =================================================================== --- test/clang-tidy/modernize-make-unique.cpp +++ test/clang-tidy/modernize-make-unique.cpp @@ -18,6 +18,7 @@ type *release(); void reset(); void reset(type *pt); + void reset(type pt); unique_ptr &operator=(unique_ptr &&); template <typename T> unique_ptr &operator=(unique_ptr<T> &&); @@ -261,6 +262,36 @@ BB.reset(new bar::Bar()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: // CHECK-FIXES: BB = std::make_unique<bar::Bar>(); + + std::unique_ptr<Foo[]> FFs; + FFs.reset(new Foo[5]); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: + // CHECK-FIXES: FFs = std::make_unique<Foo[]>(5); + FFs.reset(new Foo[5]()); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: + // CHECK-FIXES: FFs = std::make_unique<Foo[]>(5); + const int Num = 1; + FFs.reset(new Foo[Num]); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: + // CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num); + int Num2 = 1; + FFs.reset(new Foo[Num2]); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: + // CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num2); + + std::unique_ptr<int[]> FI; + FI.reset(new int[5]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: + // CHECK-FIXES: FI = std::make_unique<int[]>(5); + FI.reset(new int[5]()); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: + // CHECK-FIXES: FI = std::make_unique<int[]>(5); + FI.reset(new int[Num]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: + // CHECK-FIXES: FI = std::make_unique<int[]>(Num); + FI.reset(new int[Num2]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: + // CHECK-FIXES: FI = std::make_unique<int[]>(Num2); } void aliases() { Index: clang-tidy/modernize/MakeSmartPtrCheck.h =================================================================== --- clang-tidy/modernize/MakeSmartPtrCheck.h +++ clang-tidy/modernize/MakeSmartPtrCheck.h @@ -49,7 +49,8 @@ const QualType *Type, const CXXNewExpr *New); void checkReset(SourceManager &SM, const CXXMemberCallExpr *Member, const CXXNewExpr *New); - void replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New); + void replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New, + SourceManager &SM); }; } // namespace modernize Index: clang-tidy/modernize/MakeSmartPtrCheck.cpp =================================================================== --- clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -17,13 +17,17 @@ namespace modernize { namespace { -StringRef GetNewExprName(const CXXNewExpr *NewExpr, - const SourceManager &SM, - const LangOptions &Lang) { - return Lexer::getSourceText( +std::string GetNewExprName(const CXXNewExpr *NewExpr, + const SourceManager &SM, + const LangOptions &Lang) { + StringRef WrittenName = Lexer::getSourceText( CharSourceRange::getTokenRange( NewExpr->getAllocatedTypeSourceInfo()->getTypeLoc().getSourceRange()), SM, Lang); + if (NewExpr->isArray()) { + return WrittenName.str() + "[]"; + } + return WrittenName.str(); } } // namespace @@ -114,7 +118,7 @@ ConstructCallEnd = ConstructCallStart.getLocWithOffset(ExprStr.size()); Diag << FixItHint::CreateInsertion( ConstructCallEnd, - "<" + GetNewExprName(New, SM, getLangOpts()).str() + ">"); + "<" + GetNewExprName(New, SM, getLangOpts()) + ">"); } else { ConstructCallEnd = ConstructCallStart.getLocWithOffset(LAngle); } @@ -137,7 +141,7 @@ ")"); } - replaceNew(Diag, New); + replaceNew(Diag, New, SM); } void MakeSmartPtrCheck::checkReset(SourceManager &SM, @@ -162,23 +166,50 @@ if (Expr->isArrow()) Diag << FixItHint::CreateInsertion(ExprStart, "*"); - replaceNew(Diag, New); + replaceNew(Diag, New, SM); } void MakeSmartPtrCheck::replaceNew(DiagnosticBuilder &Diag, - const CXXNewExpr *New) { + const CXXNewExpr *New, + SourceManager& SM) { SourceLocation NewStart = New->getSourceRange().getBegin(); SourceLocation NewEnd = New->getSourceRange().getEnd(); + + std::string ArraySizeExpr; + if (const auto* ArraySize = New->getArraySize()) { + ArraySizeExpr = Lexer::getSourceText(CharSourceRange::getTokenRange( + ArraySize->getSourceRange()), + SM, getLangOpts()) + .str(); + } + + switch (New->getInitializationStyle()) { case CXXNewExpr::NoInit: { - Diag << FixItHint::CreateRemoval(SourceRange(NewStart, NewEnd)); + if (ArraySizeExpr.empty()) { + Diag << FixItHint::CreateRemoval(SourceRange(NewStart, NewEnd)); + } else { + // New array expression without written initializer: + // smart_ptr<Foo[]>(new Foo[5]); + Diag << FixItHint::CreateReplacement(SourceRange(NewStart, NewEnd), + ArraySizeExpr); + } break; } case CXXNewExpr::CallInit: { - SourceRange InitRange = New->getDirectInitRange(); - Diag << FixItHint::CreateRemoval( - SourceRange(NewStart, InitRange.getBegin())); - Diag << FixItHint::CreateRemoval(SourceRange(InitRange.getEnd(), NewEnd)); + if (ArraySizeExpr.empty()) { + SourceRange InitRange = New->getDirectInitRange(); + Diag << FixItHint::CreateRemoval( + SourceRange(NewStart, InitRange.getBegin())); + Diag << FixItHint::CreateRemoval(SourceRange(InitRange.getEnd(), NewEnd)); + } + else { + // New array expression with default/value initialization: + // smart_ptr<Foo[]>(new int[5]()); + // smart_ptr<Foo[]>(new Foo[5]()); + Diag << FixItHint::CreateReplacement(SourceRange(NewStart, NewEnd), + ArraySizeExpr); + } break; } case CXXNewExpr::ListInit: {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits