From patchwork Thu Nov 9 22:31:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 163623 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp746068vqs; Thu, 9 Nov 2023 14:32:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWug2Y11hZ5EXHDjPp3Fn5IuPTHHPFdHXy0eY2gpHVQk4vtO2ibKe+os8jG/JCxImSm2Ad X-Received: by 2002:a05:620a:13c1:b0:775:9766:cb69 with SMTP id g1-20020a05620a13c100b007759766cb69mr5600537qkl.75.1699569133856; Thu, 09 Nov 2023 14:32:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699569133; cv=pass; d=google.com; s=arc-20160816; b=VKjIEgPquV92MCgiNoTD3pfHd8Yp4FFdBNRwtz7f3XfRJO8CWi6VSdLTETBHi4ShnK of083TlYvq0UPSxaEs6qZmLWFKo5Ynqjk57fP6k4u6v2TzRjp2AJWsDwbGFkprhxEOLC +howCCmU8Jd8cGvGHdSICkeIK30HlTIgQei4oqZBtCUSLZJVMaEmDuItMimRcoy/DtK3 0vwNehVEI2VRdj95AFIQ1FP0sJMefjJmGkSgRQLbN/KZ2ejl9U4XDQ0pwDEBqdmY0Tuj 1hihY6AqgkcIioeV256JiIHfDn3j8d+PPT2dCpNLHZlyqPzoHCjbND7ymkWqWY4bWzDW oPpQ== 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 :arc-filter:dmarc-filter:delivered-to; bh=fOAU99LL6pZA5OkZkr3so+cSlF59Qt53S9+DK89ctAE=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=v0EFLeKQJgmS1di9Ju7PNeIkfq/tuA5AHdsMvdyWwJnQ3usX7OpTvlwBH3JQwpWefp XoAoFin2SjTKJF0prLb9g0LP32qCxCTY4xyprLvUwgy1alPH3X4fOmTiAAVWxnyfjKkJ SSb5MsQVcsGFsghvgexcPYhVKND/oxFO+NijWD4OlNoJhorV8vWharf4LbdJo/+0QHeX 57IOVpgwVtguNbrx2xRGlgs1t88Z7+vqzijn/vgTgaS+cdRQmk8+WpH0WP9JunKJwfDR RIhXnZFwkh/rdjYzRQZy35HY9/5tGgZnkMyVqaD4+llzNcBGO+ccE9lpvQZ3FtbL8upg 883A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=EEQPzUYw; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id h19-20020a05620a245300b007770bfd349fsi3556624qkn.603.2023.11.09.14.32.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 14:32:13 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=EEQPzUYw; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9BC5F385842C for ; Thu, 9 Nov 2023 22:32:13 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 4A6313858D33 for ; Thu, 9 Nov 2023 22:31:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4A6313858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4A6313858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699569110; cv=none; b=DHQ7ikhBx6bA6xT0zz2zTvx5uHuZhANWcz77pK7jJ7dsFbkQrmZ/Gj+3yMDd0/PodD+Oh4YveQUP3wXMCdPFVzAhWGM1OnM0OVerfHZYiK+NjvIr2qQbn8K1s+BeSzQzHF+pX8iqcvR3lU4a7bJzFZBVm6mLcIWt0Z5E4FXgnQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699569110; c=relaxed/simple; bh=1OABd26UAzI9xgL9iYTfPjiMrx9Q4fkUxkSZJlGbTlU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=FB5O8Od6YFKkE/JPLuuYUz61/SHjMXjiMcuWdhkF4niJq0sv29vWD48cABmebszlaiB5G5QVsx5dqua/58v5GdEEwiUiwJSsdMVxJsft2PBhqmGZJBUY9ZD0lPTVVwzV3CeKcLQ+uERX16jmmU9+DwSXRwi1vSPuviWJ0LEkbV4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699569107; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=fOAU99LL6pZA5OkZkr3so+cSlF59Qt53S9+DK89ctAE=; b=EEQPzUYw6GjM7HQBAvcnL7iNwhQ4Ulj4nw6a4Q9aO86BSh0e8Ma9ungEnXTDJ1j8E5mtCv 8N/0NVvUbLnvZgWVp3dV9ISQvdD3WnlgSeMREm7+p6JrUqTHGTP8LSRCVxa+B6Gd4kz+Gr WSmzjzfK7OmiwvyX6jSxW9b5mnlitYg= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-436-L4ke3HKvOwGuOClwzMHK9g-1; Thu, 09 Nov 2023 17:31:45 -0500 X-MC-Unique: L4ke3HKvOwGuOClwzMHK9g-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4CCB1380627C for ; Thu, 9 Nov 2023 22:31:45 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.8.62]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0A679502A; Thu, 9 Nov 2023 22:31:45 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] diagnostics: cleanups to diagnostic-show-locus.cc Date: Thu, 9 Nov 2023 17:31:40 -0500 Message-Id: <20231109223140.2989474-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_FILL_THIS_FORM_SHORT, T_SCC_BODY_TEXT_LINE 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: 1782127403887363114 X-GMAIL-MSGID: 1782127403887363114 Reduce implicit usage of line_table global, and move source printing to within diagnostic_context. No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-5300-g8625aa24669669. gcc/ChangeLog: * diagnostic-show-locus.cc (layout::m_line_table): New field. (compatible_locations_p): Convert to... (layout::compatible_locations_p): ...this, replacing uses of line_table global with m_line_table. (layout::layout): Convert "richloc" param from a pointer to a const reference. Initialize m_line_table member. (layout::maybe_add_location_range): Replace uses of line_table global with m_line_table. Pass the latter to linemap_client_expand_location_to_spelling_point. (layout::print_leading_fixits): Pass m_line_table to affects_line_p. (layout::print_trailing_fixits): Likewise. (gcc_rich_location::add_location_if_nearby): Update for change to layout ctor params. (diagnostic_show_locus): Convert to... (diagnostic_context::maybe_show_locus): ...this, converting richloc param from a pointer to a const reference. Make "loc" const. Split out printing part of function to... (diagnostic_context::show_locus): ...this. (selftest::test_offset_impl): Update for change to layout ctor params. (selftest::test_layout_x_offset_display_utf8): Likewise. (selftest::test_layout_x_offset_display_tab): Likewise. (selftest::test_tab_expansion): Likewise. * diagnostic.h (diagnostic_context::maybe_show_locus): New decl. (diagnostic_context::show_locus): New decl. (diagnostic_show_locus): Convert from a decl to an inline function. * gdbinit.in (break-on-diagnostic): Update from a breakpoint on diagnostic_show_locus to one on diagnostic_context::maybe_show_locus. * genmatch.cc (linemap_client_expand_location_to_spelling_point): Add "set" param and use it in place of line_table global. * input.cc (expand_location_1): Likewise. (expand_location): Update for new param of expand_location_1. (expand_location_to_spelling_point): Likewise. (linemap_client_expand_location_to_spelling_point): Add "set" param and use it in place of line_table global. * tree-diagnostic-path.cc (event_range::print): Pass line_table for new param of linemap_client_expand_location_to_spelling_point. libcpp/ChangeLog: * include/line-map.h (rich_location::get_expanded_location): Make const. (rich_location::get_line_table): New accessor. (rich_location::m_line_table): Make the pointer be const. (rich_location::m_have_expanded_location): Make mutable. (rich_location::m_expanded_location): Likewise. (fixit_hint::affects_line_p): Add const line_maps * param. (linemap_client_expand_location_to_spelling_point): Likewise. * line-map.cc (rich_location::get_expanded_location): Make const. Pass m_line_table to linemap_client_expand_location_to_spelling_point. (rich_location::maybe_add_fixit): Likewise. (fixit_hint::affects_line_p): Add set param and pass to linemap_client_expand_location_to_spelling_point. --- gcc/diagnostic-show-locus.cc | 122 +++++++++++++++++++---------------- gcc/diagnostic.h | 21 ++++-- gcc/gdbinit.in | 2 +- gcc/genmatch.cc | 7 +- gcc/input.cc | 23 ++++--- gcc/tree-diagnostic-path.cc | 2 +- libcpp/include/line-map.h | 17 +++-- libcpp/line-map.cc | 22 ++++--- 8 files changed, 129 insertions(+), 87 deletions(-) diff --git a/gcc/diagnostic-show-locus.cc b/gcc/diagnostic-show-locus.cc index 43523572fe5b..5edc319c3130 100644 --- a/gcc/diagnostic-show-locus.cc +++ b/gcc/diagnostic-show-locus.cc @@ -366,7 +366,7 @@ class layout { public: layout (const diagnostic_context &context, - rich_location *richloc, + const rich_location &richloc, diagnostic_t diagnostic_kind, pretty_printer *pp); @@ -428,7 +428,10 @@ class layout move_to_column (int *column, int dest_column, bool add_left_margin); private: + bool compatible_locations_p (location_t loc_a, location_t loc_b) const; + const diagnostic_source_printing_options &m_options; + const line_maps *m_line_table; pretty_printer *m_pp; char_display_policy m_policy; location_t m_primary_loc; @@ -930,13 +933,13 @@ test_get_line_bytes_without_trailing_whitespace () erroneously was leading to hundreds of lines of irrelevant source being printed. */ -static bool -compatible_locations_p (location_t loc_a, location_t loc_b) +bool +layout::compatible_locations_p (location_t loc_a, location_t loc_b) const { if (IS_ADHOC_LOC (loc_a)) - loc_a = get_location_from_adhoc_loc (line_table, loc_a); + loc_a = get_location_from_adhoc_loc (m_line_table, loc_a); if (IS_ADHOC_LOC (loc_b)) - loc_b = get_location_from_adhoc_loc (line_table, loc_b); + loc_b = get_location_from_adhoc_loc (m_line_table, loc_b); /* If either location is one of the special locations outside of a linemap, they are only compatible if they are equal. */ @@ -944,10 +947,10 @@ compatible_locations_p (location_t loc_a, location_t loc_b) || loc_b < RESERVED_LOCATION_COUNT) return loc_a == loc_b; - const line_map *map_a = linemap_lookup (line_table, loc_a); + const line_map *map_a = linemap_lookup (m_line_table, loc_a); linemap_assert (map_a); - const line_map *map_b = linemap_lookup (line_table, loc_b); + const line_map *map_b = linemap_lookup (m_line_table, loc_b); linemap_assert (map_b); /* Are they within the same map? */ @@ -959,9 +962,9 @@ compatible_locations_p (location_t loc_a, location_t loc_b) /* If so, then they're only compatible if either both are from the macro definition, or both from the macro arguments. */ bool loc_a_from_defn - = linemap_location_from_macro_definition_p (line_table, loc_a); + = linemap_location_from_macro_definition_p (m_line_table, loc_a); bool loc_b_from_defn - = linemap_location_from_macro_definition_p (line_table, loc_b); + = linemap_location_from_macro_definition_p (m_line_table, loc_b); if (loc_a_from_defn != loc_b_from_defn) return false; @@ -969,11 +972,11 @@ compatible_locations_p (location_t loc_a, location_t loc_b) recurse. */ const line_map_macro *macro_map = linemap_check_macro (map_a); location_t loc_a_toward_spelling - = linemap_macro_map_loc_unwind_toward_spelling (line_table, + = linemap_macro_map_loc_unwind_toward_spelling (m_line_table, macro_map, loc_a); location_t loc_b_toward_spelling - = linemap_macro_map_loc_unwind_toward_spelling (line_table, + = linemap_macro_map_loc_unwind_toward_spelling (m_line_table, macro_map, loc_b); return compatible_locations_p (loc_a_toward_spelling, @@ -1180,37 +1183,38 @@ make_policy (const diagnostic_context &dc, will fit within the max_width provided by the diagnostic_context. */ layout::layout (const diagnostic_context &context, - rich_location *richloc, + const rich_location &richloc, diagnostic_t diagnostic_kind, pretty_printer *pp) : m_options (context.m_source_printing), + m_line_table (richloc.get_line_table ()), m_pp (pp ? pp : context.printer), - m_policy (make_policy (context, *richloc)), - m_primary_loc (richloc->get_range (0)->m_loc), - m_exploc (richloc->get_expanded_location (0), m_policy, + m_policy (make_policy (context, richloc)), + m_primary_loc (richloc.get_range (0)->m_loc), + m_exploc (richloc.get_expanded_location (0), m_policy, LOCATION_ASPECT_CARET), m_colorizer (m_pp, diagnostic_kind), m_diagnostic_path_p (diagnostic_kind == DK_DIAGNOSTIC_PATH), - m_layout_ranges (richloc->get_num_locations ()), - m_fixit_hints (richloc->get_num_fixit_hints ()), - m_line_spans (1 + richloc->get_num_locations ()), + m_layout_ranges (richloc.get_num_locations ()), + m_fixit_hints (richloc.get_num_fixit_hints ()), + m_line_spans (1 + richloc.get_num_locations ()), m_linenum_width (0), m_x_offset_display (0), - m_escape_on_output (richloc->escape_on_output_p ()) + m_escape_on_output (richloc.escape_on_output_p ()) { - for (unsigned int idx = 0; idx < richloc->get_num_locations (); idx++) + for (unsigned int idx = 0; idx < richloc.get_num_locations (); idx++) { /* This diagnostic printer can only cope with "sufficiently sane" ranges. Ignore any ranges that are awkward to handle. */ - const location_range *loc_range = richloc->get_range (idx); + const location_range *loc_range = richloc.get_range (idx); maybe_add_location_range (loc_range, idx, false); } /* Populate m_fixit_hints, filtering to only those that are in the same file. */ - for (unsigned int i = 0; i < richloc->get_num_fixit_hints (); i++) + for (unsigned int i = 0; i < richloc.get_num_fixit_hints (); i++) { - const fixit_hint *hint = richloc->get_fixit_hint (i); + const fixit_hint *hint = richloc.get_fixit_hint (i); if (validate_fixit_hint_p (hint)) m_fixit_hints.safe_push (hint); } @@ -1249,18 +1253,18 @@ layout::maybe_add_location_range (const location_range *loc_range, gcc_assert (loc_range); /* Split the "range" into caret and range information. */ - source_range src_range = get_range_from_loc (line_table, loc_range->m_loc); + source_range src_range = get_range_from_loc (m_line_table, loc_range->m_loc); /* Expand the various locations. */ expanded_location start = linemap_client_expand_location_to_spelling_point - (src_range.m_start, LOCATION_ASPECT_START); + (m_line_table, src_range.m_start, LOCATION_ASPECT_START); expanded_location finish = linemap_client_expand_location_to_spelling_point - (src_range.m_finish, LOCATION_ASPECT_FINISH); + (m_line_table, src_range.m_finish, LOCATION_ASPECT_FINISH); expanded_location caret = linemap_client_expand_location_to_spelling_point - (loc_range->m_loc, LOCATION_ASPECT_CARET); + (m_line_table, loc_range->m_loc, LOCATION_ASPECT_CARET); /* If any part of the range isn't in the same file as the primary location of this diagnostic, ignore the range. */ @@ -2098,7 +2102,7 @@ layout::print_leading_fixits (linenum_type row) gcc_assert (hint->insertion_p ()); - if (hint->affects_line_p (m_exploc.file, row)) + if (hint->affects_line_p (m_line_table, m_exploc.file, row)) { /* Printing the '+' with normal colorization and the inserted line with "insert" colorization @@ -2550,7 +2554,7 @@ layout::print_trailing_fixits (linenum_type row) if (hint->ends_with_newline_p ()) continue; - if (hint->affects_line_p (m_exploc.file, row)) + if (hint->affects_line_p (m_line_table, m_exploc.file, row)) corrections.add_hint (hint); } @@ -2808,7 +2812,7 @@ gcc_rich_location::add_location_if_nearby (location_t loc, /* Use the layout location-handling logic to sanitize LOC, filtering it to the current line spans within a temporary layout instance. */ - layout layout (*global_dc, this, DK_ERROR, nullptr); + layout layout (*global_dc, *this, DK_ERROR, nullptr); location_range loc_range; loc_range.m_loc = loc; loc_range.m_range_display_kind = SHOW_RANGE_WITHOUT_CARET; @@ -2820,19 +2824,17 @@ gcc_rich_location::add_location_if_nearby (location_t loc, return true; } -/* Print the physical source code corresponding to the location of - this diagnostic, with additional annotations. - If PP is non-null, then use it rather than CONTEXT's printer. */ +/* As per diagnostic_context::show_locus, but don't print anything + if source printing is disabled, or if the location hasn't changed. */ void -diagnostic_show_locus (diagnostic_context * context, - rich_location *richloc, - diagnostic_t diagnostic_kind, - pretty_printer *pp) +diagnostic_context::maybe_show_locus (const rich_location &richloc, + diagnostic_t diagnostic_kind, + pretty_printer *pp) { - location_t loc = richloc->get_loc (); + const location_t loc = richloc.get_loc (); /* Do nothing if source-printing has been disabled. */ - if (!context->m_source_printing.enabled) + if (!m_source_printing.enabled) return; /* Don't attempt to print source for UNKNOWN_LOCATION and for builtins. */ @@ -2841,20 +2843,32 @@ diagnostic_show_locus (diagnostic_context * context, /* Don't print the same source location twice in a row, unless we have fix-it hints, or multiple locations, or a label. */ - if (loc == context->m_last_location - && richloc->get_num_fixit_hints () == 0 - && richloc->get_num_locations () == 1 - && richloc->get_range (0)->m_label == NULL) + if (loc == m_last_location + && richloc.get_num_fixit_hints () == 0 + && richloc.get_num_locations () == 1 + && richloc.get_range (0)->m_label == NULL) return; - context->m_last_location = loc; + m_last_location = loc; + + show_locus (richloc, diagnostic_kind, pp); +} - layout layout (*context, richloc, diagnostic_kind, pp); +/* Print the physical source code corresponding to the location of + this diagnostic, with additional annotations. + If PP is non-null, then use it rather than this context's printer. */ + +void +diagnostic_context::show_locus (const rich_location &richloc, + diagnostic_t diagnostic_kind, + pretty_printer *pp) +{ + layout layout (*this, richloc, diagnostic_kind, pp); for (int line_span_idx = 0; line_span_idx < layout.get_num_line_spans (); line_span_idx++) { const line_span *line_span = layout.get_line_span (line_span_idx); - if (context->m_source_printing.show_line_numbers_p) + if (m_source_printing.show_line_numbers_p) { /* With line numbers, we should show whenever the line-numbering "jumps". */ @@ -2868,7 +2882,7 @@ diagnostic_show_locus (diagnostic_context * context, { expanded_location exploc = layout.get_expanded_location (line_span); - context->m_text_callbacks.start_span (context, exploc); + m_text_callbacks.start_span (this, exploc); } } /* Iterate over the lines within this span (using linenum_arith_t to @@ -2961,7 +2975,7 @@ test_offset_impl (int caret_byte_col, int max_width, rich_location richloc (line_table, linemap_position_for_column (line_table, caret_byte_col)); - layout test_layout (dc, &richloc, DK_ERROR, nullptr); + layout test_layout (dc, richloc, DK_ERROR, nullptr); ASSERT_EQ (left_margin - test_linenum_sep, test_layout.get_linenum_width ()); ASSERT_EQ (expected_x_offset_display, @@ -3076,7 +3090,7 @@ test_layout_x_offset_display_utf8 (const line_table_case &case_) rich_location richloc (line_table, linemap_position_for_column (line_table, emoji_col)); - layout test_layout (dc, &richloc, DK_ERROR, nullptr); + layout test_layout (dc, richloc, DK_ERROR, nullptr); test_layout.print_line (1); ASSERT_STREQ (" | 1 \n" " | 1 \n" @@ -3101,7 +3115,7 @@ test_layout_x_offset_display_utf8 (const line_table_case &case_) rich_location richloc (line_table, linemap_position_for_column (line_table, emoji_col + 2)); - layout test_layout (dc, &richloc, DK_ERROR, nullptr); + layout test_layout (dc, richloc, DK_ERROR, nullptr); test_layout.print_line (1); ASSERT_STREQ (" | 1 1 \n" " | 1 2 \n" @@ -3178,7 +3192,7 @@ test_layout_x_offset_display_tab (const line_table_case &case_) { test_diagnostic_context dc; dc.m_tabstop = tabstop; - layout test_layout (dc, &richloc, DK_ERROR, nullptr); + layout test_layout (dc, richloc, DK_ERROR, nullptr); test_layout.print_line (1); const char *out = pp_formatted_text (dc.printer); ASSERT_EQ (NULL, strchr (out, '\t')); @@ -3201,7 +3215,7 @@ test_layout_x_offset_display_tab (const line_table_case &case_) dc.m_source_printing.min_margin_width = test_left_margin - test_linenum_sep + 1; dc.m_source_printing.show_line_numbers_p = true; - layout test_layout (dc, &richloc, DK_ERROR, nullptr); + layout test_layout (dc, richloc, DK_ERROR, nullptr); test_layout.print_line (1); /* We have arranged things so that two columns will be printed before @@ -5513,7 +5527,7 @@ test_tab_expansion (const line_table_case &case_) rich_location richloc (line_table, linemap_position_for_column (line_table, first_non_ws_byte_col)); - layout test_layout (dc, &richloc, DK_ERROR, nullptr); + layout test_layout (dc, richloc, DK_ERROR, nullptr); test_layout.print_line (1); ASSERT_STREQ (" This: ` ' is a tab.\n" " ^\n", @@ -5528,7 +5542,7 @@ test_tab_expansion (const line_table_case &case_) rich_location richloc (line_table, linemap_position_for_column (line_table, right_quote_byte_col)); - layout test_layout (dc, &richloc, DK_ERROR, nullptr); + layout test_layout (dc, richloc, DK_ERROR, nullptr); test_layout.print_line (1); ASSERT_STREQ (" This: ` ' is a tab.\n" " ^\n", diff --git a/gcc/diagnostic.h b/gcc/diagnostic.h index 58341cec3085..6a3228247b8a 100644 --- a/gcc/diagnostic.h +++ b/gcc/diagnostic.h @@ -410,6 +410,10 @@ public: m_option_classifier.pop (where); } + void maybe_show_locus (const rich_location &richloc, + diagnostic_t diagnostic_kind, + pretty_printer *pp); + void emit_diagram (const diagnostic_diagram &diagram); /* Various setters for use by option-handling logic. */ @@ -496,6 +500,10 @@ private: void get_any_inlining_info (diagnostic_info *diagnostic); + void show_locus (const rich_location &richloc, + diagnostic_t diagnostic_kind, + pretty_printer *pp); + /* Data members. Ideally, all of these would be private and have "m_" prefixes. */ @@ -816,10 +824,15 @@ diagnostic_report_current_module (diagnostic_context *context, context->report_current_module (where); } -extern void diagnostic_show_locus (diagnostic_context *, - rich_location *richloc, - diagnostic_t diagnostic_kind, - pretty_printer *pp = nullptr); +inline void +diagnostic_show_locus (diagnostic_context *context, + rich_location *richloc, + diagnostic_t diagnostic_kind, + pretty_printer *pp = nullptr) +{ + gcc_assert (richloc); + context->maybe_show_locus (*richloc, diagnostic_kind, pp); +} /* Because we read source files a second time after the frontend did it the first time, we need to know how the frontend handled things like character diff --git a/gcc/gdbinit.in b/gcc/gdbinit.in index a76079a46af7..39801df84cbd 100644 --- a/gcc/gdbinit.in +++ b/gcc/gdbinit.in @@ -293,7 +293,7 @@ See also 'help-gcc-hooks'. end define break-on-diagnostic -break diagnostic_show_locus +break diagnostic_context::maybe_show_locus end document break-on-diagnostic diff --git a/gcc/genmatch.cc b/gcc/genmatch.cc index e9d7afa7728f..45723425f7ab 100644 --- a/gcc/genmatch.cc +++ b/gcc/genmatch.cc @@ -62,12 +62,13 @@ static class line_maps *line_table; This is the implementation for genmatch. */ expanded_location -linemap_client_expand_location_to_spelling_point (location_t loc, +linemap_client_expand_location_to_spelling_point (const line_maps *set, + location_t loc, enum location_aspect) { const struct line_map_ordinary *map; - loc = linemap_resolve_location (line_table, loc, LRK_SPELLING_LOCATION, &map); - return linemap_expand_location (line_table, map, loc); + loc = linemap_resolve_location (set, loc, LRK_SPELLING_LOCATION, &map); + return linemap_expand_location (set, map, loc); } static bool diff --git a/gcc/input.cc b/gcc/input.cc index f7c016372913..1b229227d895 100644 --- a/gcc/input.cc +++ b/gcc/input.cc @@ -215,7 +215,8 @@ class line_maps *saved_line_table; ASPECT controls which part of the location to use. */ static expanded_location -expand_location_1 (location_t loc, +expand_location_1 (const line_maps *set, + location_t loc, bool expansion_point_p, enum location_aspect aspect) { @@ -243,11 +244,11 @@ expand_location_1 (location_t loc, location for a built-in token), let's consider the first location (toward the expansion point) that is not reserved; that is, the first location that is in real source code. */ - loc = linemap_unwind_to_first_non_reserved_loc (line_table, + loc = linemap_unwind_to_first_non_reserved_loc (set, loc, NULL); lrk = LRK_SPELLING_LOCATION; } - loc = linemap_resolve_location (line_table, loc, lrk, &map); + loc = linemap_resolve_location (set, loc, lrk, &map); /* loc is now either in an ordinary map, or is a reserved location. If it is a compound location, the caret is in a spelling location, @@ -266,18 +267,18 @@ expand_location_1 (location_t loc, { location_t start = get_start (loc); if (start != loc) - return expand_location_1 (start, expansion_point_p, aspect); + return expand_location_1 (set, start, expansion_point_p, aspect); } break; case LOCATION_ASPECT_FINISH: { location_t finish = get_finish (loc); if (finish != loc) - return expand_location_1 (finish, expansion_point_p, aspect); + return expand_location_1 (set, finish, expansion_point_p, aspect); } break; } - xloc = linemap_expand_location (line_table, map, loc); + xloc = linemap_expand_location (set, map, loc); } xloc.data = block; @@ -1127,7 +1128,7 @@ is_location_from_builtin_token (location_t loc) expanded_location expand_location (location_t loc) { - return expand_location_1 (loc, /*expansion_point_p=*/true, + return expand_location_1 (line_table, loc, /*expansion_point_p=*/true, LOCATION_ASPECT_CARET); } @@ -1141,7 +1142,8 @@ expanded_location expand_location_to_spelling_point (location_t loc, enum location_aspect aspect) { - return expand_location_1 (loc, /*expansion_point_p=*/false, aspect); + return expand_location_1 (line_table, loc, /*expansion_point_p=*/false, + aspect); } /* The rich_location class within libcpp requires a way to expand @@ -1154,10 +1156,11 @@ expand_location_to_spelling_point (location_t loc, which simply calls into expand_location_1. */ expanded_location -linemap_client_expand_location_to_spelling_point (location_t loc, +linemap_client_expand_location_to_spelling_point (const line_maps *set, + location_t loc, enum location_aspect aspect) { - return expand_location_1 (loc, /*expansion_point_p=*/false, aspect); + return expand_location_1 (set, loc, /*expansion_point_p=*/false, aspect); } diff --git a/gcc/tree-diagnostic-path.cc b/gcc/tree-diagnostic-path.cc index cd983bc20ffc..ee957cce2cf5 100644 --- a/gcc/tree-diagnostic-path.cc +++ b/gcc/tree-diagnostic-path.cc @@ -204,7 +204,7 @@ struct event_range { expanded_location exploc = linemap_client_expand_location_to_spelling_point - (initial_loc, LOCATION_ASPECT_CARET); + (line_table, initial_loc, LOCATION_ASPECT_CARET); if (exploc.file != LOCATION_FILE (dc->m_last_location)) dc->m_text_callbacks.start_span (dc, exploc); } diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h index 635d6d3077c5..615cb420adf7 100644 --- a/libcpp/include/line-map.h +++ b/libcpp/include/line-map.h @@ -1658,7 +1658,7 @@ class rich_location const location_range *get_range (unsigned int idx) const; location_range *get_range (unsigned int idx); - expanded_location get_expanded_location (unsigned int idx); + expanded_location get_expanded_location (unsigned int idx) const; void override_column (int column); @@ -1762,6 +1762,8 @@ class rich_location bool escape_on_output_p () const { return m_escape_on_output; } void set_escape_on_output (bool flag) { m_escape_on_output = flag; } + const line_maps *get_line_table () const { return m_line_table; } + private: bool reject_impossible_fixit (location_t where); void stop_supporting_fixits (); @@ -1773,17 +1775,17 @@ public: static const int STATICALLY_ALLOCATED_RANGES = 3; protected: - line_maps *m_line_table; + line_maps * const m_line_table; semi_embedded_vec m_ranges; int m_column_override; - bool m_have_expanded_location; + mutable bool m_have_expanded_location; bool m_seen_impossible_fixit; bool m_fixits_cannot_be_auto_applied; bool m_escape_on_output; - expanded_location m_expanded_location; + mutable expanded_location m_expanded_location; static const int MAX_STATIC_FIXIT_HINTS = 2; semi_embedded_vec m_fixit_hints; @@ -1916,7 +1918,9 @@ class fixit_hint const char *new_content); ~fixit_hint () { free (m_bytes); } - bool affects_line_p (const char *file, int line) const; + bool affects_line_p (const line_maps *set, + const char *file, + int line) const; location_t get_start_loc () const { return m_start; } location_t get_next_loc () const { return m_next_loc; } bool maybe_append (location_t start, @@ -2103,7 +2107,8 @@ enum location_aspect Hence we require client code of libcpp to implement the following symbol. */ extern expanded_location -linemap_client_expand_location_to_spelling_point (location_t, +linemap_client_expand_location_to_spelling_point (const line_maps *, + location_t, enum location_aspect); #endif /* !LIBCPP_LINE_MAP_H */ diff --git a/libcpp/line-map.cc b/libcpp/line-map.cc index cc9e14aa61e3..be2d09650958 100644 --- a/libcpp/line-map.cc +++ b/libcpp/line-map.cc @@ -2212,7 +2212,7 @@ rich_location::get_range (unsigned int idx) location. */ expanded_location -rich_location::get_expanded_location (unsigned int idx) +rich_location::get_expanded_location (unsigned int idx) const { if (idx == 0) { @@ -2221,7 +2221,7 @@ rich_location::get_expanded_location (unsigned int idx) { m_expanded_location = linemap_client_expand_location_to_spelling_point - (get_loc (0), LOCATION_ASPECT_CARET); + (m_line_table, get_loc (0), LOCATION_ASPECT_CARET); if (m_column_override) m_expanded_location.column = m_column_override; m_have_expanded_location = true; @@ -2231,7 +2231,7 @@ rich_location::get_expanded_location (unsigned int idx) } else return linemap_client_expand_location_to_spelling_point - (get_loc (idx), LOCATION_ASPECT_CARET); + (m_line_table, get_loc (idx), LOCATION_ASPECT_CARET); } /* Set the column of the primary location, with 0 meaning @@ -2490,10 +2490,12 @@ rich_location::maybe_add_fixit (location_t start, /* Only allow fix-it hints that affect a single line in one file. Compare the end-points. */ expanded_location exploc_start - = linemap_client_expand_location_to_spelling_point (start, + = linemap_client_expand_location_to_spelling_point (m_line_table, + start, LOCATION_ASPECT_START); expanded_location exploc_next_loc - = linemap_client_expand_location_to_spelling_point (next_loc, + = linemap_client_expand_location_to_spelling_point (m_line_table, + next_loc, LOCATION_ASPECT_START); /* They must be within the same file... */ if (exploc_start.file != exploc_next_loc.file) @@ -2581,17 +2583,21 @@ fixit_hint::fixit_hint (location_t start, /* Does this fix-it hint affect the given line? */ bool -fixit_hint::affects_line_p (const char *file, int line) const +fixit_hint::affects_line_p (const line_maps *set, + const char *file, + int line) const { expanded_location exploc_start - = linemap_client_expand_location_to_spelling_point (m_start, + = linemap_client_expand_location_to_spelling_point (set, + m_start, LOCATION_ASPECT_START); if (file != exploc_start.file) return false; if (line < exploc_start.line) return false; expanded_location exploc_next_loc - = linemap_client_expand_location_to_spelling_point (m_next_loc, + = linemap_client_expand_location_to_spelling_point (set, + m_next_loc, LOCATION_ASPECT_START); if (file != exploc_next_loc.file) return false;