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