Imported Upstream version 3.2.6
[platform/upstream/ccache.git] / unify.c
1 /*
2  * Copyright (C) 2002 Andrew Tridgell
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 3 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /*
20  * C/C++ unifier
21  *
22  * The idea is that changes that don't affect the resulting C code should not
23  * change the hash. This is achieved by folding white-space and other
24  * non-semantic fluff in the input into a single unified format.
25  *
26  * This unifier was design to match the output of the unifier in compilercache,
27  * which is flex based. The major difference is that this unifier is much
28  * faster (about 2x) and more forgiving of syntactic errors. Continuing on
29  * syntactic errors is important to cope with C/C++ extensions in the local
30  * compiler (for example, inline assembly systems).
31  */
32
33 #include "ccache.h"
34
35 static const char *const s_tokens[] = {
36         "...", ">>=", "<<=", "+=", "-=", "*=", "/=", "%=", "&=", "^=",
37         "|=",  ">>",  "<<",  "++", "--", "->", "&&", "||", "<=", ">=",
38         "==",  "!=",  ";",   "{",  "<%", "}",  "%>", ",",  ":",  "=",
39         "(",   ")",   "[",   "<:", "]",  ":>", ".",  "&",  "!",  "~",
40         "-",   "+",   "*",   "/",  "%",  "<",  ">",  "^",  "|",  "?",
41         0
42 };
43
44 #define C_ALPHA 1
45 #define C_SPACE 2
46 #define C_TOKEN 4
47 #define C_QUOTE 8
48 #define C_DIGIT 16
49 #define C_HEX   32
50 #define C_FLOAT 64
51 #define C_SIGN  128
52
53 static struct {
54         unsigned char type;
55         unsigned char num_toks;
56         const char *toks[7];
57 } tokens[256];
58
59 /* build up the table used by the unifier */
60 static void
61 build_table(void)
62 {
63         unsigned char c;
64         int i;
65         static bool done;
66
67         if (done) {
68                 return;
69         }
70         done = true;
71
72         memset(tokens, 0, sizeof(tokens));
73         for (c = 0; c < 128; c++) {
74                 if (isalpha(c) || c == '_') {
75                         tokens[c].type |= C_ALPHA;
76                 }
77                 if (isdigit(c)) {
78                         tokens[c].type |= C_DIGIT;
79                 }
80                 if (isspace(c)) {
81                         tokens[c].type |= C_SPACE;
82                 }
83                 if (isxdigit(c)) {
84                         tokens[c].type |= C_HEX;
85                 }
86         }
87         tokens['\''].type |= C_QUOTE;
88         tokens['"'].type |= C_QUOTE;
89         tokens['l'].type |= C_FLOAT;
90         tokens['L'].type |= C_FLOAT;
91         tokens['f'].type |= C_FLOAT;
92         tokens['F'].type |= C_FLOAT;
93         tokens['U'].type |= C_FLOAT;
94         tokens['u'].type |= C_FLOAT;
95
96         tokens['-'].type |= C_SIGN;
97         tokens['+'].type |= C_SIGN;
98
99         for (i = 0; s_tokens[i]; i++) {
100                 c = s_tokens[i][0];
101                 tokens[c].type |= C_TOKEN;
102                 tokens[c].toks[tokens[c].num_toks] = s_tokens[i];
103                 tokens[c].num_toks++;
104         }
105 }
106
107 /* buffer up characters before hashing them */
108 static void
109 pushchar(struct mdfour *hash, unsigned char c)
110 {
111         static unsigned char buf[64];
112         static size_t len;
113
114         if (c == 0) {
115                 if (len > 0) {
116                         hash_buffer(hash, (char *)buf, len);
117                         len = 0;
118                 }
119                 hash_buffer(hash, NULL, 0);
120                 return;
121         }
122
123         buf[len++] = c;
124         if (len == 64) {
125                 hash_buffer(hash, (char *)buf, len);
126                 len = 0;
127         }
128 }
129
130 /* hash some C/C++ code after unifying */
131 static void
132 unify(struct mdfour *hash, unsigned char *p, size_t size)
133 {
134         size_t ofs;
135         unsigned char q;
136         int i;
137
138         build_table();
139
140         for (ofs = 0; ofs < size; ) {
141                 if (p[ofs] == '#') {
142                         if ((size-ofs) > 2 && p[ofs+1] == ' ' && isdigit(p[ofs+2])) {
143                                 do {
144                                         ofs++;
145                                 } while (ofs < size && p[ofs] != '\n');
146                                 ofs++;
147                         } else {
148                                 do {
149                                         pushchar(hash, p[ofs]);
150                                         ofs++;
151                                 } while (ofs < size && p[ofs] != '\n');
152                                 pushchar(hash, '\n');
153                                 ofs++;
154                         }
155                         continue;
156                 }
157
158                 if (tokens[p[ofs]].type & C_ALPHA) {
159                         do {
160                                 pushchar(hash, p[ofs]);
161                                 ofs++;
162                         } while (ofs < size && (tokens[p[ofs]].type & (C_ALPHA|C_DIGIT)));
163                         pushchar(hash, '\n');
164                         continue;
165                 }
166
167                 if (tokens[p[ofs]].type & C_DIGIT) {
168                         do {
169                                 pushchar(hash, p[ofs]);
170                                 ofs++;
171                         } while (ofs < size &&
172                                  ((tokens[p[ofs]].type & C_DIGIT) || p[ofs] == '.'));
173                         if (ofs < size && (p[ofs] == 'x' || p[ofs] == 'X')) {
174                                 do {
175                                         pushchar(hash, p[ofs]);
176                                         ofs++;
177                                 } while (ofs < size && (tokens[p[ofs]].type & C_HEX));
178                         }
179                         if (ofs < size && (p[ofs] == 'E' || p[ofs] == 'e')) {
180                                 pushchar(hash, p[ofs]);
181                                 ofs++;
182                                 while (ofs < size && (tokens[p[ofs]].type & (C_DIGIT|C_SIGN))) {
183                                         pushchar(hash, p[ofs]);
184                                         ofs++;
185                                 }
186                         }
187                         while (ofs < size && (tokens[p[ofs]].type & C_FLOAT)) {
188                                 pushchar(hash, p[ofs]);
189                                 ofs++;
190                         }
191                         pushchar(hash, '\n');
192                         continue;
193                 }
194
195                 if (tokens[p[ofs]].type & C_SPACE) {
196                         do {
197                                 ofs++;
198                         } while (ofs < size && (tokens[p[ofs]].type & C_SPACE));
199                         continue;
200                 }
201
202                 if (tokens[p[ofs]].type & C_QUOTE) {
203                         q = p[ofs];
204                         pushchar(hash, p[ofs]);
205                         do {
206                                 ofs++;
207                                 while (ofs < size-1 && p[ofs] == '\\') {
208                                         pushchar(hash, p[ofs]);
209                                         pushchar(hash, p[ofs+1]);
210                                         ofs += 2;
211                                 }
212                                 pushchar(hash, p[ofs]);
213                         } while (ofs < size && p[ofs] != q);
214                         pushchar(hash, '\n');
215                         ofs++;
216                         continue;
217                 }
218
219                 if (tokens[p[ofs]].type & C_TOKEN) {
220                         q = p[ofs];
221                         for (i = 0; i < tokens[q].num_toks; i++) {
222                                 unsigned char *s = (unsigned char *)tokens[q].toks[i];
223                                 int len = strlen((char *)s);
224                                 if (size >= ofs+len && memcmp(&p[ofs], s, len) == 0) {
225                                         int j;
226                                         for (j = 0; s[j]; j++) {
227                                                 pushchar(hash, s[j]);
228                                                 ofs++;
229                                         }
230                                         pushchar(hash, '\n');
231                                         break;
232                                 }
233                         }
234                         if (i < tokens[q].num_toks) {
235                                 continue;
236                         }
237                 }
238
239                 pushchar(hash, p[ofs]);
240                 pushchar(hash, '\n');
241                 ofs++;
242         }
243         pushchar(hash, 0);
244 }
245
246
247 /* hash a file that consists of preprocessor output, but remove any line
248    number information from the hash
249  */
250 int
251 unify_hash(struct mdfour *hash, const char *fname)
252 {
253         char *data;
254         size_t size;
255
256         if (!read_file(fname, 0, &data, &size)) {
257                 stats_update(STATS_PREPROCESSOR);
258                 return -1;
259         }
260         unify(hash, (unsigned char *)data, size);
261         free(data);
262         return 0;
263 }