Commit ac81cf0b by David Malcolm Committed by David Malcolm

libcpp: Tweak to missing #include source location

This patch tweaks the error message location for missing header files.

Previously these read:

test.c:1:17: fatal error: 404.h: No such file or directory
 #include "404.h"
                 ^
compilation terminated.

With this patch, the pertinent string is underlined:

test.c:1:10: fatal error: 404.h: No such file or directory
 #include "404.h"
          ^~~~~~~
compilation terminated.

gcc/testsuite/ChangeLog:
	* c-c++-common/missing-header-1.c: New test case.
	* c-c++-common/missing-header-2.c: New test case.
	* c-c++-common/missing-header-3.c: New test case.
	* c-c++-common/missing-header-4.c: New test case.

libcpp/ChangeLog:
	* directives.c (do_include_common): Pass on "location" to
	_cpp_stack_include.
	* errors.c (cpp_diagnostic): Reimplement in terms of...
	(cpp_diagnostic_at): New function.
	(cpp_error_at): New function.
	(cpp_errno_filename): Add "loc" param and use it by using
	cpp_error_at rather than cpp_error.
	* files.c (find_file_in_dir): Add "loc" param and pass it to
	open_file_failed.
	(_cpp_find_file): Add "loc" param.  Use it to convert calls to
	cpp_error to cpp_error_at, and pass it to find_file_in_dir and
	open_file_failed.
	(read_file_guts): Add "loc" param.  Use it to convert calls to
	cpp_error to cpp_error_at.  Pass it to cpp_errno_filename.
	(read_file): Add "loc" param.  Pass it to open_file_failed and
	read_file_guts.
	(should_stack_file): Add "loc" param.  Pass it to read_file.
	(_cpp_stack_file): Add "loc" param.  Pass it to should_stack_file.
	(_cpp_stack_include): Add "loc" param.  Pass it to
	_cpp_find_file and _cpp_stack_file.
	(open_file_failed): Add "loc" param.  Pass it to
	cpp_errno_filename.
	(_cpp_fake_include): Add 0 as a source_location in call to
	_cpp_find_file.
	(_cpp_compare_file_date): Likewise.
	(cpp_push_include): Likewise for call to _cpp_stack_include.
	(cpp_push_default_include): Likewise.
	(_cpp_save_file_entries): Likewise for call to open_file_failed.
	(_cpp_has_header): Likewise for call to _cpp_find_file.
	* include/cpplib.h (cpp_errno_filename): Add source_location
	param.
	(cpp_error_at): New declaration.
	* init.c (cpp_read_main_file): Add 0 as a source_location in calls
	to _cpp_find_file and _cpp_stack_file.
	* internal.h (_cpp_find_file): Add source_location param.
	(_cpp_stack_file): Likewise.
	(_cpp_stack_include): Likewise.

From-SVN: r237715
parent 1a4f11c8
2016-06-22 David Malcolm <dmalcolm@redhat.com> 2016-06-22 David Malcolm <dmalcolm@redhat.com>
* c-c++-common/missing-header-1.c: New test case.
* c-c++-common/missing-header-2.c: New test case.
* c-c++-common/missing-header-3.c: New test case.
* c-c++-common/missing-header-4.c: New test case.
2016-06-22 David Malcolm <dmalcolm@redhat.com>
PR c/70339 PR c/70339
* c-c++-common/attributes-1.c: Update dg-prune-output to include * c-c++-common/attributes-1.c: Update dg-prune-output to include
hint. hint.
......
/* { dg-options "-fdiagnostics-show-caret" } */
#include "this-file-does-not-exist.h" /* { dg-error "10: this-file-does-not-exist.h: No such file or directory" } */
/* { dg-begin-multiline-output "" }
#include "this-file-does-not-exist.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
{ dg-end-multiline-output "" } */
/* { dg-options "-fdiagnostics-show-caret" } */
#include <this-file-does-not-exist.h> /* { dg-error "10: this-file-does-not-exist.h: No such file or directory" } */
/* { dg-begin-multiline-output "" }
#include <this-file-does-not-exist.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
{ dg-end-multiline-output "" } */
/* { dg-options "-fdiagnostics-show-caret -Wno-deprecated" } */
#import <this-file-does-not-exist.h> /* { dg-error "9: this-file-does-not-exist.h: No such file or directory" } */
/* { dg-begin-multiline-output "" }
#import <this-file-does-not-exist.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
{ dg-end-multiline-output "" } */
/* { dg-options "-fdiagnostics-show-caret -Wno-deprecated" } */
#import "this-file-does-not-exist.h" /* { dg-error "9: this-file-does-not-exist.h: No such file or directory" } */
/* { dg-begin-multiline-output "" }
#import "this-file-does-not-exist.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
{ dg-end-multiline-output "" } */
2016-06-22 David Malcolm <dmalcolm@redhat.com> 2016-06-22 David Malcolm <dmalcolm@redhat.com>
* directives.c (do_include_common): Pass on "location" to
_cpp_stack_include.
* errors.c (cpp_diagnostic): Reimplement in terms of...
(cpp_diagnostic_at): New function.
(cpp_error_at): New function.
(cpp_errno_filename): Add "loc" param and use it by using
cpp_error_at rather than cpp_error.
* files.c (find_file_in_dir): Add "loc" param and pass it to
open_file_failed.
(_cpp_find_file): Add "loc" param. Use it to convert calls to
cpp_error to cpp_error_at, and pass it to find_file_in_dir and
open_file_failed.
(read_file_guts): Add "loc" param. Use it to convert calls to
cpp_error to cpp_error_at. Pass it to cpp_errno_filename.
(read_file): Add "loc" param. Pass it to open_file_failed and
read_file_guts.
(should_stack_file): Add "loc" param. Pass it to read_file.
(_cpp_stack_file): Add "loc" param. Pass it to should_stack_file.
(_cpp_stack_include): Add "loc" param. Pass it to
_cpp_find_file and _cpp_stack_file.
(open_file_failed): Add "loc" param. Pass it to
cpp_errno_filename.
(_cpp_fake_include): Add 0 as a source_location in call to
_cpp_find_file.
(_cpp_compare_file_date): Likewise.
(cpp_push_include): Likewise for call to _cpp_stack_include.
(cpp_push_default_include): Likewise.
(_cpp_save_file_entries): Likewise for call to open_file_failed.
(_cpp_has_header): Likewise for call to _cpp_find_file.
* include/cpplib.h (cpp_errno_filename): Add source_location
param.
(cpp_error_at): New declaration.
* init.c (cpp_read_main_file): Add 0 as a source_location in calls
to _cpp_find_file and _cpp_stack_file.
* internal.h (_cpp_find_file): Add source_location param.
(_cpp_stack_file): Likewise.
(_cpp_stack_include): Likewise.
2016-06-22 David Malcolm <dmalcolm@redhat.com>
* include/line-map.h (fixit_hint::get_start_loc): New pure virtual * include/line-map.h (fixit_hint::get_start_loc): New pure virtual
function. function.
(fixit_hint::maybe_get_end_loc): Likewise. (fixit_hint::maybe_get_end_loc): Likewise.
......
...@@ -818,7 +818,7 @@ do_include_common (cpp_reader *pfile, enum include_type type) ...@@ -818,7 +818,7 @@ do_include_common (cpp_reader *pfile, enum include_type type)
pfile->directive->name, fname, angle_brackets, pfile->directive->name, fname, angle_brackets,
buf); buf);
_cpp_stack_include (pfile, fname, angle_brackets, type); _cpp_stack_include (pfile, fname, angle_brackets, type, location);
} }
XDELETEVEC (fname); XDELETEVEC (fname);
......
...@@ -27,6 +27,24 @@ along with this program; see the file COPYING3. If not see ...@@ -27,6 +27,24 @@ along with this program; see the file COPYING3. If not see
#include "cpplib.h" #include "cpplib.h"
#include "internal.h" #include "internal.h"
/* Print a diagnostic at the given location. */
ATTRIBUTE_FPTR_PRINTF(5,0)
static bool
cpp_diagnostic_at (cpp_reader * pfile, int level, int reason,
source_location src_loc,
const char *msgid, va_list *ap)
{
bool ret;
if (!pfile->cb.error)
abort ();
rich_location richloc (pfile->line_table, src_loc);
ret = pfile->cb.error (pfile, level, reason, &richloc, _(msgid), ap);
return ret;
}
/* Print a diagnostic at the location of the previously lexed token. */ /* Print a diagnostic at the location of the previously lexed token. */
ATTRIBUTE_FPTR_PRINTF(4,0) ATTRIBUTE_FPTR_PRINTF(4,0)
...@@ -35,7 +53,6 @@ cpp_diagnostic (cpp_reader * pfile, int level, int reason, ...@@ -35,7 +53,6 @@ cpp_diagnostic (cpp_reader * pfile, int level, int reason,
const char *msgid, va_list *ap) const char *msgid, va_list *ap)
{ {
source_location src_loc; source_location src_loc;
bool ret;
if (CPP_OPTION (pfile, traditional)) if (CPP_OPTION (pfile, traditional))
{ {
...@@ -54,13 +71,7 @@ cpp_diagnostic (cpp_reader * pfile, int level, int reason, ...@@ -54,13 +71,7 @@ cpp_diagnostic (cpp_reader * pfile, int level, int reason,
{ {
src_loc = pfile->cur_token[-1].src_loc; src_loc = pfile->cur_token[-1].src_loc;
} }
return cpp_diagnostic_at (pfile, level, reason, src_loc, msgid, ap);
if (!pfile->cb.error)
abort ();
rich_location richloc (pfile->line_table, src_loc);
ret = pfile->cb.error (pfile, level, reason, &richloc, _(msgid), ap);
return ret;
} }
/* Print a warning or error, depending on the value of LEVEL. */ /* Print a warning or error, depending on the value of LEVEL. */
...@@ -225,6 +236,25 @@ cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason, ...@@ -225,6 +236,25 @@ cpp_warning_with_line_syshdr (cpp_reader *pfile, int reason,
return ret; return ret;
} }
/* As cpp_error, but use SRC_LOC as the location of the error, without
a column override. */
bool
cpp_error_at (cpp_reader * pfile, int level, source_location src_loc,
const char *msgid, ...)
{
va_list ap;
bool ret;
va_start (ap, msgid);
ret = cpp_diagnostic_at (pfile, level, CPP_W_NONE, src_loc,
msgid, &ap);
va_end (ap);
return ret;
}
/* Print a warning or error, depending on the value of LEVEL. Include /* Print a warning or error, depending on the value of LEVEL. Include
information from errno. */ information from errno. */
...@@ -239,10 +269,12 @@ cpp_errno (cpp_reader *pfile, int level, const char *msgid) ...@@ -239,10 +269,12 @@ cpp_errno (cpp_reader *pfile, int level, const char *msgid)
that is not localized, but "" is replaced with localized "stdout". */ that is not localized, but "" is replaced with localized "stdout". */
bool bool
cpp_errno_filename (cpp_reader *pfile, int level, const char *filename) cpp_errno_filename (cpp_reader *pfile, int level, const char *filename,
source_location loc)
{ {
if (filename[0] == '\0') if (filename[0] == '\0')
filename = _("stdout"); filename = _("stdout");
return cpp_error (pfile, level, "%s: %s", filename, xstrerror (errno)); return cpp_error_at (pfile, level, loc, "%s: %s", filename,
xstrerror (errno));
} }
...@@ -992,7 +992,8 @@ extern bool cpp_warning_syshdr (cpp_reader *, int, const char *msgid, ...) ...@@ -992,7 +992,8 @@ extern bool cpp_warning_syshdr (cpp_reader *, int, const char *msgid, ...)
extern bool cpp_errno (cpp_reader *, int, const char *msgid); extern bool cpp_errno (cpp_reader *, int, const char *msgid);
/* Similarly, but with "FILENAME: " instead of "MSGID: ", where /* Similarly, but with "FILENAME: " instead of "MSGID: ", where
the filename is not localized. */ the filename is not localized. */
extern bool cpp_errno_filename (cpp_reader *, int, const char *filename); extern bool cpp_errno_filename (cpp_reader *, int, const char *filename,
source_location loc);
/* Same as cpp_error, except additionally specifies a position as a /* Same as cpp_error, except additionally specifies a position as a
(translation unit) physical line and physical column. If the line is (translation unit) physical line and physical column. If the line is
...@@ -1010,6 +1011,10 @@ extern bool cpp_warning_with_line_syshdr (cpp_reader *, int, source_location, ...@@ -1010,6 +1011,10 @@ extern bool cpp_warning_with_line_syshdr (cpp_reader *, int, source_location,
unsigned, const char *msgid, ...) unsigned, const char *msgid, ...)
ATTRIBUTE_PRINTF_5; ATTRIBUTE_PRINTF_5;
extern bool cpp_error_at (cpp_reader * pfile, int level,
source_location src_loc, const char *msgid, ...)
ATTRIBUTE_PRINTF_4;
/* In lex.c */ /* In lex.c */
extern int cpp_ideq (const cpp_token *, const char *); extern int cpp_ideq (const cpp_token *, const char *);
extern void cpp_output_line (cpp_reader *, FILE *); extern void cpp_output_line (cpp_reader *, FILE *);
......
...@@ -616,6 +616,8 @@ cpp_post_options (cpp_reader *pfile) ...@@ -616,6 +616,8 @@ cpp_post_options (cpp_reader *pfile)
const char * const char *
cpp_read_main_file (cpp_reader *pfile, const char *fname) cpp_read_main_file (cpp_reader *pfile, const char *fname)
{ {
const source_location loc = 0;
if (CPP_OPTION (pfile, deps.style) != DEPS_NONE) if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
{ {
if (!pfile->deps) if (!pfile->deps)
...@@ -626,11 +628,12 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname) ...@@ -626,11 +628,12 @@ cpp_read_main_file (cpp_reader *pfile, const char *fname)
} }
pfile->main_file pfile->main_file
= _cpp_find_file (pfile, fname, &pfile->no_search_path, false, 0, false); = _cpp_find_file (pfile, fname, &pfile->no_search_path, false, 0, false,
loc);
if (_cpp_find_failed (pfile->main_file)) if (_cpp_find_failed (pfile->main_file))
return NULL; return NULL;
_cpp_stack_file (pfile, pfile->main_file, false); _cpp_stack_file (pfile, pfile->main_file, false, loc);
/* For foo.i, read the original filename foo.c now, for the benefit /* For foo.i, read the original filename foo.c now, for the benefit
of the front ends. */ of the front ends. */
......
...@@ -645,13 +645,14 @@ extern void _cpp_destroy_hashtable (cpp_reader *); ...@@ -645,13 +645,14 @@ extern void _cpp_destroy_hashtable (cpp_reader *);
/* In files.c */ /* In files.c */
typedef struct _cpp_file _cpp_file; typedef struct _cpp_file _cpp_file;
extern _cpp_file *_cpp_find_file (cpp_reader *, const char *, cpp_dir *, extern _cpp_file *_cpp_find_file (cpp_reader *, const char *, cpp_dir *,
bool, int, bool); bool, int, bool, source_location);
extern bool _cpp_find_failed (_cpp_file *); extern bool _cpp_find_failed (_cpp_file *);
extern void _cpp_mark_file_once_only (cpp_reader *, struct _cpp_file *); extern void _cpp_mark_file_once_only (cpp_reader *, struct _cpp_file *);
extern void _cpp_fake_include (cpp_reader *, const char *); extern void _cpp_fake_include (cpp_reader *, const char *);
extern bool _cpp_stack_file (cpp_reader *, _cpp_file*, bool); extern bool _cpp_stack_file (cpp_reader *, _cpp_file*, bool,
source_location);
extern bool _cpp_stack_include (cpp_reader *, const char *, int, extern bool _cpp_stack_include (cpp_reader *, const char *, int,
enum include_type); enum include_type, source_location);
extern int _cpp_compare_file_date (cpp_reader *, const char *, int); extern int _cpp_compare_file_date (cpp_reader *, const char *, int);
extern void _cpp_report_missing_guards (cpp_reader *); extern void _cpp_report_missing_guards (cpp_reader *);
extern void _cpp_init_files (cpp_reader *); extern void _cpp_init_files (cpp_reader *);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment