<html>
<head>
<base href="http://llvm.org/bugs/" />
</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 --- - constexpr evaluation can generate spurious warnings (only) when other errors are reported"
href="http://llvm.org/bugs/show_bug.cgi?id=21287">21287</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>constexpr evaluation can generate spurious warnings (only) when other errors are reported
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</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++11
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>oneill+llvmbugs@cs.hmc.edu
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>The following code produces a compiler warning, but *only* when it is already
complaining about some other error:
#include <cstdio>
template <int x, int y>
int spurious()
{
constexpr int static_result = (x >= y) ? (1000 / (x/y)) : 0;
return static_result;
}
int main()
{
printf("%d %d\n", spurious<1,2>(), spurious<2,1>());
// If you comment out the line below, the code compiles without warnings
#error This error will trigger a spurious warning about division by zero
return 0;
}
With Clang 3.5 and r219141 from trunk, it produces the following error:
spurious.cpp:15:6: error: This error will trigger a spurious warning about
division by zero
#error This error will trigger a spurious warning about division by zero
^
spurious.cpp:6:52: warning: division by zero is undefined [-Wdivision-by-zero]
constexpr int static_result = (x >= y) ? (1000 / (x/y)) : 0;
^ ~~~~~
spurious.cpp:12:23: note: in instantiation of function template specialization
'spurious<1, 2>' requested here
printf("%d %d\n", spurious<1,2>(), spurious<2,1>());
^
1 warning and 1 error generated.
The error is wrong because the division-by-zero arm of the ternary operator
should not be evaluated (it's the true arm and the condition is false -- the
issue also arises if we flip the condition swap the arms around).
When no other error is generated (e.g., comment out #error in the code), it
correctly generates the code, here's the llvm bitcode:
define i32 @main() #0 {
entry:
%call2 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([7 x
i8]* @.str, i64 0, i64 0), i32 0, i32 500)
ret i32 0
}
As you can see, the code ran properly and no division by zero occurred.</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>