[Libreoffice-commits] core.git: compilerplugins/clang

Noel (via logerrit) logerrit at kemper.freedesktop.org
Thu Nov 12 17:56:17 UTC 2020


 compilerplugins/clang/test/xmlimport.cxx |    6 +++
 compilerplugins/clang/xmlimport.cxx      |   47 +++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 2 deletions(-)

New commits:
commit 4a7e972ea2ddad4987934fb181fcc1b7e3d125f8
Author:     Noel <noelgrandin at gmail.com>
AuthorDate: Thu Nov 12 12:44:13 2020 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Thu Nov 12 18:55:32 2020 +0100

    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 at collabora.co.uk>

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 at +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");


More information about the Libreoffice-commits mailing list