c++ preprocessor question

Started by
2 comments, last by skalco6 19 years, 6 months ago
I though about something that could be quite interesting, but don't know if it's possible at all... For debugging purpose, I would like to build and execution stack without adding explicit code each time I enter/leave a function or class method. To achieve this, I though I could just 'replace' the curly brackets '{' & '}' by some code that would build the stack of execution with the help of __FUNCTION__. To replace the brackets, I can do this, but it's really very ugly...

#define BRACKET_OPEN    { someStackFunc(__FUNCTION__);
#define BRACKET_CLOSE    someStackFunc(__FUNCTION__); }

void foo() BRACKET_OPEN
    // ...
BRACKET_CLOSE

Well... it works, but I would like to be able to do something like this...

#define BRACKET_OPEN    { someStackFunc(__FUNCTION__);
#define BRACKET_CLOSE    someStackFunc(__FUNCTION__); }

#define { BRACKET_OPEN
#define } BRACKET_CLOSE

void foo() {
    // ...
}

Well... it doesn't work and tells me there's a syntax error with those '#define { ...' and '#define } ...'. I'm looking for a workaround, if there's any, or an alternative to my idea.
Advertisement
Unfortunately, macro names have to start with a letter or some subset of other characters (like '_') - '{' and '}' aren't in the valid set of characters.

The other problem with using the macro (if you did find a way) is that '{' and '}' occur in places other than function definitions:
typedef struct _Coord{    int x;    int y;} Coord;Coord coords [] ={    {0, 0}, {0, 1}, {1, 1}, {1, 0}};

so the idea of replacing '{' and '}' would bugger the above code big time and the following code wouldn't be quite right either:
void function (void){    for (int i = 0 ; i < 10 ; ++i)    { // you wouldn't want your stack check here         // do something    }}


Is there an alternative?

I can think of two possibilities: write your own preprocessor to replace the '{' and '}' that enclose functions with the stack check code. This is not compiler specific.

The other method is compiler specific and that's to define function hooks. Using MSVC V6, this is enabled by specifing /Gh on the compiler command line (you need professional or enterprise version for this). You then create a function called __penter which is then called at the start of every function. It doesn't have the name of the function or even any access to it, but you can get the address of the function and then compare that with the map file. There's no function to catch the end of the function though, but, with a bit of jiggery-pokery of the stack you can catch the return - provided you've not got any Pascal style functions.

Skizz
Kind of a half way between the two....

class StackSetup{public:    StackSetup()    {        // init stack    }    ~StackSetup()    {        // uninit stack    }}#define PREPARE_STACK StackSetup uniqueStackSetupName__void func(){    PREPARE_STACK;    // do stuff}
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Thanks a lot Skizz!

I so much like that compiler specific solution. I'm using VC.net, so thats alright, but even if my code aims to be portable, I'll only have to debug under windows to use my upcoming feature.

By the way, there's another option I found while investigating...

/Gh : _penter (executes while entering the method or function)
/GH : _pexit (executes while leaving the method or function)

Can't wait anymore to try those!

This topic is closed to new replies.

Advertisement