forked from OSchip/llvm-project
[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 llvm-svn: 306421
This commit is contained in:
parent
ff680f0386
commit
7663debbe3
|
@ -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(SourceManager &SM,
|
|||
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(SourceManager &SM,
|
|||
")");
|
||||
}
|
||||
|
||||
replaceNew(Diag, New);
|
||||
replaceNew(Diag, New, SM);
|
||||
}
|
||||
|
||||
void MakeSmartPtrCheck::checkReset(SourceManager &SM,
|
||||
|
@ -162,23 +166,49 @@ void MakeSmartPtrCheck::checkReset(SourceManager &SM,
|
|||
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: {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue