How do I locate Uncalled Functions? [closed]

I am looking at some C/C++ code that appears to have functions that are defined, but never used. This is a pretty tedious process to trace through the code and verify. I’ve done some surveys and there are a number of tools that can do this type of analysis.

Can anyone share any experiences or tips on which tools and techniques are best or alternative methods of verifying this code is in fact not used?

1

There are some tools out there which can find ‘dead code’ in your programs. you can read about them on Stack Overflow threads here and here.
a small summary:

use the gcc compiler flags -Wunused and -Wunreachable-code, and then use a tool like lcov to find the unused methods.

1

Trace? Why? Just comment them out and run through the compiler. It will quickly tell you if you commented out some function which was referenced somewhere else.

3

You can use Cppcheck for this purpose:

$ cppcheck --enable=unusedFunction .
Checking foo.c...
1/2 files checked 0% done
Checking main.c...
2/2 files checked 0% done
[foo.c:1]: (style) The function 'foo' is never used.

If you are actively looking for unused code, use something Jake223 suggested.

But, if you just stumble upon some code that seems to be unused, you can just do a search for the method’s name in your codebase. If it’s a private method, your job is easy, search only the current class. If it’s a public method, search everything. If it’s a whole class, search for it’s name in all your code.

No hit? Perfect. Delete the code and run tests. You do have tests, don’t you? Than commit your changes to your document version system. You use one, don’t you? That way if you discover, ever, that you need it back, you just revert a change.

Deleting code should be something you do every day. Maybe you refactor some code and delete the old one. Or find old, unused code and delete it. And even if you don’t have backups, how hard it is to rewrite a function or two? And the second time you will certainly write them better than before.

2

My IDE of choice is Eclipse, and while it’s clunky and a bit of a pain to set up at first, it’s well worth the effort considering all of the tools I get. One such tool (and I’ve no idea what it’s official name is) informs you about unused code, such as classes, functions, variables, etc. The IDE simply displays a yellow line underneath the declaration. I’m not 100% sure if this works with multi-file projects, but you could always give it a shot!

Unless you’re looking for trouble (ie; you know they should be called) why bother. If they are genuinely uncalled, they will probably be removed by the linker, and in any case the wasted space is not significant. On the other hand, if you remove them and later find they were needed (perhaps in some compiler configuration you were not aware of) you will have more work to do to recover them. This is especially difficult if the removal and rediscovery are separated by a lot of time.

3

IDEs usually have this feature well implemented and it works by default (it can usually be disabled).

Whatever method you pick from the other answers to find dead code, always be aware of reflection issues. Some methods/fields may be accessed through reflection alone. Removing those methods will not trigger any alarm, except on runtime.

If you have (good) tests written for your projects, they will be of great value in this situation.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật