Jump to content
  • Advertisement
Sign in to follow this  
skalco6

c++ preprocessor question

This topic is 5394 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!