bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / store / unusedcode.cxx
blob09fc71b3fcc570745cfe0493c9fac99c3fa1a25a
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.
13 This is technically a rewriter, but it actually only generates data about code.
15 This is incomplete.
17 Checks for all function declarations for whether they are used or not. This information
18 should be output to files and in a second pass it should be checked (by another tool)
19 which functions are never used.
22 #include "plugin.hxx"
24 namespace loplugin
27 class UnusedCode
28 : public RecursiveASTVisitor< UnusedCode >
29 , public RewritePlugin
31 public:
32 explicit UnusedCode( CompilerInstance& compiler, Rewriter& rewriter );
33 virtual void run() override;
34 bool VisitFunctionDecl( const FunctionDecl* declaration );
37 UnusedCode::UnusedCode( CompilerInstance& compiler, Rewriter& rewriter )
38 : RewritePlugin( compiler, rewriter )
42 void UnusedCode::run()
44 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
47 bool UnusedCode::VisitFunctionDecl( const FunctionDecl* declaration )
49 if( ignoreLocation( declaration ))
50 return true;
51 bool isUsed = declaration->isUsed();
52 if( const CXXMethodDecl* cxxmethod = dyn_cast< CXXMethodDecl >( declaration ))
54 if( !isUsed && cxxmethod->isVirtual())
55 { // Virtual methods are used also if a method they override is used.
56 for( CXXMethodDecl::method_iterator it = cxxmethod->begin_overridden_methods();
57 it != cxxmethod->end_overridden_methods();
58 ++it )
60 if( (*it)->isUsed())
62 isUsed = true;
63 break;
68 // Fully qualified name: declaration->getQualifiedNameAsString()
69 // Is used: isUsed
70 // The main source file compiled: compiler.getSourceManager().getFileEntryForID( compiler.getSourceManager().getMainFileID())->getName()
71 return true;
74 static Plugin::Registration< UnusedCode > X( "unusedcode" );
76 } // namespace
78 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */