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
11901190 static void preprocess(int is_bof)
11911191 {
11921192 TCCState *s1 = tcc_state;
1193− int size, i, c, n, saved_parse_flags;
1193+ int i, c, n, saved_parse_flags;
11941194 char buf[1024], *q;
1195− char buf1[1024];
1196− BufferedFile *f;
11971195 Sym *s;
1198− CachedInclude *e;
1199−
1196+
12001197 saved_parse_flags = parse_flags;
12011198 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
12021199 PARSE_FLAG_LINEFEED;
@@ -1275,70 +1272,76 @@ static void preprocess(int is_bof) · 72 unmodified lines
12751272 }
12761273 }
12771274
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;
13001296 size = tcc_basename(file->filename) - file->filename;
1301− if (size > sizeof(buf1) - 1)
1302− size = sizeof(buf1) - 1;
13031297 memcpy(buf1, file->filename, size);
13041298 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 */
13181302 if (i < s1->nb_include_paths)
13191303 path = s1->include_paths[i];
13201304 else
13211305 path = s1->sysinclude_paths[i - s1->nb_include_paths];
13221306 pstrcpy(buf1, sizeof(buf1), path);
13231307 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 {
13251321 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;
13321324 }
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+
13371336 #ifdef INC_DEBUG
13381337 printf("%s: including %s\n", file->filename, buf1);
13391338 #endif
1339+
1340+ /* XXX: fix current line init */
1341+ /* push current file in stack */
1342+ *s1->include_stack_ptr++ = file;
13401343 f->inc_type = c;
1341− pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
1344+ pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf1);
13421345 file = f;
13431346 /* add include file debug info */
13441347 if (tcc_state->do_debug) {
@@ -1348,6 +1351,8 @@ static void preprocess(int is_bof) · 3 unmodified lines
13481351 ch = file->buf_ptr[0];
13491352 goto the_end;
13501353 }
1354+ error("include file '%s' not found", buf);
1355+include_done:
13511356 break;
13521357 case TOK_IFNDEF:
13531358 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