History

fix "cached include" optimization

comparing the filenames as in the #include statement can be ambiguous if including files are in different directories. Now caches and checks the really opened filename instead.

grischka committed 17 years ago

Commit aed6a7c1 parent 530b77e1 file · +59 −54Browse files

Unified · Split

tccpp.c+59−54
@@ -1190,13 +1190,10 @@ static void pragma_parse(TCCState *s1) · 1189 unmodified lines
1190static void preprocess(int is_bof)1190static void preprocess(int is_bof)
1191{1191{
1192 TCCState *s1 = tcc_state;1192 TCCState *s1 = tcc_state;
1193 int size, i, c, n, saved_parse_flags;1193 int i, c, n, saved_parse_flags;
1194 char buf[1024], *q;1194 char buf[1024], *q;
1195 char buf1[1024];
1196 BufferedFile *f;
1197 Sym *s;1195 Sym *s;
1198 CachedInclude *e;1196
1199
1200 saved_parse_flags = parse_flags;1197 saved_parse_flags = parse_flags;
1201 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | 1198 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
1202 PARSE_FLAG_LINEFEED;1199 PARSE_FLAG_LINEFEED;
@@ -1275,70 +1272,76 @@ static void preprocess(int is_bof) · 72 unmodified lines
1275 }1272 }
1276 }1273 }
1277 1274
1278 e = search_cached_include(s1, c, buf);1275 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
1279 if (e && define_find(e->ifndef_macro)) {1276 error("#include recursion too deep");
1280 /* no need to parse the include because the 'ifndef macro'1277
1281 is defined */1278 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1282#ifdef INC_DEBUG1279 for (i = -2; i < n; ++i) {
1283 printf("%s: skipping %s\n", file->filename, buf);1280 char buf1[sizeof file->filename];
1284#endif1281 BufferedFile *f;
1285 } else {1282 CachedInclude *e;
1286 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)1283 const char *path;
1287 error("#include recursion too deep");1284 int size;
1288 /* push current file in stack */1285
1289 /* XXX: fix current line init */1286 if (i == -2) {
1290 *s1->include_stack_ptr++ = file;1287 /* check absolute include path */
1291 1288 if (!IS_ABSPATH(buf))
1292 /* check absolute include path */1289 continue;
1293 if (IS_ABSPATH(buf)) {1290 buf1[0] = 0;
1294 f = tcc_open(s1, buf);1291
1295 if (f)1292 } else if (i == -1) {
1296 goto found;1293 /* search in current dir if "header.h" */
1297 }1294 if (c != '\"')
1298 if (c == '\"') {1295 continue;
1299 /* first search in current dir if "header.h" */
1300 size = tcc_basename(file->filename) - file->filename;1296 size = tcc_basename(file->filename) - file->filename;
1301 if (size > sizeof(buf1) - 1)
1302 size = sizeof(buf1) - 1;
1303 memcpy(buf1, file->filename, size);1297 memcpy(buf1, file->filename, size);
1304 buf1[size] = '\0';1298 buf1[size] = '\0';
1305 pstrcat(buf1, sizeof(buf1), buf);1299
1306 f = tcc_open(s1, buf1);1300 } else {
1307 if (f) {1301 /* search in all the include paths */
1308 if (tok == TOK_INCLUDE_NEXT)
1309 tok = TOK_INCLUDE;
1310 else
1311 goto found;
1312 }
1313 }
1314 /* now search in all the include paths */
1315 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
1316 for(i = 0; i < n; i++) {
1317 const char *path;
1318 if (i < s1->nb_include_paths)1302 if (i < s1->nb_include_paths)
1319 path = s1->include_paths[i];1303 path = s1->include_paths[i];
1320 else1304 else
1321 path = s1->sysinclude_paths[i - s1->nb_include_paths];1305 path = s1->sysinclude_paths[i - s1->nb_include_paths];
1322 pstrcpy(buf1, sizeof(buf1), path);1306 pstrcpy(buf1, sizeof(buf1), path);
1323 pstrcat(buf1, sizeof(buf1), "/");1307 pstrcat(buf1, sizeof(buf1), "/");
1324 pstrcat(buf1, sizeof(buf1), buf);1308 }
1309
1310 pstrcat(buf1, sizeof(buf1), buf);
1311
1312 e = search_cached_include(s1, c, buf1);
1313 if (e && define_find(e->ifndef_macro)) {
1314 /* no need to parse the include because the 'ifndef macro'
1315 is defined */
1316#ifdef INC_DEBUG
1317 printf("%s: skipping %s\n", file->filename, buf);
1318#endif
1319 f = NULL;
1320 } else {
1325 f = tcc_open(s1, buf1);1321 f = tcc_open(s1, buf1);
1326 if (f) {1322 if (!f)
1327 if (tok == TOK_INCLUDE_NEXT)1323 continue;
1328 tok = TOK_INCLUDE;
1329 else
1330 goto found;
1331 }
1332 }1324 }
1333 --s1->include_stack_ptr;1325
1334 error("include file '%s' not found", buf);1326 if (tok == TOK_INCLUDE_NEXT) {
1335 break;1327 tok = TOK_INCLUDE;
1336 found:1328 if (f)
1329 tcc_close(f);
1330 continue;
1331 }
1332
1333 if (!f)
1334 goto include_done;
1335
1337#ifdef INC_DEBUG1336#ifdef INC_DEBUG
1338 printf("%s: including %s\n", file->filename, buf1);1337 printf("%s: including %s\n", file->filename, buf1);
1339#endif1338#endif
1339
1340 /* XXX: fix current line init */
1341 /* push current file in stack */
1342 *s1->include_stack_ptr++ = file;
1340 f->inc_type = c;1343 f->inc_type = c;
1341 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);1344 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
1342 file = f;1345 file = f;
1343 /* add include file debug info */1346 /* add include file debug info */
1344 if (tcc_state->do_debug) {1347 if (tcc_state->do_debug) {
@@ -1348,6 +1351,8 @@ static void preprocess(int is_bof) · 3 unmodified lines
1348 ch = file->buf_ptr[0];1351 ch = file->buf_ptr[0];
1349 goto the_end;1352 goto the_end;
1350 }1353 }
1354 error("include file '%s' not found", buf);
1355include_done:
1351 break;1356 break;
1352 case TOK_IFNDEF:1357 case TOK_IFNDEF:
1353 c = 1;1358 c = 1;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t