From b2dd02bbdc703f96ed96bb37cda2047e217273e2 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 25 Apr 2010 15:44:18 +0200 Subject: [PATCH] Refactoring: move hash_*_ignoring_comments to hash_include_file_* in testutil --- Makefile.in | 4 +- ccache.c | 5 +- comments.c | 145 ---------------------------------------------------- comments.h | 10 ---- hashutil.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++- hashutil.h | 5 ++ manifest.c | 4 +- 7 files changed, 151 insertions(+), 164 deletions(-) delete mode 100644 comments.c delete mode 100644 comments.h diff --git a/Makefile.in b/Makefile.in index 73cb731b4..2a38b9db4 100644 --- a/Makefile.in +++ b/Makefile.in @@ -23,12 +23,12 @@ libs = @LIBS@ -lm sources = \ ccache.c mdfour.c hash.c execute.c util.c args.c stats.c \ cleanup.c snprintf.c unify.c manifest.c hashtable.c hashtable_itr.c \ - murmurhashneutral2.c hashutil.c comments.c getopt_long.c + murmurhashneutral2.c hashutil.c getopt_long.c all_sources = $(sources) @extra_sources@ headers = \ ccache.h hashtable.h hashtable_itr.h hashtable_private.h hashutil.h \ - manifest.h mdfour.h murmurhashneutral2.h comments.h getopt_long.h + manifest.h mdfour.h murmurhashneutral2.h getopt_long.h objs = $(all_sources:.c=.o) diff --git a/ccache.c b/ccache.c index c8b379d77..2d4391a35 100644 --- a/ccache.c +++ b/ccache.c @@ -25,7 +25,6 @@ #include "hashtable_itr.h" #include "hashutil.h" #include "manifest.h" -#include "comments.h" #include #include @@ -343,7 +342,7 @@ static void remember_include_file(char *path, size_t path_len) } hash_start(&fhash); - hash_string_ignoring_comments(&fhash, data, st.st_size); + hash_include_file_string(&fhash, data, st.st_size); h = x_malloc(sizeof(*h)); hash_result_as_bytes(&fhash, h->hash); @@ -861,7 +860,7 @@ static int find_hash(ARGS *args, enum findhash_call_mode mode) switch (mode) { case FINDHASH_DIRECT_MODE: - if (!hash_file_ignoring_comments(&hash, input_file)) { + if (!hash_include_file(&hash, input_file)) { cc_log("Failed to hash %s", input_file); failed(); } diff --git a/comments.c b/comments.c deleted file mode 100644 index 0d82f13d1..000000000 --- a/comments.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (C) 2009 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 Foundation; either version 3 of the License, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 51 - * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "ccache.h" -#include "comments.h" - -#include -#include -#include -#include -#include - -#define HASH(ch) \ - do { \ - hashbuf[hashbuflen] = ch; \ - hashbuflen++; \ - if (hashbuflen == sizeof(hashbuf)) { \ - hash_buffer(hash, hashbuf, sizeof(hashbuf)); \ - hashbuflen = 0; \ - } \ - } while (0) - -void hash_string_ignoring_comments( - struct mdfour *hash, const char *str, size_t len) -{ - const char *p; - const char *end; - char hashbuf[64]; - size_t hashbuflen = 0; - - p = str; - end = str + len; - while (1) { - if (p >= end) { - goto end; - } - switch (*p) { - case '/': - if (p+1 == end) { - break; - } - switch (*(p+1)) { - case '*': - HASH(' '); /* Don't paste tokens together when - * removing the comment. */ - p += 2; - while (p+1 < end - && (*p != '*' || *(p+1) != '/')) { - if (*p == '\n') { - /* Keep line numbers. */ - HASH('\n'); - } - p++; - } - if (p+1 == end) { - goto end; - } - p += 2; - continue; - - case '/': - p += 2; - while (p < end - && (*p != '\n' || *(p-1) == '\\')) { - p++; - } - continue; - - default: - break; - } - break; - - case '"': - HASH(*p); - p++; - while (p < end && (*p != '"' || *(p-1) == '\\')) { - HASH(*p); - p++; - } - if (p == end) { - goto end; - } - break; - - default: - break; - } - - HASH(*p); - p++; - } - -end: - hash_buffer(hash, hashbuf, hashbuflen); -} - -/* - * Add contents of a file to a hash, but don't hash comments. Returns 1 on - * success, otherwise 0. - */ -int hash_file_ignoring_comments(struct mdfour *hash, const char *path) -{ - int fd; - struct stat st; - char *data; - - fd = open(path, O_RDONLY); - if (fd == -1) { - return 0; - } - if (fstat(fd, &st) == -1) { - close(fd); - return 0; - } - if (st.st_size == 0) { - close(fd); - return 1; - } - data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - close(fd); - if (data == (void *)-1) { - return 0; - } - - hash_string_ignoring_comments(hash, data, st.st_size); - - munmap(data, st.st_size); - return 1; -} diff --git a/comments.h b/comments.h deleted file mode 100644 index bbc3f06bb..000000000 --- a/comments.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef COMMENTS_H -#define COMMENTS_H - -#include "mdfour.h" - -void hash_string_ignoring_comments( - struct mdfour *hash, const char *str, size_t len); -int hash_file_ignoring_comments(struct mdfour *hash, const char *path); - -#endif diff --git a/hashutil.c b/hashutil.c index 3e4921647..d8f51eedc 100644 --- a/hashutil.c +++ b/hashutil.c @@ -1,8 +1,29 @@ -#include +/* + * Copyright (C) 2009-2010 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 Foundation; either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "ccache.h" #include "hashutil.h" #include "murmurhashneutral2.h" +#include +#include +#include + unsigned int hash_from_string(void *str) { return murmurhashneutral2(str, strlen((const char *)str), 0); @@ -18,3 +39,122 @@ int file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2) return memcmp(fh1->hash, fh2->hash, 16) == 0 && fh1->size == fh2->size; } + +#define HASH(ch) \ + do { \ + hashbuf[hashbuflen] = ch; \ + hashbuflen++; \ + if (hashbuflen == sizeof(hashbuf)) { \ + hash_buffer(hash, hashbuf, sizeof(hashbuf)); \ + hashbuflen = 0; \ + } \ + } while (0) + +void hash_include_file_string( + struct mdfour *hash, const char *str, size_t len) +{ + const char *p; + const char *end; + char hashbuf[64]; + size_t hashbuflen = 0; + + p = str; + end = str + len; + while (1) { + if (p >= end) { + goto end; + } + switch (*p) { + case '/': + if (p+1 == end) { + break; + } + switch (*(p+1)) { + case '*': + HASH(' '); /* Don't paste tokens together when + * removing the comment. */ + p += 2; + while (p+1 < end + && (*p != '*' || *(p+1) != '/')) { + if (*p == '\n') { + /* Keep line numbers. */ + HASH('\n'); + } + p++; + } + if (p+1 == end) { + goto end; + } + p += 2; + continue; + + case '/': + p += 2; + while (p < end + && (*p != '\n' || *(p-1) == '\\')) { + p++; + } + continue; + + default: + break; + } + break; + + case '"': + HASH(*p); + p++; + while (p < end && (*p != '"' || *(p-1) == '\\')) { + HASH(*p); + p++; + } + if (p == end) { + goto end; + } + break; + + default: + break; + } + + HASH(*p); + p++; + } + +end: + hash_buffer(hash, hashbuf, hashbuflen); +} + +/* + * Add contents of a file to a hash, but don't hash comments. Returns 1 on + * success, otherwise 0. + */ +int hash_include_file(struct mdfour *hash, const char *path) +{ + int fd; + struct stat st; + char *data; + + fd = open(path, O_RDONLY); + if (fd == -1) { + return 0; + } + if (fstat(fd, &st) == -1) { + close(fd); + return 0; + } + if (st.st_size == 0) { + close(fd); + return 1; + } + data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + close(fd); + if (data == (void *)-1) { + return 0; + } + + hash_include_file_string(hash, data, st.st_size); + + munmap(data, st.st_size); + return 1; +} diff --git a/hashutil.h b/hashutil.h index e562519ed..1696f79c0 100644 --- a/hashutil.h +++ b/hashutil.h @@ -1,6 +1,7 @@ #ifndef HASHUTIL_H #define HASHUTIL_H +#include #include struct file_hash @@ -13,4 +14,8 @@ unsigned int hash_from_string(void *str); int strings_equal(void *str1, void *str2); int file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2); +void hash_include_file_string( + struct mdfour *hash, const char *str, size_t len); +int hash_include_file(struct mdfour *hash, const char *path); + #endif diff --git a/manifest.c b/manifest.c index 771f46164..9339d4751 100644 --- a/manifest.c +++ b/manifest.c @@ -21,7 +21,6 @@ #include "hashutil.h" #include "manifest.h" #include "murmurhashneutral2.h" -#include "comments.h" #include #include @@ -371,8 +370,7 @@ static int verify_object(struct manifest *mf, struct object *obj, if (!actual) { actual = x_malloc(sizeof(*actual)); hash_start(&hash); - if (!hash_file_ignoring_comments( - &hash, mf->files[fi->index])) { + if (!hash_include_file(&hash, mf->files[fi->index])) { cc_log("Failed hashing %s", mf->files[fi->index]); free(actual); -- 2.47.3