This patch picks up the work done by Julien Ruffin in:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56549#c6

_cpp_save_file_entries() in libcpp/files.cc currently saves the size and
MD5 checksums of files without any encoding conversion or BOM removal
into the PCH's pchf_data structure.

Later, when using a PCH, the compiler compares the metrics of the files
it loads against those stored in the entries in the pchf_data structure.
If it finds a match, the file is deemed to be in the PCH and the
compiler skips processing it further. See check_file_against_entries()
for the implementation, also in files.cc.

When f->buffer_valid is not true, the metrics stored in the PCH are
calculated from a raw read of the file in question, however the buffers
with which it will be compared have been read in with read_file(). This
uses _cpp_convert_input(), which strips out UTF BOM (Byte Order Mark)
and other ignored marker information. If a header contains a BOM, the
PCH entry will be recorded as 3 Bytes longer than the file loaded by the
compiler via read_line() and the checksums will differ.

Modify the process used to load the files when f->buffer_valid is not
true so that the same processing occurs, resulting in file sizes and
checksums that match.

Signed-off-by: Martyn Welch <[email protected]>
---
 libcpp/files.cc | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/libcpp/files.cc b/libcpp/files.cc
index 917b7799be9..f18ff05996a 100644
--- a/libcpp/files.cc
+++ b/libcpp/files.cc
@@ -2724,19 +2724,16 @@ _cpp_save_file_entries (cpp_reader *pfile, FILE *fp)
                    f->st.st_size, result->entries[count].sum);
       else
        {
-         FILE *ff;
-         int oldfd = f->fd;
+         if (!read_file (pfile, f, 0))
+           return false;
 
-         if (!open_file (f))
-           {
-             open_file_failed (pfile, f, 0, 0);
-             free (result);
-             return false;
-           }
-         ff = fdopen (f->fd, "rb");
-         md5_stream (ff, result->entries[count].sum);
-         fclose (ff);
-         f->fd = oldfd;
+         md5_buffer ((const char *)f->buffer,
+                     f->st.st_size, result->entries[count].sum);
+         const void* to_free = f->buffer_start;
+         f->buffer_start = NULL;
+         f->buffer = NULL;
+         f->buffer_valid = false;
+         free ((void*) to_free);
        }
       result->entries[count].size = f->st.st_size;
     }
-- 
2.53.0

Reply via email to