bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / store / tutorial / tutorial3.cxx
blob33a1249a34001b89f3dc2c1ba61510382be45637
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * Based on LLVM/Clang.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
12 #include "tutorial3.hxx"
15 This is a rewriter.
17 It looks for if statements with a comparison followed by literal return false
18 and modifies the return statements to 'return maybereturntrue;'
21 namespace loplugin
24 // Ctor, pass arguments.
25 Tutorial3::Tutorial3( const InstantiationData& data )
26 : FilteringRewritePlugin( data )
30 void Tutorial3::run()
32 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
35 bool Tutorial3::VisitIfStmt( const IfStmt* ifstmt )
37 if( ignoreLocation( ifstmt ))
38 return true;
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());
53 return true;
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" );
75 } // namespace
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */