diff --git a/tccpp.c b/tccpp.c index 770873f..b1bf113 100644 --- a/tccpp.c +++ b/tccpp.c @@ -1528,7 +1528,7 @@ ST_FUNC void preprocess(int is_bof) int i, c, n, saved_parse_flags; char buf[1024], *q; Sym *s; - int include_next_count = 0; + char *p3 = 0; saved_parse_flags = parse_flags; parse_flags = PARSE_FLAG_PREPROCESS @@ -1618,6 +1618,11 @@ ST_FUNC void preprocess(int is_bof) /* store current file in stack, but increment stack later below */ *s1->include_stack_ptr = file; + #ifdef INC_DEBUG + if (tok == TOK_INCLUDE_NEXT) + printf("%s (1) include_next file %s\n", file->filename, buf); + #endif + n = s1->nb_include_paths + s1->nb_sysinclude_paths; for (i = -2; i < n; ++i) { char buf1[sizeof file->filename]; @@ -1649,36 +1654,67 @@ ST_FUNC void preprocess(int is_bof) pstrcat(buf1, sizeof(buf1), "/"); } + #ifdef INC_DEBUG + if (tok == TOK_INCLUDE_NEXT) + printf("%s (2) include_next path <%s> name <%s>\n", file->filename, buf1, buf); + #endif + pstrcat(buf1, sizeof(buf1), buf); if (tok == TOK_INCLUDE_NEXT) { - for (f = s1->include_stack_ptr; f >= s1->include_stack; --f) - if (0 == PATHCMP((*f)->filename, buf1)) { -#ifdef INC_DEBUG - printf("%s: #include_next skipping %s\n", file->filename, buf1); -#endif - include_next_count++; + char *p1 = buf1; + if ((p1[0] == '.') && IS_DIRSEP(p1[1])) p1 += 2; + + if (p3) { + if (0 == PATHCMP(p1, p3)) { + #ifdef INC_DEBUG + printf("%s (3) include_next skipping <%s>\n", file->filename, p1); + #endif goto include_trynext; } + } + else { + for (f = s1->include_stack_ptr; f >= s1->include_stack; --f) { + char *p2 = (*f)->filename; + if ((p2[0] == '.') && IS_DIRSEP(p2[1])) p2 += 2; + if (0 == PATHCMP(p2, p1)) { + p3 = p2; + break; + } + } + #ifdef INC_DEBUG + printf("%s: (4) include_next skipping <%s> (p3=%p)\n", file->filename, buf1, p3); + #endif + goto include_trynext; + } + #ifdef INC_DEBUG + printf("%s (5) include_next considering <%s>\n", file->filename, buf1); + #endif } e = search_cached_include(s1, buf1); if (e && (define_find(e->ifndef_macro) || e->ifndef_macro == TOK_once)) { /* no need to parse the include because the 'ifndef macro' is defined */ -#ifdef INC_DEBUG - printf("%s: skipping cached %s\n", file->filename, buf1); -#endif + + #ifdef INC_DEBUG + printf("%s: skipping cached <%s>\n", file->filename, buf1); + #endif goto include_done; } - if (tcc_open(s1, buf1) < 0) + if (tcc_open(s1, buf1) < 0) { + #ifdef INC_DEBUG + printf("%s: include open failed <%s>\n", file->filename, buf1); + #endif include_trynext: continue; + } + + #ifdef INC_DEBUG + printf("%s: including <%s>\n", file->prev->filename, file->filename); + #endif -#ifdef INC_DEBUG - printf("%s: including %s\n", file->prev->filename, file->filename); -#endif /* update target deps */ dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps, tcc_strdup(buf1)); @@ -1691,8 +1727,7 @@ include_trynext: ch = file->buf_ptr[0]; goto the_end; } - if (include_next_count == 0) - tcc_error("include file '%s' not found", buf); + tcc_error("include file '%s' not found", buf); include_done: break; case TOK_IFNDEF: @@ -1709,9 +1744,9 @@ include_done: tcc_error("invalid argument for '#if%sdef'", c ? "n" : ""); if (is_bof) { if (c) { -#ifdef INC_DEBUG - printf("#ifndef %s\n", get_tok_str(tok, NULL)); -#endif + #ifdef INC_DEBUG + printf("#ifndef %s\n", get_tok_str(tok, NULL)); + #endif file->ifndef_macro = tok; } }