6

For a peripheral requirement in an embedded system, I have to declare a variable to read a register, but won't use the value later. Hence, I get compiler warning about my unused variable naturally. How can I suppress the warning? I have 2 ways in mind:

  1. using compiler directives, I hesitate because they are compiler-dependent
  2. adding a dummy read from variable, like:

    volatile int var;
    
    var = peripheral_register;
    
    var = var;
    

Do you have a better idea?

diiN__________
  • 7,393
  • 6
  • 42
  • 69
Reza Amani
  • 95
  • 1
  • 7
  • 5
    Traditionally, you do `(void)variable` for this purpose. – fuz Nov 02 '16 at 02:10
  • @FUZxxl: That's actually the correct way. Other expressions can still generate a warning (it is no guarantee some rubbish compiler complains about this and doesn't on some other). – too honest for this site Nov 02 '16 at 03:29
  • 1
    Possible duplicate of [Ignoring return values in C](http://stackoverflow.com/questions/11888594/ignoring-return-values-in-c) – stdcall Nov 02 '16 at 05:11
  • 3) dont access peripherals in this way, hoping the compiler generates the instruction wanted and to produce a load having to give the compiler a reason to create the instruction(s) at all. – old_timer Nov 02 '16 at 06:27
  • Yuck, I just realized that even GCC gives crappy warnings like this for `volatile`. "variable x is set but not used". I'd consider that to be a compiler bug. – Lundin Nov 02 '16 at 09:09
  • @dwelch The compiler has to generate a read instruction for this code, or it violates the very core of the C standard: the definition of program execution. – Lundin Nov 02 '16 at 09:12
  • @Lundin What comprises a read from a volatile variable is implementation defined, so technically the compiler is in the green. – fuz Nov 02 '16 at 09:18
  • @FUZxxl https://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf – Lundin Nov 02 '16 at 09:21
  • @Lundin, that was the point, a choice is to not do it this way and not have this problem (or other problems associated with the volatile pointer address approach). – old_timer Nov 02 '16 at 13:15
  • @FUZxxl: Well, `volatile` is supposed to signal a side-effect, so it should not. But as it can be easily silenced with `(void)`, I don't see a problem. – too honest for this site Nov 02 '16 at 22:54
  • @Lundin: There is a defect report for the current version of the standard, stating most if not all compilers implement it as meant to be. – too honest for this site Nov 02 '16 at 22:55
  • @FUZxxl: If you read the defect report, it might become clear. Of course the read semantics themselves have to be implementation defined, as the standard has no concept of memory access (caches, write-trough, shared areas, etc.). But which constitutes a read (or write) is clear. Note that `volatile` does not guarantee atomic accesses anyway; use `_Atomic()` for those. – too honest for this site Nov 03 '16 at 00:12
  • @Olaf Which one do you mean? – fuz Nov 03 '16 at 00:38
  • Last time I checked there was only one about `volatile` and its implementations. Sorry, I always forget the number. – too honest for this site Nov 03 '16 at 00:41
  • @FUZxxl: see above, forgot to address you – too honest for this site Nov 03 '16 at 02:03

3 Answers3

8

If all you need to do is read the register (to clear some status flag for example), then you do not need a receiving variable at all just:

(void)peripheral_register ;

is sufficient assuming the register is itself is declared volatile so that it must be read.

Otherwise you could make your dummy var global with external linkage - that way the compiler cannot determine that it is not read elsewhere; but that's a far uglier solution.

Clifford
  • 88,407
  • 13
  • 85
  • 165
3

Define a macro as

#define unused(x) ((void)x)

if peripheral_register is the unused variable the just invoke the macro as unused(peripheral_register). This will remove the warning.

fuz
  • 88,405
  • 25
  • 200
  • 352
Darshan b
  • 157
  • 7
2

Your variable is used. Reading a volatile variable is a side-effect.

If you get a compiler warning, I would strongly suspect that your compiler does not conform to the C standard. You should disassemble to code to ensure that the read actually takes place. If not, your compiler is broken beyond repair.

That being said, you can cast any expression to (void) to silence compiler warnings, for example (void)var;. But you shouldn't need to do that in this case.

Lundin
  • 195,001
  • 40
  • 254
  • 396