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

Bjoern Michaelsen bjoern.michaelsen at canonical.com
Wed Nov 19 06:59:10 PST 2014


 compilerplugins/clang/store/cascadingcondop.cxx |   66 ++++++++++++++++++++++++
 compilerplugins/clang/store/cascadingcondop.hxx |   38 +++++++++++++
 2 files changed, 104 insertions(+)

New commits:
commit b736204f3ba6ee9813ae109071c9d442c2fb2219
Author: Bjoern Michaelsen <bjoern.michaelsen at canonical.com>
Date:   Wed Nov 19 15:08:05 2014 +0100

    add clang plugin for detecting cascading condops
    
    Change-Id: I1b782bb04b09bee5c3db2261f9390a7b2edf4564
    Reviewed-on: https://gerrit.libreoffice.org/12967
    Reviewed-by: Björn Michaelsen <bjoern.michaelsen at canonical.com>
    Tested-by: Björn Michaelsen <bjoern.michaelsen at canonical.com>

diff --git a/compilerplugins/clang/store/cascadingcondop.cxx b/compilerplugins/clang/store/cascadingcondop.cxx
new file mode 100644
index 0000000..f18a93b
--- /dev/null
+++ b/compilerplugins/clang/store/cascadingcondop.cxx
@@ -0,0 +1,66 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * Based on LLVM/Clang.
+ *
+ * This file is distributed under the University of Illinois Open Source
+ * License. See LICENSE.TXT for details.
+ *
+ */
+
+#include "cascadingcondop.hxx"
+
+/*
+This is a compile check.
+
+It checks for conditional operators in conditional operators, which are
+errorprone, e.g.
+ Thing foo = IsBar() ? ( IsBaz() ? b1 : b2 ) : b3;
+
+However, it finds 556 cases in sw/source alone, thus likely needs some more
+restricting, e.g. by checking for multiline conditional operator statements or
+a certain length in characters (but that needs the Context/SourceManager, which
+I havent played with yet).
+*/
+
+namespace loplugin
+{
+
+// Ctor, nothing special, pass the argument(s).
+CascadingCondOp::CascadingCondOp( const InstantiationData& data )
+    : Plugin( data )
+{
+}
+
+// Perform the actual action.
+void CascadingCondOp::run()
+{
+    // Traverse the whole AST of the translation unit (i.e. examine the whole source file).
+    // The Clang AST helper class will call VisitReturnStmt for every return statement.
+    TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
+}
+
+void CascadingCondOp::Walk( const Stmt* stmt )
+{
+    for(Stmt::const_child_iterator it = stmt->child_begin(); it != stmt->child_end(); ++it)
+    {
+        if ( const ConditionalOperator* condop = dyn_cast< ConditionalOperator >( *it ))
+            report( DiagnosticsEngine::Warning, "cascading conditional operator", condop->getLocStart());
+        Walk(*it);
+    }
+}
+
+bool CascadingCondOp::VisitStmt( const Stmt* stmt )
+{
+    if ( const ConditionalOperator* condop = dyn_cast< ConditionalOperator >( stmt ))
+        Walk(condop);
+    return true;
+}
+
+// Register the plugin action with the LO plugin handling.
+static Plugin::Registration< CascadingCondOp > X( "cascadingcondop" );
+
+} // namespace loplugin
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/store/cascadingcondop.hxx b/compilerplugins/clang/store/cascadingcondop.hxx
new file mode 100644
index 0000000..e648bdf
--- /dev/null
+++ b/compilerplugins/clang/store/cascadingcondop.hxx
@@ -0,0 +1,38 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * Based on LLVM/Clang.
+ *
+ * This file is distributed under the University of Illinois Open Source
+ * License. See LICENSE.TXT for details.
+ *
+ */
+
+#ifndef CASCADINGCONDOP_H
+#define CASCADINGCONDOP_H
+
+#include "plugin.hxx"
+
+namespace loplugin
+{
+
+// The class implementing the plugin action.
+class CascadingCondOp
+    // Inherits from the Clang class that will allow examing the Clang AST tree (i.e. syntax tree).
+    : public RecursiveASTVisitor< CascadingCondOp >
+    // And the base class for LO Clang plugins.
+    , public Plugin
+    {
+    public:
+        CascadingCondOp( const InstantiationData& data );
+        virtual void run() override;
+        void Walk( const Stmt* stmt );
+        bool VisitStmt( const Stmt* stmt );
+    };
+
+} // namespace loplugin
+
+#endif // CASCADINGCONDOP_H
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list