tcc -MD: drop system includes and duplicates

Also:
- print filenames in errors for binary input files
  (was lost with the thread support recently)
This commit is contained in:
grischka 2020-06-17 08:21:37 +02:00
parent e7a4140d28
commit cbef54653a
6 changed files with 51 additions and 38 deletions

View File

@ -508,15 +508,13 @@ static void error1(int mode, const char *fmt, va_list ap)
BufferedFile **pf, *f; BufferedFile **pf, *f;
TCCState *s1 = tcc_state; TCCState *s1 = tcc_state;
/* 's1->error_set_jmp_enabled' means that we're called from buf[0] = '\0';
within the parser/generator and 'tcc_state' was already if (s1 == NULL)
set (i.e. not by the function above). /* can happen only if called from tcc_malloc(): 'out of memory' */
goto no_file;
Otherwise, 's1 = NULL' means we're called because of severe if (!s1->error_set_jmp_enabled) {
problems from tcc_malloc() which under normal conditions /* tcc_state just was set by tcc_enter_state() */
should never happen. */
if (s1 && !s1->error_set_jmp_enabled) {
tcc_state = NULL; tcc_state = NULL;
POST_SEM(); POST_SEM();
} }
@ -528,24 +526,25 @@ static void error1(int mode, const char *fmt, va_list ap)
mode = ERROR_ERROR; mode = ERROR_ERROR;
} }
buf[0] = '\0'; f = NULL;
if (s1->error_set_jmp_enabled) { /* we're called while parsing a file */
/* use upper file if inline ":asm:" or token ":paste:" */ /* use upper file if inline ":asm:" or token ":paste:" */
for (f = file; f && f->filename[0] == ':'; f = f->prev) for (f = file; f && f->filename[0] == ':'; f = f->prev)
; ;
}
if (f) { if (f) {
for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++) for(pf = s1->include_stack; pf < s1->include_stack_ptr; pf++)
strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n", strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
(*pf)->filename, (*pf)->line_num); (*pf)->filename, (*pf)->line_num);
if (s1->error_set_jmp_enabled) {
strcat_printf(buf, sizeof(buf), "%s:%d: ", strcat_printf(buf, sizeof(buf), "%s:%d: ",
f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL)); f->filename, f->line_num - !!(tok_flags & TOK_FLAG_BOL));
} else { } else if (s1->current_filename) {
strcat_printf(buf, sizeof(buf), "%s: ", strcat_printf(buf, sizeof(buf), "%s: ", s1->current_filename);
f->filename);
} }
} else {
no_file:
if (0 == buf[0])
strcat_printf(buf, sizeof(buf), "tcc: "); strcat_printf(buf, sizeof(buf), "tcc: ");
}
if (mode == ERROR_WARN) if (mode == ERROR_WARN)
strcat_printf(buf, sizeof(buf), "warning: "); strcat_printf(buf, sizeof(buf), "warning: ");
else else
@ -1071,10 +1070,7 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
return -1; return -1;
} }
/* update target deps */ s1->current_filename = filename;
dynarray_add(&s1->target_deps, &s1->nb_target_deps,
tcc_strdup(filename));
if (flags & AFF_TYPE_BIN) { if (flags & AFF_TYPE_BIN) {
ElfW(Ehdr) ehdr; ElfW(Ehdr) ehdr;
int obj_type; int obj_type;
@ -1126,8 +1122,11 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
} }
close(fd); close(fd);
} else { } else {
/* update target deps */
dynarray_add(&s1->target_deps, &s1->nb_target_deps, tcc_strdup(filename));
ret = tcc_compile(s1, flags, filename, fd); ret = tcc_compile(s1, flags, filename, fd);
} }
s1->current_filename = NULL;
return ret; return ret;
} }

3
tcc.h
View File

@ -911,6 +911,9 @@ struct TCCState {
/* option -dnum (for general development purposes) */ /* option -dnum (for general development purposes) */
int g_debug; int g_debug;
/* for warnings/errors for object files*/
const char *current_filename;
/* used by main and tcc_parse_args only */ /* used by main and tcc_parse_args only */
struct filespec **files; /* files seen on command line */ struct filespec **files; /* files seen on command line */
int nb_files; /* number thereof */ int nb_files; /* number thereof */

View File

@ -3125,7 +3125,7 @@ ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level)
/* test CPU specific stuff */ /* test CPU specific stuff */
if (ehdr.e_ident[5] != ELFDATA2LSB || if (ehdr.e_ident[5] != ELFDATA2LSB ||
ehdr.e_machine != EM_TCC_TARGET) { ehdr.e_machine != EM_TCC_TARGET) {
tcc_error_noabort("bad architecture: %s", filename); tcc_error_noabort("bad architecture");
return -1; return -1;
} }

