I really do not think a '-Wpedantic -Wconversion' warning should
be generated for the following code, but it is
(with GCC 6.4.1 and 7.3.1 on RHEL-7.5 Linux) :
$ echo '
typedef unsigned short U16_t;
static void f(void)
{ U16_t a = 1;
a-=1;
}' > t.C;
$ g++ -std=c++14 -Wall -Wextra -pedantic -Wc++11-compat \
-Wconversion -Wcast-align -Wcast-qual -Wfloat-equal \
-Wmissing-declarations -Wlogical-op -Wpacked -Wundef \
-Wuninitialized -Wvariadic-macros -Wwrite-strings \
-c t.C -o /dev/null
t.C:4:8: conversion to 'U16_t' {aka short unsigned int} from 'int' may \
alter its value.
I don't control the warning flags, as shown above,
that my code must compile against without warnings.
But I think the warning issued above is a GCC bug -
when I look at the code generated, (compile with -S -o t.S),
I see it actually does generate a 16-bit subtraction, which
is what I wanted:
.file "t.C"
.text
.globl _Z1fv
.type _Z1fv, @function
_Z1fv:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movw $1, -2(%rbp)
subw $1, -2(%rbp)
# ^^^^ this is a two-byte word subtract
nop
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size _Z1fv, .-_Z1fv
.ident "GCC: (GNU) 6.4.1 20180321"
.section .note.GNU-stack,"",@progbits
So why is it generating a warning about conversion from 'int' ?
I don't see any conversion going on in assembler output -
(cast 'a' to a temporary int, do 32-bit integer subtraction of '1'
from it, and store sign-extended low 16 bits in 'a' - this is NOT
what is going on here ) .
I'd like to remove either '-pedantic' or '-Wconversion' from the
warning flags, but this is not an option .
Please can GCC fix this warning bug eventually - I have to wade
through code that generates thousands of them per compilation.
Thanks & Best Regards,
Jason