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: 152939 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2451811vqb; Sat, 14 Oct 2023 05:24:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHNHyySQl71ofchx8SRNFEzVDaXKs8RSkFIBodprWs4uoyYfVEmpKNRx6Fv8DNMW9q9fIsu X-Received: by 2002:a05:6102:34f6:b0:455:c426:1b22 with SMTP id bi22-20020a05610234f600b00455c4261b22mr28692563vsb.16.1697286263106; Sat, 14 Oct 2023 05:24:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697286263; cv=pass; d=google.com; s=arc-20160816; b=O0mzG60Oo2VAkvY4yywop2CGrKOBP2KoJCGJoGylHgzfDlUc3sDMI548vIqyMsM+SL bpbqdInFPkPOO0Wg6WEc0uBx5QwvJEFGA65GdOzZfbznvTiRQdlj/WEwIwoojsYsriSW nn3XTWAiHeJpZc6glas3NxXPU0LQFQE9WIWczZtpeOqWYPcqG+RUIE0cYL0Kd2GfjD5s pRHFEGRRkGI1rpHSOFj/xNinbv23YfNjHl9mJQXbrcmIbZ6jA+tDSOZ0n/pTruZbOu2n hx22VV5ssL+Y17dMsYIFe1l83CAev+3p/DIXMX30z+V8pEGmQ6zbhxbxHR5upM5oaoOt hUEQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:arc-filter:delivered-to; bh=amrz/TMmgy0v7nnkdWauk9EtD8+G94o2hqIf1ezCPzo=; fh=D2tRalIjW6KCgVuqwFHStdMb9uqe8HSQc2WEvRJ6uLE=; b=xp/aLomadfRP4jLmXRuJl7cvIA1obsZJUCbcZaqlZpDWpY5/0QHgAOTlGVAvIvZAuH mccJevdgKkM9KqUC5bempdKGKh4L0v6qwdCrtemrFuoBwBsnhCsLdJp1OemS+uwXa6RU 3rcbTrGPczUxzy74BUCH6sSMNCcXOMrV4VbeviuJBfzUwwaKjB3Elf41GIxoSMW4Xv5Z dq6RvmfnWGTQOxmeORnbSVvefdDYMLt3OfNoAIY/vW/LSS5BaDmSfPsD7zJROefjL9E6 n42YzLNCGIttYp/eTi5CFHmjVedCzH+CWvs9Sa92amFHitzESbu6UXL1WYcM9j3+qrt6 j1GA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gdcproject.org header.s=MBO0001 header.b=orxQv3Ps; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gdcproject.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id f13-20020a05622a104d00b004032d91e8b7si2741959qte.295.2023.10.14.05.24.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Oct 2023 05:24:23 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gdcproject.org header.s=MBO0001 header.b=orxQv3Ps; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=gdcproject.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB5033858401 for ; Sat, 14 Oct 2023 12:24:22 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779733640296763071 X-GMAIL-MSGID: 1779733640296763071 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. */