2 * Copyright (C) 2002 Andrew Tridgell
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)
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
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
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.
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).
35 static const char *const s_tokens[] = {
36 "...", ">>=", "<<=", "+=", "-=", "*=", "/=", "%=", "&=", "^=",
37 "|=", ">>", "<<", "++", "--", "->", "&&", "||", "<=", ">=",
38 "==", "!=", ";", "{", "<%", "}", "%>", ",", ":", "=",
39 "(", ")", "[", "<:", "]", ":>", ".", "&", "!", "~",
40 "-", "+", "*", "/", "%", "<", ">", "^", "|", "?",
55 unsigned char num_toks;
59 /* build up the table used by the unifier */
72 memset(tokens, 0, sizeof(tokens));
73 for (c = 0; c < 128; c++) {
74 if (isalpha(c) || c == '_') {
75 tokens[c].type |= C_ALPHA;
78 tokens[c].type |= C_DIGIT;
81 tokens[c].type |= C_SPACE;
84 tokens[c].type |= C_HEX;
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;
96 tokens['-'].type |= C_SIGN;
97 tokens['+'].type |= C_SIGN;
99 for (i = 0; s_tokens[i]; i++) {
101 tokens[c].type |= C_TOKEN;
102 tokens[c].toks[tokens[c].num_toks] = s_tokens[i];
103 tokens[c].num_toks++;
107 /* buffer up characters before hashing them */
109 pushchar(struct mdfour *hash, unsigned char c)
111 static unsigned char buf[64];
116 hash_buffer(hash, (char *)buf, len);
119 hash_buffer(hash, NULL, 0);
125 hash_buffer(hash, (char *)buf, len);
130 /* hash some C/C++ code after unifying */
132 unify(struct mdfour *hash, unsigned char *p, size_t size)
140 for (ofs = 0; ofs < size; ) {
142 if ((size-ofs) > 2 && p[ofs+1] == ' ' && isdigit(p[ofs+2])) {
145 } while (ofs < size && p[ofs] != '\n');
149 pushchar(hash, p[ofs]);
151 } while (ofs < size && p[ofs] != '\n');
152 pushchar(hash, '\n');
158 if (tokens[p[ofs]].type & C_ALPHA) {
160 pushchar(hash, p[ofs]);
162 } while (ofs < size && (tokens[p[ofs]].type & (C_ALPHA|C_DIGIT)));
163 pushchar(hash, '\n');
167 if (tokens[p[ofs]].type & C_DIGIT) {
169 pushchar(hash, p[ofs]);
171 } while (ofs < size &&
172 ((tokens[p[ofs]].type & C_DIGIT) || p[ofs] == '.'));
173 if (ofs < size && (p[ofs] == 'x' || p[ofs] == 'X')) {
175 pushchar(hash, p[ofs]);
177 } while (ofs < size && (tokens[p[ofs]].type & C_HEX));
179 if (ofs < size && (p[ofs] == 'E' || p[ofs] == 'e')) {
180 pushchar(hash, p[ofs]);
182 while (ofs < size && (tokens[p[ofs]].type & (C_DIGIT|C_SIGN))) {
183 pushchar(hash, p[ofs]);
187 while (ofs < size && (tokens[p[ofs]].type & C_FLOAT)) {
188 pushchar(hash, p[ofs]);
191 pushchar(hash, '\n');
195 if (tokens[p[ofs]].type & C_SPACE) {
198 } while (ofs < size && (tokens[p[ofs]].type & C_SPACE));
202 if (tokens[p[ofs]].type & C_QUOTE) {
204 pushchar(hash, p[ofs]);
207 while (ofs < size-1 && p[ofs] == '\\') {
208 pushchar(hash, p[ofs]);
209 pushchar(hash, p[ofs+1]);
212 pushchar(hash, p[ofs]);
213 } while (ofs < size && p[ofs] != q);
214 pushchar(hash, '\n');
219 if (tokens[p[ofs]].type & C_TOKEN) {
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) {
226 for (j = 0; s[j]; j++) {
227 pushchar(hash, s[j]);
230 pushchar(hash, '\n');
234 if (i < tokens[q].num_toks) {
239 pushchar(hash, p[ofs]);
240 pushchar(hash, '\n');
247 /* hash a file that consists of preprocessor output, but remove any line
248 number information from the hash
251 unify_hash(struct mdfour *hash, const char *fname)
256 if (!read_file(fname, 0, &data, &size)) {
257 stats_update(STATS_PREPROCESSOR);
260 unify(hash, (unsigned char *)data, size);