From 4a7e972ea2ddad4987934fb181fcc1b7e3d125f8 Mon Sep 17 00:00:00 2001 From: Noel Date: Thu, 12 Nov 2020 12:44:13 +0200 Subject: loplugin:xmlimport add check for passing XML_TOK* constants to a non-sal_uInt16 parameter, which is a sign of an incomplete fastparser conversion. Change-Id: Icad5bf9eb40fc15fd07b0d9ea79f83ed083de784 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/105638 Tested-by: Jenkins Reviewed-by: Noel Grandin --- compilerplugins/clang/xmlimport.cxx | 47 +++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'compilerplugins/clang/xmlimport.cxx') diff --git a/compilerplugins/clang/xmlimport.cxx b/compilerplugins/clang/xmlimport.cxx index e01f387e0c74..b18554dfc768 100644 --- a/compilerplugins/clang/xmlimport.cxx +++ b/compilerplugins/clang/xmlimport.cxx @@ -73,10 +73,12 @@ public: bool VisitCXXMemberCallExpr(const CXXMemberCallExpr*); bool VisitBinaryOperator(const BinaryOperator*); bool VisitSwitchStmt(const SwitchStmt*); + bool VisitCallExpr(const CallExpr*); private: bool isXmlTokEnum(const Expr*); bool isUInt16(const Expr*); + bool isUInt16(QualType); std::unordered_map startFastElementSet; std::unordered_map StartElementSet; @@ -316,6 +318,42 @@ bool XmlImport::VisitSwitchStmt(const SwitchStmt* switchStmt) return true; } +bool XmlImport::VisitCallExpr(const CallExpr* callExpr) +{ + auto beginLoc = compat::getBeginLoc(callExpr); + if (!beginLoc.isValid() || ignoreLocation(callExpr)) + return true; + + const FunctionDecl* functionDecl; + if (isa(callExpr)) + functionDecl = dyn_cast(callExpr)->getMethodDecl(); + else + functionDecl = callExpr->getDirectCallee(); + if (!functionDecl) + return true; + for (unsigned i = 0; i != callExpr->getNumArgs(); ++i) + { + auto argExpr = compat::IgnoreImplicit(callExpr->getArg(i)); + if (!isXmlTokEnum(argExpr)) + continue; + // if the condition is an enum type, ignore this switch + auto condEnumType = functionDecl->getParamDecl(i) + ->getType() + ->getUnqualifiedDesugaredType() + ->getAs(); + if (condEnumType) + continue; + if (isUInt16(functionDecl->getParamDecl(i)->getType())) + return true; + report(DiagnosticsEngine::Warning, + "passing XML_TOK enum to 'sal_Int32', wrong param or XML token type", + compat::getBeginLoc(callExpr)) + << callExpr->getSourceRange(); + } + + return true; +} + bool XmlImport::isXmlTokEnum(const Expr* expr) { expr = compat::IgnoreImplicit(expr); @@ -335,9 +373,14 @@ bool XmlImport::isXmlTokEnum(const Expr* expr) bool XmlImport::isUInt16(const Expr* expr) { expr = compat::IgnoreImplicit(expr); - if (expr->getType()->isSpecificBuiltinType(BuiltinType::UShort)) + return isUInt16(expr->getType()); +} + +bool XmlImport::isUInt16(QualType qt) +{ + if (qt->isSpecificBuiltinType(BuiltinType::UShort)) return true; - return bool(loplugin::TypeCheck(expr->getType()).Typedef("sal_uInt16")); + return bool(loplugin::TypeCheck(qt).Typedef("sal_uInt16")); } loplugin::Plugin::Registration xmlimport("xmlimport"); -- cgit