1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
12 #include "tutorial3.hxx"
17 It looks for if statements with a comparison followed by literal return false
18 and modifies the return statements to 'return maybereturntrue;'
24 // Ctor, pass arguments.
25 Tutorial3::Tutorial3( const InstantiationData
& data
)
26 : FilteringRewritePlugin( data
)
32 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
35 bool Tutorial3::VisitIfStmt( const IfStmt
* ifstmt
)
37 if( ignoreLocation( ifstmt
))
39 if( const BinaryOperator
* oper
= dyn_cast
< BinaryOperator
>( ifstmt
->getCond()))
41 if( oper
->getOpcode() == BO_EQ
)
43 // Modify the sub-statement if it is 'return false'.
44 modifyReturnFalse( ifstmt
->getThen());
45 // Modify the sub-statement if it is '{ return false; }'.
46 if( const CompoundStmt
* compound
= dyn_cast
< CompoundStmt
>( ifstmt
->getThen()))
48 if( compound
->size() == 1 ) // one statement
49 modifyReturnFalse( *compound
->body_begin());
56 void Tutorial3::modifyReturnFalse( const Stmt
* stmt
)
58 // Is it return statement?
59 if( const ReturnStmt
* returnstmt
= dyn_cast
< ReturnStmt
>( stmt
))
61 // dyn_cast_or_null<> can also be passed NULL, unlike dyn_cast<>
62 if( const CXXBoolLiteralExpr
* boolliteral
= dyn_cast_or_null
< CXXBoolLiteralExpr
>( returnstmt
->getRetValue()))
64 if( boolliteral
->getValue() == false )
65 { // It is, modify the false to true using LO plugin helper function.
66 replaceText( boolliteral
->getSourceRange(), "maybereturntrue" );
72 // Register the plugin action with the LO plugin handling.
73 static Plugin::Registration
< Tutorial3
> tutorial3( "tutorial3" );
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */