[cfe-dev] expression statements, volatiles, and C vs. C++

John Regehr regehr at cs.utah.edu
Sat Mar 6 09:37:32 PST 2010


[This is a re-send of a message I sent to the main LLVM list last night. 
  Resending since it's really a front-end issue.]

The question is, what should C and C++ compilers do with this code?

   volatile int x;

   void foo (void) {
     x;
   }

This question is not totally stupid: embedded systems use code like this 
when reading a hardware register has a useful side effect (usually 
clearing the register).

It is reasonably clear that a C compiler should load from x and throw 
away the value.  clang and llvm-gcc do this, as do most other good C 
compilers.

However, clang++ and llvm-g++ also load from x and this does not appear 
to be supported by the 1998 C++ standard.  In 6.2, it is explicitly 
stated that for an expression statement, no conversion from lvalue to 
rvalue occurs.  If there's no rvalue, there should not be a load from x.

Anyway, I'm curious: is the C-like interpretation of a volatile 
expression statement considered to be a feature by the LLVM maintainers? 
  If so, what is the rationale?

I haven't done extensive testing, but there do exist compiler families 
(such as those from IAR and Intel) where the C compiler loads from x and 
the C++ compiler does not.

[I just sent a very message very much like this one to the GCC mailing 
list.]

Thanks,

John Regehr



More information about the cfe-dev mailing list