Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/LibreOffice/compilerplugins/clang/store/   (Office von Apache Version 25.8.3.2©)  Datei vom 5.10.2025 mit Größe 4 kB image not shown  

Quelle  postfixincrementfix.cxx   Sprache: C

 
/* -*- 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 )
    {
    }

void PostfixIncrementFix::run()
    {
    TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
    }

bool PostfixIncrementFix::VisitCXXOperatorCallExpr( const CXXOperatorCallExpr* op )
    {
    if( ignoreLocation( op ))
        return true;
    // 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.
    return true;
    }

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 )
        return true;
    // check if foo++ can be safely replaced by ++foo
    switch( parent->getStmtClass())
        {
        case Stmt::CompoundStmtClass:
            return true;
        // 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:
            return false;
        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();
            return false;
        }
    }

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:
            return true// there are already parentheses, ok to move the ++
        case Stmt::MemberExprClass:
            return false// ++a->b , avoid
        case Stmt::DeclRefExprClass:
            return true;
        default:
            {
            report( DiagnosticsEngine::Fatal, "cannot analyze operator++ (plugin needs fixing)",
                expr->getLocStart()) << operand->getSourceRange();
            expr->dump();
            operand->dump();
            return false;
            }
        }
    }

static Plugin::Registration< PostfixIncrementFix > X( "postfixincrementfix" );

// namespace

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Messung V0.5
C=92 H=98 G=94

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

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.