diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2021-01-12 15:53:36 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2021-01-12 22:28:18 +0100 |
commit | 94f6765d6ecc3145fa2d266231124003cf953118 (patch) | |
tree | be042584170eb3e0c05f0d023225aace13d41dc8 | |
parent | 82cb5a3bb27bb70c97b3d04b9de97a3ff67231c5 (diff) |
Avoid loplugin:stringviewparam when there already is a string_view overload
This avoids clang-cl
> In file included from core/connectivity/source/drivers/ado/Aolevariant.cxx:20:
> connectivity/source/inc\ado/Aolevariant.hxx(72,40): error: replace function parameter of type 'const rtl::OUString &' with 'std::u16string_view' [loplugin:stringviewparam]
> OLEVariant(const OUString& us)
> ~~~~~~~~~~~~~~~~^~
which would make that OLEVariant ctor overload redundant with the existing
OLEVariant(std::u16string_view us);
overload, but with the OUString overload gone, implicit conversions from
OUString to OLEVariant would no longer work, e.g.,
> connectivity/source/drivers/ado/AColumn.cxx(184,76): error: no viable conversion from 'rtl::OUString' to 'const connectivity::ado::OLEVariant'
> OTools::putValue(m_aColumn.get_Properties(), sAdoPropertyName, getString(rValue));
> ^~~~~~~~~~~~~~~~~
Change-Id: I92a5cc29d9fd2a5ff1a951f79df64879d0f71743
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/109180
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
-rw-r--r-- | compilerplugins/clang/stringviewparam.cxx | 55 | ||||
-rw-r--r-- | compilerplugins/clang/test/stringviewparam.cxx | 12 |
2 files changed, 67 insertions, 0 deletions
diff --git a/compilerplugins/clang/stringviewparam.cxx b/compilerplugins/clang/stringviewparam.cxx index a96d8cad4929..86ee7c21b393 100644 --- a/compilerplugins/clang/stringviewparam.cxx +++ b/compilerplugins/clang/stringviewparam.cxx @@ -206,6 +206,57 @@ SmallVector<DeclRefExpr const*, 2> relevantCXXOperatorCallExpr(CXXOperatorCallEx return {}; } +//TODO: current implementation is not at all general, just tests what we encounter in practice: +bool hasStringViewOverload(ParmVarDecl const* decl) +{ + auto const d1 = cast<FunctionDecl>(decl->getDeclContext()); + auto const ctx = d1->getDeclContext(); + if (!ctx->isLookupContext()) + { + return false; + } + auto const res = ctx->lookup(d1->getDeclName()); + auto const idx = decl->getFunctionScopeIndex(); + auto const n = d1->getNumParams(); + assert(n > idx); + for (auto i = res.begin(); i != res.end(); ++i) + { + auto const d2 = dyn_cast<FunctionDecl>(*i); + if (d2 == nullptr) + { + continue; + } + if (d2->getNumParams() != n) + { + continue; + } + auto match = true; + for (unsigned j = 0; j != n; ++j) + { + if (j == idx) + { + //TODO: check for exactly std::string_view or std::u16string_view: + if (!isStringView(d2->getParamDecl(j)->getType())) + { + match = false; + break; + } + } + else if (d1->getParamDecl(j)->getType().getCanonicalType() + != d2->getParamDecl(j)->getType().getCanonicalType()) + { + match = false; + break; + } + } + if (match) + { + return true; + } + } + return false; +} + class StringViewParam final : public loplugin::FunctionAddress<loplugin::FilteringPlugin<StringViewParam>> { @@ -439,6 +490,10 @@ private: { continue; } + if (hasStringViewOverload(i)) + { + continue; + } auto const t = relevantStringType(i->getType().getNonReferenceType()); assert(t != StringType::None); report(DiagnosticsEngine::Warning, diff --git a/compilerplugins/clang/test/stringviewparam.cxx b/compilerplugins/clang/test/stringviewparam.cxx index 56fadbea71cf..24dab18e0f9c 100644 --- a/compilerplugins/clang/test/stringviewparam.cxx +++ b/compilerplugins/clang/test/stringviewparam.cxx @@ -71,4 +71,16 @@ struct Converter } }; +void f9(std::u16string_view); +void f9(OUString const& s) { return f9(std::u16string_view(s)); } + +struct S10 +{ + S10(std::u16string_view); + S10(OUString const& s) + : S10(std::u16string_view(s)) + { + } +}; + /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |