1 // Copyright (C) 2002 Andrew Tridgell
2 // Copyright (C) 2009-2019 Joel Rosdahl
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
20 // The idea is that changes that don't affect the resulting C code should not
21 // change the hash. This is achieved by folding white-space and other
22 // non-semantic fluff in the input into a single unified format.
24 // This unifier was design to match the output of the unifier in compilercache,
25 // which is flex based. The major difference is that this unifier is much
26 // faster (about 2x) and more forgiving of syntactic errors. Continuing on
27 // syntactic errors is important to cope with C/C++ extensions in the local
28 // compiler (for example, inline assembly systems).
34 static bool print_unified = true;
36 static const char *const s_tokens[] = {
37 "...", ">>=", "<<=", "+=", "-=", "*=", "/=", "%=", "&=", "^=",
38 "|=", ">>", "<<", "++", "--", "->", "&&", "||", "<=", ">=",
39 "==", "!=", ";", "{", "<%", "}", "%>", ",", ":", "=",
40 "(", ")", "[", "<:", "]", ":>", ".", "&", "!", "~",
41 "-", "+", "*", "/", "%", "<", ">", "^", "|", "?",
56 unsigned char num_toks;
60 // Build up the table used by the unifier.
70 memset(tokens, 0, sizeof(tokens));
71 for (unsigned char c = 0; c < 128; c++) {
72 if (isalpha(c) || c == '_') {
73 tokens[c].type |= C_ALPHA;
76 tokens[c].type |= C_DIGIT;
79 tokens[c].type |= C_SPACE;
82 tokens[c].type |= C_HEX;
85 tokens['\''].type |= C_QUOTE;
86 tokens['"'].type |= C_QUOTE;
87 tokens['l'].type |= C_FLOAT;
88 tokens['L'].type |= C_FLOAT;
89 tokens['f'].type |= C_FLOAT;
90 tokens['F'].type |= C_FLOAT;
91 tokens['U'].type |= C_FLOAT;
92 tokens['u'].type |= C_FLOAT;
94 tokens['-'].type |= C_SIGN;
95 tokens['+'].type |= C_SIGN;
97 for (int i = 0; s_tokens[i]; i++) {
98 unsigned char c = s_tokens[i][0];
99 tokens[c].type |= C_TOKEN;
100 tokens[c].toks[tokens[c].num_toks] = s_tokens[i];
101 tokens[c].num_toks++;
105 // Buffer up characters before hashing them.
107 pushchar(struct hash *hash, unsigned char c)
109 static unsigned char buf[64];
114 hash_buffer(hash, (char *)buf, len);
116 printf("%.*s", (int) len, buf);
125 hash_buffer(hash, (char *)buf, len);
127 printf("%.*s", (int) len, buf);
133 // Hash some C/C++ code after unifying.
135 unify(struct hash *hash, unsigned char *p, size_t size)
139 for (size_t ofs = 0; ofs < size;) {
141 if ((size-ofs) > 2 && p[ofs+1] == ' ' && isdigit(p[ofs+2])) {
144 } while (ofs < size && p[ofs] != '\n');
148 pushchar(hash, p[ofs]);
150 } while (ofs < size && p[ofs] != '\n');
151 pushchar(hash, '\n');
157 if (tokens[p[ofs]].type & C_ALPHA) {
159 pushchar(hash, p[ofs]);
161 } while (ofs < size && (tokens[p[ofs]].type & (C_ALPHA|C_DIGIT)));
162 pushchar(hash, '\n');
166 if (tokens[p[ofs]].type & C_DIGIT) {
168 pushchar(hash, p[ofs]);
170 } while (ofs < size &&
171 ((tokens[p[ofs]].type & C_DIGIT) || p[ofs] == '.'));
172 if (ofs < size && (p[ofs] == 'x' || p[ofs] == 'X')) {
174 pushchar(hash, p[ofs]);
176 } while (ofs < size && (tokens[p[ofs]].type & C_HEX));
178 if (ofs < size && (p[ofs] == 'E' || p[ofs] == 'e')) {
179 pushchar(hash, p[ofs]);
181 while (ofs < size && (tokens[p[ofs]].type & (C_DIGIT|C_SIGN))) {
182 pushchar(hash, p[ofs]);
186 while (ofs < size && (tokens[p[ofs]].type & C_FLOAT)) {
187 pushchar(hash, p[ofs]);
190 pushchar(hash, '\n');
194 if (tokens[p[ofs]].type & C_SPACE) {
197 } while (ofs < size && (tokens[p[ofs]].type & C_SPACE));
201 if (tokens[p[ofs]].type & C_QUOTE) {
202 unsigned char q = p[ofs];
203 pushchar(hash, p[ofs]);
206 while (ofs < size-1 && p[ofs] == '\\') {
207 pushchar(hash, p[ofs]);
208 pushchar(hash, p[ofs+1]);
211 pushchar(hash, p[ofs]);
212 } while (ofs < size && p[ofs] != q);
213 pushchar(hash, '\n');
218 if (tokens[p[ofs]].type & C_TOKEN) {
219 unsigned char q = p[ofs];
221 for (i = 0; i < tokens[q].num_toks; i++) {
222 const unsigned char *s = (const unsigned char *)tokens[q].toks[i];
223 int len = strlen((const 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 number
248 // information from the hash.
250 unify_hash(struct hash *hash, const char *fname, bool debug)
254 if (!read_file(fname, 0, &data, &size)) {
255 stats_update(STATS_PREPROCESSOR);
258 print_unified = debug;
259 unify(hash, (unsigned char *)data, size);