<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - void cast: expression not evaluated for its side effects"
href="https://bugs.llvm.org/show_bug.cgi?id=34842">34842</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>void cast: expression not evaluated for its side effects
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>f.hollerer@gmx.net
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>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.
<span class="quote">> 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.</span >
This also complies to ISO/IEC 9899:1999 C standard:
<span class="quote">> 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.)</span >
Unfortunately, clang does not behave this way. It issues a warning and
optimizes away the expression.
<a href="https://godbolt.org/g/Ts3L3P">https://godbolt.org/g/Ts3L3P</a>
After some discussion on
<a href="https://github.com/isocpp/CppCoreGuidelines/issues/1048">https://github.com/isocpp/CppCoreGuidelines/issues/1048</a> I think this is an
abnormality of clang which should be fixed.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>