diff options
Diffstat (limited to 'compilerplugins/clang/stringconstant.cxx')
-rw-r--r-- | compilerplugins/clang/stringconstant.cxx | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/compilerplugins/clang/stringconstant.cxx b/compilerplugins/clang/stringconstant.cxx index f9b3f5a55ce6..79d7c61c7156 100644 --- a/compilerplugins/clang/stringconstant.cxx +++ b/compilerplugins/clang/stringconstant.cxx @@ -11,6 +11,7 @@ #include <limits> #include <stack> #include <string> +#include <iostream> #include "compat.hxx" #include "plugin.hxx" @@ -104,6 +105,8 @@ private: void handleOUStringCtor( CallExpr const * expr, unsigned arg, std::string const & qname); + void handleOUStringCtor2( + CallExpr const * expr, unsigned arg, std::string const & qname); std::stack<Expr const *> calls_; }; @@ -546,6 +549,12 @@ bool StringConstant::VisitCallExpr(CallExpr const * expr) { TreatEmpty::Error); return true; } + // For places where we are calling a method with a 'const OUString&' param + for (unsigned i=0; i < fdecl->getNumParams(); ++i) + { + if (fdecl->getParamDecl(i)->getType().getAsString() == "const ::rtl::OUString &") + handleOUStringCtor2(expr, i, qname); + } return true; } @@ -1283,6 +1292,40 @@ void StringConstant::handleOUStringCtor( << qname << expr->getSourceRange(); } +// For places where we are calling a method with an 'const OUString&' param +// +void StringConstant::handleOUStringCtor2( + CallExpr const * expr, unsigned arg, std::string const & qname) +{ + auto e0 = expr->getArg(arg)->IgnoreParenImpCasts(); + auto e1 = dyn_cast<CXXFunctionalCastExpr>(e0); + if (e1 == nullptr) { + return; + } + e0 = e1->getSubExpr()->IgnoreParenImpCasts(); + auto e2 = dyn_cast<CXXBindTemporaryExpr>(e0); + if (e2 == nullptr) { + return; + } + auto e3 = dyn_cast<CXXConstructExpr>( + e2->getSubExpr()->IgnoreParenImpCasts()); + if (e3 == nullptr) { + return; + } + if (e3->getNumArgs() == 1) + { + std::string s = e3->getArg(0)->getType().getAsString(); + if (s == "sal_Unicode" || s == "char") + return; + } + report( + DiagnosticsEngine::Warning, + ("in call of %0, replace OUString constructed from a string literal" + " directly with the string literal"), + e3->getExprLoc()) + << qname << expr->getSourceRange(); +} + loplugin::Plugin::Registration< StringConstant > X("stringconstant", true); } |