From 6bf2ff0d52a90acdc54f18c75d1978f6b4de4609 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Tue, 7 Jul 2020 11:28:59 -0700 Subject: preprocessor: Better line info for & With C++ module header units it becomes important to distinguish between macros defined in forced headers (& commandline & builtins) from those defined in the header file being processed. We weren't making that easy because we treated the builtins and command-line locations somewhat file-like, with incrementing line numbers, and showing them as included from line 1 of the main file. This patch does 3 things: 0) extend the idiom that 'line 0' of a file means 'the file as a whole' 1) builtins and command-line macros are shown as-if included from line zero. 2) when emitting preprocessed output we keep resetting the line number so that re-reading that preprocessed output will get the same set of locations for the command line etc. For instance the new c-c++-common/cpp/line-2.c test, now emits In file included from : ./line-2.h:4:2: error: #error wrong 4 | #error wrong | ^~~~~ line-2.c:3:11: error: macro "bill" passed 1 arguments, but takes just 0 3 | int bill(1); | ^ In file included from : ./line-2.h:3: note: macro "bill" defined here 3 | #define bill() 2 | Before it told you about including from :31. the preprocessed output looks like: ... (There's a new optimization in do_line_marker to stop each of these line markers causing a new line map. We can simply rewind the location, and keep using the same line map.) libcpp/ * directives.c (do_linemarker): Optimize rewinding to line zero. * files.c (_cpp_stack_file): Start on line zero when about to inject headers. (cpp_push_include, cpp_push_default_include): Use highest_line as the location. * include/cpplib.h (cpp_read_main_file): Add injecting parm. * init.c (cpp_read_main_file): Likewise, inform _cpp_stack_file. * internal.h (enum include_type): Add IT_MAIN_INJECT. gcc/c-family/ * c-opts.c (c_common_post_options): Add 'injecting' arg to cpp_read_main_file. (c_finish_options): Add linemap_line_start calls for builtin and cmd maps. Force token position to line_table's highest line. * c-ppoutput.c (print_line_1): Refactor, print line zero. (cb_define): Always increment source line. gcc/testsuite/ * c-c++-common/cpp/line-2.c: New. * c-c++-common/cpp/line-2.h: New. * c-c++-common/cpp/line-3.c: New. * c-c++-common/cpp/line-4.c: New. * c-c++-common/cpp/line-4.h: New. --- gcc/c-family/c-ppoutput.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'gcc/c-family/c-ppoutput.c') diff --git a/gcc/c-family/c-ppoutput.c b/gcc/c-family/c-ppoutput.c index 1e2b32b..44c6f30 100644 --- a/gcc/c-family/c-ppoutput.c +++ b/gcc/c-family/c-ppoutput.c @@ -560,26 +560,23 @@ print_line_1 (location_t src_loc, const char *special_flags, FILE *stream) if (src_loc != UNKNOWN_LOCATION && !flag_no_line_commands) { const char *file_path = LOCATION_FILE (src_loc); - int sysp; size_t to_file_len = strlen (file_path); unsigned char *to_file_quoted = (unsigned char *) alloca (to_file_len * 4 + 1); - unsigned char *p; print.src_line = LOCATION_LINE (src_loc); print.src_file = file_path; /* cpp_quote_string does not nul-terminate, so we have to do it ourselves. */ - p = cpp_quote_string (to_file_quoted, - (const unsigned char *) file_path, - to_file_len); + unsigned char *p = cpp_quote_string (to_file_quoted, + (const unsigned char *) file_path, + to_file_len); *p = '\0'; fprintf (stream, "# %u \"%s\"%s", - print.src_line == 0 ? 1 : print.src_line, - to_file_quoted, special_flags); + print.src_line, to_file_quoted, special_flags); - sysp = in_system_header_at (src_loc); + int sysp = in_system_header_at (src_loc); if (sysp == 2) fputs (" 3 4", stream); else if (sysp == 1) @@ -677,8 +674,7 @@ cb_define (cpp_reader *pfile, location_t line, cpp_hashnode *node) linemap_resolve_location (line_table, line, LRK_MACRO_DEFINITION_LOCATION, &map); - if (LINEMAP_LINE (map) != 0) - print.src_line++; + print.src_line++; } static void -- cgit v1.1