Thread: How to find all calls to a method of some class which has common name

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    Arrow How to find all calls to a method of some class which has common name

    Hello,

    I have a C++/IDE question, let's say I have a class ABC which defines a method
    Code:
    ABC::mandatoryMethod(...){}
    as the name of the function suggests all classes need to define it. Now, how do you guy search for all calls to that mandatoryMethod on this particular class ABC?
    Since this methods is common for all classes we will get plenty of search results.... I wonder if there is a clever way to get only those of our interest (called on class ABC)


    Regards,
    Martin

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Most decent C++ aware IDEs have things like
    - find declaration
    - find definition
    - find references

    Highlight the thing you're interested and pick door number 3.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    @Salem, I gave visual studio a try and it looks like it finds all occurences of the method no matter if a call or declaration not to mention a care of call on specific class....do you agree?

    Also its not possible for IDE to find all references IMHO, because methods may be called by a pointer or in a way of inheritance or in runtime even dynamically in the runtime...
    Last edited by MartinR; 01-16-2021 at 11:33 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Why do you want to find all calls of that method? Usually I'd imagine it is to perform a rename, and your IDE may have a specific refactoring tool for that, but then indirect invocation of the method through a pointer wouldn't matter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Quote Originally Posted by laserlight View Post
    Why do you want to find all calls of that method? Usually I'd imagine it is to perform a rename, and your IDE may have a specific refactoring tool for that, but then indirect invocation of the method through a pointer wouldn't matter.
    Why? I get into new project written in C++ and I try to get the code flow. For example I recognize some point in the program which I am sure will be called, lets call it
    Code:
    classABC::functionABC() {...}
    now the point is to find all callers of that function so I establish a flow... and this is in essence my question...lets neglect debugger and breakpoints for the moment.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Oh, but that wouldn't help you understand the code flow, if by that you mean the flow of control through the program. That would be done by tracing function call graphs rather than by finding all callers of specific functions.

    Quote Originally Posted by MartinR
    Also its not possible for IDE to find all references IMHO, because methods may be called by a pointer or in a way of inheritance or in runtime even dynamically in the runtime...
    I think "references" just means the places where the name refers to the particular function (or object, class, etc) rather than something else with the same name. That said, static analysis can handle inheritance and certain function pointer usage.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Quote Originally Posted by laserlight View Post
    Oh, but that wouldn't help you understand the code flow, if by that you mean the flow of control through the program. That would be done by tracing function call graphs rather than by finding all callers of specific functions.
    Yeah and these function call graphs can't be generated by any IDE right? Only the debugger (by means of a breakpoint) or somebody could generate it base on their experience?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you're looking for run-time, you need some kind of profiling tool.
    Gprof - Wikipedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    @Salem, the problem is I work with the firmware on the embedded device - no OS no nothing. So I end up using GDB to trace calls...do you see any other way?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Which hardware device?

    If you're fortunate enough, maybe your CPU has hardware breakpoint or trace registers.

    If so, you could implement a local handler to increment a local counter that doesn't involve stopping GDB every time the function is called.

    You can then use GDB to just examine the counter at the end of the test.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Its Audio DSP, yes it does support HW brakpoints. As for the custom handler, yes this is actaully a good idea - such handler could record all callers in some data structure (say a linked list) which can be read later on. In the meantime I managed to hookup HW debugger to this device and now I can easily backtrace the code execution for some particular scenario at least.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Method Calls another Method
    By FourAngels in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2015, 12:08 PM
  2. Replies: 4
    Last Post: 04-15-2014, 07:57 PM
  3. Logic To Find Least Common Number Combination
    By EdMarx in forum C Programming
    Replies: 5
    Last Post: 11-29-2013, 07:16 AM
  4. Replies: 1
    Last Post: 10-11-2011, 10:21 PM
  5. class method calls
    By Shingetsu Kurai in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2011, 08:52 PM

Tags for this Thread