[clang-tools-extra] r306421 - [clang-tidy] Handle new array expressions in modernize-make-unique check.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 27 09:25:05 PDT 2017
Author: hokein
Date: Tue Jun 27 09:25:05 2017
New Revision: 306421
URL: http://llvm.org/viewvc/llvm-project?rev=306421&view=rev
Log:
[clang-tidy] Handle new array expressions in modernize-make-unique check.
Reviewers: alexfh
Reviewed By: alexfh
Subscribers: JDevlieghere, xazax.hun, cfe-commits
Differential Revision: https://reviews.llvm.org/D34674
Modified:
clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp
clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h
clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp
Modified: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp?rev=306421&r1=306420&r2=306421&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp Tue Jun 27 09:25:05 2017
@@ -17,13 +17,17 @@ namespace tidy {
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 @@ void MakeSmartPtrCheck::checkConstruct(S
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 @@ void MakeSmartPtrCheck::checkConstruct(S
")");
}
- replaceNew(Diag, New);
+ replaceNew(Diag, New, SM);
}
void MakeSmartPtrCheck::checkReset(SourceManager &SM,
@@ -162,23 +166,49 @@ void MakeSmartPtrCheck::checkReset(Sourc
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: {
Modified: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h?rev=306421&r1=306420&r2=306421&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h Tue Jun 27 09:25:05 2017
@@ -49,7 +49,8 @@ private:
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
Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp?rev=306421&r1=306420&r2=306421&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp Tue Jun 27 09:25:05 2017
@@ -18,6 +18,7 @@ public:
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 @@ void initialization(int T, Base b) {
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() {
More information about the cfe-commits
mailing list