]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Refactoring: move hash_*_ignoring_comments to hash_include_file_* in testutil
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 25 Apr 2010 13:44:18 +0000 (15:44 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 25 Apr 2010 13:44:18 +0000 (15:44 +0200)
Makefile.in
ccache.c
comments.c [deleted file]
comments.h [deleted file]
hashutil.c
hashutil.h
manifest.c

index 73cb731b4d4c3b0cb46900480082d534530c4717..2a38b9db4bc715afbaef26feef715a12bba6e27a 100644 (file)
@@ -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)
 
index c8b379d77247b280a76ade2ec73429653bbd39ef..2d4391a3599fb6ffe8200b44cad47ac8995142e0 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -25,7 +25,6 @@
 #include "hashtable_itr.h"
 #include "hashutil.h"
 #include "manifest.h"
-#include "comments.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -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 (file)
index 0d82f13..0000000
+++ /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 <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#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 (file)
index bbc3f06..0000000
+++ /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
index 3e4921647dfb066f4cdc51feb8c53de471c49b12..d8f51eedcebf6ce0c2586828f5c8866061ecf366 100644 (file)
@@ -1,8 +1,29 @@
-#include <string.h>
+/*
+ * 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 <string.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+
 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;
+}
index e562519ed25c67ce050d23f61f0dd84e4047ce2d..1696f79c0895358ffdd9b6f975b131f0a62cbaa6 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef HASHUTIL_H
 #define HASHUTIL_H
 
+#include <mdfour.h>
 #include <inttypes.h>
 
 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
index 771f46164973d90bc98773f408463bf88df34bb4..9339d47516332c7c93dc65294d4ffbd26e2922f2 100644 (file)
@@ -21,7 +21,6 @@
 #include "hashutil.h"
 #include "manifest.h"
 #include "murmurhashneutral2.h"
-#include "comments.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -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);