diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-04-10 20:17:54 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-04-11 14:45:38 +0200 |
commit | 85c2ed8dc790689ce69ff0a08ff5a4de98df54b7 (patch) | |
tree | f7d7f6fce51d1a4443608971da7d9c42b1201fa3 /compilerplugins | |
parent | 117688bd3f51a7a50b2620aa7dcc0c065f29d402 (diff) |
loplugin:stringview add check for getToken().toInt32
where we can convert that to
o3tl::toInt32(o3tl::getToken(
and avoid the heap allocation of a temporary string
Change-Id: Ib11c19c6e6cdc0de3e551affd3578d181e292de4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132810
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/stringview.cxx | 50 | ||||
-rw-r--r-- | compilerplugins/clang/test/stringview.cxx | 5 |
2 files changed, 36 insertions, 19 deletions
diff --git a/compilerplugins/clang/stringview.cxx b/compilerplugins/clang/stringview.cxx index d7b48a43a4b8..e706f4b397da 100644 --- a/compilerplugins/clang/stringview.cxx +++ b/compilerplugins/clang/stringview.cxx @@ -46,7 +46,8 @@ public: return !( loplugin::isSamePathname(fn, SRCDIR "/sal/qa/OStringBuffer/rtl_OStringBuffer.cxx") || loplugin::isSamePathname(fn, SRCDIR "/sal/qa/rtl/strings/test_ostring_concat.cxx") - || loplugin::isSamePathname(fn, SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx")); + || loplugin::isSamePathname(fn, SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx") + || loplugin::isSamePathname(fn, SRCDIR "/sal/qa/rtl/oustring/rtl_OUString2.cxx")); } virtual void run() override @@ -336,33 +337,44 @@ void StringView::handleCXXMemberCallExpr(CXXMemberCallExpr const* expr) } } -/** check for calls to O[U]StringBuffer::append that could be passed as a - std::u16string_view */ bool StringView::VisitCXXMemberCallExpr(CXXMemberCallExpr const* expr) { if (ignoreLocation(expr)) { return true; } - if (!loplugin::TypeCheck(expr->getType()) - .Class("OUStringBuffer") - .Namespace("rtl") - .GlobalNamespace() - && !loplugin::TypeCheck(expr->getType()) - .Class("OStringBuffer") - .Namespace("rtl") - .GlobalNamespace()) - { - return true; - } - auto const dc = loplugin::DeclCheck(expr->getMethodDecl()); - if (dc.Function("append") || dc.Function("indexOf") || dc.Function("lastIndexOf")) + /** check for calls to O[U]StringBuffer::append that could be passed as a + std::u16string_view */ + if (loplugin::TypeCheck(expr->getType()) + .Class("OUStringBuffer") + .Namespace("rtl") + .GlobalNamespace() + || loplugin::TypeCheck(expr->getType()) + .Class("OStringBuffer") + .Namespace("rtl") + .GlobalNamespace()) { - handleSubExprThatCouldBeView(expr->getArg(0)); + auto const dc = loplugin::DeclCheck(expr->getMethodDecl()); + if (dc.Function("append") || dc.Function("indexOf") || dc.Function("lastIndexOf")) + { + handleSubExprThatCouldBeView(expr->getArg(0)); + } + else if (dc.Function("insert")) + { + handleSubExprThatCouldBeView(expr->getArg(1)); + } } - else if (dc.Function("insert")) + + // rather than getToken...toInt32, use o3tl::toInt(o3tl::getToken(...) + auto tc = loplugin::TypeCheck(expr->getImplicitObjectArgument()->getType()); + if (tc.Class("OUString").Namespace("rtl").GlobalNamespace() + || tc.Class("OString").Namespace("rtl").GlobalNamespace()) { - handleSubExprThatCouldBeView(expr->getArg(1)); + auto const dc = loplugin::DeclCheck(expr->getMethodDecl()); + if (dc.Function("toInt32") || dc.Function("toInt64")) + { + handleSubExprThatCouldBeView(expr->getImplicitObjectArgument()); + } } return true; } diff --git a/compilerplugins/clang/test/stringview.cxx b/compilerplugins/clang/test/stringview.cxx index 7e637175e259..398f14b3dd12 100644 --- a/compilerplugins/clang/test/stringview.cxx +++ b/compilerplugins/clang/test/stringview.cxx @@ -213,4 +213,9 @@ void f5(OUString s) s += OUString(std::u16string_view(u"foo")); } +void f6(OUString s) +{ + // expected-error@+1 {{rather than getToken, pass with a view using o3tl::getToken() [loplugin:stringview]}} + s.getToken(1, ' ').toInt32(); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |