On 4/17/19 8:57 AM, Joseph Myers wrote:
On Mon, 1 Apr 2019, Patrick Palka wrote:
A possibly related project is to "defer" output of diagnostics
until we know the stmt/expression we emit it for survived dead
code elimination. Here there's the question what to key the
diagnostic off and how to move it (that is, detect if the code
causing it really fully went dead).
Interesting. Which diagnostics would you have in mind to defer in this way?
For example, where the C front end does folding early for diagnostics such
as -Wsign-compare, and then makes its own limited attempt to see if e.g.
an expression of signed type must have nonnegative value. It would be
appropriate for the diagnostic to be done later (so more powerful
optimizations can be used to tell if the value in fact is in a range
meaning the diagnostic is not needed, or if it's in dead code), and that
would reduce the number of places the C front end needs to call
c_fully_fold (with a following call to c_wrap_maybe_const to avoid
repeated recursive folding of the same trees).
Besides the front-ends, diagnostics currently issued during folding
(-Wrestrict, -Wstringop-overflow, and -Wstringop-truncation) would
also benefit from the same approach. For instance, the warning below
could be avoided. (The warning has to be issued from the folder in
order to detect the problem before the strncpy call is turned into
memcpy or MEM_REF.)
Martin
$ cat a.c && gcc -O -S -Wall -Wextra -fdump-tree-gimple=/dev/stdout
-o/dev/stdout a.c
void f (char *d, unsigned i)
{
if (i & (1LU << 34))
__builtin_strncpy (d, "123", 3);
}
.file "a.c"
f (char * d, unsigned int i)
{
_1 = (long unsigned int) i;
_2 = _1 & 17179869184;
if (_2 != 0) goto <D.1910>; else goto <D.1911>;
<D.1910>:
__builtin_strncpy (d, "123", 3);
<D.1911>:
}
a.c: In function ‘f’:
a.c:4:4: warning: ‘__builtin_strncpy’ output truncated before
terminating nul copying 3 bytes from a string of the same length
[-Wstringop-truncation]
4 | __builtin_strncpy (d, "123", 3);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.text
.globl f
.type f, @function
f:
.LFB0:
.cfi_startproc
ret
.cfi_endproc
.LFE0:
.size f, .-f
.ident "GCC: (GNU) 9.0.1 20190417 (experimental)"
.section .note.GNU-stack,"",@progbits