DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Trending

  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Optimize Deployment Pipelines for Speed, Security and Seamless Automation
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem

Find Unused Functions in C/C++ Code With Coverage Analysis

Learn how function coverage features like Coco's can be used to reduce code bloat by finding unused functions in your application.

By 
Sebastian Paulsen user avatar
Sebastian Paulsen
·
Updated Sep. 24, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

Unused Functions Impact Maintenance

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 number 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.

Coco helps by displaying the function coverage of the execution of the program. In this tip of the week, I would like to show you the perks of using Coco's Function Coverage for finding unused functions in your code project.

The Example Source Code

For this presentation I'm using the following C code:

#include <stdio.h>
#include <string.h>

int A(char *arg)
{
    printf("A\n");
    return !strcmp(arg, "TRUE");
}

int B(char *arg)
{
    printf("B\n");
    return !strcmp(arg, "TRUE");
}

int C(char *arg)
{
    printf("C\n");
    return !strcmp(arg, "TRUE");
}

int D(char *arg)
{
    printf("D\n");
    return !strcmp(arg, "TRUE");
}


int main(int argc, char **argv)
{
    if ((A(argv[1]) || B(argv[2])) && (C(argv[3]) || D(argv[4]))) {
        printf("TRUE\n");
    } else {
        printf("FALSE\n");
    }
    return 0;
}

Instrumenting Code, and Running Tests

We will first instrument the code with Coco's Coveragescanner tool. The result is an instrumented executable, and also a .csmes file which contains a database of code lines and their source locations.

$ csgcc -o prog example.c

Next, we run the instrumented program with specific arguments to cause it to execute a code path. This will create an execution report (.csexe file) which contains information about which code was executed. This file will be imported into the generated (.csmes file). As it turns out, with these arguments, the functions B and D are not hit by the test case.

$ ./prog TRUE FALSE TRUE FALSE

We can import the .csexe file into the .csmes file with the following command.

$ cmcsexeimport -m prog.csmes -e prog.csexe -t execution

The final step is to generate the report for the unused functions. Hereby we use the option --function-coverage to specify the coverage level. '--format-unexecuted=' prints the unused functions into the file result.txt by first writing the file( '%f')  and then then line ('%l').

$ cmreport --function-coverage -m prog.csmes --format-unexecuted='%f %l' -t result.txt

Listing Unused Functions

The result.txt file contains now:

.../example.c 10
.../example.c 22

If we want to analyze more runs of the program, we can just run the ./prog command with different arguments. The executions will result in additional data concatenated to the .csexe file.

For further information about cmreport and other features of Coco, follow this link.

Published at DZone with permission of Sebastian Paulsen. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: