diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /compilerplugins/clang/store/unusedcode.cxx | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compilerplugins/clang/store/unusedcode.cxx')
-rw-r--r-- | compilerplugins/clang/store/unusedcode.cxx | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/compilerplugins/clang/store/unusedcode.cxx b/compilerplugins/clang/store/unusedcode.cxx new file mode 100644 index 0000000000..32fc4d3c2d --- /dev/null +++ b/compilerplugins/clang/store/unusedcode.cxx @@ -0,0 +1,77 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ + +/* +This is technically a rewriter, but it actually only generates data about code. + +This is incomplete. + +Checks for all function declarations for whether they are used or not. This information +should be output to files and in a second pass it should be checked (by another tool) +which functions are never used. +*/ + +#include "plugin.hxx" + +namespace loplugin +{ + +class UnusedCode + : public loplugin::FilteringRewritePlugin< UnusedCode > + { + public: + explicit UnusedCode( CompilerInstance& compiler, Rewriter& rewriter ); + virtual void run() override; + bool VisitFunctionDecl( const FunctionDecl* declaration ); + }; + +UnusedCode::UnusedCode( CompilerInstance& compiler, Rewriter& rewriter ) + : FilteringRewritePlugin( compiler, rewriter ) + { + } + +void UnusedCode::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool UnusedCode::VisitFunctionDecl( const FunctionDecl* declaration ) + { + if( ignoreLocation( declaration )) + return true; + bool isUsed = declaration->isUsed(); + if( const CXXMethodDecl* cxxmethod = dyn_cast< CXXMethodDecl >( declaration )) + { + if( !isUsed && cxxmethod->isVirtual()) + { // Virtual methods are used also if a method they override is used. + for( CXXMethodDecl::method_iterator it = cxxmethod->begin_overridden_methods(); + it != cxxmethod->end_overridden_methods(); + ++it ) + { + if( (*it)->isUsed()) + { + isUsed = true; + break; + } + } + } + } + // Fully qualified name: declaration->getQualifiedNameAsString() + // Is used: isUsed + // The main source file compiled: compiler.getSourceManager().getFileEntryForID( compiler.getSourceManager().getMainFileID())->getName() + return true; + } + +static Plugin::Registration< UnusedCode > X( "unusedcode" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |