Skip to content

Commit

Permalink
Include names of include files in the hash again
Browse files Browse the repository at this point in the history
This is essentially a revert of 5908e65 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.
  • Loading branch information
jrosdahl committed Feb 5, 2017
1 parent 984f4e1 commit 7cad4ac
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 36 deletions.
33 changes: 23 additions & 10 deletions 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
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
69 changes: 43 additions & 26 deletions test.sh
Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.h>' >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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 7cad4ac

Please sign in to comment.