diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2021-05-13 11:57:17 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2021-05-14 13:11:50 +0200 |
commit | af16aa625682b649e8843237652b9246d519cbae (patch) | |
tree | 2ea597c328318d6b75761b71af313bef02b5ad77 /compilerplugins | |
parent | f40cbba63f13e7081fc5901769651fd4d43ea34d (diff) |
Improve loplugin:stringview
Issue the "instead of O[U]String, pass [u16]string_view" diagnostic also for
operator call arguments. (The "rather than copy, pass subView()" diagnostic is
already part of handleSubExprThatCouldBeView, so no need to repeat it explicitly
for operator call arguments.)
(And many call sites don't even require an explicit [u16]string_view, esp. with
the recent ad48b2b02f83eed41fb1eb8d16de7e804156fcf1 "Optimized OString operator
+= overloads". Just some test code in sal/qa/ that explicitly tests the
O[U]String functionality had to be excluded.)
Change-Id: I8d55ba5a7fa16a563f5ffe43d245125c88c793bc
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115589
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/stringview.cxx | 34 | ||||
-rw-r--r-- | compilerplugins/clang/test/stringview.cxx | 2 |
2 files changed, 17 insertions, 19 deletions
diff --git a/compilerplugins/clang/stringview.cxx b/compilerplugins/clang/stringview.cxx index cc717079732f..0c060ce93513 100644 --- a/compilerplugins/clang/stringview.cxx +++ b/compilerplugins/clang/stringview.cxx @@ -39,7 +39,14 @@ public: { } - bool preRun() override { return true; } + bool preRun() override + { + auto const fn = handler.getMainFileName(); + 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")); + } virtual void run() override { @@ -65,32 +72,21 @@ bool StringView::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* cxxOperator if (ignoreLocation(cxxOperatorCallExpr)) return true; - auto check = [&](const Expr* expr) -> void { - auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(compat::IgnoreImplicit(expr)); - if (!memberCallExpr) - return; - auto methodDecl = memberCallExpr->getMethodDecl(); - if (!methodDecl || !methodDecl->getIdentifier() || methodDecl->getName() != "copy") - return; - report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()", - compat::getBeginLoc(expr)) - << expr->getSourceRange(); - }; auto op = cxxOperatorCallExpr->getOperator(); if (op == OO_Plus && cxxOperatorCallExpr->getNumArgs() == 2) { - check(cxxOperatorCallExpr->getArg(0)); - check(cxxOperatorCallExpr->getArg(1)); + handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(0))); + handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(1))); } if (compat::isComparisonOp(cxxOperatorCallExpr)) { - check(cxxOperatorCallExpr->getArg(0)); - check(cxxOperatorCallExpr->getArg(1)); + handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(0))); + handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(1))); } else if (op == OO_PlusEqual) - check(cxxOperatorCallExpr->getArg(1)); + handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(1))); else if (op == OO_Subscript) - check(cxxOperatorCallExpr->getArg(0)); + handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(0))); else if (op == OO_Equal) { if (loplugin::TypeCheck(cxxOperatorCallExpr->getType()) @@ -102,7 +98,7 @@ bool StringView::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* cxxOperator .Namespace("rtl") .GlobalNamespace()) { - check(cxxOperatorCallExpr->getArg(1)); + handleSubExprThatCouldBeView(compat::IgnoreImplicit(cxxOperatorCallExpr->getArg(1))); } } return true; diff --git a/compilerplugins/clang/test/stringview.cxx b/compilerplugins/clang/test/stringview.cxx index edd2305f2c69..66d35975bf13 100644 --- a/compilerplugins/clang/test/stringview.cxx +++ b/compilerplugins/clang/test/stringview.cxx @@ -152,6 +152,8 @@ void f5(OUString s) buf.append(s.copy(12)); // expected-error@+1 {{instead of an 'rtl::OUString' constructed from a 'std::u16string_view' (aka 'basic_string_view<char16_t>'), pass a 'std::u16string_view' [loplugin:stringview]}} buf.append(OUString(std::u16string_view(u"foo"))); + // expected-error@+1 {{instead of an 'rtl::OUString' constructed from a 'std::u16string_view' (aka 'basic_string_view<char16_t>'), pass a 'std::u16string_view' [loplugin:stringview]}} + s += OUString(std::u16string_view(u"foo")); } /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |