Disabling gcc warnings

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Disabling gcc warnings

5,379 次查看
d_
Contributor I

Hi,

 

I'm new to the CodeWarrior environment.  I'm used to making sure there are not only no errors (duh!) but no warnings in my compiles.  Some warnings are generated in SOUP and I don't want to modify the SOUP, and some warnings are justifiable (which in some medical environments must be justified).  What I have done in the past is to disable warnings globally when they can have no affect on the code, or disable warnings for a single line of code (or a file, but not as good).

 

I'm using an E02 processor and am stuck with the gcc compiler.  I've searched the archives here and there are plenty of hits for disabling warnings, but after digging through them none seem to apply to the gcc compiler.  I'm also used to getting warning numbers, but this compiler only generates messages.  For example,

 

format '%d' expects argument of type 'int', but argument 2 has type 'uint32_t' [-Wformat]

variable 'c' set but not used [-Wunused-but-set-variable]

 

The first one I can take care of by just commenting out the line of code, only used for debug.  But it would be nice to disable the warning for just this line.

 

The second one is a problem because the hardware requires a register read to set conditions in the peripheral, even though the data isn't used.  In other compilers, I've been able to eliminate this kind of warning by casting the variable as (void) in the statement that uses it.

 

Any ideas?

 

Thanks!

 

Dave

标签 (1)
0 项奖励
回复
4 回复数

4,270 次查看
matthewkendall
Contributor V

Performing a no-effect access to the variable, for example by casting it to void, works fine to suppress the "set but not used" warning in gcc.

For example, this gives warning: variable 'c' set but not used [-Wunused-but-set-variable]

char c;

c = 0;

Whereas this gives no warnings:

char c;

c = 0;

(void)c;    // suppress warning

You can also do it all in one go, although it is a little less clear:

char c;

(void)(c = 0);

But perhaps you can avoid the problem altogether. You say:

the hardware requires a register read to set conditions in the peripheral, even though the data isn't used

so it sounds like you are actually doing something like this:

char c;

volatile char *p = REGISTER_ADDRESS;

c = *p;  // read register to clear hardware

(void)c; // suppress warning

when you could probably just do this:

volatile char *p = REGISTER_ADDRESS;

*p;  // read register to clear hardware

Note that you need the volatile type qualifier to prevent the access being optimised away. You will get a "statement has no effect" warning without it (unless you suppress that with a cast to void also).

0 项奖励
回复

4,270 次查看
d_
Contributor I

__attribute__ ((unused)) worked, as did (void)(c = 0);  What I tried (and thought I used before) was (void) c = 0;, which doesn't work.  Following c = 0; with (void) c; generates an error.  I didn't try the volatiles.

I'll take a look at the documentation.  Those pdf's are buried deep in the tree.

Thanks Jorge and Matthew!

Dave


0 项奖励
回复

4,270 次查看
Jorge_Gonzalez
NXP Employee
NXP Employee

Hello Dave:

The only way to bypass that specific warning is by adding an attribute to the variable declaration. Here an example:

int main(void)

{

    __attribute__ ((unused)) int counter = 0;

  

  

    for(;;)

    {

    }

  

    return 0;

}

For your reference there are some documents (kind of hidden in there actually) about GCC compiler and linker in your CodeWarrior installation folder:

C:\Freescale\CW MCU v10.6\Cross_Tools\arm-none-eabi-gcc-4_7_3\share\doc\gcc-arm-none-eabi\pdf  -> ld.pdf

C:\Freescale\CW MCU v10.6\Cross_Tools\arm-none-eabi-gcc-4_7_3\share\doc\gcc-arm-none-eabi\pdf\gcc  -> gcc.pdf

Hope this helps!


Regards!,
Jorge Gonzalez

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 项奖励
回复

4,270 次查看
d_
Contributor I

Thanks Jorge. I'll try that and look for the document.

Dave

0 项奖励
回复