From patchwork Sat Oct 14 12:23:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Buclaw X-Patchwork-Id: 1848795 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gdcproject.org header.i=@gdcproject.org header.a=rsa-sha256 header.s=MBO0001 header.b=orxQv3Ps; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4S72cN1zmZz1yp0 for ; Sat, 14 Oct 2023 23:24:14 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 802E23857736 for ; Sat, 14 Oct 2023 12:24:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) by sourceware.org (Postfix) with ESMTPS id 7AA993858C60 for ; Sat, 14 Oct 2023 12:23:58 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7AA993858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=80.241.56.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697286240; cv=none; b=iob49nhJXMsSvE9sAFP8ZvMhrIGnetpeOkpkPj2pR4HDLQ2vR0Ap1xGAf5rQvPE6MzBWNdAx5xCxLKe58OhFd9AOARtf9u+hGxNmT7voRIS1dq0VlcZT8GnoPhUtEALjdBrpS3+W6cFORr4sDhg9s+lrdMPP9nkEJkUnwmnT4P0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697286240; c=relaxed/simple; bh=+RapepkhEcHNub1oFdrtG+xUHs6+ZAQf24YVPndIzGg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=MdVF1/jIr49LTi1Nx4m+XGqtxa6+3GPtAoh6T9vH9AiaRjj4iUuJaq+k077SAuijwZD5aBJwulZzYyxnU1KiEfFQkShgXW/gjevJu9cLA1fO/TEet27j6EvuiQUiWBR0gcdc7LutGWxVZl4MofffjKZ8J/gqIU8FBc8bgnyWQQM= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7AA993858C60 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gdcproject.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gdcproject.org Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4S72bv4LMjz9slN; Sat, 14 Oct 2023 14:23:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gdcproject.org; s=MBO0001; t=1697286235; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=amrz/TMmgy0v7nnkdWauk9EtD8+G94o2hqIf1ezCPzo=; b=orxQv3PsY5tBX6MpAx9SHFm+N2WJmj5PHE7bCW/jtM54WwJ/Q6r1BwLJ3W7Kp/Q3uT+ub8 Ce8MWI0YjKUjRkpXt5TnN59as7cMbyT5gAycV3u1WunsFkU5SaxJJ5jlpPyWkvwa/jqJCf VIPYuKWj2bvQgHoqoOgpUmlnuH/f8mhGZQXJtOkzfV1j7FFmEGsTrcsub9M6tVtDE17vkD vPFSooHypy5NsvG70C7zLUvgqYXr5Z2pflWodtaWDiJav7nOsPwjDys0CRcIfFbO0taRU1 pFlAGJHG0sdTaQ4Ok4Ywghf4GkHT4gBkaeSGUyUFBTM1VpZXsd/aOUd+xxdgAA== From: Iain Buclaw To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Reduce code duplication of writing generated files. Date: Sat, 14 Oct 2023 14:23:52 +0200 Message-Id: <20231014122352.1665945-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4S72bv4LMjz9slN X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, This is a small refactoring ahead of the next merge from upstream, where a few more front-end routines will stop doing the file handling themselves. Bootstrapped and regression tested on x86_64-linux-gnu/-m32, committed to mainline. Regards, Iain. --- gcc/d/ChangeLog: * d-lang.cc (d_write_file): New function. (d_parse_file): Reduce code duplication. --- gcc/d/d-lang.cc | 91 ++++++++++++++++--------------------------------- 1 file changed, 29 insertions(+), 62 deletions(-) diff --git a/gcc/d/d-lang.cc b/gcc/d/d-lang.cc index 7dddcf5ac91..f290acf494b 100644 --- a/gcc/d/d-lang.cc +++ b/gcc/d/d-lang.cc @@ -978,6 +978,30 @@ d_add_builtin_module (Module *m) builtin_modules.push (m); } +/* Writes to FILENAME. DATA is the full content of the file to be written. */ + +static void +d_write_file (const char *filename, const char *data) +{ + FILE *stream; + + if (filename && (filename[0] != '-' || filename[1] != '\0')) + stream = fopen (filename, "w"); + else + stream = stdout; + + if (!stream) + { + error ("unable to open %s for writing: %m", filename); + return; + } + + fprintf (stream, "%s", data); + + if (stream != stdout && (ferror (stream) || fclose (stream))) + error ("writing output file %s: %m", filename); +} + /* Implements the lang_hooks.parse_file routine for language D. */ static void @@ -1264,8 +1288,6 @@ d_parse_file (void) if (d_option.deps) { obstack buffer; - FILE *deps_stream; - gcc_obstack_init (&buffer); for (size_t i = 0; i < modules.length; i++) @@ -1275,27 +1297,8 @@ d_parse_file (void) if (d_option.deps_filename_user) d_option.deps_filename = d_option.deps_filename_user; - if (d_option.deps_filename) - { - deps_stream = fopen (d_option.deps_filename, "w"); - if (!deps_stream) - { - fatal_error (input_location, "opening dependency file %s: %m", - d_option.deps_filename); - goto had_errors; - } - } - else - deps_stream = stdout; - - fprintf (deps_stream, "%s", (char *) obstack_finish (&buffer)); - - if (deps_stream != stdout - && (ferror (deps_stream) || fclose (deps_stream))) - { - fatal_error (input_location, "closing dependency file %s: %m", - d_option.deps_filename); - } + d_write_file (d_option.deps_filename, + (char *) obstack_finish (&buffer)); } if (global.params.vtemplates) @@ -1306,29 +1309,7 @@ d_parse_file (void) { OutBuffer buf; json_generate (&buf, &modules); - - const char *name = global.params.json.name.ptr; - FILE *json_stream; - - if (name && (name[0] != '-' || name[1] != '\0')) - { - const char *nameext - = FileName::defaultExt (name, json_ext.ptr); - json_stream = fopen (nameext, "w"); - if (!json_stream) - { - fatal_error (input_location, "opening json file %s: %m", nameext); - goto had_errors; - } - } - else - json_stream = stdout; - - fprintf (json_stream, "%s", buf.peekChars ()); - - if (json_stream != stdout - && (ferror (json_stream) || fclose (json_stream))) - fatal_error (input_location, "closing json file %s: %m", name); + d_write_file (global.params.json.name.ptr, buf.peekChars ()); } /* Generate Ddoc files. */ @@ -1391,22 +1372,8 @@ d_parse_file (void) /* We want to write the mixin expansion file also on error. */ if (global.params.mixinOut.doOutput) { - FILE *mixin_stream = fopen (global.params.mixinOut.name.ptr, "w"); - - if (mixin_stream) - { - OutBuffer *buf = global.params.mixinOut.buffer; - fprintf (mixin_stream, "%s", buf->peekChars ()); - - if (ferror (mixin_stream) || fclose (mixin_stream)) - fatal_error (input_location, "closing mixin file %s: %m", - global.params.mixinOut.name.ptr); - } - else - { - fatal_error (input_location, "opening mixin file %s: %m", - global.params.mixinOut.name.ptr); - } + d_write_file (global.params.mixinOut.name.ptr, + global.params.mixinOut.buffer->peekChars ()); } /* Remove generated .di files on error. */