Kaydet (Commit) b736204f authored tarafından Bjoern Michaelsen's avatar Bjoern Michaelsen Kaydeden (comit) Björn Michaelsen

add clang plugin for detecting cascading condops

Change-Id: I1b782bb04b09bee5c3db2261f9390a7b2edf4564
Reviewed-on: https://gerrit.libreoffice.org/12967Reviewed-by: 's avatarBjörn Michaelsen <bjoern.michaelsen@canonical.com>
Tested-by: 's avatarBjörn Michaelsen <bjoern.michaelsen@canonical.com>
üst 58ce60da
/* -*- 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: */
/* -*- 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: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment