Update git submodules
[LibreOffice.git] / compilerplugins / clang / store / unusedcode.cxx
blob32fc4d3c2d7b2a9f2054bf99f1dac50131fd51a3
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 loplugin::FilteringRewritePlugin< UnusedCode >
30 public:
31 explicit UnusedCode( CompilerInstance& compiler, Rewriter& rewriter );
32 virtual void run() override;
33 bool VisitFunctionDecl( const FunctionDecl* declaration );
36 UnusedCode::UnusedCode( CompilerInstance& compiler, Rewriter& rewriter )
37 : FilteringRewritePlugin( compiler, rewriter )
41 void UnusedCode::run()
43 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
46 bool UnusedCode::VisitFunctionDecl( const FunctionDecl* declaration )
48 if( ignoreLocation( declaration ))
49 return true;
50 bool isUsed = declaration->isUsed();
51 if( const CXXMethodDecl* cxxmethod = dyn_cast< CXXMethodDecl >( declaration ))
53 if( !isUsed && cxxmethod->isVirtual())
54 { // Virtual methods are used also if a method they override is used.
55 for( CXXMethodDecl::method_iterator it = cxxmethod->begin_overridden_methods();
56 it != cxxmethod->end_overridden_methods();
57 ++it )
59 if( (*it)->isUsed())
61 isUsed = true;
62 break;
67 // Fully qualified name: declaration->getQualifiedNameAsString()
68 // Is used: isUsed
69 // The main source file compiled: compiler.getSourceManager().getFileEntryForID( compiler.getSourceManager().getMainFileID())->getName()
70 return true;
73 static Plugin::Registration< UnusedCode > X( "unusedcode" );
75 } // namespace
77 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */