This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH 09/22] Add selftest::read_file (..., FILE *, ...)
- From: David Malcolm <dmalcolm at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: David Malcolm <dmalcolm at redhat dot com>
- Date: Fri, 4 Aug 2017 18:04:40 -0400
- Subject: [PATCH 09/22] Add selftest::read_file (..., FILE *, ...)
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=dmalcolm at redhat dot com
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com AC204369C4
- References: <1501884293-9047-1-git-send-email-dmalcolm@redhat.com>
This patch is a hack used by the followup checkers.cc patch,
and ought to be removed in any final version of the kit.
gcc/ChangeLog:
* selftest.c (read_file): New overload.
* selftest.h (read_file): New overload.
---
gcc/selftest.c | 16 +++++++++++++---
gcc/selftest.h | 7 +++++++
2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/gcc/selftest.c b/gcc/selftest.c
index b41b9f5..a6674be 100644
--- a/gcc/selftest.c
+++ b/gcc/selftest.c
@@ -162,7 +162,19 @@ read_file (const location &loc, const char *path)
FILE *f_in = fopen (path, "r");
if (!f_in)
fail_formatted (loc, "unable to open file: %s", path);
+ char *result = read_file (loc, f_in, path);
+ fclose (f_in);
+ return result;
+}
+
+/* Read all of F_IN into memory, returning a 0-terminated buffer
+ that must be freed by the caller. F_IN is *not* closed.
+ Fail (and abort) if there are any problems, with LOC as the reported
+ location of the failure, using DESC as a description of the file. */
+char *
+read_file (const location &loc, FILE *f_in, const char *desc)
+{
/* Read content, allocating FIXME. */
char *result = NULL;
size_t total_sz = 0;
@@ -186,11 +198,9 @@ read_file (const location &loc, const char *path)
}
if (!feof (f_in))
- fail_formatted (loc, "error reading from %s: %s", path,
+ fail_formatted (loc, "error reading from %s: %s", desc,
xstrerror (errno));
- fclose (f_in);
-
/* 0-terminate the buffer. */
gcc_assert (total_sz < alloc_sz);
result[total_sz] = '\0';
diff --git a/gcc/selftest.h b/gcc/selftest.h
index e86ce38..541bb71 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -153,6 +153,13 @@ for_each_line_table_case (void (*testcase) (const line_table_case &));
extern char *read_file (const location &loc, const char *path);
+/* Read all of F_IN into memory, returning a 0-terminated buffer
+ that must be freed by the caller. F_IN is *not* closed.
+ Fail (and abort) if there are any problems, with LOC as the reported
+ location of the failure, using DESC as a description of the file. */
+
+extern char *read_file (const location &loc, FILE *infile, const char *desc);
+
/* A helper function for writing tests that interact with the
garbage collector. */
--
1.8.5.3