[llvm-bugs] [Bug 34842] New: void cast: expression not evaluated for its side effects

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Oct 5 00:02:34 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=34842

            Bug ID: 34842
           Summary: void cast: expression not evaluated for its side
                    effects
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: f.hollerer at gmx.net
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org

I usually use a (void) cast to express that a expression should be evaluated
for its side effect, but that the result is discarded.

Example:

extern volatile uint32_t* uart_rx_reg;

void foo()
{
    // do something here
    :
    // uart_rx_reg is accessed for its side effect to empty the receive
register
    (void) *uart_rx_reg;    // read and discard received character
}

This complies to MISRA C 2012 Rule 2.2, where you can find a similar example.

> A cast to void is assumed to indicate a value that is intentionally not
> being used. The cast is therefore not dead code itself. It is treated
> as using its operand which is therefore also not dead code.

This also complies to ISO/IEC 9899:1999 C standard:

> 6.3.2.2 void
> The (nonexistent) value of a void expression (an expression that has
> type void) shall not be used in any way, and implicit or explicit
> conversions (except to void) shall not be applied to such an expression.
> If an expression of any other type is evaluated as a void expression,
> its value or designator is discarded. (A void expression is evaluated for
> its side effects.)

Unfortunately, clang does not behave this way. It issues a warning and
optimizes away the expression.

https://godbolt.org/g/Ts3L3P

After some discussion on
https://github.com/isocpp/CppCoreGuidelines/issues/1048 I think this is an
abnormality of clang which should be fixed.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20171005/45d8b4f6/attachment.html>


More information about the llvm-bugs mailing list