From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 37005 invoked by alias); 4 Aug 2017 21:36:53 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 36800 invoked by uid 89); 4 Aug 2017 21:36:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:2242 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 04 Aug 2017 21:36:51 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AC204369C4 for ; Fri, 4 Aug 2017 21:30:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com AC204369C4 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@redhat.com Received: from c64.redhat.com (ovpn-112-14.phx2.redhat.com [10.3.112.14]) by smtp.corp.redhat.com (Postfix) with ESMTP id D5CD561F25; Fri, 4 Aug 2017 21:30:20 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 09/22] Add selftest::read_file (..., FILE *, ...) Date: Fri, 04 Aug 2017 21:36:00 -0000 Message-Id: <1501884293-9047-10-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1501884293-9047-1-git-send-email-dmalcolm@redhat.com> References: <1501884293-9047-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes X-SW-Source: 2017-08/txt/msg00425.txt.bz2 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