expression statements, volatiles, and C vs. C++

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

LLVM doesn't know about any of this C/C++ specific stuff. If g++ do
something (such as the load), so will llvm-g++. I'd suggest you send
this to the clang mailing list.
BTW, this issue is very interesting. I've never used C++ for embedded
systems so never realised this difference.

something (such as the load), so will llvm-g++. I'd suggest you send
this to the clang mailing list.

Ah good idea, I forgot about that list.

Thanks,

John Regehr