Hash a delimiter string between parts to separate them

Previously, "gcc -I-O2 -c file.c" and "gcc -I -O2 -c file.c" would hash to the
same sum.
This commit is contained in:
Joel Rosdahl 2010-04-27 18:19:51 +02:00
parent 45c981c012
commit 025e2a58a9
4 changed files with 21 additions and 3 deletions

View File

@ -215,7 +215,7 @@ enum findhash_call_mode {
* this string. A typical example would be if the format of one of the files
* stored in the cache changes in a backwards-incompatible way.
*/
static const char HASH_PREFIX[] = "ccache3";
static const char HASH_PREFIX[] = "3";
/*
something went badly wrong - just execute the real compiler
@ -730,6 +730,7 @@ get_object_name_from_cpp(ARGS *args, struct mdfour *hash)
if (!hash_file(hash, path_stderr)) {
fatal("Failed to open %s", path_stderr);
}
hash_delimiter(hash);
i_tmpfile = path_stdout;
@ -775,16 +776,19 @@ static int find_hash(ARGS *args, enum findhash_call_mode mode)
hash_start(&hash);
hash_buffer(&hash, HASH_PREFIX, sizeof(HASH_PREFIX));
hash_delimiter(&hash);
/* when we are doing the unifying tricks we need to include
the input file name in the hash to get the warnings right */
if (enable_unify) {
hash_string(&hash, input_file);
}
hash_delimiter(&hash);
/* we have to hash the extension, as a .i file isn't treated the same
by the compiler as a .ii file */
hash_string(&hash, i_extension);
hash_delimiter(&hash);
/* first the arguments */
for (i=1;i<args->argc;i++) {
@ -833,11 +837,13 @@ static int find_hash(ARGS *args, enum findhash_call_mode mode)
if (!hash_file(&hash, args->argv[i]+8)) {
failed();
}
hash_delimiter(&hash);
continue;
}
/* All other arguments are included in the hash. */
hash_string(&hash, args->argv[i]);
hash_delimiter(&hash);
}
/* The compiler driver size and date. This is a simple minded way
@ -853,6 +859,7 @@ static int find_hash(ARGS *args, enum findhash_call_mode mode)
if (st.st_nlink > 1) {
hash_string(&hash, basename(args->argv[0]));
}
hash_delimiter(&hash);
compilercheck = getenv("CCACHE_COMPILERCHECK");
if (!compilercheck) {
@ -866,6 +873,7 @@ static int find_hash(ARGS *args, enum findhash_call_mode mode)
hash_int(&hash, st.st_size);
hash_int(&hash, st.st_mtime);
}
hash_delimiter(&hash);
/* possibly hash the current working directory */
if (getenv("CCACHE_HASHDIR")) {
@ -875,6 +883,7 @@ static int find_hash(ARGS *args, enum findhash_call_mode mode)
free(cwd);
}
}
hash_delimiter(&hash);
switch (mode) {
case FINDHASH_DIRECT_MODE:

View File

@ -54,6 +54,7 @@ enum stats {
};
void hash_start(struct mdfour *md);
void hash_delimiter(struct mdfour *md);
void hash_string(struct mdfour *md, const char *s);
void hash_int(struct mdfour *md, int x);
int hash_fd(struct mdfour *md, int fd);

8
hash.c
View File

@ -26,6 +26,8 @@
#include <string.h>
#include <unistd.h>
#define HASH_DELIMITER "\000cCaChE\000"
void hash_buffer(struct mdfour *md, const void *s, size_t len)
{
mdfour_update(md, (unsigned char *)s, len);
@ -36,6 +38,12 @@ void hash_start(struct mdfour *md)
mdfour_begin(md);
}
void hash_delimiter(struct mdfour *md)
{
/* Hash some string that is unlikely to occur in the input. */
hash_buffer(md, HASH_DELIMITER, sizeof(HASH_DELIMITER));
}
void hash_string(struct mdfour *md, const char *s)
{
hash_buffer(md, s, strlen(s));

View File

@ -891,7 +891,7 @@ EOF
cd ..
cd dir2
CCACHE_BASEDIR="$PWD" $CCACHE $COMPILER -I $PWD/include -c src/test.c
CCACHE_BASEDIR="$PWD" $CCACHE $COMPILER -I$PWD/include -c src/test.c
checkstat 'cache hit (direct)' 0
checkstat 'cache hit (preprocessed)' 1
checkstat 'cache miss' 1
@ -913,7 +913,7 @@ EOF
cd ..
cd dir2
CCACHE_BASEDIR="$PWD" $CCACHE $COMPILER -I $PWD/include -c src/test.c
CCACHE_BASEDIR="$PWD" $CCACHE $COMPILER -I$PWD/include -c src/test.c
checkstat 'cache hit (direct)' 1
checkstat 'cache hit (preprocessed)' 0
checkstat 'cache miss' 1