OSDN Git Service

* line-map.c: New.
[pf3gnuchains/gcc-fork.git] / gcc / line-map.c
1 /* Map logical line numbers to (source file, line number) pairs.
2    Copyright (C) 2001
3    Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19  In other words, you are welcome to use, share and improve this program.
20  You are forbidden to forbid anyone else to use, share and improve
21  what you give them.   Help stamp out software-hoarding!  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "line-map.h"
26
27 /* Initialize a line map set.  */
28
29 void
30 init_line_maps (set)
31      struct line_maps *set;
32 {
33   set->maps = 0;
34   set->allocated = 0;
35   set->used = 0;
36 }
37
38 /* Free a line map set.  */
39
40 void free_line_maps (set)
41      struct line_maps *set;
42 {
43   if (set->maps)
44     free (set->maps);
45 }
46
47 /* Add a mapping of logical source line to physical source file and
48    line number.  Ther text pointed to by TO_FILE must have a lifetime
49    at least as long as the final call to lookup_line ().
50
51    FROM_LINE should be monotonic increasing across calls to this
52    function.  */
53
54 struct line_map *
55 add_line_map (set, reason, from_line, to_file, to_line)
56      struct line_maps *set;
57      enum lc_reason reason;
58      unsigned int from_line;
59      const char *to_file;
60      unsigned int to_line;
61 {
62   struct line_map *map;
63
64   if (set->used && from_line < set->maps[set->used - 1].from_line)
65     abort ();
66
67   if (set->used == set->allocated)
68     {
69       set->allocated = 2 * set->allocated + 256;
70       set->maps = (struct line_map *)
71         xrealloc (set->maps, set->allocated * sizeof (struct line_map));
72     }
73
74   map = &set->maps[set->used];
75   map->from_line = from_line;
76   map->to_file = to_file;
77   map->to_line = to_line;
78
79   if (set->used == 0)
80     map->included_from = -1;
81   else if (reason == LC_ENTER)
82     map->included_from = set->used - 1;
83   else if (reason == LC_RENAME)
84     map->included_from = map[-1].included_from;
85   else if (reason == LC_LEAVE)
86     {
87       if (map[-1].included_from < 0)
88         abort ();
89       map->included_from = set->maps[map[-1].included_from].included_from;
90     }
91
92   set->used++;
93   return map;
94 }
95
96 /* Translate a logical line number into a (source file, line) pair.  */
97
98 struct line_map *
99 lookup_line (set, line)
100      struct line_maps *set;
101      unsigned int line;
102 {
103   unsigned int md, mn = 0, mx = set->used;
104
105   if (mx == 0)
106     abort ();
107
108   while (mx - mn > 1)
109     {
110       md = (mn + mx) / 2;
111       if (set->maps[md].from_line > line)
112         mx = md;
113       else
114         mn = md;
115     }
116
117   return &set->maps[mn];
118 }