I'm currently implementing a profiling system into an application.
I have a two macro functions which are defined based on a compiler flag (NDEBUG). When NDEBUG is not defined, these two functions (profilingStart / profilingEnd) generate profiling reports which show the time when profilingStart was called, vs the time when profilingEnd was called.
The concern is the potential for a mismatch to occur -- i.e, a scenario when profilingStart has been called, but profilingEnd has not (or vice-versa). My code will already recognize these situations at run-time, but it would be preferable if an error resulted during compile time due to this mismatch.
One suggestion has been to use the do{...}while(); construct to ensure the profiling functions are properly paired. The start macro function would contain the do{ and the end macro would contain the }while(). If one is missing, we would get an error at compile time. However, there are some issues with this -- you could only use the profilingStart() and profilingEnd() calls at the start and end of the function which is being profiled, as using them within the function could impact the scope of local variables (as they may go out of scope due to the do{...}while() call).
Another idea I have had is just to declare a variable in the profilingStart function and then attempt to modify the contents of that variable in the profilingEnd function. This prevents scope issues and would generate a compiler error if the variable was never declared. However, I would never have any method of verifying that the contents of the variable are modified in the end function. This only helps with half of the problem, as it does not verify the call of the profilingEnd function.
Any comments are appreciated, as always. Thanks in advance.
EDIT: There may be some confusion regarding my comment(s) regarding scope. profilingStart() and profilingEnd() will always be called within the same function. They may just not be called at the very beginning / very end of the function. Here is an example of what I meant:
int DoSomething(void)
{
profilingStart();
int a;
DoMath(a);
profilingStop();
return a; // a is out of scope here, as the do{...}while(0) construct has gone out of scope
}