From patchwork Thu Nov 3 19:59:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 15153 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp738947wru; Thu, 3 Nov 2022 12:59:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5d/ccHnblCr7gECuFvDPIyJ3Iy79O7Uoflry8H9T8wJLHjhGQQRqlX5gBv6pShV6rKAeHV X-Received: by 2002:a17:906:6a1b:b0:7ad:ba1e:879b with SMTP id qw27-20020a1709066a1b00b007adba1e879bmr27626845ejc.311.1667505593123; Thu, 03 Nov 2022 12:59:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667505593; cv=none; d=google.com; s=arc-20160816; b=mBrNej9Ejz83BpZjm8DGBub10myDMrAOPIvdvjN8gGijc7vcLYF1iEm6jziIFerz0Q dfqUS7mRxyUDTf/5LxljS4RX7zPYrzpbsFF2gsAI1pP9uJs1C8+YVQzUty0hKofi0F12 LJlddnWcPlcg8pevMbNb+hoFSF8E06HESgHgHzpc6CoU9SXhUXrlI1KIgvYChXke5oVM Bstp2vlXrra+c1RnA4sOgsU5LowFA5CikTNT4B6NaXTMW9e3o6CPGMh+3G+9roh5QoqW NkSxW4htZyvlfigabrxKy48rKEzK+d5kuYkEwrK2ZJJRjGN+mZ/Ka9uPX6ffIVqA1n5q FD/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=zQ4afx7uaOwPvv0clfFTKyLH/qazzhJ9n2JC4H+dZhM=; b=cb3UfyUpQbCJQHL4m4fTiFehKkp762WJy/O+6aSKqRO54A8I+9sYmy5PK8R1GuUKC7 3oD5m61WpYKfrKx/HJyfQ2tBrXQK0+9OnFIh2mRKsHya2Qz0wnAIvQ4Us2uozc+wa8AK XtqILHXr30VCYOiErVypfjW2GiHei9JCcrNeLxoZtzXg4OWFTZEu8PTaIx6op4hCDJ4l RF3xyXSIr+szU6LRCkfr5WLO7iRyWW85zHuCnb1+MyU3bs94ymMkZzBFTiqxtM0D0u8f ENDiKm+ziUAsEFoNgU00f0L7OBCu7/3J370slGUXn1PitIt19nxJXp8A5S0NctjsKoDZ avxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=asSoCmha; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id du15-20020a17090772cf00b0078e54eda758si2607707ejc.611.2022.11.03.12.59.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 12:59:53 -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=@gcc.gnu.org header.s=default header.b=asSoCmha; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DB673385842A for ; Thu, 3 Nov 2022 19:59:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB673385842A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667505591; bh=zQ4afx7uaOwPvv0clfFTKyLH/qazzhJ9n2JC4H+dZhM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=asSoCmhassHn8SrHrXhPX2honvCWp+ZKbJWTisHXNs3epuTyiADx4KkDB2Z5RP3B3 h10dNSrSlMy2YDyV9GNt/ztLBf/wxF/WZkWIS5jm5t3LPAQ+HBPx+URqq7xcl1fcFM KqSzjuQEKtUqPsRpEHpDgeeIChkbHt8gycl0YiYA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 694F33858D1E for ; Thu, 3 Nov 2022 19:59:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 694F33858D1E Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-231-r4rfpr3iMNqQvaGkQvqDnQ-1; Thu, 03 Nov 2022 15:59:06 -0400 X-MC-Unique: r4rfpr3iMNqQvaGkQvqDnQ-1 Received: by mail-qt1-f199.google.com with SMTP id gc12-20020a05622a59cc00b003a5444280e1so2555042qtb.13 for ; Thu, 03 Nov 2022 12:59:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=zQ4afx7uaOwPvv0clfFTKyLH/qazzhJ9n2JC4H+dZhM=; b=FbiSCojW2z02CGLLDqHK0XD56c7LkLS5NXbo6k0oLxIpFURJhy+OEKTzazlpoM/NIf frS0XGpI99ywfLSn45UZvt5IMY8wb3i4zMVVhnVi527BM4i1ouAzCE5b8hQSjijBmd9Q LBaS5dUCHRxB9w2LdTF84awb8O6V5MNqNmAMMDbNQW+fTIGaMbSiZmD9/p2vONZSifA9 iUPUYXPXGfzj1OH8kNUv5WnLg8jfqrAX8WxMZWvVPI+pX+iVH53IcA0ug3IucYed5Bwm XWb7yv0xH9nRCC3lf/k7RNr9wsLHjgLxCiBW03VjJteYlJjv7+ZsQK+jGLKbVEvrMsMO 2ndA== X-Gm-Message-State: ACrzQf1Fd+3hkIh7ECjLgiXhp7+zMUA3Iw1Qa/+937mqma4kVSDUKNeE hcxNkAkePNN/KzdTNGotOQbb52XT2mRjr8DwOI1nkgsCJOHytTrftG5CgT47GXRZ4/TTQxL+w89 FXWYiR56sheq4PRQHCcn+z1WJMSZrudWeirXeOj/QXq7qH7AwcAw5+OCK/66jzSYtpw== X-Received: by 2002:a05:6214:2b06:b0:4bb:5716:d1c3 with SMTP id jx6-20020a0562142b0600b004bb5716d1c3mr28714601qvb.85.1667505545171; Thu, 03 Nov 2022 12:59:05 -0700 (PDT) X-Received: by 2002:a05:6214:2b06:b0:4bb:5716:d1c3 with SMTP id jx6-20020a0562142b0600b004bb5716d1c3mr28714586qvb.85.1667505544774; Thu, 03 Nov 2022 12:59:04 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id fa3-20020a05622a4cc300b003a4f14378d1sm1069636qtb.33.2022.11.03.12.59.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 12:59:04 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH RFA] input: add get_source_text_between Date: Thu, 3 Nov 2022 15:59:02 -0400 Message-Id: <20221103195902.2114479-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748506344721977291?= X-GMAIL-MSGID: =?utf-8?q?1748506344721977291?= Tested x86_64-pc-linux-gnu, OK for trunk? -- >8 -- The c++-contracts branch uses this to retrieve the source form of the contract predicate, to be returned by contract_violation::comment(). gcc/ChangeLog: * input.cc (get_source_text_between): New fn. * input.h (get_source_text_between): Declare. --- gcc/input.h | 1 + gcc/input.cc | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) base-commit: a4cd2389276a30c39034a83d640ce68fa407bac1 prerequisite-patch-id: 329bc16a88dc9a3b13cd3fcecb3678826cc592dc prerequisite-patch-id: 49e922c10f6da687d9da3f6a0fd20f324bd352d6 diff --git a/gcc/input.h b/gcc/input.h index 11c571d076f..f18769950b5 100644 --- a/gcc/input.h +++ b/gcc/input.h @@ -111,6 +111,7 @@ class char_span }; extern char_span location_get_source_line (const char *file_path, int line); +extern char *get_source_text_between (location_t, location_t); extern bool location_missing_trailing_newline (const char *file_path); diff --git a/gcc/input.cc b/gcc/input.cc index a28abfac5ac..9b36356338a 100644 --- a/gcc/input.cc +++ b/gcc/input.cc @@ -949,6 +949,82 @@ location_get_source_line (const char *file_path, int line) return char_span (buffer, len); } +/* Return a copy of the source text between two locations. The caller is + responsible for freeing the return value. */ + +char * +get_source_text_between (location_t start, location_t end) +{ + expanded_location expstart = + expand_location_to_spelling_point (start, LOCATION_ASPECT_START); + expanded_location expend = + expand_location_to_spelling_point (end, LOCATION_ASPECT_FINISH); + + /* If the locations are in different files or the end comes before the + start, abort and return nothing. */ + if (!expstart.file || !expend.file) + return NULL; + if (strcmp (expstart.file, expend.file) != 0) + return NULL; + if (expstart.line > expend.line) + return NULL; + if (expstart.line == expend.line + && expstart.column > expend.column) + return NULL; + + /* For a single line we need to trim both edges. */ + if (expstart.line == expend.line) + { + char_span line = location_get_source_line (expstart.file, expstart.line); + if (line.length () < 1) + return NULL; + int s = expstart.column - 1; + int l = expend.column - s; + if (line.length () < (size_t)expend.column) + return NULL; + return line.subspan (s, l).xstrdup (); + } + + struct obstack buf_obstack; + obstack_init (&buf_obstack); + + /* Loop through all lines in the range and append each to buf; may trim + parts of the start and end lines off depending on column values. */ + for (int l = expstart.line; l <= expend.line; ++l) + { + char_span line = location_get_source_line (expstart.file, l); + if (line.length () < 1 && (l != expstart.line && l != expend.line)) + continue; + + /* For the first line in the range, only start at expstart.column */ + if (l == expstart.line) + { + if (expstart.column == 0) + return NULL; + if (line.length () < (size_t)expstart.column - 1) + return NULL; + line = line.subspan (expstart.column - 1, + line.length() - expstart.column + 1); + } + /* For the last line, don't go past expend.column */ + else if (l == expend.line) + { + if (line.length () < (size_t)expend.column) + return NULL; + line = line.subspan (0, expend.column); + } + + obstack_grow (&buf_obstack, line.get_buffer (), line.length ()); + } + + /* NUL-terminate and finish the buf obstack. */ + obstack_1grow (&buf_obstack, 0); + const char *buf = (const char *) obstack_finish (&buf_obstack); + + /* TODO should we collapse/trim newlines and runs of spaces? */ + return xstrdup (buf); +} + /* Determine if FILE_PATH missing a trailing newline on its final line. Only valid to call once all of the file has been loaded, by requesting a line number beyond the end of the file. */