avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] Compiler bug or Incorrect C ?


From: Weddington, Eric
Subject: RE: [avr-gcc-list] Compiler bug or Incorrect C ?
Date: Wed, 28 Jan 2009 23:56:36 -0700

 

> -----Original Message-----
> From: 
> address@hidden 
> [mailto:address@hidden
> org] On Behalf Of Anton James Erasmus
> Sent: Wednesday, January 28, 2009 11:48 PM
> To: address@hidden
> Subject: [avr-gcc-list] Compiler bug or Incorrect C ?
> 
> Hi,
> 
> I have the following C code which gives the compiler
> error.
> "error: initializer element is not a constant"
> 
> #define COMPILE_HOUR (((__TIME__[0]-'0')*10) +
> (__TIME__[1]-'0'))
> #define COMPILE_MINUTE (((__TIME__[3]-'0')*10) +
> (__TIME__[4]-'0'))
> #define COMPILE_SECOND (((__TIME__[6]-'0')*10) +
> (__TIME__[7]-'0'))
> 
> const unsigned char gCompileHour=COMPILE_HOUR;
> const unsigned char gCompileMinute=COMPILE_MINUTE;
> const unsigned char gCompileSecond=COMPILE_SECOND;    
> 
> but the following code compiles without problems and one
> can see in the assembly generated that the macros do
> collapse to a single constant.
> 
> #define COMPILE_HOUR (((__TIME__[0]-'0')*10) +
> (__TIME__[1]-'0'))
> #define COMPILE_MINUTE (((__TIME__[3]-'0')*10) +
> (__TIME__[4]-'0'))
> #define COMPILE_SECOND (((__TIME__[6]-'0')*10) +
> (__TIME__[7]-'0'))
> 
> 
> unsigned char GetCompileHour(void)
> {
>   unsigned char hour=COMPILE_HOUR;
>   return(hour);
> }
> 
> unsigned char GetCompileMinute(void)
> {
>   unsigned char minute=COMPILE_MINUTE;
>   return(minute);
> }
> 
> unsigned char GetCompileSecond(void)
> {
>   unsigned char second=COMPILE_SECOND;
>   return(second);
> }
> 
<snip> 
> So is the code that gives an error actually incorrect C, or
> is it a compiler bug ?
> 

IMO, incorrect C. Anything that produces code has to go into a function; it 
cannot be outside of a function. However, an argument can be made that your 
macros should resolve to be a constant, therefore don't require using a 
function. But how you are using it, a builtin preprocessor string that you are 
using like any ordinary string stored in data... I wouldn't be surprised that 
the compiler might throw a fit about it. Perhaps it does create code, and only 
through the optimization process does it finally resolve to a constant, 
therefore it would require being in a function.

Anyway, my 2 cents.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]