Recently, I wanted to be able to write a generic Code analysis tool (in fact the word is a bit too.) In fact the C code). See these days I see Code dizzy, despite the source insight help, still not enough wisdom to think a lot of places.
The following are some of the main problems that are encountered today:
1. Very many functions are defined as macros and are then called.
This will require you to search for the corresponding definition. Then look for the call place.
2, the structure of the program is very much used in the finite state machine, the function will be put into the array.
Will encounter the same problem as above.
3. Existing tools can not show all the functions of the call and the called relationship, so that the code is always elephant, inevitably there will be omissions.
So I would like to write a tool for code analysis. Ability to help with faster, clearer analysis of code. Now the initial plan to use Python development, after all, third-party libraries more, development is more convenient. This post as the beginning of the project of the Maiden Post, also equivalent to their own project to start.
Now there are some basic ideas, such as the following:
Features that need to be implemented:
1. Analyze the functions in each C file (some text parsing algorithms are required to infer and differentiate function definitions and function calls) and store them.
2, the relationship of storing functions (called and being called) by means of a graph is stored by the structure of the adjacency list.
3. Suppose a function is not called. Infer whether it is replaced (defined as a macro or as an array element) and handles it.
Analysis code The first step: Establish the function call relationship list.
Pseudo code:
While (not the last function in the last file)
{
Current_function= the name of the function obtained by text parsing
Fill current_function with child nodes (analyze functions called in current_function)
If (current_function is already stored in the diagram)
{
The linked list you just created is placed in the location of the current_function in the diagram.
}
Else
{
Current_function as a top-level node into the diagram
}
}
Clean up the top-level node (address the third question raised above.) )
Parsing code Step two: show the current_function call relationship.
After the function call diagram is established, it is assumed that to display the current_function call relationship, simply traverse the graph with current_function as the starting point.
Now I'm just a couple of things up there. The next step is to make sure that you meet so many problems. I believe that there are more ways than difficulties. Come on.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
C Code Analyzer (one Open champion)