Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ using namespace clang::ast_matchers;
namespace clang::tidy::modernize {

static constexpr char ConstructorCall[] = "constructorCall";
static constexpr char DirectVar[] = "directVar";
static constexpr char ResetCall[] = "resetCall";
static constexpr char NewExpression[] = "newExpression";

Expand Down Expand Up @@ -78,18 +79,18 @@ void MakeSmartPtrCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
auto IsPlacement = hasAnyPlacementArg(anything());

Finder->addMatcher(
traverse(
TK_AsIs,
cxxBindTemporaryExpr(has(ignoringParenImpCasts(
cxxConstructExpr(
hasType(getSmartPointerTypeMatcher()), argumentCountIs(1),
hasArgument(
0, cxxNewExpr(hasType(pointsTo(qualType(hasCanonicalType(
equalsBoundNode(PointerType))))),
CanCallCtor, unless(IsPlacement))
.bind(NewExpression)),
unless(isInTemplateInstantiation()))
.bind(ConstructorCall))))),
traverse(TK_AsIs,
cxxConstructExpr(
anyOf(hasParent(cxxBindTemporaryExpr()),
hasParent(varDecl().bind(DirectVar))),
hasType(getSmartPointerTypeMatcher()), argumentCountIs(1),
hasArgument(
0, cxxNewExpr(hasType(pointsTo(qualType(hasCanonicalType(
equalsBoundNode(PointerType))))),
CanCallCtor, unless(IsPlacement))
.bind(NewExpression)),
unless(isInTemplateInstantiation()))
.bind(ConstructorCall)),
this);

Finder->addMatcher(
Expand All @@ -116,6 +117,7 @@ void MakeSmartPtrCheck::check(const MatchFinder::MatchResult &Result) {
SourceManager &SM = *Result.SourceManager;
const auto *Construct =
Result.Nodes.getNodeAs<CXXConstructExpr>(ConstructorCall);
const auto *DVar = Result.Nodes.getNodeAs<VarDecl>(DirectVar);
const auto *Reset = Result.Nodes.getNodeAs<CXXMemberCallExpr>(ResetCall);
const auto *Type = Result.Nodes.getNodeAs<QualType>(PointerType);
const auto *New = Result.Nodes.getNodeAs<CXXNewExpr>(NewExpression);
Expand All @@ -138,13 +140,14 @@ void MakeSmartPtrCheck::check(const MatchFinder::MatchResult &Result) {
if (!Initializes && IgnoreDefaultInitialization)
return;
if (Construct)
checkConstruct(SM, Result.Context, Construct, Type, New);
checkConstruct(SM, Result.Context, Construct, DVar, Type, New);
else if (Reset)
checkReset(SM, Result.Context, Reset, New);
}

void MakeSmartPtrCheck::checkConstruct(SourceManager &SM, ASTContext *Ctx,
const CXXConstructExpr *Construct,
const VarDecl *DVar,
const QualType *Type,
const CXXNewExpr *New) {
SourceLocation ConstructCallStart = Construct->getExprLoc();
Expand Down Expand Up @@ -187,9 +190,14 @@ void MakeSmartPtrCheck::checkConstruct(SourceManager &SM, ASTContext *Ctx,
ConstructCallEnd = ConstructCallStart.getLocWithOffset(LAngle);
}

std::string FinalMakeSmartPtrFunctionName = MakeSmartPtrFunctionName.str();
if (DVar)
FinalMakeSmartPtrFunctionName =
ExprStr.str() + " = " + MakeSmartPtrFunctionName.str();

Diag << FixItHint::CreateReplacement(
CharSourceRange::getCharRange(ConstructCallStart, ConstructCallEnd),
MakeSmartPtrFunctionName);
FinalMakeSmartPtrFunctionName);

// If the smart_ptr is built with brace enclosed direct initialization, use
// parenthesis instead.
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class MakeSmartPtrCheck : public ClangTidyCheck {
const bool IgnoreDefaultInitialization;

void checkConstruct(SourceManager &SM, ASTContext *Ctx,
const CXXConstructExpr *Construct, const QualType *Type,
const CXXNewExpr *New);
const CXXConstructExpr *Construct, const VarDecl *DVar,
const QualType *Type, const CXXNewExpr *New);
void checkReset(SourceManager &SM, ASTContext *Ctx,
const CXXMemberCallExpr *Reset, const CXXNewExpr *New);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ void basic() {
}

std::shared_ptr<int> R(new int());
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_shared instead
// CHECK-FIXES: std::shared_ptr<int> R = std::make_shared<int>();
std::shared_ptr<int> S(new int);

// Create the shared_ptr as a parameter to a function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ void basic() {
}

std::unique_ptr<int> R(new int());
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
// CHECK-FIXES: std::unique_ptr<int> R = std::make_unique<int>();
std::unique_ptr<int> S(new int);

// Create the unique_ptr as a parameter to a function.
Expand Down