Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find all places a given member function or ctor is called in g++ code?

Tags:

c++

gcc

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

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

In this example, it is not obvious looking at this that snark may be a char *, which is what I'm interested in.

Attempts To Solve This So Far

I've looked into some of the dump features of gcc, and generated some of them, but I haven't been able to find any that tell me that the given line of code will generate a call to the string constructor taking a const char *. I've also compiled some code with -s to save the generated equivalent assembly code. But this suffers from two things: the function names are "mangled," so it's impossible to know what is being called in C++ terms; and there are no line numbers of any sort, so even finding the equivalent place in the source file would be tough.

Motivation and Background

In my project, we're porting 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 one to initialize a string with a NULL pointer, treating it as an empty string. The Gnu tools' generated code fails with some flavor of a null dereference error. So we need to find all of the potential cases of this, and remedy them. (For example, by adding code to check for NULL and using a pointer to a "" string instead.)

So if anyone out there has had to deal with the base problem and can offer other suggestions, those, too, would be welcomed.

like image 350
Jerry Oberle Avatar asked Dec 07 '16 01:12

Jerry Oberle


1 Answers

Have you considered using static analysis?

Clang has one called clang analyzer that is extensible.

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

There is a manual for that here.

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

like image 94
Alexander Oh Avatar answered Sep 28 '22 12:09

Alexander Oh