• Forum
  • Lounge
  • Find unused functions in C/C++ code with

 
Find unused functions in C/C++ code with coverage analysis

Hi C++ community,

Unused functions in a software project can cause code bloat, but they also create more work for the developers of tests, especially in the situation where code coverage (the quality) of tests is being measured. If a function is not used, then removing it from a project will reduce not only the complexity of the project, but also the amount of tests that need to be written for it. The problem of finding unused functions in software projects is that it can often be time consuming and frustrating.

Read https://www.froglogic.com/blog/find-unused-functions-c-code-coverage/ to learn how to use froglogic’s Coco product to find unused functions in your code.

Reginald
Thanks for the info Reggie
For Emacs lovers, here is a neat little function which I've lifted from a friendly emacs page.
;;================
;; Grep for the symbol under the cursor. Works only for C / C++ / H / RC files
;; http://www.jjj.de/emacs/emacspage.html
( defun grep-curdirsrc-symbol-at-point ()
"Grep current directory for symbol at cursor. bouton Flèche Suiv"
( interactive )
( cd "~/Documents/mesdocs/le_c/newbase" )

( grep ( concat "grep -w -n -e " (current-word) " *.cpp *.hpp *c *h"
) ) )
;; FWI it's written in Lisp and easily added ti tour emacs init file.

:: in its own window, it will show live links to the offending symbols.

¿what if foo() is being used by bar(), but nobody uses bar()? (or there is a circular usage)
What I do is sic my grep dog on the symbol bar and it will only find declaration / definition and nothing else.
It will also find "bar" in your comments, if any.
Easy
Chained function calls (foo() used by bar()) or even circular usage (foo() invoking itself) is not a problem is no problem if you take the approach described by the original poster: dynamic analysis will never detect an invocation of foo() at runtime. Provided that you have sufficient coverage by tests, of course.

A static analysis, on the other hand, does require a global and complete tracking of all dependencies.
Topic archived. No new replies allowed.