__func__ macro emulation

C

chasdev

I'm using a preprocessor/compiler (HP aCC) that doesn't support the
__func__ macro (a la gcc: expands to the name of the enclosing
function). I've thought about using an extra preprocessing step, writing
a parser, etc but this seems like way too much work. Has anyone tried
this before? Any suggestions welcome!
 
P

Pavel Vozenilek

chasdev said:
I'm using a preprocessor/compiler (HP aCC) that doesn't support the
__func__ macro (a la gcc: expands to the name of the enclosing
function). I've thought about using an extra preprocessing step, writing
a parser, etc but this seems like way too much work. Has anyone tried
this before? Any suggestions welcome!
aCC should use __FUNCTION__.

You may take look on file boost/current_function.hpp from Boost
distribution, it contains macro expanded properly under many
compilers.

/Pavel
 
S

Samuele Armondi

Samuele Armondi said:
Maybe you could manually keep track of the current function, i.e:

#include <vector>
#include <string>
#include <iostream>

class FunctionTracer
{
std::cout << ft.CurrentFunction();
}

This is untested code, I just wrote as it came...so don't blame me if its
broken and buggy (as it probaly will be!). I hope it helps anyway.
S. Armondi
Sorry, I forgot to add the CurrentFunction() code... the code should look
like this:

#include <vector>
#include <string>
#include <iostream>

class FunctionTracer
{
private:
std::vector<std::string> Trace;
int MaxSteps;
public:
FunctionTracer(int);
void EnterFunction(const std::string&);
void LeaveFunction(const std::string&);
void SetMaxSteps(int);
void Reset();
const std::string& CurrentFunction();
};

FunctionTracer::FunctionTracer(int n) : MaxSteps(n)
{
Trace.clear();
Trace.reserve(MaxSteps);
}

void FunctionTracer::EnterFunction(const std::string& s)
{
if (Trace.size() == MaxSteps)
Trace.clear();
Trace.push-back(s);
}

void FunctionTracer::LeaveFunction(const std::string& s)
{
if (Trace.back() == s) // the function we are leaving will
be th
e last one we entered
Trace.pop_back();
else
; // error!
}

void SetMaxSteps(int n)
{std::cout << ft.CurrentFunction();
if (MaxSteps != n)
{
MaxSteps = n;
Trace.reserve(MaxSteps);
}
}

void Reset()
{
Trace.clear();
}

const std::string& CurrentFunction()
{
return Trace.back();
}

//test it

FunctionTracer ft(10);

void f1()
{
ft.EnterFunction("f1");
std::cout << "in function 1\n";
ft.LeaveFunction("f1");
}
void f2()
{
ft.EnterFunction("f2");
std::cout << "in function2\n";
ft.LeaveFunction("f2");
}

int main()
{
ft.EnterFunction("main");
f1();
std::cout << ft.CurrentFunction();
f2();
std::cout << ft.CurrentFunction();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top