From patchwork Wed Aug 2 15:02:12 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 21846 Received: (qmail 24860 invoked by alias); 2 Aug 2017 15:02:44 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 24749 invoked by uid 89); 2 Aug 2017 15:02:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS, UNSUBSCRIBE_BODY autolearn=ham version=3.3.2 spammy=4477 X-HELO: gproxy10-pub.mail.unifiedlayer.com Received: from gproxy10-pub.mail.unifiedlayer.com (HELO gproxy10-pub.mail.unifiedlayer.com) (69.89.20.226) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 02 Aug 2017 15:02:41 +0000 Received: from cmgw4 (unknown [10.0.90.85]) by gproxy10.mail.unifiedlayer.com (Postfix) with ESMTP id 66D94140902 for ; Wed, 2 Aug 2017 09:02:37 -0600 (MDT) Received: from box522.bluehost.com ([74.220.219.122]) by cmgw4 with id sF2a1v00p2f2jeq01F2dR5; Wed, 02 Aug 2017 09:02:37 -0600 X-Authority-Analysis: v=2.2 cv=aJeAkf1m c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=KeKAF7QvOSUA:10 a=zstS-IiYAAAA:8 a=dPGR6xk72-kezJjcJu4A:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 174-29-39-24.hlrn.qwest.net ([174.29.39.24]:54692 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1dcvAQ-002dpC-1w; Wed, 02 Aug 2017 09:02:34 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA v3 08/23] Remove an unlink cleanup Date: Wed, 2 Aug 2017 09:02:12 -0600 Message-Id: <20170802150227.24460-9-tom@tromey.com> In-Reply-To: <20170802150227.24460-1-tom@tromey.com> References: <20170802150227.24460-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1dcvAQ-002dpC-1w X-Source-Sender: 174-29-39-24.hlrn.qwest.net (bapiya.Home) [174.29.39.24]:54692 X-Source-Auth: tom+tromey.com X-Email-Count: 9 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-Local-Domain: yes compile/compile.c had its own cleanup to unlink a file. This patch replaces this cleanup with gdb::unlinker. ChangeLog 2017-08-02 Tom Tromey * compile/compile.c (cleanup_unlink_file): Remove. (compile_to_object): Use gdb::unlinker. (eval_compile_command): Likewise. --- gdb/ChangeLog | 6 ++++++ gdb/compile/compile.c | 38 +++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 3b5286c..4796785 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,11 @@ 2017-08-02 Tom Tromey + * compile/compile.c (cleanup_unlink_file): Remove. + (compile_to_object): Use gdb::unlinker. + (eval_compile_command): Likewise. + +2017-08-02 Tom Tromey + * utils.h (make_cleanup_fclose): Remove. * utils.c (do_fclose_cleanup, make_cleanup_fclose): Remove. diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c index d8c505f..5269aaf 100644 --- a/gdb/compile/compile.c +++ b/gdb/compile/compile.c @@ -39,6 +39,8 @@ #include "osabi.h" #include "gdb_wait.h" #include "valprint.h" +#include "common/gdb_optional.h" +#include "common/gdb_unlinker.h" @@ -427,16 +429,6 @@ cleanup_compile_instance (void *arg) inst->destroy (inst); } -/* A cleanup function to unlink a file. */ - -static void -cleanup_unlink_file (void *arg) -{ - const char *filename = (const char *) arg; - - unlink (filename); -} - /* A helper function suitable for use as the "print_callback" in the compiler object. */ @@ -455,7 +447,7 @@ compile_to_object (struct command_line *cmd, const char *cmd_string, enum compile_i_scope_types scope) { struct compile_instance *compiler; - struct cleanup *cleanup, *inner_cleanup; + struct cleanup *cleanup; const struct block *expr_block; CORE_ADDR trash_pc, expr_pc; int argc; @@ -547,12 +539,15 @@ compile_to_object (struct command_line *cmd, const char *cmd_string, compile_file_names fnames = get_new_file_names (); + gdb::optional source_remover; + { gdb_file_up src = gdb_fopen_cloexec (fnames.source_file (), "w"); if (src == NULL) perror_with_name (_("Could not open source file for writing")); - inner_cleanup = make_cleanup (cleanup_unlink_file, - (void *) fnames.source_file ()); + + source_remover.emplace (fnames.source_file ()); + if (fputs (code.c_str (), src.get ()) == EOF) perror_with_name (_("Could not write to source file")); } @@ -572,7 +567,9 @@ compile_to_object (struct command_line *cmd, const char *cmd_string, fprintf_unfiltered (gdb_stdlog, "object file produced: %s\n\n", fnames.object_file ()); - discard_cleanups (inner_cleanup); + /* Keep the source file. */ + source_remover->keep (); + do_cleanups (cleanup); return fnames; @@ -594,14 +591,13 @@ void eval_compile_command (struct command_line *cmd, const char *cmd_string, enum compile_i_scope_types scope, void *scope_data) { - struct cleanup *cleanup_unlink; struct compile_module *compile_module; compile_file_names fnames = compile_to_object (cmd, cmd_string, scope); - cleanup_unlink = make_cleanup (cleanup_unlink_file, - (void *) fnames.object_file ()); - make_cleanup (cleanup_unlink_file, (void *) fnames.source_file ()); + gdb::unlinker object_remover (fnames.object_file ()); + gdb::unlinker source_remover (fnames.source_file ()); + compile_module = compile_object_load (fnames, scope, scope_data); if (compile_module == NULL) { @@ -610,7 +606,11 @@ eval_compile_command (struct command_line *cmd, const char *cmd_string, COMPILE_I_PRINT_VALUE_SCOPE, scope_data); return; } - discard_cleanups (cleanup_unlink); + + /* Keep the files. */ + source_remover.keep (); + object_remover.keep (); + compile_object_run (compile_module); }