]> git.ipfire.org Git - thirdparty/gcc.git/commit
preprocessor: Fix tracking of system header state [PR60014,PR60723]
authorLewis Hyatt <lhyatt@gmail.com>
Thu, 6 Oct 2022 22:05:02 +0000 (18:05 -0400)
committerLewis Hyatt <lhyatt@gmail.com>
Wed, 12 Oct 2022 22:08:31 +0000 (18:08 -0400)
commitddb7f0a0cac48762ba6408d69538f8115c4a2739
tree0200cd9ddfa77a34ff96a816cd14f0bd74f2f9f0
parentf77281b25ca6bb34ba271fa826e1e79a15df95fe
preprocessor: Fix tracking of system header state [PR60014,PR60723]

The token_streamer class (which implements gcc mode -E and
-save-temps/-no-integrated-cpp) needs to keep track whether the last tokens
output were in a system header, so that it can generate line marker
annotations as necessary for a downstream consumer to reconstruct the
state. The logic for tracking it, which was added by r5-1863 to resolve
PR60723, has some edge case issues as revealed by the three new test
cases. The first, coming from the original PR60014, was incidentally fixed by
r9-1926 for unrelated reasons. The other two were still failing on master
prior to this commit. Such code paths were not realizable prior to r13-1544,
which made it possible for the token streamer to see CPP_PRAGMA tokens in more
contexts.

The two main issues being corrected here are:

1) print.prev_was_system_token needs to indicate whether the previous token
output was in a system location. However, it was not being set on every token,
only on those that triggered the main code path; specifically it was not
triggered on a CPP_PRAGMA token. Testcase 2 covers this case.

2) The token_streamer uses a variable "line_marker_emitted" to remember
whether a line marker has been emitted while processing a given token, so that
it wouldn't be done more than once in case multiple conditions requiring a
line marker are true. There was no reason for this to be a member variable
that retains its value from token to token, since it is just needed for
tracking the state locally while processing a single given token. The fact
that it could retain its value for a subsequent token is rather difficult to
observe, but testcase 3 demonstrates incorrect behavior resulting from
that. Moving this to a local variable also simplifies understanding the
control flow going forward.

gcc/c-family/ChangeLog:

PR preprocessor/60014
PR preprocessor/60723
* c-ppoutput.cc (class token_streamer): Remove member
line_marker_emitted to...
(token_streamer::stream): ...a local variable here. Set
print.prev_was_system_token on all code paths.

gcc/testsuite/ChangeLog:

PR preprocessor/60014
PR preprocessor/60723
* gcc.dg/cpp/pr60014-1.c: New test.
* gcc.dg/cpp/pr60014-1.h: New test.
* gcc.dg/cpp/pr60014-2.c: New test.
* gcc.dg/cpp/pr60014-2.h: New test.
* gcc.dg/cpp/pr60014-3.c: New test.
* gcc.dg/cpp/pr60014-3.h: New test.
gcc/c-family/c-ppoutput.cc
gcc/testsuite/gcc.dg/cpp/pr60014-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/pr60014-1.h [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/pr60014-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/pr60014-2.h [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/pr60014-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/cpp/pr60014-3.h [new file with mode: 0644]