summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamuel Benzaquen <sbenza@google.com>2015-04-09 17:51:01 +0000
committerSamuel Benzaquen <sbenza@google.com>2015-04-09 17:51:01 +0000
commit91d85dc6bc2fa2837b74fe05443824df7cdde6bd (patch)
tree44c0edecb2595fba0bdc217e7a4761a11cc9618c
parent6d61b63cc50b26949aa0f78c6d90a0044bc1d432 (diff)
downloadbcm5719-llvm-91d85dc6bc2fa2837b74fe05443824df7cdde6bd.tar.gz
bcm5719-llvm-91d85dc6bc2fa2837b74fe05443824df7cdde6bd.zip
[clang-tidy] Ignore expressions with incompatible deleters.
Summary: Do not warn on .reset(.release()) expressions if the deleters are not compatible. Using plain assignment will probably not work. Reviewers: klimek Subscribers: curdeius, cfe-commits Differential Revision: http://reviews.llvm.org/D8422 llvm-svn: 234512
-rw-r--r--clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp64
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp30
2 files changed, 87 insertions, 7 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp
index d7ba7e73ae5..436cd101216 100644
--- a/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UniqueptrResetReleaseCheck.cpp
@@ -22,17 +22,75 @@ void UniqueptrResetReleaseCheck::registerMatchers(MatchFinder *Finder) {
memberCallExpr(
on(expr().bind("left")), callee(memberExpr().bind("reset_member")),
callee(methodDecl(hasName("reset"),
- ofClass(hasName("::std::unique_ptr")))),
+ ofClass(recordDecl(hasName("::std::unique_ptr"),
+ decl().bind("left_class"))))),
has(memberCallExpr(
on(expr().bind("right")),
callee(memberExpr().bind("release_member")),
- callee(methodDecl(hasName("release"),
- ofClass(hasName("::std::unique_ptr")))))))
+ callee(methodDecl(
+ hasName("release"),
+ ofClass(recordDecl(hasName("::std::unique_ptr"),
+ decl().bind("right_class"))))))))
.bind("reset_call"),
this);
}
+namespace {
+const Type *getDeleterForUniquePtr(const MatchFinder::MatchResult &Result,
+ StringRef ID) {
+ const auto *Class =
+ Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>(ID);
+ if (!Class)
+ return nullptr;
+ auto DeleterArgument = Class->getTemplateArgs()[1];
+ if (DeleterArgument.getKind() != TemplateArgument::Type)
+ return nullptr;
+ return DeleterArgument.getAsType().getTypePtr();
+}
+
+bool areDeletersCompatible(const MatchFinder::MatchResult &Result) {
+ const Type *LeftDeleterType = getDeleterForUniquePtr(Result, "left_class");
+ const Type *RightDeleterType = getDeleterForUniquePtr(Result, "right_class");
+
+ if (LeftDeleterType->getUnqualifiedDesugaredType() ==
+ RightDeleterType->getUnqualifiedDesugaredType()) {
+ // Same type. We assume they are compatible.
+ // This check handles the case where the deleters are function pointers.
+ return true;
+ }
+
+ const CXXRecordDecl *LeftDeleter = LeftDeleterType->getAsCXXRecordDecl();
+ const CXXRecordDecl *RightDeleter = RightDeleterType->getAsCXXRecordDecl();
+ if (!LeftDeleter || !RightDeleter)
+ return false;
+
+ if (LeftDeleter->getCanonicalDecl() == RightDeleter->getCanonicalDecl()) {
+ // Same class. We assume they are compatible.
+ return true;
+ }
+
+ const auto *LeftAsTemplate =
+ dyn_cast<ClassTemplateSpecializationDecl>(LeftDeleter);
+ const auto *RightAsTemplate =
+ dyn_cast<ClassTemplateSpecializationDecl>(RightDeleter);
+ if (LeftAsTemplate && RightAsTemplate &&
+ LeftAsTemplate->getSpecializedTemplate() ==
+ RightAsTemplate->getSpecializedTemplate()) {
+ // They are different instantiations of the same template. We assume they
+ // are compatible.
+ // This handles things like std::default_delete<Base> vs.
+ // std::default_delete<Derived>.
+ return true;
+ }
+ return false;
+}
+
+} // namespace
+
void UniqueptrResetReleaseCheck::check(const MatchFinder::MatchResult &Result) {
+ if (!areDeletersCompatible(Result))
+ return;
+
const auto *ResetMember = Result.Nodes.getNodeAs<MemberExpr>("reset_member");
const auto *ReleaseMember =
Result.Nodes.getNodeAs<MemberExpr>("release_member");
diff --git a/clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp b/clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp
index 2839f167b9f..3d1183991f2 100644
--- a/clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp
+++ b/clang-tools-extra/test/clang-tidy/misc-uniqueptr-reset-release.cpp
@@ -2,12 +2,16 @@
// REQUIRES: shell
namespace std {
+
template <typename T>
+struct default_delete {};
+
+template <typename T, class Deleter = std::default_delete<T>>
struct unique_ptr {
- unique_ptr<T>();
- explicit unique_ptr<T>(T *);
- template <typename U>
- unique_ptr<T>(unique_ptr<U> &&);
+ unique_ptr();
+ explicit unique_ptr(T *);
+ template <typename U, typename E>
+ unique_ptr(unique_ptr<U, E> &&);
void reset(T *);
T *release();
};
@@ -20,6 +24,9 @@ std::unique_ptr<Foo> Create();
std::unique_ptr<Foo> &Look();
std::unique_ptr<Foo> *Get();
+using FooFunc = void (*)(Foo *);
+using BarFunc = void (*)(Bar *);
+
void f() {
std::unique_ptr<Foo> a, b;
std::unique_ptr<Bar> c;
@@ -44,5 +51,20 @@ void f() {
Get()->reset(Get()->release());
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: prefer ptr1 = std::move(ptr2)
// CHECK-FIXES: *Get() = std::move(*Get());
+
+ std::unique_ptr<Bar, FooFunc> func_a, func_b;
+ func_a.reset(func_b.release());
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: prefer ptr1 = std::move(ptr2)
+ // CHECK-FIXES: func_a = std::move(func_b);
}
+void negatives() {
+ std::unique_ptr<Foo> src;
+ struct OtherDeleter {};
+ std::unique_ptr<Foo, OtherDeleter> dest;
+ dest.reset(src.release());
+
+ std::unique_ptr<Bar, FooFunc> func_a;
+ std::unique_ptr<Bar, BarFunc> func_b;
+ func_a.reset(func_b.release());
+}
OpenPOWER on IntegriCloud