When processing CRLF sequences, _cpp_clean_line correctly incremented
the source pointer 's' to skip over the CR character, but forgot to
update the destination pointer 'd' to the same location. This caused
both the CR and LF characters to be written to the output buffer,
outputting LFLF instead of the intended LF.

libcpp/ChangeLog:

        PR preprocessor/88424
        * lex.cc (_cpp_clean_line): Update destination pointer when
        skipping CR in CRLF sequence to prevent double line feeds.

gcc/testsuite/ChangeLog:

        PR preprocessor/88424
        * gcc.dg/cpp/pr88424.c: New test to verify CRLF handling
        in comments with -C option.
        * .gitattributes: Make sure testcase is always checked out with CRLF
        * line endings.

Signed-off-by: Peter Damianov <[email protected]>
---
v3: Minor change to testcase to remove redundant scan-file

 gcc/testsuite/.gitattributes       |  1 +
 gcc/testsuite/gcc.dg/cpp/pr88424.c | 15 +++++++++++++++
 libcpp/lex.cc                      |  1 +
 3 files changed, 17 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/cpp/pr88424.c

diff --git a/gcc/testsuite/.gitattributes b/gcc/testsuite/.gitattributes
index 562b12e16eb..6a363d2b998 100644
--- a/gcc/testsuite/.gitattributes
+++ b/gcc/testsuite/.gitattributes
@@ -1 +1,2 @@
 * -whitespace
+gcc.dg/cpp/pr88424.c text eol=crlf
diff --git a/gcc/testsuite/gcc.dg/cpp/pr88424.c 
b/gcc/testsuite/gcc.dg/cpp/pr88424.c
new file mode 100644
index 00000000000..9bd86213998
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/cpp/pr88424.c
@@ -0,0 +1,15 @@
+/* { dg-do preprocess } */
+/* { dg-options "-C" } */
+
+/* Test that CRLF line endings in comments are preserved correctly with -C.
+   Previously, the preprocessor would convert CRLF sequences in comments
+   to LFLF, causing extra blank lines in the output. 
+   
+   This test ensures that comments with CRLF line endings maintain their
+   original number of newlines when processed with the -C option.  */
+
+/* This comment has CRLF line endings
+ and should appear as two lines in the output, not three.  */
+
+/* Check that the comment does appear correctly with single line feed. */
+/* { dg-final { scan-file pr88424.i "comment has CRLF line endings\n and 
should appear" } } */
diff --git a/libcpp/lex.cc b/libcpp/lex.cc
index 2ba9d588708..d8938fbadc5 100644
--- a/libcpp/lex.cc
+++ b/libcpp/lex.cc
@@ -944,6 +944,7 @@ _cpp_clean_line (cpp_reader *pfile)
       if (__builtin_expect (c == '\r', false) && s[1] == '\n')
        {
          s++;
+         d = (uchar *) s;  /* Move d to point to the LF, not the CR */
          if (s == buffer->rlimit)
            goto done;
        }
-- 
2.47.3

Reply via email to