2011-08-19 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libcpp / include / line-map.h
blob3c84035e95fd896ca38b8b89aed45fbd73dd88cd
1 /* Map logical line numbers to (source file, line number) pairs.
2 Copyright (C) 2001, 2003, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
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.
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.
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/>.
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! */
23 #ifndef LIBCPP_LINE_MAP_H
24 #define LIBCPP_LINE_MAP_H
26 #ifndef GTY
27 #define GTY(x) /* nothing */
28 #endif
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};
38 /* The type of line numbers. */
39 typedef unsigned int linenum_type;
41 /* A logical line/column number, i.e. an "index" into a line_map. */
42 typedef unsigned int source_location;
44 /* Memory allocation function typedef. Works like xrealloc. */
45 typedef void *(*line_map_realloc) (void *, size_t);
47 /* Physical source file TO_FILE at line TO_LINE at column 0 is represented
48 by the logical START_LOCATION. TO_LINE+L at column C is represented by
49 START_LOCATION+(L*(1<<column_bits))+C, as long as C<(1<<column_bits),
50 and the result_location is less than the next line_map's start_location.
51 (The top line is line 1 and the leftmost column is column 1; line/column 0
52 means "entire file/line" or "unknown line/column" or "not applicable".)
53 INCLUDED_FROM is an index into the set that gives the line mapping
54 at whose end the current one was included. File(s) at the bottom
55 of the include stack have this set to -1. REASON is the reason for
56 creation of this line map, SYSP is one for a system header, two for
57 a C system header file that therefore needs to be extern "C"
58 protected in C++, and zero otherwise. */
59 struct GTY(()) line_map {
60 const char *to_file;
61 linenum_type to_line;
62 source_location start_location;
63 int included_from;
64 ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
65 /* The sysp field isn't really needed now that it's in cpp_buffer. */
66 unsigned char sysp;
67 /* Number of the low-order source_location bits used for a column number. */
68 unsigned int column_bits : 8;
71 /* A set of chronological line_map structures. */
72 struct GTY(()) line_maps {
73 struct line_map * GTY ((length ("%h.used"))) maps;
74 unsigned int allocated;
75 unsigned int used;
77 unsigned int cache;
79 /* Depth of the include stack, including the current file. */
80 unsigned int depth;
82 /* If true, prints an include trace a la -H. */
83 bool trace_includes;
85 /* Highest source_location "given out". */
86 source_location highest_location;
88 /* Start of line of highest source_location "given out". */
89 source_location highest_line;
91 /* The maximum column number we can quickly allocate. Higher numbers
92 may require allocating a new line_map. */
93 unsigned int max_column_hint;
95 /* If non-null, the allocator to use when resizing 'maps'. If null,
96 xrealloc is used. */
97 line_map_realloc reallocator;
100 /* Initialize a line map set. */
101 extern void linemap_init (struct line_maps *);
103 /* Free a line map set. */
104 extern void linemap_free (struct line_maps *);
106 /* Check for and warn about line_maps entered but not exited. */
108 extern void linemap_check_files_exited (struct line_maps *);
110 /* Return a source_location for the start (i.e. column==0) of
111 (physical) line TO_LINE in the current source file (as in the
112 most recent linemap_add). MAX_COLUMN_HINT is the highest column
113 number we expect to use in this line (but it does not change
114 the highest_location). */
116 extern source_location linemap_line_start
117 (struct line_maps *set, linenum_type to_line, unsigned int max_column_hint);
119 /* Add a mapping of logical source line to physical source file and
120 line number.
122 The text pointed to by TO_FILE must have a lifetime
123 at least as long as the final call to lookup_line (). An empty
124 TO_FILE means standard input. If reason is LC_LEAVE, and
125 TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
126 natural values considering the file we are returning to.
128 A call to this function can relocate the previous set of
129 maps, so any stored line_map pointers should not be used. */
130 extern const struct line_map *linemap_add
131 (struct line_maps *, enum lc_reason, unsigned int sysp,
132 const char *to_file, linenum_type to_line);
134 /* Given a logical line, returns the map from which the corresponding
135 (source file, line) pair can be deduced. */
136 extern const struct line_map *linemap_lookup
137 (struct line_maps *, source_location);
139 /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
140 be reserved for libcpp user as special values, no token from libcpp
141 will contain any of those locations. */
142 #define RESERVED_LOCATION_COUNT 2
144 /* Converts a map and a source_location to source line. */
145 #define SOURCE_LINE(MAP, LOC) \
146 ((((LOC) - (MAP)->start_location) >> (MAP)->column_bits) + (MAP)->to_line)
148 #define SOURCE_COLUMN(MAP, LOC) \
149 (((LOC) - (MAP)->start_location) & ((1 << (MAP)->column_bits) - 1))
151 /* Returns the last source line within a map. This is the (last) line
152 of the #include, or other directive, that caused a map change. */
153 #define LAST_SOURCE_LINE(MAP) \
154 SOURCE_LINE (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
155 #define LAST_SOURCE_COLUMN(MAP) \
156 SOURCE_COLUMN (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)
162 /* Returns the map a given map was included from. */
163 #define INCLUDED_FROM(SET, MAP) (&(SET)->maps[(MAP)->included_from])
165 /* Nonzero if the map is at the bottom of the include stack. */
166 #define MAIN_FILE_P(MAP) ((MAP)->included_from < 0)
168 extern source_location
169 linemap_position_for_column (struct line_maps *set, unsigned int to_column);
171 #endif /* !LIBCPP_LINE_MAP_H */