diff options
author | Noel <noelgrandin@gmail.com> | 2020-11-12 12:44:13 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2020-11-12 18:55:32 +0100 |
commit | 4a7e972ea2ddad4987934fb181fcc1b7e3d125f8 (patch) | |
tree | 98512b519851bda03153165239c952989992be0e /compilerplugins | |
parent | 6b15b6a6ab294e1d4a3a4bfb5ac81630aa08015c (diff) |
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 <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/test/xmlimport.cxx | 6 | ||||
-rw-r--r-- | compilerplugins/clang/xmlimport.cxx | 47 |
2 files changed, 51 insertions, 2 deletions
diff --git a/compilerplugins/clang/test/xmlimport.cxx b/compilerplugins/clang/test/xmlimport.cxx index d2fb23a69c78..fa1d42a2f1ed 100644 --- a/compilerplugins/clang/test/xmlimport.cxx +++ b/compilerplugins/clang/test/xmlimport.cxx @@ -224,4 +224,10 @@ void test20(sal_uInt32 p, sal_uInt16 q, XmlTokens e) break; } } +void callInt32(sal_Int32); +void test21() +{ + // expected-error@+1 {{passing XML_TOK enum to 'sal_Int32', wrong param or XML token type [loplugin:xmlimport]}} + callInt32(XML_TOK_1); +} /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ 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<const CXXRecordDecl*, const CXXMethodDecl*> startFastElementSet; std::unordered_map<const CXXRecordDecl*, const CXXMethodDecl*> 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<CXXMemberCallExpr>(callExpr)) + functionDecl = dyn_cast<CXXMemberCallExpr>(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<EnumType>(); + 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("xmlimport"); |