This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: What is the address of this line?
Brian Dessent writes:
> John Carter wrote:
>
> > So we using gcc on an embedded platform. ie. Very constrained flash & ram.
> >
> > We also using asserts.
> >
> > Storing __LINE__ and __FILE__ is taking too much flash.
> >
> > Question: I would like to create an assert macro ....
> >
> > #define Assert( expression) do {\
> > if(!(expression)) { \
> > assert_occurred_at_program_counter = THE_ADDRESS_OF_THIS_LINE(); \
> > }
> > } while(0)
> >
> > So how do I write that magical function / macro
> > THE_ADDRESS_OF_THIS_LINE(); That returns the address / PC at that line?
> > Preferable in a CPU neutral fashion, otherwise for a Sparc CPU.
>
> How about something like: (see also section 5.2 of the manual)
>
> #define Assert(expression) ({ \
> __label__ here; \
> if (!(expression)) { \
> here: assert_occurred_at_program_counter = &&here; \
> } \
> })
I also thought of
void *foo ()
{
return __builtin_return_address(0);
}
Andrew.