aboutsummaryrefslogtreecommitdiff
path: root/libcpp/files.c
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-06-22 15:29:21 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-06-22 15:29:21 +0000
commitac81cf0b2bf5efdd716d10d1c218eb5a17e1035b (patch)
tree895845f1cef79ed1f8c6a341ccf3973a27e22944 /libcpp/files.c
parent1a4f11c88ae761d4c618e540e07e4e32e85850d1 (diff)
downloadgcc-ac81cf0b2bf5efdd716d10d1c218eb5a17e1035b.zip
gcc-ac81cf0b2bf5efdd716d10d1c218eb5a17e1035b.tar.gz
gcc-ac81cf0b2bf5efdd716d10d1c218eb5a17e1035b.tar.bz2
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
Diffstat (limited to 'libcpp/files.c')
-rw-r--r--libcpp/files.c105
1 files changed, 63 insertions, 42 deletions
diff --git a/libcpp/files.c b/libcpp/files.c
index 5aa8d1a..c8bb637 100644
--- a/libcpp/files.c
+++ b/libcpp/files.c
@@ -171,14 +171,18 @@ static bool open_file (_cpp_file *file);
static bool pch_open_file (cpp_reader *pfile, _cpp_file *file,
bool *invalid_pch);
static bool find_file_in_dir (cpp_reader *pfile, _cpp_file *file,
- bool *invalid_pch);
-static bool read_file_guts (cpp_reader *pfile, _cpp_file *file);
-static bool read_file (cpp_reader *pfile, _cpp_file *file);
-static bool should_stack_file (cpp_reader *, _cpp_file *file, bool import);
+ bool *invalid_pch, source_location loc);
+static bool read_file_guts (cpp_reader *pfile, _cpp_file *file,
+ source_location loc);
+static bool read_file (cpp_reader *pfile, _cpp_file *file,
+ source_location loc);
+static bool should_stack_file (cpp_reader *, _cpp_file *file, bool import,
+ source_location loc);
static struct cpp_dir *search_path_head (cpp_reader *, const char *fname,
int angle_brackets, enum include_type);
static const char *dir_name_of_file (_cpp_file *file);
-static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int);
+static void open_file_failed (cpp_reader *pfile, _cpp_file *file, int,
+ source_location);
static struct cpp_file_hash_entry *search_cache (struct cpp_file_hash_entry *head,
const cpp_dir *start_dir);
static _cpp_file *make_cpp_file (cpp_reader *, cpp_dir *, const char *fname);
@@ -368,10 +372,12 @@ maybe_shorter_path (const char * file)
/* Try to open the path FILE->name appended to FILE->dir. This is
where remap and PCH intercept the file lookup process. Return true
if the file was found, whether or not the open was successful.
- Set *INVALID_PCH to true if a PCH file is found but wasn't valid. */
+ Set *INVALID_PCH to true if a PCH file is found but wasn't valid.
+ Use LOC when emitting any diagnostics. */
static bool
-find_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch)
+find_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch,
+ source_location loc)
{
char *path;
@@ -424,7 +430,7 @@ find_file_in_dir (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch)
if (file->err_no != ENOENT)
{
- open_file_failed (pfile, file, 0);
+ open_file_failed (pfile, file, 0, loc);
return true;
}
@@ -495,10 +501,13 @@ _cpp_find_failed (_cpp_file *file)
If IMPLICIT_PREINCLUDE then it is OK for the file to be missing.
If present, it is OK for a precompiled header to be included after
it.
-*/
+
+ Use LOC as the location for any errors. */
+
_cpp_file *
_cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
- bool fake, int angle_brackets, bool implicit_preinclude)
+ bool fake, int angle_brackets, bool implicit_preinclude,
+ source_location loc)
{
struct cpp_file_hash_entry *entry;
void **hash_slot;
@@ -510,7 +519,7 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
/* Ensure we get no confusion between cached files and directories. */
if (start_dir == NULL)
- cpp_error (pfile, CPP_DL_ICE, "NULL directory in find_file");
+ cpp_error_at (pfile, CPP_DL_ICE, loc, "NULL directory in find_file");
hash_slot
= htab_find_slot_with_hash (pfile->file_hash, fname,
@@ -530,7 +539,7 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
/* Try each path in the include chain. */
for (; !fake ;)
{
- if (find_file_in_dir (pfile, file, &invalid_pch))
+ if (find_file_in_dir (pfile, file, &invalid_pch, loc))
break;
file->dir = file->dir->next;
@@ -579,7 +588,7 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
return NULL;
}
else
- open_file_failed (pfile, file, angle_brackets);
+ open_file_failed (pfile, file, angle_brackets, loc);
break;
}
@@ -658,9 +667,11 @@ _cpp_find_file (cpp_reader *pfile, const char *fname, cpp_dir *start_dir,
except for plain files and block devices, since there is no
reliable portable way of doing this.
+ Use LOC for any diagnostics.
+
FIXME: Flush file cache and try again if we run out of memory. */
static bool
-read_file_guts (cpp_reader *pfile, _cpp_file *file)
+read_file_guts (cpp_reader *pfile, _cpp_file *file, source_location loc)
{
ssize_t size, total, count;
uchar *buf;
@@ -668,7 +679,8 @@ read_file_guts (cpp_reader *pfile, _cpp_file *file)
if (S_ISBLK (file->st.st_mode))
{
- cpp_error (pfile, CPP_DL_ERROR, "%s is a block device", file->path);
+ cpp_error_at (pfile, CPP_DL_ERROR, loc,
+ "%s is a block device", file->path);
return false;
}
@@ -685,7 +697,8 @@ read_file_guts (cpp_reader *pfile, _cpp_file *file)
does not bite us. */
if (file->st.st_size > INTTYPE_MAXIMUM (ssize_t))
{
- cpp_error (pfile, CPP_DL_ERROR, "%s is too large", file->path);
+ cpp_error_at (pfile, CPP_DL_ERROR, loc,
+ "%s is too large", file->path);
return false;
}
@@ -718,13 +731,13 @@ read_file_guts (cpp_reader *pfile, _cpp_file *file)
if (count < 0)
{
- cpp_errno_filename (pfile, CPP_DL_ERROR, file->path);
+ cpp_errno_filename (pfile, CPP_DL_ERROR, file->path, loc);
free (buf);
return false;
}
if (regular && total != size && STAT_SIZE_RELIABLE (file->st))
- cpp_error (pfile, CPP_DL_WARNING,
+ cpp_error_at (pfile, CPP_DL_WARNING, loc,
"%s is shorter than expected", file->path);
file->buffer = _cpp_convert_input (pfile,
@@ -739,9 +752,10 @@ read_file_guts (cpp_reader *pfile, _cpp_file *file)
/* Convenience wrapper around read_file_guts that opens the file if
necessary and closes the file descriptor after reading. FILE must
- have been passed through find_file() at some stage. */
+ have been passed through find_file() at some stage. Use LOC for
+ any diagnostics. */
static bool
-read_file (cpp_reader *pfile, _cpp_file *file)
+read_file (cpp_reader *pfile, _cpp_file *file, source_location loc)
{
/* If we already have its contents in memory, succeed immediately. */
if (file->buffer_valid)
@@ -753,11 +767,11 @@ read_file (cpp_reader *pfile, _cpp_file *file)
if (file->fd == -1 && !open_file (file))
{
- open_file_failed (pfile, file, 0);
+ open_file_failed (pfile, file, 0, loc);
return false;
}
- file->dont_read = !read_file_guts (pfile, file);
+ file->dont_read = !read_file_guts (pfile, file, loc);
close (file->fd);
file->fd = -1;
@@ -765,9 +779,11 @@ read_file (cpp_reader *pfile, _cpp_file *file)
}
/* Returns TRUE if FILE's contents have been successfully placed in
- FILE->buffer and the file should be stacked, otherwise false. */
+ FILE->buffer and the file should be stacked, otherwise false.
+ Use LOC for any diagnostics. */
static bool
-should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
+should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import,
+ source_location loc)
{
_cpp_file *f;
@@ -802,7 +818,7 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
return false;
}
- if (!read_file (pfile, file))
+ if (!read_file (pfile, file, loc))
return false;
/* Check the file against the PCH file. This is done before
@@ -849,7 +865,7 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
/* The file is not stacked anymore. We can reuse it. */
ref_file = f;
- same_file_p = read_file (pfile, ref_file)
+ same_file_p = read_file (pfile, ref_file, loc)
/* Size might have changed in read_file(). */
&& ref_file->st.st_size == file->st.st_size
&& !memcmp (ref_file->buffer,
@@ -873,14 +889,15 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
/* Place the file referenced by FILE into a new buffer on the buffer
stack if possible. IMPORT is true if this stacking attempt is
because of a #import directive. Returns true if a buffer is
- stacked. */
+ stacked. Use LOC for any diagnostics. */
bool
-_cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
+_cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import,
+ source_location loc)
{
cpp_buffer *buffer;
int sysp;
- if (!should_stack_file (pfile, file, import))
+ if (!should_stack_file (pfile, file, import, loc))
return false;
if (pfile->buffer == NULL || file->dir == NULL)
@@ -990,7 +1007,7 @@ dir_name_of_file (_cpp_file *file)
Returns true if a buffer was stacked. */
bool
_cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
- enum include_type type)
+ enum include_type type, source_location loc)
{
struct cpp_dir *dir;
_cpp_file *file;
@@ -1013,7 +1030,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
return false;
file = _cpp_find_file (pfile, fname, dir, false, angle_brackets,
- type == IT_DEFAULT);
+ type == IT_DEFAULT, loc);
if (type == IT_DEFAULT && file == NULL)
return false;
@@ -1029,7 +1046,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
&& type != IT_CMDLINE && type != IT_DEFAULT)
pfile->line_table->highest_location--;
- stacked = _cpp_stack_file (pfile, file, type == IT_IMPORT);
+ stacked = _cpp_stack_file (pfile, file, type == IT_IMPORT, loc);
if (!stacked)
/* _cpp_stack_file didn't stack the file, so let's rollback the
@@ -1041,7 +1058,8 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
/* Could not open FILE. The complication is dependency output. */
static void
-open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets)
+open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets,
+ source_location loc)
{
int sysp = pfile->line_table->highest_line > 1 && pfile->buffer ? pfile->buffer->sysp : 0;
bool print_dep = CPP_OPTION (pfile, deps.style) > (angle_brackets || !!sysp);
@@ -1057,7 +1075,8 @@ open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets)
being used, we must also flag an error. */
if (CPP_OPTION (pfile, deps.need_preprocessor_output))
cpp_errno_filename (pfile, CPP_DL_FATAL,
- file->path ? file->path : file->name);
+ file->path ? file->path : file->name,
+ loc);
}
else
{
@@ -1072,10 +1091,12 @@ open_file_failed (cpp_reader *pfile, _cpp_file *file, int angle_brackets)
|| print_dep
|| CPP_OPTION (pfile, deps.need_preprocessor_output))
cpp_errno_filename (pfile, CPP_DL_FATAL,
- file->path ? file->path : file->name);
+ file->path ? file->path : file->name,
+ loc);
else
cpp_errno_filename (pfile, CPP_DL_WARNING,
- file->path ? file->path : file->name);
+ file->path ? file->path : file->name,
+ loc);
}
}
@@ -1323,7 +1344,7 @@ cpp_clear_file_cache (cpp_reader *pfile)
void
_cpp_fake_include (cpp_reader *pfile, const char *fname)
{
- _cpp_find_file (pfile, fname, pfile->buffer->file->dir, true, 0, false);
+ _cpp_find_file (pfile, fname, pfile->buffer->file->dir, true, 0, false, 0);
}
/* Not everyone who wants to set system-header-ness on a buffer can
@@ -1441,7 +1462,7 @@ _cpp_compare_file_date (cpp_reader *pfile, const char *fname,
if (!dir)
return -1;
- file = _cpp_find_file (pfile, fname, dir, false, angle_brackets, false);
+ file = _cpp_find_file (pfile, fname, dir, false, angle_brackets, false, 0);
if (file->err_no)
return -1;
@@ -1459,7 +1480,7 @@ _cpp_compare_file_date (cpp_reader *pfile, const char *fname,
bool
cpp_push_include (cpp_reader *pfile, const char *fname)
{
- return _cpp_stack_include (pfile, fname, false, IT_CMDLINE);
+ return _cpp_stack_include (pfile, fname, false, IT_CMDLINE, 0);
}
/* Pushes the given file, implicitly included at the start of a
@@ -1468,7 +1489,7 @@ cpp_push_include (cpp_reader *pfile, const char *fname)
bool
cpp_push_default_include (cpp_reader *pfile, const char *fname)
{
- return _cpp_stack_include (pfile, fname, true, IT_DEFAULT);
+ return _cpp_stack_include (pfile, fname, true, IT_DEFAULT, 0);
}
/* Do appropriate cleanup when a file INC's buffer is popped off the
@@ -1853,7 +1874,7 @@ _cpp_save_file_entries (cpp_reader *pfile, FILE *fp)
if (!open_file (f))
{
- open_file_failed (pfile, f, 0);
+ open_file_failed (pfile, f, 0, 0);
free (result);
return false;
}
@@ -1980,7 +2001,7 @@ _cpp_has_header (cpp_reader *pfile, const char *fname, int angle_brackets,
cpp_dir *start_dir = search_path_head (pfile, fname, angle_brackets, type);
_cpp_file *file = _cpp_find_file (pfile, fname, start_dir,
/*fake=*/false, angle_brackets,
- /*implicit_preinclude=*/false);
+ /*implicit_preinclude=*/false, 0);
return file->err_no != ENOENT;
}