From b736204f3ba6ee9813ae109071c9d442c2fb2219 Mon Sep 17 00:00:00 2001 From: Bjoern Michaelsen Date: Wed, 19 Nov 2014 15:08:05 +0100 Subject: add clang plugin for detecting cascading condops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1b782bb04b09bee5c3db2261f9390a7b2edf4564 Reviewed-on: https://gerrit.libreoffice.org/12967 Reviewed-by: Björn Michaelsen Tested-by: Björn Michaelsen --- compilerplugins/clang/store/cascadingcondop.cxx | 66 +++++++++++++++++++++++++ compilerplugins/clang/store/cascadingcondop.hxx | 38 ++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 compilerplugins/clang/store/cascadingcondop.cxx create mode 100644 compilerplugins/clang/store/cascadingcondop.hxx (limited to 'compilerplugins') diff --git a/compilerplugins/clang/store/cascadingcondop.cxx b/compilerplugins/clang/store/cascadingcondop.cxx new file mode 100644 index 000000000000..f18a93b28bb0 --- /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 000000000000..e648bdf15e50 --- /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: */ -- cgit v1.2.3