From b52f309f2b9037ee53ab8ac2d66967c012ba82f1 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Fri, 12 Apr 2019 18:39:22 +0200 Subject: improve loplugin simplifyconstruct to find stuff like OUString s = OUString("xxx") Change-Id: Ie7ed074c1ae012734c67a2a89c564c1900a4ab04 Reviewed-on: https://gerrit.libreoffice.org/70697 Tested-by: Jenkins Reviewed-by: Noel Grandin --- compilerplugins/clang/simplifyconstruct.cxx | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'compilerplugins/clang/simplifyconstruct.cxx') diff --git a/compilerplugins/clang/simplifyconstruct.cxx b/compilerplugins/clang/simplifyconstruct.cxx index 652a980cec6a..c3e28ce7bcda 100644 --- a/compilerplugins/clang/simplifyconstruct.cxx +++ b/compilerplugins/clang/simplifyconstruct.cxx @@ -31,6 +31,7 @@ public: virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } bool VisitCXXConstructExpr(CXXConstructExpr const*); + bool VisitVarDecl(VarDecl const*); // ignore some contexts within which nullptr is fine bool TraverseReturnStmt(ReturnStmt*) { return true; } @@ -69,6 +70,42 @@ bool SimplifyConstruct::VisitCXXConstructExpr(CXXConstructExpr const* constructE return true; } +bool SimplifyConstruct::VisitVarDecl(VarDecl const* varDecl) +{ + if (ignoreLocation(varDecl)) + return true; + // cannot use OUString s("xxx") style syntax in a parameter + if (isa(varDecl)) + return true; + varDecl = varDecl->getCanonicalDecl(); + if (!varDecl->getInit()) + return true; + if (varDecl->getInitStyle() != VarDecl::InitializationStyle::CInit) + return true; + if (!varDecl->getType()->isRecordType()) + return true; + if (isa(varDecl->getType())) + return true; + + auto init = varDecl->getInit()->IgnoreImplicit(); + auto functionalCast = dyn_cast(init); + if (!functionalCast) + return true; + + // e.g. the LANGUAGE_DONTKNOW defines + if (compiler.getSourceManager().isMacroBodyExpansion(compat::getBeginLoc(init))) + return true; + + // varDecl->getInit()->IgnoreImplicit()->dump(); + // varDecl->getType()->dump(); + // varDecl->getType()->getUnqualifiedDesugaredType()->dump(); + + report(DiagnosticsEngine::Warning, "simplify", varDecl->getLocation()) + << varDecl->getSourceRange(); + + return true; +} + loplugin::Plugin::Registration simplifyconstruct("simplifyconstruct", true); } -- cgit