OSDN Git Service

2009-04-21 Taras Glek <tglek@mozilla.com>
[pf3gnuchains/gcc-fork.git] / libcpp / include / line-map.h
1 /* Map logical line numbers to (source file, line number) pairs.
2    Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009
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 3, 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; see the file COPYING3.  If not see
17 <http://www.gnu.org/licenses/>.
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 #ifndef LIBCPP_LINE_MAP_H
24 #define LIBCPP_LINE_MAP_H
25
26 #ifndef GTY
27 #define GTY(x) /* nothing */
28 #endif
29
30 /* Reason for adding a line change with add_line_map ().  LC_ENTER is
31    when including a new file, e.g. a #include directive in C.
32    LC_LEAVE is when reaching a file's end.  LC_RENAME is when a file
33    name or line number changes for neither of the above reasons
34    (e.g. a #line directive in C); LC_RENAME_VERBATIM is like LC_RENAME
35    but a filename of "" is not specially interpreted as standard input.  */
36 enum lc_reason {LC_ENTER = 0, LC_LEAVE, LC_RENAME, LC_RENAME_VERBATIM};
37
38 /* The type of line numbers.  */
39 typedef unsigned int linenum_type;
40
41 /* A logical line/column number, i.e. an "index" into a line_map.  */
42 /* Long-term, we want to use this to replace struct location_s (in input.h),
43    and effectively typedef source_location location_t.  */
44 typedef unsigned int source_location;
45
46 /* Memory allocation function typedef.  Works like xrealloc.  */
47 typedef void *(*line_map_realloc) (void *, size_t);
48
49 /* Physical source file TO_FILE at line TO_LINE at column 0 is represented
50    by the logical START_LOCATION.  TO_LINE+L at column C is represented by
51    START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
52    and the result_location is less than the next line_map's start_location.
53    (The top line is line 1 and the leftmost column is column 1; line/column 0
54    means "entire file/line" or "unknown line/column" or "not applicable".)
55    INCLUDED_FROM is an index into the set that gives the line mapping
56    at whose end the current one was included.  File(s) at the bottom
57    of the include stack have this set to -1.  REASON is the reason for
58    creation of this line map, SYSP is one for a system header, two for
59    a C system header file that therefore needs to be extern "C"
60    protected in C++, and zero otherwise.  */
61 struct GTY(()) line_map {
62   const char *to_file;
63   linenum_type to_line;
64   source_location start_location;
65   int included_from;
66   ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
67   /* The sysp field isn't really needed now that it's in cpp_buffer.  */
68   unsigned char sysp;
69   /* Number of the low-order source_location bits used for a column number.  */
70   unsigned int column_bits : 8;
71 };
72
73 /* A set of chronological line_map structures.  */
74 struct GTY(()) line_maps {
75   struct line_map * GTY ((length ("%h.used"))) maps;
76   unsigned int allocated;
77   unsigned int used;
78
79   unsigned int cache;
80
81   /* The most recently listed include stack, if any, starts with
82      LAST_LISTED as the topmost including file.  -1 indicates nothing
83      has been listed yet.  */
84   int last_listed;
85
86   /* Depth of the include stack, including the current file.  */
87   unsigned int depth;
88
89   /* If true, prints an include trace a la -H.  */
90   bool trace_includes;
91
92   /* Highest source_location "given out".  */
93   source_location highest_location;
94
95   /* Start of line of highest source_location "given out".  */
96   source_location highest_line;
97
98   /* The maximum column number we can quickly allocate.  Higher numbers
99      may require allocating a new line_map.  */
100   unsigned int max_column_hint;
101
102   /* If non-null, the allocator to use when resizing 'maps'.  If null,
103      xrealloc is used.  */
104   line_map_realloc reallocator;
105 };
106
107 /* Initialize a line map set.  */
108 extern void linemap_init (struct line_maps *);
109
110 /* Free a line map set.  */
111 extern void linemap_free (struct line_maps *);
112
113 /* Check for and warn about line_maps entered but not exited.  */
114
115 extern void linemap_check_files_exited (struct line_maps *);
116
117 /* Return a source_location for the start (i.e. column==0) of
118    (physical) line TO_LINE in the current source file (as in the
119    most recent linemap_add).   MAX_COLUMN_HINT is the highest column
120    number we expect to use in this line (but it does not change
121    the highest_location).  */
122
123 extern source_location linemap_line_start
124 (struct line_maps *set, linenum_type to_line,  unsigned int max_column_hint);
125
126 /* Add a mapping of logical source line to physical source file and
127    line number.
128
129    The text pointed to by TO_FILE must have a lifetime
130    at least as long as the final call to lookup_line ().  An empty
131    TO_FILE means standard input.  If reason is LC_LEAVE, and
132    TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
133    natural values considering the file we are returning to.
134
135    A call to this function can relocate the previous set of
136    maps, so any stored line_map pointers should not be used.  */
137 extern const struct line_map *linemap_add
138   (struct line_maps *, enum lc_reason, unsigned int sysp,
139    const char *to_file, linenum_type to_line);
140
141 /* Given a logical line, returns the map from which the corresponding
142    (source file, line) pair can be deduced.  */
143 extern const struct line_map *linemap_lookup
144   (struct line_maps *, source_location);
145
146 /* Converts a map and a source_location to source line.  */
147 #define SOURCE_LINE(MAP, LOC) \
148   ((((LOC) - (MAP)->start_location) >> (MAP)->column_bits) + (MAP)->to_line)
149
150 #define SOURCE_COLUMN(MAP, LOC) \
151   (((LOC) - (MAP)->start_location) & ((1 << (MAP)->column_bits) - 1))
152
153 /* Returns the last source line within a map.  This is the (last) line
154    of the #include, or other directive, that caused a map change.  */
155 #define LAST_SOURCE_LINE(MAP) \
156   SOURCE_LINE (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
157 #define LAST_SOURCE_LINE_LOCATION(MAP) \
158   ((((MAP)[1].start_location - 1 - (MAP)->start_location) \
159     & ~((1 << (MAP)->column_bits) - 1))                   \
160    + (MAP)->start_location)
161
162 /* Returns the map a given map was included from.  */
163 #define INCLUDED_FROM(SET, MAP) (&(SET)->maps[(MAP)->included_from])
164
165 /* Nonzero if the map is at the bottom of the include stack.  */
166 #define MAIN_FILE_P(MAP) ((MAP)->included_from < 0)
167
168 /* Set LOC to a source position that is the same line as the most recent
169    linemap_line_start, but with the specified TO_COLUMN column number.  */
170
171 #define LINEMAP_POSITION_FOR_COLUMN(LOC, SET, TO_COLUMN) do { \
172   unsigned int to_column = (TO_COLUMN); \
173   struct line_maps *set = (SET); \
174   if (__builtin_expect (to_column >= set->max_column_hint, 0)) \
175     (LOC) = linemap_position_for_column (set, to_column); \
176   else { \
177     source_location r = set->highest_line; \
178     r = r + to_column; \
179     if (r >= set->highest_location) \
180       set->highest_location = r; \
181     (LOC) = r;                   \
182   }} while (0)
183     
184
185 extern source_location
186 linemap_position_for_column (struct line_maps *set, unsigned int to_column);
187 #endif /* !LIBCPP_LINE_MAP_H  */