P
P
progchip6662015-07-08 20:43:09
Programming
progchip666, 2015-07-08 20:43:09

How to get rid of the message about unused variables?

In embedder practice, quite often you have to use a variable to read the value of a register. Moreover, the read value itself is not required in the future, only the fact of reading the register during which the service bits are reset is important.
Everything would be fine, but annoying messages appear that the variable is initialized, but is not used anywhere. As in the example below, it happened with the status variable.
Question - is it possible to eliminate the generation of these warnings with the help of a cunning variable declaration? And yes, we are talking about the ANSI C language.

void I2C1_EV_IRQHandler()//приём данных в режиме SLAVE
{
  uint16_t status;


  if (I2C_GetITStatus(I2C1, I2C_IT_ADDR) == SET)
   {//проверяем адрес устройства
    status = I2C1->SR1;
    status = I2C1->SR2;
    SlaveBufPos = 0;
   }

Another important addition.
I would also like that as a result of changing the optimization level, the compiler does not "optimize lines"
status = I2C1->SR1;
    status = I2C1->SR2;

Answer the question

In order to leave comments, you need to log in

6 answer(s)
V
Vladimir Martyanov, 2015-07-08
@vilgeforce

The ban on "optimization" can be done by declaring the variable volatile

M
MiiNiPaa, 2015-07-08
@MiiNiPaa

Try assigning the variable to itself. The normal compiler will throw out this operation, and the warning will not be generated.

R
Rsa97, 2015-07-08
@Rsa97

It all depends on the specific compiler, for example, in Visual Studio, warnings can be disabled via #pragma by number.

A
Adamos, 2015-07-09
@Adamos

void check_register( uint16_t status ) { status = status; }
or
uint16_t check_register( uint16_t status ) { return status; }
and call
check_register(I2C1->SR1);
a normal compiler will not call such a function, but it will also not be able to ignore it;)

L
Larry Underwood, 2015-07-08
@Hydro

Documentation for GCC
-w compiler switch

P
progchip666, 2015-07-09
@progchip666

A graceful way out, will have to try!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question