How can I find all the places where the specified member function or ctor is called in g ++ code?

I am trying to find all the places in a large and old code base where certain constructors or functions are called. In particular, these are certain constructors and member functions in the std::string class (i.e. basic_string<char> ). For example, suppose a line of code exists:

 std::string foo(fiddle->faddle(k, 9).snark); 

In this example, it is not obvious that snark might be char * , which interests me.

Attempts to solve it so far

I looked at some functions of the gcc dump and generated some of them, but I could not find anyone who would tell me that this line of code would call a string constructor takes a const char * . I also compiled code with -s to save the generated equivalent assembly code. But this is due to two things: function names are "crippled", so it is impossible to know what is called in terms of C ++; and there are no line numbers, so even finding the equivalent location in the source file would not be easy.

Motivation and background.

In my project, we are moving a large old code base from HP-UX (and their aCC C ++ compiler) to RedHat Linux and gcc / g ++ v.4.8.5. The HP tool chain allowed you to initialize a string with a NULL pointer, treating it as an empty string. Generated Gnu tool code with some flavor of zero dereference error. Therefore, we need to find all possible options for this and fix them. (For example, adding code to check for NULL and using the pointer to the string "" instead.)

So, if someone there had to solve a basic problem and could offer other suggestions, this would also be welcome.

+6
source share
3 answers

Have you considered using static analysis?

Clang has one called the clang analyzer, which is extensible.

You can write a custom plugin that checks for this particular behavior by injecting a visitor to clang ast who looks for string variable declarations and checks to set it to null.

There is a guide for here .

See also: https://github.com/facebook/facebook-clang-plugins/blob/master/analyzer/DanglingDelegateFactFinder.cpp

+1
source

First, I would create a header like this:

 #include <string> class dbg_string : public std::string { public: using std::string::string; dbg_string(const char*) = delete; }; #define string dbg_string 

Then modify your makefile and add "-include dbg_string.h" to cflags to force it to be included in every source file without changes.

You can also check how NULL is defined on your platform and add specific overload for it (e.g. dbg_string (int)).

0
source

You can try CppDepend and its CQLinq - a powerful code query language to determine where some constructors / methods / fields / types are used.

 from m in Methods where m.IsUsing ("CClassView.CClassView()") select new { m, m.NbLinesOfCode } 
0
source

Source: https://habr.com/ru/post/1012966/


All Articles