tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / compilerplugins / clang / store / tutorial / tutorial1.cxx
blob9f7c97fb776fd1ce879454a61024ab83bf79e6a4
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 "tutorial1.hxx"
15 This is a compile check.
17 Checks all return statements and warns if they return literal false (i.e. 'return false').
20 namespace loplugin
23 // Ctor, nothing special, pass the argument(s).
24 Tutorial1::Tutorial1( const InstantiationData& data )
25 : FilteringPlugin( data )
29 // Perform the actual action.
30 void Tutorial1::run()
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 ))
44 return true;
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 ))
51 { // It is.
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)
60 return true;
63 // Register the plugin action with the LO plugin handling.
64 static Plugin::Registration< Tutorial1 > tutorial1( "tutorial1" );
66 } // namespace
68 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */