malcolm.parsons updated this revision to Diff 75541.
malcolm.parsons added a comment.
Correct operator signature.
Remove unused parameter.
Rename method.
Reformat.
https://reviews.llvm.org/D25898
Files:
clang-tidy/modernize/MakeSmartPtrCheck.cpp
clang-tidy/modernize/MakeSmartPtrCheck.h
docs/ReleaseNotes.rst
docs/clang-tidy/checks/modernize-make-shared.rst
docs/clang-tidy/checks/modernize-make-unique.rst
test/clang-tidy/modernize-make-shared.cpp
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
@@ -8,6 +8,7 @@
template <typename type, typename Deleter = std::default_delete<type>>
class unique_ptr {
public:
+ unique_ptr();
unique_ptr(type *ptr);
unique_ptr(const unique_ptr<type> &t) = delete;
unique_ptr(unique_ptr<type> &&t);
@@ -17,11 +18,13 @@
type *release();
void reset();
void reset(type *pt);
+ unique_ptr &operator=(unique_ptr &&);
+ template <typename T>
+ unique_ptr &operator=(unique_ptr<T> &&);
private:
type *ptr;
};
-
}
struct Base {
@@ -46,7 +49,8 @@
struct Empty {};
-template<class T> using unique_ptr_ = std::unique_ptr<T>;
+template <class T>
+using unique_ptr_ = std::unique_ptr<T>;
void *operator new(__SIZE_TYPE__ Count, void *Ptr);
@@ -63,11 +67,27 @@
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
// CHECK-FIXES: std::unique_ptr<int> P1 = std::make_unique<int>();
+ P1.reset(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
+ // CHECK-FIXES: P1 = std::make_unique<int>();
+
+ P1 = std::unique_ptr<int>(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
+ // CHECK-FIXES: P1 = std::make_unique<int>();
+
// Without parenthesis.
std::unique_ptr<int> P2 = std::unique_ptr<int>(new int);
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
// CHECK-FIXES: std::unique_ptr<int> P2 = std::make_unique<int>();
+ P2.reset(new int);
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
+ // CHECK-FIXES: P2 = std::make_unique<int>();
+
+ P2 = std::unique_ptr<int>(new int);
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
+ // CHECK-FIXES: P2 = std::make_unique<int>();
+
// With auto.
auto P3 = std::unique_ptr<int>(new int());
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
@@ -79,6 +99,10 @@
unique_ptr<int> Q = unique_ptr<int>(new int());
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
// CHECK-FIXES: unique_ptr<int> Q = std::make_unique<int>();
+
+ Q = unique_ptr<int>(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
+ // CHECK-FIXES: Q = std::make_unique<int>();
}
std::unique_ptr<int> R(new int());
@@ -88,19 +112,36 @@
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
// CHECK-FIXES: int T = g(std::make_unique<int>());
- // Only replace if the type in the template is the same than the type returned
+ // Only replace if the type in the template is the same as the type returned
// by the new operator.
auto Pderived = std::unique_ptr<Base>(new Derived());
+ // OK to replace for reset and assign
+ Pderived.reset(new Derived());
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead
+ // CHECK-FIXES: Pderived = std::make_unique<Derived>();
+
+ Pderived = std::unique_ptr<Derived>(new Derived());
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use std::make_unique instead
+ // CHECK-FIXES: Pderived = std::make_unique<Derived>();
+
+ // FIXME: OK to replace if assigned to unique_ptr<Base>
+ Pderived = std::unique_ptr<Base>(new Derived());
+
+ // FIXME: OK to replace when auto is not used
+ std::unique_ptr<Base> PBase = std::unique_ptr<Base>(new Derived());
+
// The pointer is returned by the function, nothing to do.
std::unique_ptr<Base> RetPtr = getPointer();
// This emulates std::move.
- std::unique_ptr<int> Move = static_cast<std::unique_ptr<int>&&>(P1);
+ std::unique_ptr<int> Move = static_cast<std::unique_ptr<int> &&>(P1);
- // Placemenet arguments should not be removed.
+ // Placement arguments should not be removed.
int *PInt = new int;
std::unique_ptr<int> Placement = std::unique_ptr<int>(new (PInt) int{3});
+ Placement.reset(new (PInt) int{3});
+ Placement = std::unique_ptr<int>(new (PInt) int{3});
}
// Calling make_smart_ptr from within a member function of a type with a
@@ -116,6 +157,8 @@
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
// CHECK-FIXES: auto callsPublic = std::make_unique<Private>();
auto ptr = std::unique_ptr<Private>(new Private(42));
+ ptr.reset(new Private(42));
+ ptr = std::unique_ptr<Private>(new Private(42));
}
virtual ~Private();
@@ -132,6 +175,8 @@
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
// CHECK-FIXES: auto callsPublic = std::make_unique<Protected>(1, 2);
auto ptr = std::unique_ptr<Protected>(new Protected);
+ ptr.reset(new Protected);
+ ptr = std::unique_ptr<Protected>(new Protected);
}
};
@@ -142,17 +187,25 @@
std::unique_ptr<DPair> PDir1 = std::unique_ptr<DPair>(new DPair(1, T));
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
// CHECK-FIXES: std::unique_ptr<DPair> PDir1 = std::make_unique<DPair>(1, T);
+ PDir1.reset(new DPair(1, T));
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
+ // CHECK-FIXES: PDir1 = std::make_unique<DPair>(1, T);
// Direct initialization with braces.
std::unique_ptr<DPair> PDir2 = std::unique_ptr<DPair>(new DPair{2, T});
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
// CHECK-FIXES: std::unique_ptr<DPair> PDir2 = std::make_unique<DPair>(2, T);
+ PDir2.reset(new DPair{2, T});
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
+ // CHECK-FIXES: PDir2 = std::make_unique<DPair>(2, T);
// Aggregate initialization.
std::unique_ptr<APair> PAggr = std::unique_ptr<APair>(new APair{T, 1});
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
// CHECK-FIXES: std::unique_ptr<APair> PAggr = std::make_unique<APair>(APair{T, 1});
-
+ PAggr.reset(new APair{T, 1});
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
+ // CHECK-FIXES: std::make_unique<APair>(APair{T, 1});
// Test different kinds of initialization of the pointee, when the unique_ptr
// is initialized with braces.
@@ -172,7 +225,6 @@
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead
// CHECK-FIXES: std::unique_ptr<APair> PAggr2 = std::make_unique<APair>(APair{T, 2});
-
// Direct initialization with parenthesis, without arguments.
std::unique_ptr<DPair> PDir5 = std::unique_ptr<DPair>(new DPair());
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
@@ -204,32 +256,54 @@
// We use 'Base' instead of 'struct Base'.
typedef std::unique_ptr<Base> BasePtr;
BasePtr StructType = BasePtr(new Base);
- // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
- // CHECK-FIXES: BasePtr StructType = std::make_unique<Base>();
+// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
+// CHECK-FIXES: BasePtr StructType = std::make_unique<Base>();
#define PTR unique_ptr<int>
std::unique_ptr<int> Macro = std::PTR(new int);
- // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
- // CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>();
+// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
+// CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>();
#undef PTR
std::unique_ptr<int> Using = unique_ptr_<int>(new int);
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
// CHECK-FIXES: std::unique_ptr<int> Using = std::make_unique<int>();
}
void whitespaces() {
+ // clang-format off
auto Space = std::unique_ptr <int>(new int());
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead
// CHECK-FIXES: auto Space = std::make_unique<int>();
auto Spaces = std :: unique_ptr <int>(new int());
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead
// CHECK-FIXES: auto Spaces = std::make_unique<int>();
+ // clang-format on
}
void nesting() {
auto Nest = std::unique_ptr<std::unique_ptr<int>>(new std::unique_ptr<int>(new int));
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use std::make_unique instead
// CHECK-FIXES: auto Nest = std::make_unique<std::unique_ptr<int>>(new int);
+ Nest.reset(new std::unique_ptr<int>(new int));
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead
+ // CHECK-FIXES: Nest = std::make_unique<std::unique_ptr<int>>(new int);
+ Nest->reset(new int);
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
+ // CHECK-FIXES: *Nest = std::make_unique<int>();
+}
+
+void reset() {
+ std::unique_ptr<int> P;
+ P.reset();
+ P.reset(nullptr);
+ P.reset(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use std::make_unique instead
+ // CHECK-FIXES: P = std::make_unique<int>();
+
+ auto Q = &P;
+ Q->reset(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead
+ // CHECK-FIXES: *Q = std::make_unique<int>();
}
Index: test/clang-tidy/modernize-make-shared.cpp
===================================================================
--- test/clang-tidy/modernize-make-shared.cpp
+++ test/clang-tidy/modernize-make-shared.cpp
@@ -5,6 +5,7 @@
template <typename type>
class shared_ptr {
public:
+ shared_ptr();
shared_ptr(type *ptr);
shared_ptr(const shared_ptr<type> &t) {}
shared_ptr(shared_ptr<type> &&t) {}
@@ -14,6 +15,9 @@
type *release();
void reset();
void reset(type *pt);
+ shared_ptr &operator=(shared_ptr &&);
+ template <typename T>
+ shared_ptr &operator=(shared_ptr<T> &&);
private:
type *ptr;
@@ -60,11 +64,27 @@
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_shared instead [modernize-make-shared]
// CHECK-FIXES: std::shared_ptr<int> P1 = std::make_shared<int>();
+ P1.reset(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_shared instead [modernize-make-shared]
+ // CHECK-FIXES: P1 = std::make_shared<int>();
+
+ P1 = std::shared_ptr<int>(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_shared instead [modernize-make-shared]
+ // CHECK-FIXES: P1 = std::make_shared<int>();
+
// Without parenthesis.
std::shared_ptr<int> P2 = std::shared_ptr<int>(new int);
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_shared instead [modernize-make-shared]
// CHECK-FIXES: std::shared_ptr<int> P2 = std::make_shared<int>();
+ P2.reset(new int);
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_shared instead [modernize-make-shared]
+ // CHECK-FIXES: P2 = std::make_shared<int>();
+
+ P2 = std::shared_ptr<int>(new int);
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_shared instead [modernize-make-shared]
+ // CHECK-FIXES: P2 = std::make_shared<int>();
+
// With auto.
auto P3 = std::shared_ptr<int>(new int());
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_shared instead
@@ -76,6 +96,10 @@
shared_ptr<int> Q = shared_ptr<int>(new int());
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_shared instead
// CHECK-FIXES: shared_ptr<int> Q = std::make_shared<int>();
+
+ Q = shared_ptr<int>(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_shared instead
+ // CHECK-FIXES: Q = std::make_shared<int>();
}
std::shared_ptr<int> R(new int());
@@ -85,19 +109,36 @@
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_shared instead
// CHECK-FIXES: int T = g(std::make_shared<int>());
- // Only replace if the type in the template is the same than the type returned
+ // Only replace if the type in the template is the same as the type returned
// by the new operator.
auto Pderived = std::shared_ptr<Base>(new Derived());
+ // OK to replace for reset and assign
+ Pderived.reset(new Derived());
+ // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_shared instead
+ // CHECK-FIXES: Pderived = std::make_shared<Derived>();
+
+ Pderived = std::shared_ptr<Derived>(new Derived());
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use std::make_shared instead
+ // CHECK-FIXES: Pderived = std::make_shared<Derived>();
+
+ // FIXME: OK to replace if assigned to shared_ptr<Base>
+ Pderived = std::shared_ptr<Base>(new Derived());
+
+ // FIXME: OK to replace when auto is not used
+ std::shared_ptr<Base> PBase = std::shared_ptr<Base>(new Derived());
+
// The pointer is returned by the function, nothing to do.
std::shared_ptr<Base> RetPtr = getPointer();
// This emulates std::move.
std::shared_ptr<int> Move = static_cast<std::shared_ptr<int> &&>(P1);
- // Placemenet arguments should not be removed.
+ // Placement arguments should not be removed.
int *PInt = new int;
std::shared_ptr<int> Placement = std::shared_ptr<int>(new (PInt) int{3});
+ Placement.reset(new (PInt) int{3});
+ Placement = std::shared_ptr<int>(new (PInt) int{3});
}
// Calling make_smart_ptr from within a member function of a type with a
@@ -113,6 +154,8 @@
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_shared instead
// CHECK-FIXES: auto callsPublic = std::make_shared<Private>();
auto ptr = std::shared_ptr<Private>(new Private(42));
+ ptr.reset(new Private(42));
+ ptr = std::shared_ptr<Private>(new Private(42));
}
virtual ~Private();
@@ -129,6 +172,8 @@
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_shared instead
// CHECK-FIXES: auto callsPublic = std::make_shared<Protected>(1, 2);
auto ptr = std::shared_ptr<Protected>(new Protected);
+ ptr.reset(new Protected);
+ ptr = std::shared_ptr<Protected>(new Protected);
}
};
@@ -139,16 +184,25 @@
std::shared_ptr<DPair> PDir1 = std::shared_ptr<DPair>(new DPair(1, T));
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_shared instead
// CHECK-FIXES: std::shared_ptr<DPair> PDir1 = std::make_shared<DPair>(1, T);
+ PDir1.reset(new DPair(1, T));
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_shared instead
+ // CHECK-FIXES: PDir1 = std::make_shared<DPair>(1, T);
// Direct initialization with braces.
std::shared_ptr<DPair> PDir2 = std::shared_ptr<DPair>(new DPair{2, T});
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_shared instead
// CHECK-FIXES: std::shared_ptr<DPair> PDir2 = std::make_shared<DPair>(2, T);
+ PDir2.reset(new DPair{2, T});
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_shared instead
+ // CHECK-FIXES: PDir2 = std::make_shared<DPair>(2, T);
// Aggregate initialization.
std::shared_ptr<APair> PAggr = std::shared_ptr<APair>(new APair{T, 1});
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_shared instead
// CHECK-FIXES: std::shared_ptr<APair> PAggr = std::make_shared<APair>(APair{T, 1});
+ PAggr.reset(new APair{T, 1});
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_shared instead
+ // CHECK-FIXES: std::make_shared<APair>(APair{T, 1});
// Test different kinds of initialization of the pointee, when the shared_ptr
// is initialized with braces.
@@ -229,4 +283,24 @@
auto Nest = std::shared_ptr<std::shared_ptr<int>>(new std::shared_ptr<int>(new int));
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use std::make_shared instead
// CHECK-FIXES: auto Nest = std::make_shared<std::shared_ptr<int>>(new int);
+ Nest.reset(new std::shared_ptr<int>(new int));
+ // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_shared instead
+ // CHECK-FIXES: Nest = std::make_shared<std::shared_ptr<int>>(new int);
+ Nest->reset(new int);
+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_shared instead
+ // CHECK-FIXES: *Nest = std::make_shared<int>();
+}
+
+void reset() {
+ std::shared_ptr<int> P;
+ P.reset();
+ P.reset(nullptr);
+ P.reset(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use std::make_shared instead
+ // CHECK-FIXES: P = std::make_shared<int>();
+
+ auto Q = &P;
+ Q->reset(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_shared instead
+ // CHECK-FIXES: *Q = std::make_shared<int>();
}
Index: docs/clang-tidy/checks/modernize-make-unique.rst
===================================================================
--- docs/clang-tidy/checks/modernize-make-unique.rst
+++ docs/clang-tidy/checks/modernize-make-unique.rst
@@ -14,3 +14,14 @@
// becomes
auto my_ptr = std::make_unique<MyPair>(1, 2);
+
+This check also finds calls to ``std::unique_ptr::reset()`` with a ``new``
+expression, and replaces it with a call to ``std::make_unique``.
+
+.. code-block:: c++
+
+ my_ptr.reset(new MyPair(1, 2));
+
+ // becomes
+
+ my_ptr = std::make_unique<MyPair>(1, 2);
Index: docs/clang-tidy/checks/modernize-make-shared.rst
===================================================================
--- docs/clang-tidy/checks/modernize-make-shared.rst
+++ docs/clang-tidy/checks/modernize-make-shared.rst
@@ -14,3 +14,14 @@
// becomes
auto my_ptr = std::make_shared<MyPair>(1, 2);
+
+This check also finds calls to ``std::shared_ptr::reset()`` with a ``new``
+expression, and replaces it with a call to ``std::make_shared``.
+
+.. code-block:: c++
+
+ my_ptr.reset(new MyPair(1, 2));
+
+ // becomes
+
+ my_ptr = std::make_shared<MyPair>(1, 2);
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -81,6 +81,12 @@
Warns if an object is used after it has been moved, without an intervening
reinitialization.
+- `modernize-make-unique
+ <http://clang.llvm.org/extra/clang-tidy/checks/modernize-make-unique.html>`_
+ and `modernize-make-shared
+ <http://clang.llvm.org/extra/clang-tidy/checks/modernize-make-shared.html>`_
+ now handle calls to the smart pointer's reset method.
+
- New `mpi-buffer-deref
<http://clang.llvm.org/extra/clang-tidy/checks/mpi-buffer-deref.html>`_ check
Index: clang-tidy/modernize/MakeSmartPtrCheck.h
===================================================================
--- clang-tidy/modernize/MakeSmartPtrCheck.h
+++ clang-tidy/modernize/MakeSmartPtrCheck.h
@@ -39,10 +39,17 @@
static const char PointerType[];
static const char ConstructorCall[];
+ static const char ResetCall[];
static const char NewExpression[];
private:
std::string makeSmartPtrFunctionName;
+
+ void checkConstruct(SourceManager &SM, const CXXConstructExpr *Construct,
+ const QualType *Type, const CXXNewExpr *New);
+ void checkReset(SourceManager &SM, const CXXMemberCallExpr *Member,
+ const CXXNewExpr *New);
+ void replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New);
};
} // namespace modernize
Index: clang-tidy/modernize/MakeSmartPtrCheck.cpp
===================================================================
--- clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -18,6 +18,7 @@
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,
@@ -31,8 +32,8 @@
// Calling make_smart_ptr from within a member function of a type with a
// private or protected constructor would be ill-formed.
- auto CanCallCtor = unless(has(ignoringImpCasts(cxxConstructExpr(
- hasDeclaration(decl(unless(isPublic())))))));
+ auto CanCallCtor = unless(has(ignoringImpCasts(
+ cxxConstructExpr(hasDeclaration(decl(unless(isPublic())))))));
Finder->addMatcher(
cxxBindTemporaryExpr(has(ignoringParenImpCasts(
@@ -45,6 +46,14 @@
.bind(NewExpression)))
.bind(ConstructorCall)))),
this);
+
+ Finder->addMatcher(
+ cxxMemberCallExpr(
+ thisPointerType(getSmartPointerTypeMatcher()),
+ callee(cxxMethodDecl(hasName("reset"))),
+ hasArgument(0, cxxNewExpr(CanCallCtor).bind(NewExpression)))
+ .bind(ResetCall),
+ this);
}
void MakeSmartPtrCheck::check(const MatchFinder::MatchResult &Result) {
@@ -55,12 +64,24 @@
SourceManager &SM = *Result.SourceManager;
const auto *Construct =
Result.Nodes.getNodeAs<CXXConstructExpr>(ConstructorCall);
+ const auto *Reset = Result.Nodes.getNodeAs<CXXMemberCallExpr>(ResetCall);
const auto *Type = Result.Nodes.getNodeAs<QualType>(PointerType);
const auto *New = Result.Nodes.getNodeAs<CXXNewExpr>(NewExpression);
if (New->getNumPlacementArgs() != 0)
return;
+ if (Construct) {
+ checkConstruct(SM, Construct, Type, New);
+ } else if (Reset) {
+ checkReset(SM, Reset, New);
+ }
+}
+
+void MakeSmartPtrCheck::checkConstruct(SourceManager &SM,
+ const CXXConstructExpr *Construct,
+ const QualType *Type,
+ const CXXNewExpr *New) {
SourceLocation ConstructCallStart = Construct->getExprLoc();
bool Invalid = false;
@@ -105,6 +126,36 @@
")");
}
+ replaceNew(Diag, New);
+}
+
+void MakeSmartPtrCheck::checkReset(SourceManager &SM,
+ const CXXMemberCallExpr *Reset,
+ const CXXNewExpr *New) {
+ const auto *Expr = cast<MemberExpr>(Reset->getCallee());
+ SourceLocation OperatorLoc = Expr->getOperatorLoc();
+ SourceLocation ResetCallStart = Reset->getExprLoc();
+ SourceLocation ExprStart = Expr->getLocStart();
+ SourceLocation ExprEnd =
+ Lexer::getLocForEndOfToken(Expr->getLocEnd(), 0, SM, getLangOpts());
+
+ auto Diag = diag(ResetCallStart, "use %0 instead")
+ << makeSmartPtrFunctionName;
+
+ Diag << FixItHint::CreateReplacement(
+ CharSourceRange::getCharRange(OperatorLoc, ExprEnd),
+ " = " + makeSmartPtrFunctionName + "<" +
+ New->getAllocatedType().getAsString(getLangOpts()) + ">");
+
+ if (Expr->isArrow()) {
+ Diag << FixItHint::CreateInsertion(ExprStart, "*");
+ }
+
+ replaceNew(Diag, New);
+}
+
+void MakeSmartPtrCheck::replaceNew(DiagnosticBuilder &Diag,
+ const CXXNewExpr *New) {
SourceLocation NewStart = New->getSourceRange().getBegin();
SourceLocation NewEnd = New->getSourceRange().getEnd();
switch (New->getInitializationStyle()) {
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits