#include "cpplib.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. */
ATTRIBUTE_FPTR_PRINTF(4,0)
const char *msgid, va_list *ap)
{
source_location src_loc;
- bool ret;
if (CPP_OPTION (pfile, traditional))
{
{
src_loc = pfile->cur_token[-1].src_loc;
}
-
- 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;
+ return cpp_diagnostic_at (pfile, level, reason, src_loc, msgid, ap);
}
/* Print a warning or error, depending on the value of LEVEL. */
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
information from errno. */
that is not localized, but "" is replaced with localized "stdout". */
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')
filename = _("stdout");
- return cpp_error (pfile, level, "%s: %s", filename, xstrerror (errno));
+ return cpp_error_at (pfile, level, loc, "%s: %s", filename,
+ xstrerror (errno));
}
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);
/* 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;
if (file->err_no != ENOENT)
{
- open_file_failed (pfile, file, 0);
+ open_file_failed (pfile, file, 0, loc);
return true;
}
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;
/* 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,
/* 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;
return NULL;
}
else
- open_file_failed (pfile, file, angle_brackets);
+ open_file_failed (pfile, file, angle_brackets, loc);
break;
}
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;
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;
}
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;
}
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,
/* 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)
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;
}
/* 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;
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
/* 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,
/* 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)
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;
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;
&& 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
/* 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);
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
{
|| 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);
}
}
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
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;
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
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
if (!open_file (f))
{
- open_file_failed (pfile, f, 0);
+ open_file_failed (pfile, f, 0, 0);
free (result);
return false;
}
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;
}