summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel <noelgrandin@gmail.com>2020-11-18 10:10:40 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2020-11-24 09:45:04 +0100
commitbb06f51308428500c9c8d11ae05f0aa03ecc179c (patch)
treeb18620e8572ed6d4c43c8605660d59f5f7a7e531 /compilerplugins
parent42e8e16cf93dcf944e5c1106f76aaa32057c0397 (diff)
loplugin:stringviewparam extend to comparison operators
which means that some call sites have to change to use unicode string literals i.e. u"foo" instead of "foo" Change-Id: Ie51c3adf56d343dd1d1710777f9d2a43ee66221c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/106125 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/stringviewparam.cxx42
-rw-r--r--compilerplugins/clang/test/stringviewparam.cxx5
2 files changed, 39 insertions, 8 deletions
diff --git a/compilerplugins/clang/stringviewparam.cxx b/compilerplugins/clang/stringviewparam.cxx
index 0f5ebfc70a3b..c359c1a9dc82 100644
--- a/compilerplugins/clang/stringviewparam.cxx
+++ b/compilerplugins/clang/stringviewparam.cxx
@@ -106,9 +106,14 @@ DeclRefExpr const* relevantDeclRefExpr(Expr const* expr)
return e;
}
+bool isStringView(QualType qt)
+{
+ return bool(loplugin::TypeCheck(qt).ClassOrStruct("basic_string_view").StdNamespace());
+}
+
DeclRefExpr const* relevantImplicitCastExpr(ImplicitCastExpr const* expr)
{
- if (!loplugin::TypeCheck(expr->getType()).ClassOrStruct("basic_string_view").StdNamespace())
+ if (!isStringView(expr->getType()))
{
return nullptr;
}
@@ -158,17 +163,38 @@ DeclRefExpr const* relevantCXXMemberCallExpr(CXXMemberCallExpr const* expr)
DeclRefExpr const* relevantCXXOperatorCallExpr(CXXOperatorCallExpr const* expr)
{
- // TODO OO_EqualEqual and similar
- if (expr->getOperator() != OO_Subscript)
+ if (expr->getOperator() == OO_Subscript)
{
- return nullptr;
+ auto const e = expr->getArg(0);
+ if (relevantStringType(e->getType()) == StringType::None)
+ {
+ return nullptr;
+ }
+ return relevantDeclRefExpr(e);
}
- auto const e = expr->getArg(0);
- if (relevantStringType(e->getType()) == StringType::None)
+ else if (compat::isComparisonOp(expr))
{
- return nullptr;
+ // TODO Can't currently convert rtl::OString because we end up with ambiguous operator==
+ // (one in string_view header and one in rtl/string.hxx header)
+ auto st1 = relevantStringType(compat::IgnoreImplicit(expr->getArg(0))->getType());
+ auto st2 = relevantStringType(compat::IgnoreImplicit(expr->getArg(1))->getType());
+ if (st1 == StringType::RtlOustring && st2 == StringType::RtlOustring)
+ {
+ auto e1 = relevantDeclRefExpr(expr->getArg(0));
+ if (e1)
+ return e1;
+ return relevantDeclRefExpr(expr->getArg(1));
+ }
+ if (st1 == StringType::RtlOustring && isStringView(expr->getArg(1)->getType()))
+ {
+ return relevantDeclRefExpr(expr->getArg(0));
+ }
+ if (st2 == StringType::RtlOustring && isStringView(expr->getArg(0)->getType()))
+ {
+ return relevantDeclRefExpr(expr->getArg(1));
+ }
}
- return relevantDeclRefExpr(e);
+ return nullptr;
}
class StringViewParam final
diff --git a/compilerplugins/clang/test/stringviewparam.cxx b/compilerplugins/clang/test/stringviewparam.cxx
index afa0454b0b43..decd17408506 100644
--- a/compilerplugins/clang/test/stringviewparam.cxx
+++ b/compilerplugins/clang/test/stringviewparam.cxx
@@ -50,4 +50,9 @@ template <> void f5<OUString>(OUString const&) {}
void f6([[maybe_unused]] OUString const&) {}
+// expected-error@+1 {{replace function parameter of type 'const rtl::OUString &' with 'std::u16string_view' [loplugin:stringviewparam]}}
+bool f7(const OUString& p1, OUString p2) { return p1 == p2; }
+// expected-error@+1 {{replace function parameter of type 'const rtl::OUString &' with 'std::u16string_view' [loplugin:stringviewparam]}}
+bool f8(const OUString& p1, std::u16string_view p2) { return p1 == p2; }
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */