This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: What is the address of this line?


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; \
    } \
})

Brian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]