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 "tutorial1.hxx"
15 This is a compile check.
17 Checks all return statements and warns if they return literal false (i.e. 'return false').
23 // Ctor, nothing special, pass the argument(s).
24 Tutorial1::Tutorial1( const InstantiationData
& data
)
25 : FilteringPlugin( data
)
29 // Perform the actual action.
32 // Traverse the whole AST of the translation unit (i.e. examine the whole source file).
33 // The Clang AST helper class will call VisitReturnStmt for every return statement.
34 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
37 // This function is called for every return statement.
38 // Returning true means to continue with examining the AST, false means to stop (just always return true).
39 bool Tutorial1::VisitReturnStmt( const ReturnStmt
* returnstmt
)
41 // Helper function from the LO base plugin class, call at the very beginning to ignore sources
42 // that should not be processed (e.g. system headers).
43 if( ignoreLocation( returnstmt
))
45 // Get the expression in the return statement (see ReturnStmt API docs).
46 const Expr
* expression
= returnstmt
->getRetValue();
47 if( expression
== NULL
)
48 return true; // plain 'return;' without expression
49 // Check if the expression is a bool literal (Clang uses dyn_cast<> instead of dynamic_cast<>).
50 if( const CXXBoolLiteralExpr
* boolliteral
= dyn_cast
< CXXBoolLiteralExpr
>( expression
))
52 if( boolliteral
->getValue() == false ) // Is it 'return false;' ? (See CXXBoolLiteralExpr API docs)
53 { // Ok, warn, use LO plugin helper function.
54 report( DiagnosticsEngine::Warning
, // It's just a warning.
55 "returning false", // the message
56 boolliteral
->getLocStart()) // and the exact position where the message should point
57 << returnstmt
->getSourceRange(); // and the full return statement to highlight (optional)
63 // Register the plugin action with the LO plugin handling.
64 static Plugin::Registration
< Tutorial1
> tutorial1( "tutorial1" );
68 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */