public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "libin.dang at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug preprocessor/108900] [libcpp] cpp gives wrong line number information with a file huge number of lines
Date: Thu, 28 Dec 2023 14:10:02 +0000	[thread overview]
Message-ID: <bug-108900-4-E2QqchL2c2@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-108900-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108900

--- Comment #3 from Libin Dang <libin.dang at gmail dot com> ---
For the uploaded test case, `-fdump-internal-locations' gives the following
output:

ORDINARY MAP: 9
  location_t interval: 274912 <= loc < 1342178464
  file: header3.h
  starting at line: 1
  column and range bits: 12
  column bits: 7
  range bits: 5 
  reason: 0 (LC_ENTER)
  included from location: 270816 (in ordinary map 8)
header3.h:  1|loc:274912|header3 begins
                        |44555555555555
                        |99000111222233
                        |47047036036926
                        |46802468024680
header3.h:  2|loc:279008|header3 begins
                        |99999999999999
                        |00111222233344
                        |47036036926925
                        |02468024680246
...
header3.h:327613|loc:1342173664|header3 begins
                               |33333333334444
                               |67778889990001
                               |92692582581481
                               |68024680246802
header3.h:327614|loc:1342177760|#include "header2.h"
                               |77777778888888888888
                               |78889990001112223334
                               |92582581481470470360
                               |24680246802468024680

However, location 1342173664 (0x4ffff1e0) <
LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES (0x50000000) but location 1342177760
(0x500001e0) > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES, therefore these two
locations should not in the same line map.

In linemap_line_start() of libcpp/line-map.cc, if it does not create new line
map (add_map is false), it will return the following new location:

 r = set->highest_line + (line_delta << map->m_column_and_range_bits);

and this location will be included in the last ordinary map, even that location
is greater than LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES and therefore should
be in a new line map.

The following patch can fix this issue:

--- a/libcpp/line-map.cc
+++ b/libcpp/line-map.cc
@@ -860,7 +860,9 @@ linemap_line_start (line_maps *set, linenum_type to_line,
       || (highest > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES
          && map->m_range_bits > 0)
       || (highest > LINE_MAP_MAX_LOCATION_WITH_COLS
-         && (set->max_column_hint || highest >= LINE_MAP_MAX_LOCATION)))
+         && (set->max_column_hint || highest >= LINE_MAP_MAX_LOCATION))
+      || ((set->highest_line + (line_delta << map->m_column_and_range_bits))
+         > LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES))
     add_map = true;
   else
     max_column_hint = set->max_column_hint;

After applied this change, the `-fdump-internal-locations' options gives the
following results:

ORDINARY MAP: 9
  location_t interval: 274912 <= loc < 1342174176
  file: header3.h
  starting at line: 1
  column and range bits: 12
  column bits: 7
  range bits: 5
  reason: 0 (LC_ENTER)
  included from location: 270816 (in ordinary map 8)
header3.h:  1|loc:274912|header3 begins
                        |44555555555555
                        |99000111222233
                        |47047036036926
                        |46802468024680
header3.h:  2|loc:279008|header3 begins
                        |99999999999999
                        |00111222233344
                        |47036036926925
                        |02468024680246
...
header3.h:327613|loc:1342173664|header3 begins
                               |33333333334444
                               |67778889990001
                               |92692582581481
                               |68024680246802

ORDINARY MAP: 10
  location_t interval: 1342174176 <= loc < 1342178272
  file: header3.h
  starting at line: 327614
  column and range bits: 12
  column bits: 7
  range bits: 5
  reason: 2 (LC_RENAME)
  included from location: 270816 (in ordinary map 8)
header3.h:327614|loc:1342174176|#include "header2.h"
                               |44444444444444444444
                               |22233344445556667778
                               |04703603692692582581
                               |80246802468024680246

      parent reply	other threads:[~2023-12-28 14:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-23  8:35 [Bug preprocessor/108900] New: [libcpp] cpp gives wrong line number information libin.dang at gmail dot com
2023-02-25 14:50 ` [Bug preprocessor/108900] [libcpp] cpp gives wrong line number information with a file huge number of lines libin.dang at gmail dot com
2023-05-04 10:19 ` libin.dang at gmail dot com
2023-12-28 14:10 ` libin.dang at gmail dot com [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-108900-4-E2QqchL2c2@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).