/* -*- 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"postfixincrementfix.hxx"
/* This is a rewriter.
Change all postfix ++ operators of non-trivial types to prefix if possible.
*/
namespace loplugin
{
PostfixIncrementFix::PostfixIncrementFix( const InstantiationData& data )
: FilteringRewritePlugin( data )
{
}
bool PostfixIncrementFix::VisitCXXOperatorCallExpr( const CXXOperatorCallExpr* op )
{ if( ignoreLocation( op )) returntrue; // postfix ++ has two arguments (the operand and the hidden extra int) if( op->getOperator() == OO_PlusPlus && op->getNumArgs() == 2 )
fixPostfixOperator( op ); // For primitive types it would be UnaryOperatorExpr, but probably no good reason to change those. returntrue;
}
void PostfixIncrementFix::fixPostfixOperator( const CXXOperatorCallExpr* op )
{ if( !canChangePostfixToPrefix( op, op )) return; if( !shouldDoChange( op->getArg( 0 ))) return; // Adding spaces around the moved ++ should not be necessary // (there might a problem with e.g. a+b++ -> a+++b (i.e. a++ +b), // but we don't change such expressions). if( insertText( op->getLocStart(), "++" )) // insert is intentionally first, in case it fails
removeText( op->getCallee()->getSourceRange());
}
bool PostfixIncrementFix::canChangePostfixToPrefix( const Stmt* stmt , const CXXOperatorCallExpr* op )
{ const Stmt* parent = parentStmt( stmt ); if( parent == NULL ) returntrue; // check if foo++ can be safely replaced by ++foo switch( parent->getStmtClass())
{ case Stmt::CompoundStmtClass: returntrue; // these mean somebody is going to use it case Stmt::ImplicitCastExprClass: case Stmt::MaterializeTemporaryExprClass: case Stmt::BinaryOperatorClass: case Stmt::UnaryOperatorClass: case Stmt::CallExprClass: case Stmt::CXXOperatorCallExprClass: returnfalse; case Stmt::CXXBindTemporaryExprClass: // tricky, it may just mean the temporary will be cleaned up // (by ExprWithCleanups), ignore and go up return canChangePostfixToPrefix( parent, op ); case Stmt::ExprWithCleanupsClass: // cleanup of a temporary, should be harmless (if the use // of the postfix ++ operator here relies on the fact that // the dtor for the object will be called, that's pretty insane // code). Ignore and go up. return canChangePostfixToPrefix( parent, op ); case Stmt::ParenExprClass: // parentheses, go up return canChangePostfixToPrefix( parent, op ); case Stmt::IfStmtClass: // cannot be changed in condition, can be changed in statements return cast< IfStmt >( parent )->getCond() != stmt; case Stmt::WhileStmtClass: return cast< WhileStmt >( parent )->getCond() != stmt; case Stmt::DoStmtClass: return cast< DoStmt >( parent )->getCond() != stmt; case Stmt::ForStmtClass: return cast< ForStmt >( parent )->getCond() != stmt; default:
report( DiagnosticsEngine::Fatal, "cannot analyze operator++ (plugin needs fixing)",
op->getLocStart()) << parent->getSourceRange();
parent->dump(); returnfalse;
}
}
bool PostfixIncrementFix::shouldDoChange( const Expr* operand )
{ // TODO Changing 'a->b++' to '++a->b' is technically the same, but the latter probably looks confusing, // so either avoid that, or add parentheses. Avoid for now. const Expr* expr = const_cast< Expr* >( operand )->IgnoreImplicit(); // does not have const version switch( expr->getStmtClass())
{ case Stmt::ParenExprClass: returntrue; // there are already parentheses, ok to move the ++ case Stmt::MemberExprClass: returnfalse; // ++a->b , avoid case Stmt::DeclRefExprClass: returntrue; default:
{
report( DiagnosticsEngine::Fatal, "cannot analyze operator++ (plugin needs fixing)",
expr->getLocStart()) << operand->getSourceRange();
expr->dump();
operand->dump(); returnfalse;
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.