From 7cad4ac5b8630c070f6e9a4909a5ee455807c94d Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 5 Feb 2017 21:51:28 +0100 Subject: [PATCH] Include names of include files in the hash again This is essentially a revert of 5908e656 and 9cffdc65 (a partial fix of the problem). The idea of pull request #88 ("preprocessor mode: enable using identical header in different paths") did not work out well in practice since it broke handling of dependency files. See the new "-MMD for different ..." test cases which fail without the revert. Fixes #134. Reverts #88. --- ccache.c | 33 +++++++++++++++++++-------- test.sh | 69 +++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 66 insertions(+), 36 deletions(-) diff --git a/ccache.c b/ccache.c index b93e027ff..62f24ace7 100644 --- a/ccache.c +++ b/ccache.c @@ -1,7 +1,7 @@ // ccache -- a fast C/C++ compiler cache // // Copyright (C) 2002-2007 Andrew Tridgell -// Copyright (C) 2009-2016 Joel Rosdahl +// Copyright (C) 2009-2017 Joel Rosdahl // // This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free @@ -557,7 +557,6 @@ remember_include_file(char *path, struct mdfour *cpp_hash, bool system) } } - // Let's hash the include file. if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_MTIME) && st.st_mtime >= time_of_compilation) { cc_log("Include file %s too new", path); @@ -570,6 +569,7 @@ remember_include_file(char *path, struct mdfour *cpp_hash, bool system) goto failure; } + // Let's hash the include file content. struct mdfour fhash; hash_start(&fhash); @@ -813,8 +813,28 @@ process_preprocessed_file(struct mdfour *hash, const char *path) has_absolute_include_headers = is_absolute_path(inc_path); } inc_path = make_relative_path(inc_path); + + bool should_hash_inc_path = true; + if (!conf->hash_dir) { + char *cwd = gnu_getcwd(); + if (str_startswith(inc_path, cwd) && str_endswith(inc_path, "//")) { + // When compiling with -g or similar, GCC adds the absolute path to + // CWD like this: + // + // # 1 "CWD//" + // + // If the user has opted out of including the CWD in the hash, don't + // hash it. See also how debug_prefix_map is handled. + should_hash_inc_path = false; + } + free(cwd); + } + if (should_hash_inc_path) { + hash_string(hash, inc_path); + } + remember_include_file(inc_path, hash, system); - p = q; + p = q; // Everything of interest between p and q has been hashed now. } else if (q[0] == '.' && q[1] == 'i' && q[2] == 'n' && q[3] == 'c' && q[4] == 'b' && q[5] == 'i' && q[6] == 'n') { // An assembler .incbin statement (which could be part of inline @@ -1545,13 +1565,6 @@ calculate_common_hash(struct args *args, struct mdfour *hash) } } - // Possibly hash input file location to avoid false positive cache hits since - // the dependency file includes the source file path. - if (generating_dependencies) { - hash_delimiter(hash, "inputfile"); - hash_string(hash, input_file); - } - // Possibly hash the coverage data file path. if (generating_coverage && profile_arcs) { char *dir = dirname(output_obj); diff --git a/test.sh b/test.sh index ada869736..b4c95b1af 100755 --- a/test.sh +++ b/test.sh @@ -3,7 +3,7 @@ # A simple test suite for ccache. # # Copyright (C) 2002-2007 Andrew Tridgell -# Copyright (C) 2009-2016 Joel Rosdahl +# Copyright (C) 2009-2017 Joel Rosdahl # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software @@ -975,20 +975,6 @@ EOF expect_stat 'cache miss' 0 expect_stat 'unsupported compiler option' 1 - # ------------------------------------------------------------------------- - TEST "-MMD for different source files" - - mkdir a b - touch a/source.c b/source.c - $CCACHE_COMPILE -MMD -c a/source.c - expect_file_content source.d "source.o: a/source.c" - - $CCACHE_COMPILE -MMD -c b/source.c - expect_file_content source.d "source.o: b/source.c" - - $CCACHE_COMPILE -MMD -c a/source.c - expect_file_content source.d "source.o: a/source.c" - # ------------------------------------------------------------------------- TEST "-Wp,-P" @@ -1483,7 +1469,7 @@ SUITE_direct() { # ------------------------------------------------------------------------- TEST "CCACHE_NODIRECT" - $CCACHE_COMPILE -c test.c + CCACHE_NODIRECT=1 $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 0 expect_stat 'cache hit (preprocessed)' 0 expect_stat 'cache miss' 1 @@ -1553,6 +1539,37 @@ EOF done expect_stat 'files in cache' 12 + # ------------------------------------------------------------------------- + TEST "-MMD for different source files" + + mkdir a b + touch a/source.c b/source.c + backdate a/source.h b/source.h + $CCACHE_COMPILE -MMD -c a/source.c + expect_file_content source.d "source.o: a/source.c" + + $CCACHE_COMPILE -MMD -c b/source.c + expect_file_content source.d "source.o: b/source.c" + + $CCACHE_COMPILE -MMD -c a/source.c + expect_file_content source.d "source.o: a/source.c" + + # ------------------------------------------------------------------------- + TEST "-MMD for different include file paths" + + mkdir a b + touch a/source.h b/source.h + backdate a/source.h b/source.h + echo '#include ' >source.c + $CCACHE_COMPILE -MMD -Ia -c source.c + expect_file_content source.d "source.o: source.c a/source.h" + + $CCACHE_COMPILE -MMD -Ib -c source.c + expect_file_content source.d "source.o: source.c b/source.h" + + $CCACHE_COMPILE -MMD -Ia -c source.c + expect_file_content source.d "source.o: source.c a/source.h" + # ------------------------------------------------------------------------- TEST "-Wp,-MD" @@ -1887,8 +1904,8 @@ EOF $CCACHE_COMPILE -c `pwd`/file.c expect_stat 'cache hit (direct)' 1 - expect_stat 'cache hit (preprocessed)' 1 - expect_stat 'cache miss' 1 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 2 # ------------------------------------------------------------------------- TEST "__FILE__ in include file disables direct mode" @@ -1916,8 +1933,8 @@ EOF $CCACHE_COMPILE -c `pwd`/file2_h.c expect_stat 'cache hit (direct)' 1 - expect_stat 'cache hit (preprocessed)' 1 - expect_stat 'cache miss' 1 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 2 # ------------------------------------------------------------------------- TEST "__FILE__ in source file ignored if sloppy" @@ -2123,13 +2140,13 @@ EOF CPATH=subdir2 $CCACHE_COMPILE -c foo.c expect_stat 'cache hit (direct)' 1 - expect_stat 'cache hit (preprocessed)' 1 - expect_stat 'cache miss' 1 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 2 # subdir2 is part of the preprocessor output CPATH=subdir2 $CCACHE_COMPILE -c foo.c expect_stat 'cache hit (direct)' 2 - expect_stat 'cache hit (preprocessed)' 1 - expect_stat 'cache miss' 1 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 2 # ------------------------------------------------------------------------- TEST "Comment in strings" @@ -2278,8 +2295,8 @@ SUITE_basedir() { # CCACHE_BASEDIR="" is the default: $CCACHE_COMPILE -I`pwd`/include -c src/test.c expect_stat 'cache hit (direct)' 0 - expect_stat 'cache hit (preprocessed)' 1 - expect_stat 'cache miss' 1 + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 2 # ------------------------------------------------------------------------- TEST "Path normalization" -- 2.47.2