Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc unused-but-set-variable warning on volatile

Tags:

c

gcc

I have a little function that writes values to HW using the volatile variable

void gige_rx_prepare(void) {

    volatile uint hw_write;

    // more code here

    hw_write = 0x32;
}

The gcc version 4.7.3 (Altera 13.1 Build 162) flags this variable as set but unused even though, being a volatile, it facilitates writing of the HW registers.

I still would like to see this warning on any other variable. Is there a way to avoid this warning on volatile variables without resorting to setting gcc attributes for each volatile variable in the code?

like image 828
ilya1725 Avatar asked Feb 10 '14 21:02

ilya1725


1 Answers

Local variable is not a good representation of a h/w register and that's part of the reason why you see the warning.

Compiler complains (correctly) because hw_write is a local variable on the stack. In this case compiler does have enough data to infer that it's a pointless assignment. If it were a global variable or a pointer to a volatile uint, then there would be no warning as variable lifetime would not be limited by the scope of the function and thus it could've been used somewhere else.

Following examples compile without any warnings:

volatile int hw_write2;  // h/w register
void gige_rx_prepare2(void) {


    // more code here

    hw_write2 = 0x32;
}

void gige_rx_prepare3(void) {
    volatile int *hw_write3 = (void*)0x1234; // pointer to h/w register.


    // more code here

    *hw_write3 = 0x32;
}
like image 162
ArtemB Avatar answered Sep 22 '22 11:09

ArtemB