View File

@ -861,7 +861,7 @@ static void pe_build_imports(struct pe_info *pe)
v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name); v = (ADDR3264)GetProcAddress(dllref->handle, ordinal?(char*)0+ordinal:name);
} }
if (!v) if (!v)
tcc_error_noabort("can't build symbol '%s'", name); tcc_error_noabort("could not resolve symbol '%s'", name);
} else } else
#endif #endif
if (ordinal) { if (ordinal) {

15
tccpp.c
View File

@ -1808,7 +1808,7 @@ ST_FUNC void preprocess(int is_bof)
tcc_error("#include recursion too deep"); tcc_error("#include recursion too deep");
/* push current file on stack */ /* push current file on stack */
*s1->include_stack_ptr++ = file; *s1->include_stack_ptr++ = file;
i = tok == TOK_INCLUDE_NEXT ? file->include_next_index: 0; i = tok == TOK_INCLUDE_NEXT ? file->include_next_index + 1 : 0;
n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths; n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
for (; i < n; ++i) { for (; i < n; ++i) {
char buf1[sizeof file->filename]; char buf1[sizeof file->filename];
@ -1851,12 +1851,17 @@ ST_FUNC void preprocess(int is_bof)
if (tcc_open(s1, buf1) < 0) if (tcc_open(s1, buf1) < 0)
continue; continue;
file->include_next_index = i + 1; file->include_next_index = i;
#ifdef INC_DEBUG #ifdef INC_DEBUG
printf("%s: including %s\n", file->prev->filename, file->filename); printf("%s: including %s\n", file->prev->filename, file->filename);
#endif #endif
/* update target deps */ /* update target deps */
if (s1->gen_deps) { if (s1->gen_deps) {
BufferedFile *bf = file;
while (i == 1 && (bf = bf->prev))
i = bf->include_next_index;
/* skip system include files */
if (n - i > s1->nb_sysinclude_paths)
dynarray_add(&s1->target_deps, &s1->nb_target_deps, dynarray_add(&s1->target_deps, &s1->nb_target_deps,
tcc_strdup(buf1)); tcc_strdup(buf1));
} }
@ -3866,8 +3871,10 @@ ST_FUNC int tcc_preprocess(TCCState *s1)
s1->dflag &= ~1; s1->dflag &= ~1;
} }
token_seen = TOK_LINEFEED, spcs = 0; token_seen = TOK_LINEFEED, spcs = 0, level = 0;
pp_line(s1, file, 0); if (file->prev)
pp_line(s1, file->prev, level++);
pp_line(s1, file, level);
for (;;) { for (;;) {
iptr = s1->include_stack_ptr; iptr = s1->include_stack_ptr;
next(); next();

View File

@ -519,7 +519,7 @@ ST_FUNC void gen_makedeps(TCCState *s1, const char *target, const char *filename
{ {
FILE *depout; FILE *depout;
char buf[1024]; char buf[1024];
int i; int i, k;
if (!filename) { if (!filename) {
/* compute filename automatically: dir/file.o -> dir/file.d */ /* compute filename automatically: dir/file.o -> dir/file.d */
@ -535,10 +535,14 @@ ST_FUNC void gen_makedeps(TCCState *s1, const char *target, const char *filename
depout = fopen(filename, "w"); depout = fopen(filename, "w");
if (!depout) if (!depout)
tcc_error("could not open '%s'", filename); tcc_error("could not open '%s'", filename);
fprintf(depout, "%s:", target);
fprintf(depout, "%s: \\\n", target); for (i = 0; i<s1->nb_target_deps; ++i) {
for (i=0; i<s1->nb_target_deps; ++i) for (k = 0; k < i; ++k)
fprintf(depout, " %s \\\n", s1->target_deps[i]); if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
goto next;
fprintf(depout, " \\\n %s", s1->target_deps[i]);
next:;
}
fprintf(depout, "\n"); fprintf(depout, "\n");
fclose(depout); fclose(depout);
} }