"BDSM" Gets a list of C + + functions

Source: Internet
Author: User

Here today to share with you how to get a list of functions in C + + files, the problem and inspiration from the colleague small W self-developed C + + code coverage tool, the principle is by piling the existing code, when running to the function to mark the function, the final statistic to get the function coverage of the code.

Source Code of the program


Post-Piling code


The principle is basically as follows, here will not give you detailed introduction.

So the point is, how do you parse and get the functions in the CPP file?

The first thought of the method is to use the idea of compiling principles to get functions by adding rules, such as: "{", "}", "(", ")" for character matching to determine the start and end of a function. At first, the results are very effective, can be a quick sub-simple code, but with the input method project in the actual application of the problem more and more. Failure to judge a function in the case of a function declaration using a macro definition

1. The function definition contains a macro, there is no way to recognize the function name.


2. Inside the function if a macro definition is used, a mismatch in parentheses may occur, resulting in a dead loop.


And so on, with the continuous deepening of the project in a variety of complex environment-induced problems, and then add rules to avoid these problems, but also can cause new problems. Every time because of the modification of the problem caused by new problems and near collapse!!!

So we may need to find a new way to get the functions in the CPP file. Inspiration comes from notepad++ How does it parse the function? Research found that notepad++ can use plug-in function list to the CPP file parsing, fortunately this plugin is to provide source code!!! Found notepad++ parsing CPP files are also done according to certain rules,


So we just need to repackage the plugin into a DLL file in our project to meet our needs, the advantage is to save us to write a variety of rules, the disadvantage is that our wonderful code it still has an unrecognized function, And the most important thing is that we don't know how to add the rules, oh my gad!

Just to get the name of the function, we embed a hard-coded string in the body of the function, which is tedious and prone to error, rather than looking at how to use the new C99 feature to get the function name when the program runs. The answer is: use __FUNCTION__ and related macros. Gets the function name through the macro definition.


Although this method is feasible, but there are some technical problems do not break through, to be solved after the next time to the decomposition of everyone.

Reprint Please specify:http://blog.csdn.net/sogouauto

"BDSM" Gets a list of C + + functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.