diff options
author | Bjoern Michaelsen <bjoern.michaelsen@canonical.com> | 2014-11-19 15:08:05 +0100 |
---|---|---|
committer | Björn Michaelsen <bjoern.michaelsen@canonical.com> | 2014-11-19 14:58:40 +0000 |
commit | b736204f3ba6ee9813ae109071c9d442c2fb2219 (patch) | |
tree | 50c4be237612cce208bd8361cdc0bc9d8048ebd0 | |
parent | 58ce60da28b019be3dcf52c6b9fc51b91361137e (diff) |
add clang plugin for detecting cascading condops
Change-Id: I1b782bb04b09bee5c3db2261f9390a7b2edf4564
Reviewed-on: https://gerrit.libreoffice.org/12967
Reviewed-by: Björn Michaelsen <bjoern.michaelsen@canonical.com>
Tested-by: Björn Michaelsen <bjoern.michaelsen@canonical.com>
-rw-r--r-- | compilerplugins/clang/store/cascadingcondop.cxx | 66 | ||||
-rw-r--r-- | compilerplugins/clang/store/cascadingcondop.hxx | 38 |
2 files changed, 104 insertions, 0 deletions
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: */ |