summaryrefslogtreecommitdiff
path: root/compilerplugins/clang/refcounting.cxx
diff options
context:
space:
mode:
authorNoel <noel.grandin@collabora.co.uk>2021-02-24 15:44:52 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-03-05 08:09:21 +0100
commit5146d482a20494069670496786a1ba3037e979ce (patch)
treee23716bfb1e1fada668dc3210163d234892803cf /compilerplugins/clang/refcounting.cxx
parent8e49505fa7352023c252bf794ae84c9082d7264e (diff)
loplugin:refcounting return objects properly
check that when we return ref-counted objects, we do so using rtl::Reference, so that the object actually has a non-zero ref count. Change-Id: Ib3ffae0d2502f6d117550c82fde5449729c27324 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111487 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins/clang/refcounting.cxx')
-rw-r--r--compilerplugins/clang/refcounting.cxx29
1 files changed, 29 insertions, 0 deletions
diff --git a/compilerplugins/clang/refcounting.cxx b/compilerplugins/clang/refcounting.cxx
index 7f1d7f38fba8..319a23b83a9b 100644
--- a/compilerplugins/clang/refcounting.cxx
+++ b/compilerplugins/clang/refcounting.cxx
@@ -58,6 +58,7 @@ public:
bool VisitTypeLoc(clang::TypeLoc typeLoc);
bool VisitCXXDeleteExpr(const CXXDeleteExpr *);
bool VisitBinaryOperator(const BinaryOperator *);
+ bool VisitReturnStmt(const ReturnStmt *);
// Creation of temporaries with one argument are represented by
// CXXFunctionalCastExpr, while any other number of arguments are
@@ -520,6 +521,7 @@ bool RefCounting::VisitCXXDeleteExpr(const CXXDeleteExpr * cxxDeleteExpr)
}
return true;
}
+
bool RefCounting::VisitFieldDecl(const FieldDecl * fieldDecl) {
if (ignoreLocation(fieldDecl)) {
return true;
@@ -599,6 +601,33 @@ bool RefCounting::VisitFieldDecl(const FieldDecl * fieldDecl) {
return true;
}
+bool RefCounting::VisitReturnStmt(const ReturnStmt * returnStmt) {
+ if (ignoreLocation(returnStmt)) {
+ return true;
+ }
+
+ if (!returnStmt->getRetValue())
+ return true;
+ auto cxxNewExpr = dyn_cast<CXXNewExpr>(compat::IgnoreImplicit(returnStmt->getRetValue()));
+ if (!cxxNewExpr)
+ return true;
+
+ auto qt = returnStmt->getRetValue()->getType();
+ if (!qt->isPointerType())
+ return false;
+ qt = qt->getPointeeType();
+
+ if (containsOWeakObjectSubclass(qt)) {
+ report(
+ DiagnosticsEngine::Warning,
+ "new object of cppu::OWeakObject subclass %0 being returned via raw pointer, should be returned by via rtl::Reference",
+ compat::getBeginLoc(returnStmt))
+ << qt
+ << returnStmt->getSourceRange();
+ }
+
+ return true;
+}
bool RefCounting::VisitVarDecl(const VarDecl * varDecl) {
if (ignoreLocation(varDecl))