<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 - -Wconversion missing warning on truncation"
href="https://bugs.llvm.org/show_bug.cgi?id=35409">35409</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>-Wconversion missing warning on truncation
</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>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Frontend
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>lebedev.ri@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>With Serus's help in IRC, the following issue was discovered:
#include <stdint.h>
#include <stdio.h>
#include <limits>
template<typename T>
T mul(T a, T b) {
printf("%u * %u", a, b);
T c = a * b;
printf(" = %u\n", c);
return c;
}
int main(int argc, char *[]) {
mul(static_cast<unsigned char>(argc), std::numeric_limits<unsigned
char>::max());
return 0;
}
GCC output:
$ g++ -std=c++14 -O2 -Wall -Wextra -Wconversion -pedantic -pthread main.cpp &&
./a.out a b
main.cpp: In instantiation of 'T mul(T, T) [with T = unsigned char]':
main.cpp:14:84: required from here
main.cpp:8:11: warning: conversion to 'unsigned char' from 'int' may alter its
value [-Wconversion]
T c = a * b;
~~^~~
3 * 255 = 253
Clang output:
clang++ -std=c++14 -O2 -Wall -Weverything -Wno-c++98-compat -pedantic -pthread
main.cpp && ./a.out a b
3 * 255 = 253
An alternative variant of the testcase, on which clang does warn:
#include <stdint.h>
#include <stdio.h>
#include <limits>
template<typename T>
T mul(T a, T b) {
printf("%u * %u", a, b);
auto c = a * b;
T d = c;
printf(" = %u\n", c);
return d;
}
int main(int argc, char *[]) {
mul(static_cast<unsigned char>(argc), std::numeric_limits<unsigned
char>::max());
return 0;
}
clang++ -std=c++14 -O2 -Wall -Weverything -Wno-c++98-compat -pedantic -pthread
main.cpp && ./a.out a b
main.cpp:9:9: warning: implicit conversion loses integer precision: 'int' to
'unsigned char' [-Wconversion]
T d = c;
~ ^
main.cpp:15:5: note: in instantiation of function template specialization
'mul<unsigned char>' requested here
mul(static_cast<unsigned char>(argc), std::numeric_limits<unsigned
char>::max());
^
1 warning generated.
3 * 255 = 765</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>