OSDN Git Service

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