From patchwork Wed Aug 9 22:14:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lewis Hyatt X-Patchwork-Id: 133586 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp40311vqi; Wed, 9 Aug 2023 15:16:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG3vae/ehFVAfCqNvOU91XAOjId1Yz6sdlmk3KOtkKqvNWOwVbD/il1hxYERqPNU2x6RVUm X-Received: by 2002:a17:906:11e:b0:988:8be0:3077 with SMTP id 30-20020a170906011e00b009888be03077mr342727eje.31.1691619391288; Wed, 09 Aug 2023 15:16:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691619391; cv=none; d=google.com; s=arc-20160816; b=TGxFolUqyrbqdEskchsxLEtBJBjbM7+eeBTA1ED67uDnsx2UPoLToJ3/Z35qxXYIrP mvxpA7APa/Xxr4fe4W9f8/PReljiEgWQ7m8nlnG+Q/GTwbOeobPVq7QSxmJpKELpl0Vg VnOo186fkrsYdeaeyBk8uyg04g+LUhe3IrbD/BMMZNeKAhma910f7dxfI4GL1hbvHjwi CMoXGcmCIYdM/HjagkUHV+L0DksAnYdYLU/k9ZB3w+ucSO+KEDcOT4TdT+MoIrccg+lw PtUQvVOlIehKe0UscCLv2p9xvHXuIgfEZhz0/z9XR6JD6bYnhIQt0b3yHzoJ4o39rUaM oJxQ== 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:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=2bqiaX8fGQCtU58i1/wgdRfXIIyLV0jx8UQ8dKb9s3I=; fh=hLxXrzU+VDBolomQxjoi9c6yn4Oij2Jaf7BaYMHGh24=; b=OAuIRnJUE9oPSQ0Q3qaxxxHlIKLkQKhD5Z7+T9AiY9w623lKfOXzoG9UN1B6WuQ9jG hDf8UFiq8jq6Ko6HODmkx6xhk+KREfdVtAtG2vRxYV8Y2LEmELjxSeOYRpqzo1XnHFZi 6X5jLBW/scdkB9BRLlxODI0ej3uUbGlVafheeD9grsof1ObhWYNx5BhOC8/68nhlWlby 5W6JUFu2ts5fnyHiANzkYuMvue4ZhSmjQXZSo0bPUvluOUnoF6i+m6GJPoSoFFab4uSX sclMk4GdgTrGNmheJIzuurxN3ngkFFDyQPT9b06K7bxjdLx4IEA9HLYg3i4961AnIirD aK3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=QOlxPhva; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id q23-20020a17090622d700b0099279b2cdd5si121492eja.825.2023.08.09.15.16.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 15:16:31 -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=QOlxPhva; 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 1D81A38515CC for ; Wed, 9 Aug 2023 22:15:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1D81A38515CC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691619340; bh=2bqiaX8fGQCtU58i1/wgdRfXIIyLV0jx8UQ8dKb9s3I=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=QOlxPhvaH43YYpjBVbm13Ty9PI4toUU5zyx8iC+alVLc2KVN98FoN025KiXYb0So6 SvWkrEvqvqOohCcUkZDURoTXJoYape6sEsBY4Awe8m08hs0x9kyFWj8p11JLQ+A2VV sF9KTuNPveD0w9ur0xuu4oRSI9hy6QJJfRI4cC6s= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by sourceware.org (Postfix) with ESMTPS id 901593857B9B for ; Wed, 9 Aug 2023 22:14:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 901593857B9B Received: by mail-qt1-x836.google.com with SMTP id d75a77b69052e-40fe9c38800so1455701cf.0 for ; Wed, 09 Aug 2023 15:14:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691619288; x=1692224088; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2bqiaX8fGQCtU58i1/wgdRfXIIyLV0jx8UQ8dKb9s3I=; b=X2HGC2eGSJ/EBgCw452G++NKrH7Rxxe1inIsZga65i/GMhfyrPNBRvLGmMX9GPvtMW hub7XADdxz97ATvj86X5/pVNWUXq4cmC2nA4jqdlt5lGnLmqXmKNJIq8tTXTBmwk5ft+ FIoPHbyS6w91kSL10beHkxANaXWnKxsSZsUzeOJp6Vgg5+rzyqjekHmyO9MMzKD5g4Y2 MDyjxrdIyp44BPRF93Lss1oBQerl/9qrH4RzhaxRnXysAXh1EWJVEzz5FhTubx6GzItV Og+bs7YkuxzE6/fUgRZTWDgBBGevbHuJI+xpXWUSCO9ItcU40fD61hQStZJXJuEiNRPD vb8w== X-Gm-Message-State: AOJu0Yzl1JW+N3G6oObvKgM6q1w4lBZZ7SNavEMfuGMz9dGv0wANPNqC v9NHRtzWNz8Y9QuyywIxjC/6vNsiYYs= X-Received: by 2002:ac8:5981:0:b0:403:a91f:893d with SMTP id e1-20020ac85981000000b00403a91f893dmr992718qte.26.1691619287652; Wed, 09 Aug 2023 15:14:47 -0700 (PDT) Received: from localhost.localdomain (96-67-140-173-static.hfc.comcastbusiness.net. [96.67.140.173]) by smtp.gmail.com with ESMTPSA id ce11-20020a05622a41cb00b0040fef71dc1esm46334qtb.10.2023.08.09.15.14.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 15:14:47 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: David Malcolm , Lewis Hyatt Subject: [PATCH v4 2/8] libcpp: diagnostics: Support generated data in expanded locations Date: Wed, 9 Aug 2023 18:14:08 -0400 Message-Id: <20230809221414.2849878-3-lhyatt@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230809221414.2849878-1-lhyatt@gmail.com> References: <20230809221414.2849878-1-lhyatt@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3038.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Lewis Hyatt via Gcc-patches From: Lewis Hyatt Reply-To: Lewis Hyatt Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773791494741757416 X-GMAIL-MSGID: 1773791494741757416 The previous patch in this series introduced the concept of LC_GEN line maps. This patch continues on the path to using them to improve _Pragma diagnostics, by adding a new source_id SRC member to struct expanded_location, which is populated by linemap_expand_location. This member allows call sites to detect and handle when a location refers to generated data rather than a plain file name. The previous FILE member of expanded_location is preserved (although redundant with SRC), so that call sites which do not and never will care about generated data do not need to be concerned about it. Call sites that will care are modified here, to use SRC rather than FILE for comparing locations. libcpp/ChangeLog: * include/line-map.h (struct expanded_location): Add SRC member. Add zero-initializers for all members, since source_id is not a POD type. (class fixit_hint): Adjust prototype. * line-map.cc (linemap_expand_location): Populate the new SRC member in the expanded_location. (rich_location::maybe_add_fixit): Compare explocs with the new SRC field instead of the FILE field. (fixit_hint::affects_line_p): Accept a source_id instead of a file name, and use it for the comparisons. gcc/c-family/ChangeLog: * c-format.cc (get_corrected_substring): Compare explocs with the new SRC field instead of the FILE field. * c-indentation.cc (should_warn_for_misleading_indentation): Likewise. (assert_get_visual_column_succeeds): Initialize the SRC field in the test expanded_location. (assert_get_visual_column_fails): Likewise. gcc/ChangeLog: * diagnostic-show-locus.cc (make_range): Adapt to the new constructor semantics for struct expanded_location. (layout::maybe_add_location_range): Compare explocs with the new SRC field instead of the FILE field. (layout::validate_fixit_hint_p): Likewise. (layout::print_leading_fixits): Use the SRC field in struct expanded_location to query fixit_hint::affects_line_p. (layout::print_trailing_fixits): Likewise. * diagnostic.cc (diagnostic_report_current_module): Use the new SRC field in expanded_location to detect LC_GEN locations and identify them as such. (assert_location_text): Adapt to the new constructor semantics for struct expanded_location. * input.cc (expand_location_1): Likewise. And when libcpp's linemap_expand_location returns a null FILE for generated data, replace it with special_fname_generated (). (total_lines_num): Handle a generic source_id argument rather than a file name only. (get_source_text_between): Compare explocs with the new SRC field instead of the FILE field. (get_substring_ranges_for_loc): Likewise. * edit-context.cc (edit_context::apply_fixit): Ignore locations in generated data. * input.h (LOCATION_SRC): New accessor macro. --- gcc/c-family/c-format.cc | 4 ++-- gcc/c-family/c-indentation.cc | 10 +++++----- gcc/diagnostic-show-locus.cc | 30 +++++++++++++++++------------- gcc/diagnostic.cc | 19 ++++++++++++------- gcc/edit-context.cc | 2 +- gcc/input.cc | 21 +++++++++++---------- gcc/input.h | 1 + libcpp/include/line-map.h | 24 ++++++++++++++---------- libcpp/line-map.cc | 15 +++++++-------- 9 files changed, 70 insertions(+), 56 deletions(-) diff --git a/gcc/c-family/c-format.cc b/gcc/c-family/c-format.cc index b4eeebcb30e..529b1408179 100644 --- a/gcc/c-family/c-format.cc +++ b/gcc/c-family/c-format.cc @@ -4522,9 +4522,9 @@ get_corrected_substring (const substring_loc &fmt_loc, = expand_location_to_spelling_point (fmt_substring_range.m_start); expanded_location finish = expand_location_to_spelling_point (fmt_substring_range.m_finish); - if (caret.file != start.file) + if (caret.src != start.src) return NULL; - if (start.file != finish.file) + if (start.src != finish.src) return NULL; if (caret.line != start.line) return NULL; diff --git a/gcc/c-family/c-indentation.cc b/gcc/c-family/c-indentation.cc index e8d3dece770..fce74991aae 100644 --- a/gcc/c-family/c-indentation.cc +++ b/gcc/c-family/c-indentation.cc @@ -334,7 +334,7 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, const unsigned int tab_width = global_dc->tabstop; /* They must be in the same file. */ - if (next_stmt_exploc.file != body_exploc.file) + if (next_stmt_exploc.src != body_exploc.src) return false; /* If NEXT_STMT_LOC and BODY_LOC are on the same line, consider @@ -363,7 +363,7 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, ^ DON'T WARN HERE. */ if (next_stmt_exploc.line == body_exploc.line) { - if (guard_exploc.file != body_exploc.file) + if (guard_exploc.src != body_exploc.src) return true; if (guard_exploc.line < body_exploc.line) /* The guard is on a line before a line that contains both @@ -372,7 +372,7 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, else if (guard_exploc.line == body_exploc.line) { /* They're all on the same line. */ - gcc_assert (guard_exploc.file == next_stmt_exploc.file); + gcc_assert (guard_exploc.src == next_stmt_exploc.src); gcc_assert (guard_exploc.line == next_stmt_exploc.line); unsigned int guard_vis_column; unsigned int guard_line_first_nws; @@ -692,7 +692,7 @@ assert_get_visual_column_succeeds (const location &loc, unsigned int expected_first_nws) { expanded_location exploc; - exploc.file = file; + exploc.src = exploc.file = file; exploc.line = line; exploc.column = column; exploc.data = NULL; @@ -730,7 +730,7 @@ assert_get_visual_column_fails (const location &loc, const unsigned int tab_width) { expanded_location exploc; - exploc.file = file; + exploc.src = exploc.file = file; exploc.line = line; exploc.column = column; exploc.data = NULL; diff --git a/gcc/diagnostic-show-locus.cc b/gcc/diagnostic-show-locus.cc index a2aa6b4e0b5..bf969ab6d6a 100644 --- a/gcc/diagnostic-show-locus.cc +++ b/gcc/diagnostic-show-locus.cc @@ -697,9 +697,9 @@ static cpp_char_column_policy def_policy () } /* Create some expanded locations for testing layout_range. The filename - member of the explocs is set to the empty string. This member will only be + member of the explocs is set to NULL. This member will only be inspected by the calls to location_compute_display_column() made from the - layout_point constructors. That function will check for an empty filename + layout_point constructors. That function will check for a NULL filename argument and not attempt to open it, rather treating the non-existent data as if the display width were the same as the byte count. Tests exercising a real difference between byte count and display width are performed later, @@ -708,10 +708,14 @@ static cpp_char_column_policy def_policy () static layout_range make_range (int start_line, int start_col, int end_line, int end_col) { - const expanded_location start_exploc - = {"", start_line, start_col, NULL, false}; - const expanded_location finish_exploc - = {"", end_line, end_col, NULL, false}; + expanded_location start_exploc; + start_exploc.line = start_line; + start_exploc.column = start_col; + + expanded_location finish_exploc; + finish_exploc.line = end_line; + finish_exploc.column = end_col; + return layout_range (exploc_with_display_col (start_exploc, def_policy (), LOCATION_ASPECT_START), exploc_with_display_col (finish_exploc, def_policy (), @@ -1268,12 +1272,12 @@ layout::maybe_add_location_range (const location_range *loc_range, /* If any part of the range isn't in the same file as the primary location of this diagnostic, ignore the range. */ - if (start.file != m_exploc.file) + if (start.src != m_exploc.src) return false; - if (finish.file != m_exploc.file) + if (finish.src != m_exploc.src) return false; if (loc_range->m_range_display_kind == SHOW_RANGE_WITH_CARET) - if (caret.file != m_exploc.file) + if (caret.src != m_exploc.src) return false; /* Sanitize the caret location for non-primary ranges. */ @@ -1437,9 +1441,9 @@ layout::get_expanded_location (const line_span *line_span) const bool layout::validate_fixit_hint_p (const fixit_hint *hint) { - if (LOCATION_FILE (hint->get_start_loc ()) != m_exploc.file) + if (LOCATION_SRC (hint->get_start_loc ()) != m_exploc.src) return false; - if (LOCATION_FILE (hint->get_next_loc ()) != m_exploc.file) + if (LOCATION_SRC (hint->get_next_loc ()) != m_exploc.src) return false; return true; @@ -2102,7 +2106,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_exploc.src, row)) { /* Printing the '+' with normal colorization and the inserted line with "insert" colorization @@ -2554,7 +2558,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_exploc.src, row)) corrections.add_hint (hint); } diff --git a/gcc/diagnostic.cc b/gcc/diagnostic.cc index c523f215bae..10a377ea209 100644 --- a/gcc/diagnostic.cc +++ b/gcc/diagnostic.cc @@ -798,13 +798,17 @@ diagnostic_report_current_module (diagnostic_context *context, location_t where) if (!includes_seen (context, map)) { bool first = true, need_inc = true, was_module = MAP_MODULE_P (map); - expanded_location s = {}; + const bool was_gen = ORDINARY_MAP_GENERATED_DATA_P (map); + expanded_location s; do { where = linemap_included_from (map); map = linemap_included_from_linemap (line_table, map); bool is_module = MAP_MODULE_P (map); - s.file = LINEMAP_FILE (map); + s.src = ORDINARY_MAP_SOURCE_ID (map); + s.file = (s.src.is_buffer () + ? special_fname_generated () + : s.src.get_filename_or_buffer ()); s.line = SOURCE_LINE (map, where); int col = -1; if (first && context->show_column) @@ -823,10 +827,13 @@ diagnostic_report_current_module (diagnostic_context *context, location_t where) N_("of module"), N_("In module imported at"), /* 6 */ N_("imported at"), + N_("In buffer generated from"), /* 8 */ }; - unsigned index = (was_module ? 6 : is_module ? 4 - : need_inc ? 2 : 0) + !first; + const unsigned index + = was_gen ? 8 + : ((was_module ? 6 : is_module ? 4 : need_inc ? 2 : 0) + + !first); pp_verbatim (context->printer, "%s%s %r%s%s%R", first ? "" : was_module ? ", " : ",\n", @@ -2691,11 +2698,9 @@ assert_location_text (const char *expected_loc_text, dc.column_origin = origin; expanded_location xloc; - xloc.file = filename; + xloc.src = xloc.file = filename; xloc.line = line; xloc.column = column; - xloc.data = NULL; - xloc.sysp = false; char *actual_loc_text = diagnostic_get_location_text (&dc, xloc); ASSERT_STREQ (expected_loc_text, actual_loc_text); diff --git a/gcc/edit-context.cc b/gcc/edit-context.cc index 6f5bc6b9d8f..15052aec417 100644 --- a/gcc/edit-context.cc +++ b/gcc/edit-context.cc @@ -295,7 +295,7 @@ edit_context::apply_fixit (const fixit_hint *hint) { expanded_location start = expand_location (hint->get_start_loc ()); expanded_location next_loc = expand_location (hint->get_next_loc ()); - if (start.file != next_loc.file) + if (start.src != next_loc.src || start.src.is_buffer ()) return false; if (start.line != next_loc.line) return false; diff --git a/gcc/input.cc b/gcc/input.cc index c1735215b29..c2559614a99 100644 --- a/gcc/input.cc +++ b/gcc/input.cc @@ -236,8 +236,6 @@ expand_location_1 (location_t loc, loc = LOCATION_LOCUS (loc); } - memset (&xloc, 0, sizeof (xloc)); - if (loc >= RESERVED_LOCATION_COUNT) { if (!expansion_point_p) @@ -288,7 +286,12 @@ expand_location_1 (location_t loc, xloc.data = block; if (loc <= BUILTINS_LOCATION) - xloc.file = loc == UNKNOWN_LOCATION ? NULL : special_fname_builtin (); + { + xloc.file = loc == UNKNOWN_LOCATION ? NULL : special_fname_builtin (); + xloc.src = xloc.file; + } + else if (xloc.src.is_buffer ()) + xloc.file = special_fname_generated (); return xloc; } @@ -323,11 +326,11 @@ diagnostic_file_cache_fini (void) equals the actual number of lines of the file. */ static size_t -total_lines_num (const char *file_path) +total_lines_num (source_id src) { size_t r = 0; location_t l = 0; - if (linemap_get_file_highest_location (line_table, file_path, &l)) + if (linemap_get_file_highest_location (line_table, src, &l)) { gcc_assert (l >= RESERVED_LOCATION_COUNT); expanded_location xloc = expand_location (l); @@ -990,9 +993,7 @@ get_source_text_between (location_t start, location_t end) /* If the locations are in different files or the end comes before the start, give up and return nothing. */ - if (!expstart.file || !expend.file) - return NULL; - if (strcmp (expstart.file, expend.file) != 0) + if (!expstart.src || expend.src != expstart.src) return NULL; if (expstart.line > expend.line) return NULL; @@ -1788,7 +1789,7 @@ get_substring_ranges_for_loc (cpp_reader *pfile, expanded_location finish = expand_location_to_spelling_point (src_range.m_finish, LOCATION_ASPECT_FINISH); - if (start.file != finish.file) + if (start.src != finish.src) return "range endpoints are in different files"; if (start.line != finish.line) return "range endpoints are on different lines"; @@ -1839,7 +1840,7 @@ get_substring_ranges_for_loc (cpp_reader *pfile, return "start and finish are spelled in different ordinary maps"; /* The file from linemap_resolve_location ought to match that from expand_location_to_spelling_point. */ - if (ORDINARY_MAP_SOURCE_ID (start_ord_map) != start.file) + if (ORDINARY_MAP_SOURCE_ID (start_ord_map) != start.src) return "mismatching file after resolving linemap"; location_t start_loc diff --git a/gcc/input.h b/gcc/input.h index 1b81a995f86..5c578f1a9de 100644 --- a/gcc/input.h +++ b/gcc/input.h @@ -175,6 +175,7 @@ extern location_t location_with_discriminator (location_t, int); extern bool has_discriminator (location_t); extern int get_discriminator_from_loc (location_t); +#define LOCATION_SRC(LOC) ((expand_location (LOC)).src) #define LOCATION_FILE(LOC) ((expand_location (LOC)).file) #define LOCATION_LINE(LOC) ((expand_location (LOC)).line) #define LOCATION_COLUMN(LOC)((expand_location (LOC)).column) diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h index e59123b18c5..76617fe6129 100644 --- a/libcpp/include/line-map.h +++ b/libcpp/include/line-map.h @@ -1410,18 +1410,22 @@ linemap_location_before_p (class line_maps *set, typedef struct { - /* The name of the source file involved. */ - const char *file; + /* The file name of the location involved, or NULL if the location + is not in an external file. */ + const char *file = nullptr; - /* The line-location in the source file. */ - int line; - - int column; + /* A source_id recording the file name and/or the in-memory content, + as appropriate. Users that need to handle in-memory content need + to use this rather than FILE. */ + source_id src; - void *data; + /* The line-location in the source file. */ + int line = 0; + int column = 0; + void *data = nullptr; - /* In a system header?. */ - bool sysp; + /* In a system header? */ + bool sysp = false; } expanded_location; class range_label; @@ -2065,7 +2069,7 @@ 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 (source_id src, 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, diff --git a/libcpp/line-map.cc b/libcpp/line-map.cc index e63916054e0..7704c60773b 100644 --- a/libcpp/line-map.cc +++ b/libcpp/line-map.cc @@ -1905,8 +1905,6 @@ linemap_expand_location (line_maps *set, { expanded_location xloc; - - memset (&xloc, 0, sizeof (xloc)); if (IS_ADHOC_LOC (loc)) { xloc.data = get_data_from_adhoc_loc (set, loc); @@ -1932,8 +1930,9 @@ linemap_expand_location (line_maps *set, abort (); const line_map_ordinary *ord_map = linemap_check_ordinary (map); - - xloc.file = LINEMAP_FILE (ord_map); + xloc.src = ORDINARY_MAP_SOURCE_ID (ord_map); + if (!xloc.src.is_buffer ()) + xloc.file = xloc.src.get_filename_or_buffer (); xloc.line = SOURCE_LINE (ord_map, loc); xloc.column = SOURCE_COLUMN (ord_map, loc); xloc.sysp = LINEMAP_SYSP (ord_map) != 0; @@ -2534,7 +2533,7 @@ rich_location::maybe_add_fixit (location_t start, = linemap_client_expand_location_to_spelling_point (next_loc, LOCATION_ASPECT_START); /* They must be within the same file... */ - if (exploc_start.file != exploc_next_loc.file) + if (exploc_start.src != exploc_next_loc.src) { stop_supporting_fixits (); return; @@ -2619,19 +2618,19 @@ 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 (source_id src, int line) const { expanded_location exploc_start = linemap_client_expand_location_to_spelling_point (m_start, LOCATION_ASPECT_START); - if (file != exploc_start.file) + if (src != exploc_start.src) return false; if (line < exploc_start.line) return false; expanded_location exploc_next_loc = linemap_client_expand_location_to_spelling_point (m_next_loc, LOCATION_ASPECT_START); - if (file != exploc_next_loc.file) + if (src != exploc_next_loc.src) return false; if (line > exploc_next_loc.line) return false;