[llvm-bugs] [Bug 35409] New: -Wconversion missing warning on truncation
via llvm-bugs
llvm-bugs at lists.llvm.org
Fri Nov 24 01:38:44 PST 2017
https://bugs.llvm.org/show_bug.cgi?id=35409
Bug ID: 35409
Summary: -Wconversion missing warning on truncation
Product: clang
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: Frontend
Assignee: unassignedclangbugs at nondot.org
Reporter: lebedev.ri at gmail.com
CC: llvm-bugs at lists.llvm.org
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
--
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/20171124/c0e78a5b/attachment.html>
More information about the llvm-bugs
mailing list