summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@suse.cz>2013-04-22 17:55:45 +0200
committerLuboš Luňák <l.lunak@suse.cz>2013-04-22 17:56:47 +0200
commitb8dd39697607148fc9180ee8396f0251b467e956 (patch)
treea26f310e9199ea7e8fab9913d08681c77ecfe1da /compilerplugins
parent6099811d666ddaf1d9f445891096f97ab53175cc (diff)
base for unusedcode compiler plugin
Needs work to actually do something useful, but the basics are there. Change-Id: I193922f2f5572760c8c20def0f9b830138f47fef
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/store/unusedcode.cxx75
1 files changed, 75 insertions, 0 deletions
diff --git a/compilerplugins/clang/store/unusedcode.cxx b/compilerplugins/clang/store/unusedcode.cxx
new file mode 100644
index 000000000000..d42cb6020ccd
--- /dev/null
+++ b/compilerplugins/clang/store/unusedcode.cxx
@@ -0,0 +1,75 @@
+/*
+ * 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 RecursiveASTVisitor< UnusedCode >
+ , public RewritePlugin
+ {
+ public:
+ explicit UnusedCode( CompilerInstance& compiler, Rewriter& rewriter );
+ virtual void run();
+ bool VisitFunctionDecl( FunctionDecl* declaration );
+ };
+
+UnusedCode::UnusedCode( CompilerInstance& compiler, Rewriter& rewriter )
+ : RewritePlugin( compiler, rewriter )
+ {
+ }
+
+void UnusedCode::run()
+ {
+ TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+bool UnusedCode::VisitFunctionDecl( FunctionDecl* declaration )
+ {
+ if( ignoreLocation( declaration ))
+ return true;
+ bool isUsed = declaration->isUsed();
+ if( 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