From patchwork Tue Jun 22 09:08:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 30628 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 17F0E27BC81; Tue, 22 Jun 2021 10:12:21 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id D291A27BC78 for ; Tue, 22 Jun 2021 10:12:20 +0100 (BST) Received: from localhost ([::1]:54592 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lvcSB-00060V-UI for patchwork@mira.cbaines.net; Tue, 22 Jun 2021 05:12:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59188) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lvcQ0-0002aB-Nv for guix-patches@gnu.org; Tue, 22 Jun 2021 05:10:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:54520) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lvcPz-0003hy-DU for guix-patches@gnu.org; Tue, 22 Jun 2021 05:10:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lvcPz-0007qj-91 for guix-patches@gnu.org; Tue, 22 Jun 2021 05:10:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#49169] [PATCH 10/11] utils: 'edit-expression' copies part of the original source map. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 22 Jun 2021 09:10:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 49169 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 49169@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 49169-submit@debbugs.gnu.org id=B49169.162435294630058 (code B ref 49169); Tue, 22 Jun 2021 09:10:03 +0000 Received: (at 49169) by debbugs.gnu.org; 22 Jun 2021 09:09:06 +0000 Received: from localhost ([127.0.0.1]:37825 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lvcP3-0007od-Qb for submit@debbugs.gnu.org; Tue, 22 Jun 2021 05:09:06 -0400 Received: from eggs.gnu.org ([209.51.188.92]:32884) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lvcOr-0007lU-IP for 49169@debbugs.gnu.org; Tue, 22 Jun 2021 05:08:54 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]:52830) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lvcOm-0002rn-8x; Tue, 22 Jun 2021 05:08:48 -0400 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=49370 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lvcOm-0000B8-1j; Tue, 22 Jun 2021 05:08:48 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Tue, 22 Jun 2021 11:08:29 +0200 Message-Id: <20210622090830.15561-10-ludo@gnu.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210622090830.15561-1-ludo@gnu.org> References: <20210622090830.15561-1-ludo@gnu.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/utils.scm (source-location-key/stamp): New procedure. (go-to-location): Use it. (move-source-location-map!): New procedure. (edit-expression): Call it. --- guix/utils.scm | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/guix/utils.scm b/guix/utils.scm index e6d0761679..65d709a01f 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -34,6 +34,7 @@ #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) #:use-module (srfi srfi-39) + #:use-module (srfi srfi-71) #:use-module (ice-9 ftw) #:use-module (rnrs io ports) ;need 'port-position' etc. #:use-module ((rnrs bytevectors) #:select (bytevector-u8-set!)) @@ -344,14 +345,20 @@ a list of command-line arguments passed to the compression program." ;; 'go-to-location'. (make-hash-table)) -(define (go-to-location port line column) +(define (source-location-key/stamp stat) + "Return two values: the key for STAT in %SOURCE-LOCATION-MAP, and a stamp +used to invalidate corresponding entries." + (let ((key (list (stat:ino stat) (stat:dev stat))) + (stamp (list (stat:mtime stat) (stat:mtimensec stat) + (stat:size stat)))) + (values key stamp))) + +(define* (go-to-location port line column) "Jump to LINE and COLUMN (both one-indexed) in PORT. Maintain a source location map such that this can boil down to seek(2) and a few read(2) calls, which can drastically speed up repetitive operations on large files." (let* ((stat (stat port)) - (key (list (stat:ino stat) (stat:dev stat))) - (stamp (list (stat:mtime stat) (stat:mtimensec stat) - (stat:size stat))) + (key stamp (source-location-key/stamp stat)) ;; Look for an up-to-date source map for KEY. The map is a vlist ;; where each entry gives the byte offset of the beginning of a line: @@ -398,6 +405,20 @@ which can drastically speed up repetitive operations on large files." (set-port-line! port (- line 1)) (set-port-column! port (- column 1)))) +(define (move-source-location-map! source target line) + "Move the source location map from SOURCE up to LINE to TARGET. SOURCE and +TARGET must be stat buffers as returned by 'stat'." + (let* ((source-key (source-location-key/stamp source)) + (target-key target-stamp (source-location-key/stamp target))) + (match (hash-ref %source-location-map source-key) + (#f #t) + ((_ ... source-map) + ;; Strip the source map and update the associated stamp. + (let ((source-map (vlist-take source-map (max line 1)))) + (hash-remove! %source-location-map source-key) + (hash-set! %source-location-map target-key + `(,@target-stamp ,source-map))))))) + (define* (edit-expression source-properties proc #:key (encoding "UTF-8")) "Edit the expression specified by SOURCE-PROPERTIES using PROC, which should be a procedure that takes the original expression in string and returns a new @@ -435,7 +456,13 @@ This procedure returns #t on success." ;; post-bv maybe the end-of-file object. (when (not (eof-object? post-bv)) (put-bytevector out post-bv)) - #t))))))))) + #t)) + + ;; Due to 'with-atomic-file-output', IN and FILE no longer share + ;; the same inode, but we can reassign the source map up to LINE + ;; to the new file. + (move-source-location-map! (stat in) (stat file) + (+ 1 line))))))))) ;;;