c-family: Fix hang with -Wsequence-point [PR98126]
authorMarek Polacek <polacek@redhat.com>
Thu, 3 Dec 2020 23:35:00 +0000 (18:35 -0500)
committerMarek Polacek <polacek@redhat.com>
Mon, 7 Dec 2020 16:54:30 +0000 (11:54 -0500)
commit1cac89da2cb1f2a7c2d93f7f325484c2d1619ca8
treed83a2760dce18de383aec51214737555dd9ad222
parentffb268ffcf9f21e2981a71887324eb0aec245eea
c-family: Fix hang with -Wsequence-point [PR98126]

verify_sequence_points uses verify_tree to recursively walk the
subexpressions of an expression, and while recursing, it also
keeps lists of expressions found after/before a sequence point.
For a large expression, the list can grow significantly.  And
merge_tlist is at least N(n^2): for a list of length n it will
iterate n(n -1) times, and call candidate_equal_p each time, and
that can recurse further.  warn_for_collision also has to go
through the whole list.  With a large-enough expression, the
compilation can easily get stuck here for 24 hours.

This patch is a simple kludge: if we see that the expression is
overly complex, don't even try.

gcc/c-family/ChangeLog:

PR c++/98126
* c-common.c (verify_tree_lim_r): New function.
(verify_sequence_points): Use it.  Use nullptr instead of 0.

gcc/testsuite/ChangeLog:

PR c++/98126
* g++.dg/warn/Wsequence-point-4.C: New test.
gcc/c-family/c-common.c
gcc/testsuite/g++.dg/warn/Wsequence-point-4.C [new file with mode: 0644]