diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-04-24 11:01:43 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2023-04-24 12:45:22 +0200 |
commit | dba55c304a330a355147a39e53ec4c7cf5c5c3f5 (patch) | |
tree | dab1da47f32006f6d4362f492f0c8d6255737b5d /compilerplugins | |
parent | 8505d29d61a76ffa506be0dc86fe2ec7ed647483 (diff) |
loplugin:unnecessarygetstr extend to createFromAscii
idea from mike kaganski
Change-Id: I0ecb9cad091d7a048d2ddae73165bf22748f3872
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150907
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/test/unnecessarygetstr.cxx | 10 | ||||
-rw-r--r-- | compilerplugins/clang/unnecessarygetstr.cxx | 71 |
2 files changed, 55 insertions, 26 deletions
diff --git a/compilerplugins/clang/test/unnecessarygetstr.cxx b/compilerplugins/clang/test/unnecessarygetstr.cxx index 12905ec5d233..68ed153649ad 100644 --- a/compilerplugins/clang/test/unnecessarygetstr.cxx +++ b/compilerplugins/clang/test/unnecessarygetstr.cxx @@ -85,4 +85,14 @@ void test3(Foo2& foo) } } +namespace test4 +{ +void test() +{ + std::string s; + // expected-error@+1 {{unnecessary call to 'c_str' when passing to OUString::createFromAscii [loplugin:unnecessarygetstr]}} + OUString::createFromAscii(s.c_str()); +} +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/unnecessarygetstr.cxx b/compilerplugins/clang/unnecessarygetstr.cxx index 66f2fa2851fe..3caf3776e5f7 100644 --- a/compilerplugins/clang/unnecessarygetstr.cxx +++ b/compilerplugins/clang/unnecessarygetstr.cxx @@ -14,6 +14,7 @@ #include "check.hxx" #include "plugin.hxx" +#include "config_clang.h" // Find matches of // @@ -82,39 +83,57 @@ public: auto const tc1 = loplugin::TypeCheck(cxxConstruct->getConstructor()->getParent()); if (!(tc1.ClassOrStruct("basic_string_view").StdNamespace())) continue; - auto e = dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreImplicit()); - if (!e) - continue; - auto const t = e->getObjectType(); - auto const tc2 = loplugin::TypeCheck(t); - if (tc2.Class("OString").Namespace("rtl").GlobalNamespace() - || tc2.Class("OUString").Namespace("rtl").GlobalNamespace() - || tc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace() - || tc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace() - || tc2.ClassOrStruct("StringNumber").Namespace("rtl").GlobalNamespace()) - { - if (loplugin::DeclCheck(e->getMethodDecl()).Function("getStr")) - report(DiagnosticsEngine::Warning, - "unnecessary call to 'getStr' when passing to string_view arg", - e->getExprLoc()) - << e->getSourceRange(); - } - else if (tc2.Class("basic_string").StdNamespace()) - { - if (loplugin::DeclCheck(e->getMethodDecl()).Function("c_str")) - report(DiagnosticsEngine::Warning, - "unnecessary call to 'c_str' when passing to string_view arg", - e->getExprLoc()) - << e->getSourceRange(); - } + checkForGetStr(cxxConstruct->getArg(0), "string_view arg"); } } + if (loplugin::DeclCheck(func) + .Function("createFromAscii") + .Class("OUString") + .Namespace("rtl")) + { + checkForGetStr(callExpr->getArg(0), "OUString::createFromAscii"); + } return true; } - bool preRun() override { return compiler.getLangOpts().CPlusPlus; } + bool preRun() override + { + if (!compiler.getLangOpts().CPlusPlus) + return false; + std::string fn(handler.getMainFileName()); + loplugin::normalizeDotDotInFilePath(fn); + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/qa/")) + return false; + return true; + } private: + void checkForGetStr(Expr* arg, const char* msg) + { + auto e = dyn_cast<CXXMemberCallExpr>(arg->IgnoreImplicit()); + if (!e) + return; + auto const t = e->getObjectType(); + auto const tc2 = loplugin::TypeCheck(t); + if (tc2.Class("OString").Namespace("rtl").GlobalNamespace() + || tc2.Class("OUString").Namespace("rtl").GlobalNamespace() + || tc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace() + || tc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace() + || tc2.ClassOrStruct("StringNumber").Namespace("rtl").GlobalNamespace()) + { + if (loplugin::DeclCheck(e->getMethodDecl()).Function("getStr")) + report(DiagnosticsEngine::Warning, + "unnecessary call to 'getStr' when passing to %0", e->getExprLoc()) + << msg << e->getSourceRange(); + } + else if (tc2.Class("basic_string").StdNamespace()) + { + if (loplugin::DeclCheck(e->getMethodDecl()).Function("c_str")) + report(DiagnosticsEngine::Warning, "unnecessary call to 'c_str' when passing to %0", + e->getExprLoc()) + << msg << e->getSourceRange(); + } + } void run() override { if (preRun()) |