How to determine the correct pairs of function calls

I am looking for a tool capable of detecting paired calls to paired functions in a nested manner, as shown below:

f()   // depth 0
   f()  //depth 1
   g()
g()

At each call depth there f()should be a call to g()form a pair of function calls. This is especially important when entering and exiting a critical section.

+5
source share
4 answers

In C ++, one option is to wrap the calls f()and g()in the constructor and destructor of the class and just call these functions creates an instance of an instance of this class. For instance,

struct FAndGCaller
{
    FAndGCaller() { f(); }
    ~FAndGCaller() { g(); }
};

This can then be used in any scope block as follows:

{
    FAndGCaller call_f_then_later_g; // calls f()

} // calls g()

, , f() g() , .

(SBRM, Resource Assquisition, , RAII) .

+11

for -loop .

#define SAVETHEDAY for (bool seen = ((void)f(), true); seen; seen = ((void)g(), false))

f g.

SAVETHEDAY {

    SAVETHEDAY {

    }
}

:

  • .
  • ++ C99.
  • for -loop .

:

  • break, return continue , g .
  • ++ throw , g
  • , - ().
  • ++, , ,

continue , - . ++, for -variable, f g .

+2

( ) , f(), . , g(), . . - , ( g(), f()).

, - C () ++, . , . , , clang ( ), , , , .

0

The Coccinelle tool for semantic search and correction of C code is designed for this kind of task (see also this LWN article on the tool).

0
source

All Articles