From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 126748 invoked by alias); 5 Oct 2016 15:44:38 -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 126656 invoked by uid 89); 5 Oct 2016 15:44:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.5 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=1516, kit 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; Wed, 05 Oct 2016 15:44:36 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6142381255 for ; Wed, 5 Oct 2016 15:44:35 +0000 (UTC) Received: from c64.redhat.com (vpn-236-9.phx2.redhat.com [10.3.236.9]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u95FiV5K027350; Wed, 5 Oct 2016 11:44:34 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 02/16] (approved) Add selftest::read_file Date: Wed, 05 Oct 2016 15:45:00 -0000 Message-Id: <1475684110-2521-3-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1475684110-2521-1-git-send-email-dmalcolm@redhat.com> References: <1475684110-2521-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes X-SW-Source: 2016-10/txt/msg00272.txt.bz2 Jeff: "Seems reasonable. Install when you have a need." This is used later in the kit by the selftests for final.c gcc/ChangeLog: * selftest.c (selftest::read_file): New function. (selftest::test_read_file): New function. (selftest::selftest_c_tests): Call test_read_file. * selftest.h (selftest::read_file): New decl. --- gcc/selftest.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gcc/selftest.h | 7 +++++++ 2 files changed, 67 insertions(+) diff --git a/gcc/selftest.c b/gcc/selftest.c index 0db69d2..cf7031f 100644 --- a/gcc/selftest.c +++ b/gcc/selftest.c @@ -151,6 +151,53 @@ temp_source_file::temp_source_file (const location &loc, fclose (out); } +/* Read the contents of PATH into memory, returning a 0-terminated buffer + that must be freed by the caller. + Fail (and abort) if there are any problems, with LOC as the reported + location of the failure. */ + +char * +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); + + /* Read content, allocating FIXME. */ + char *result = NULL; + size_t total_sz = 0; + size_t alloc_sz = 0; + char buf[4096]; + size_t iter_sz_in; + + while ( (iter_sz_in = fread (buf, 1, sizeof (buf), f_in)) ) + { + gcc_assert (alloc_sz >= total_sz); + size_t old_total_sz = total_sz; + total_sz += iter_sz_in; + /* Allow 1 extra byte for 0-termination. */ + if (alloc_sz < (total_sz + 1)) + { + size_t new_alloc_sz = alloc_sz ? alloc_sz * 2: total_sz + 1; + result = (char *)xrealloc (result, new_alloc_sz); + alloc_sz = new_alloc_sz; + } + memcpy (result + old_total_sz, buf, iter_sz_in); + } + + if (!feof (f_in)) + fail_formatted (loc, "error reading from %s: %s", path, + xstrerror (errno)); + + fclose (f_in); + + /* 0-terminate the buffer. */ + gcc_assert (total_sz < alloc_sz); + result[total_sz] = '\0'; + + return result; +} + /* Selftests for the selftest system itself. */ /* Sanity-check the ASSERT_ macros with various passing cases. */ @@ -181,6 +228,18 @@ test_named_temp_file () fclose (f); } +/* Verify read_file (and also temp_source_file). */ + +static void +test_read_file () +{ + temp_source_file t (SELFTEST_LOCATION, "test1.s", + "\tjmp\t.L2\n"); + char *buf = read_file (SELFTEST_LOCATION, t.get_filename ()); + ASSERT_STREQ ("\tjmp\t.L2\n", buf); + free (buf); +} + /* Run all of the selftests within this file. */ void @@ -188,6 +247,7 @@ selftest_c_tests () { test_assertions (); test_named_temp_file (); + test_read_file (); } } // namespace selftest diff --git a/gcc/selftest.h b/gcc/selftest.h index 3938560..e5f5c60 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -146,6 +146,13 @@ class line_table_test extern void for_each_line_table_case (void (*testcase) (const line_table_case &)); +/* Read the contents of PATH into memory, returning a 0-terminated buffer + that must be freed by the caller. + Fail (and abort) if there are any problems, with LOC as the reported + location of the failure. */ + +extern char *read_file (const location &loc, const char *path); + /* Declarations for specific families of tests (by source file), in alphabetical order. */ extern void bitmap_c_tests (); -- 1.8.5.3