]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcpp/include/line-map.h
traditional.c (lex_identifier): Use CPP_HASHNODE.
[thirdparty/gcc.git] / libcpp / include / line-map.h
CommitLineData
d82fc108 1/* Map logical line numbers to (source file, line number) pairs.
5ffeb913 2 Copyright (C) 2001, 2003, 2004, 2007
d82fc108
NB
3 Free Software Foundation, Inc.
4
5This program is free software; you can redistribute it and/or modify it
6under the terms of the GNU General Public License as published by the
7Free Software Foundation; either version 2, or (at your option) any
8later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
200031d1 17Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
d82fc108
NB
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
4f4e53dd
PB
23#ifndef LIBCPP_LINE_MAP_H
24#define LIBCPP_LINE_MAP_H
d82fc108 25
5ffeb913
TT
26#ifndef GTY
27#define GTY(x) /* nothing */
28#endif
29
47d89cf3
NB
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). */
35enum lc_reason {LC_ENTER = 0, LC_LEAVE, LC_RENAME};
36
12f9df4e 37/* A logical line/column number, i.e. an "index" into a line_map. */
8b82c528 38/* Long-term, we want to use this to replace struct location_s (in input.h),
7d40b45f
PB
39 and effectively typedef source_location location_t. */
40typedef unsigned int source_location;
8b82c528 41
5ffeb913
TT
42/* Memory allocation function typedef. Works like xrealloc. */
43typedef void *(*line_map_realloc) (void *, size_t);
44
12f9df4e
PB
45/* Physical source file TO_FILE at line TO_LINE at column 0 is represented
46 by the logical START_LOCATION. TO_LINE+L at column C is represented by
47 START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
48 and the result_location is less than the next line_map's start_location.
49 (The top line is line 1 and the leftmost column is column 1; line/column 0
50 means "entire file/line" or "unknown line/column" or "not applicable".)
51 INCLUDED_FROM is an index into the set that gives the line mapping
52 at whose end the current one was included. File(s) at the bottom
53 of the include stack have this set to -1. REASON is the reason for
54 creation of this line map, SYSP is one for a system header, two for
55 a C system header file that therefore needs to be extern "C"
56 protected in C++, and zero otherwise. */
5ffeb913 57struct line_map GTY(())
d82fc108
NB
58{
59 const char *to_file;
60 unsigned int to_line;
12f9df4e 61 source_location start_location;
d82fc108 62 int included_from;
47d89cf3 63 ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
9ac97460 64 /* The sysp field isn't really needed now that it's in cpp_buffer. */
47d89cf3 65 unsigned char sysp;
9ac97460 66 /* Number of the low-order source_location bits used for a column number. */
12f9df4e 67 unsigned int column_bits : 8;
d82fc108
NB
68};
69
ef6e958a 70/* A set of chronological line_map structures. */
5ffeb913 71struct line_maps GTY(())
d82fc108 72{
5ffeb913 73 struct line_map * GTY ((length ("%h.used"))) maps;
d82fc108
NB
74 unsigned int allocated;
75 unsigned int used;
fde84349 76
9132fbb7
PB
77 unsigned int cache;
78
fde84349
NB
79 /* The most recently listed include stack, if any, starts with
80 LAST_LISTED as the topmost including file. -1 indicates nothing
81 has been listed yet. */
82 int last_listed;
5993019d 83
d8693c6f
NB
84 /* Depth of the include stack, including the current file. */
85 unsigned int depth;
86
5993019d
NB
87 /* If true, prints an include trace a la -H. */
88 bool trace_includes;
12f9df4e 89
9ac97460 90 /* Highest source_location "given out". */
12f9df4e
PB
91 source_location highest_location;
92
500bee0a
PB
93 /* Start of line of highest source_location "given out". */
94 source_location highest_line;
95
12f9df4e 96 /* The maximum column number we can quickly allocate. Higher numbers
9ac97460 97 may require allocating a new line_map. */
12f9df4e 98 unsigned int max_column_hint;
5ffeb913
TT
99
100 /* If non-null, the allocator to use when resizing 'maps'. If null,
101 xrealloc is used. */
102 line_map_realloc reallocator;
d82fc108
NB
103};
104
d82fc108 105/* Initialize a line map set. */
a2f7be91 106extern void linemap_init (struct line_maps *);
d82fc108
NB
107
108/* Free a line map set. */
a2f7be91 109extern void linemap_free (struct line_maps *);
d82fc108 110
9ac97460 111/* Check for and warn about line_maps entered but not exited. */
12f9df4e
PB
112
113extern void linemap_check_files_exited (struct line_maps *);
114
115/* Return a source_location for the start (i.e. column==0) of
116 (physical) line TO_LINE in the current source file (as in the
117 most recent linemap_add). MAX_COLUMN_HINT is the highest column
118 number we expect to use in this line (but it does not change
9ac97460 119 the highest_location). */
12f9df4e
PB
120
121extern source_location linemap_line_start
a2981930 122(struct line_maps *set, unsigned int to_line, unsigned int max_column_hint);
12f9df4e 123
d82fc108 124/* Add a mapping of logical source line to physical source file and
9074464c
GK
125 line number.
126
127 The text pointed to by TO_FILE must have a lifetime
128 at least as long as the final call to lookup_line (). An empty
129 TO_FILE means standard input. If reason is LC_LEAVE, and
47d89cf3
NB
130 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
131 natural values considering the file we are returning to.
d82fc108 132
a2981930 133 A call to this function can relocate the previous set of
42dcba34 134 maps, so any stored line_map pointers should not be used. */
a2f7be91 135extern const struct line_map *linemap_add
e2b9853b 136 (struct line_maps *, enum lc_reason, unsigned int sysp,
12f9df4e 137 const char *to_file, unsigned int to_line);
d82fc108
NB
138
139/* Given a logical line, returns the map from which the corresponding
140 (source file, line) pair can be deduced. */
7d40b45f
PB
141extern const struct line_map *linemap_lookup
142 (struct line_maps *, source_location);
d82fc108 143
fde84349
NB
144/* Print the file names and line numbers of the #include commands
145 which led to the map MAP, if any, to stderr. Nothing is output if
146 the most recently listed stack is the same as the current one. */
a2f7be91
ZW
147extern void linemap_print_containing_files (struct line_maps *,
148 const struct line_map *);
fde84349 149
12f9df4e
PB
150/* Converts a map and a source_location to source line. */
151#define SOURCE_LINE(MAP, LINE) \
152 ((((LINE) - (MAP)->start_location) >> (MAP)->column_bits) + (MAP)->to_line)
153
154#define SOURCE_COLUMN(MAP, LINE) \
155 (((LINE) - (MAP)->start_location) & ((1 << (MAP)->column_bits) - 1))
d82fc108
NB
156
157/* Returns the last source line within a map. This is the (last) line
158 of the #include, or other directive, that caused a map change. */
12f9df4e
PB
159#define LAST_SOURCE_LINE(MAP) \
160 SOURCE_LINE (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
161#define LAST_SOURCE_LINE_LOCATION(MAP) \
22234f56 162 ((((MAP)[1].start_location - 1 - (MAP)->start_location) \
12f9df4e
PB
163 & ~((1 << (MAP)->column_bits) - 1)) \
164 + (MAP)->start_location)
d82fc108 165
fde84349
NB
166/* Returns the map a given map was included from. */
167#define INCLUDED_FROM(SET, MAP) (&(SET)->maps[(MAP)->included_from])
168
cc2902df 169/* Nonzero if the map is at the bottom of the include stack. */
d82fc108
NB
170#define MAIN_FILE_P(MAP) ((MAP)->included_from < 0)
171
500bee0a 172/* Set LOC to a source position that is the same line as the most recent
9ac97460 173 linemap_line_start, but with the specified TO_COLUMN column number. */
67821e3a 174
2bf41bf0 175#define LINEMAP_POSITION_FOR_COLUMN(LOC, SET, TO_COLUMN) do { \
500bee0a
PB
176 unsigned int to_column = (TO_COLUMN); \
177 struct line_maps *set = (SET); \
178 if (__builtin_expect (to_column >= set->max_column_hint, 0)) \
179 (LOC) = linemap_position_for_column (set, to_column); \
180 else { \
181 source_location r = set->highest_line; \
182 r = r + to_column; \
183 if (r >= set->highest_location) \
184 set->highest_location = r; \
185 (LOC) = r; \
2bf41bf0 186 }} while (0)
500bee0a
PB
187
188
189extern source_location
190linemap_position_for_column (struct line_maps *set, unsigned int to_column);
4f4e53dd 191#endif /* !LIBCPP_LINE_MAP_H */