line-map.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /* Map logical line numbers to (source file, line number) pairs.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 3, or (at your option) any
  6. later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING3. If not see
  13. <http://www.gnu.org/licenses/>.
  14. In other words, you are welcome to use, share and improve this program.
  15. You are forbidden to forbid anyone else to use, share and improve
  16. what you give them. Help stamp out software-hoarding! */
  17. #ifndef LIBCPP_LINE_MAP_H
  18. #define LIBCPP_LINE_MAP_H
  19. #ifndef GTY
  20. #define GTY(x) /* nothing */
  21. #endif
  22. /* Reason for creating a new line map with linemap_add. LC_ENTER is
  23. when including a new file, e.g. a #include directive in C.
  24. LC_LEAVE is when reaching a file's end. LC_RENAME is when a file
  25. name or line number changes for neither of the above reasons
  26. (e.g. a #line directive in C); LC_RENAME_VERBATIM is like LC_RENAME
  27. but a filename of "" is not specially interpreted as standard
  28. input. LC_ENTER_MACRO is when a macro expansion is about to start. */
  29. enum lc_reason
  30. {
  31. LC_ENTER = 0,
  32. LC_LEAVE,
  33. LC_RENAME,
  34. LC_RENAME_VERBATIM,
  35. LC_ENTER_MACRO
  36. /* FIXME: add support for stringize and paste. */
  37. };
  38. /* The type of line numbers. */
  39. typedef unsigned int linenum_type;
  40. /* A logical line/column number, i.e. an "index" into a line_map. */
  41. typedef unsigned int source_location;
  42. /* Memory allocation function typedef. Works like xrealloc. */
  43. typedef void *(*line_map_realloc) (void *, size_t);
  44. /* Memory allocator function that returns the actual allocated size,
  45. for a given requested allocation. */
  46. typedef size_t (*line_map_round_alloc_size_func) (size_t);
  47. /* An ordinary line map encodes physical source locations. Those
  48. physical source locations are called "spelling locations".
  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. The highest possible source location is MAX_SOURCE_LOCATION. */
  56. struct GTY(()) line_map_ordinary {
  57. const char *to_file;
  58. linenum_type to_line;
  59. /* An index into the set that gives the line mapping at whose end
  60. the current one was included. File(s) at the bottom of the
  61. include stack have this set to -1. */
  62. int included_from;
  63. /* SYSP is one for a system header, two for a C system header file
  64. that therefore needs to be extern "C" protected in C++, and zero
  65. otherwise. This field isn't really needed now that it's in
  66. cpp_buffer. */
  67. unsigned char sysp;
  68. /* Number of the low-order source_location bits used for a column number. */
  69. unsigned int column_bits : 8;
  70. };
  71. /* This is the highest possible source location encoded within an
  72. ordinary or macro map. */
  73. #define MAX_SOURCE_LOCATION 0x7FFFFFFF
  74. struct cpp_hashnode;
  75. /* A macro line map encodes location of tokens coming from a macro
  76. expansion.
  77. Please note that this struct line_map_macro is a field of struct
  78. line_map below, go read the comments of struct line_map below and
  79. then come back here.
  80. The offset from START_LOCATION is used to index into
  81. MACRO_LOCATIONS; this holds the original location of the token. */
  82. struct GTY(()) line_map_macro {
  83. /* The cpp macro which expansion gave birth to this macro map. */
  84. struct cpp_hashnode * GTY ((nested_ptr (union tree_node,
  85. "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
  86. "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
  87. macro;
  88. /* The number of tokens inside the replacement-list of MACRO. */
  89. unsigned int n_tokens;
  90. /* This array of location is actually an array of pairs of
  91. locations. The elements inside it thus look like:
  92. x0,y0, x1,y1, x2,y2, ...., xn,yn.
  93. where n == n_tokens;
  94. Remember that these xI,yI are collected when libcpp is about to
  95. expand a given macro.
  96. yI is the location in the macro definition, either of the token
  97. itself or of a macro parameter that it replaces.
  98. Imagine this:
  99. #define PLUS(A, B) A + B <--- #1
  100. int a = PLUS (1,2); <--- #2
  101. There is a macro map for the expansion of PLUS in #2. PLUS is
  102. expanded into its expansion-list. The expansion-list is the
  103. replacement-list of PLUS where the macro parameters are replaced
  104. with their arguments. So the replacement-list of PLUS is made of
  105. the tokens:
  106. A, +, B
  107. and the expansion-list is made of the tokens:
  108. 1, +, 2
  109. Let's consider the case of token "+". Its y1 [yI for I == 1] is
  110. its spelling location in #1.
  111. y0 (thus for token "1") is the spelling location of A in #1.
  112. And y2 (of token "2") is the spelling location of B in #1.
  113. When the token is /not/ an argument for a macro, xI is the same
  114. location as yI. Otherwise, xI is the location of the token
  115. outside this macro expansion. If this macro was expanded from
  116. another macro expansion, xI is a virtual location representing
  117. the token in that macro expansion; otherwise, it is the spelling
  118. location of the token.
  119. Note that a virtual location is a location returned by
  120. linemap_add_macro_token. It encodes the relevant locations (x,y
  121. pairs) of that token across the macro expansions from which it
  122. (the token) might come from.
  123. In the example above x1 (for token "+") is going to be the same
  124. as y1. x0 is the spelling location for the argument token "1",
  125. and x2 is the spelling location for the argument token "2". */
  126. source_location * GTY((atomic)) macro_locations;
  127. /* This is the location of the expansion point of the current macro
  128. map. It's the location of the macro name. That location is held
  129. by the map that was current right before the current one. It
  130. could have been either a macro or an ordinary map, depending on
  131. if we are in a nested expansion context not. */
  132. source_location expansion;
  133. };
  134. /* A line_map encodes a sequence of locations.
  135. There are two kinds of maps. Ordinary maps and macro expansion
  136. maps, a.k.a macro maps.
  137. A macro map encodes source locations of tokens that are part of a
  138. macro replacement-list, at a macro expansion point. E.g, in:
  139. #define PLUS(A,B) A + B
  140. No macro map is going to be created there, because we are not at a
  141. macro expansion point. We are at a macro /definition/ point. So the
  142. locations of the tokens of the macro replacement-list (i.e, A + B)
  143. will be locations in an ordinary map, not a macro map.
  144. On the other hand, if we later do:
  145. int a = PLUS (1,2);
  146. The invocation of PLUS here is a macro expansion. So we are at a
  147. macro expansion point. The preprocessor expands PLUS (1,2) and
  148. replaces it with the tokens of its replacement-list: 1 + 2. A macro
  149. map is going to be created to hold (or rather to map, haha ...) the
  150. locations of the tokens 1, + and 2. The macro map also records the
  151. location of the expansion point of PLUS. That location is mapped in
  152. the map that is active right before the location of the invocation
  153. of PLUS. */
  154. struct GTY(()) line_map {
  155. source_location start_location;
  156. /* The reason for creation of this line map. */
  157. ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
  158. union map_u {
  159. struct line_map_ordinary GTY((tag ("0"))) ordinary;
  160. struct line_map_macro GTY((tag ("1"))) macro;
  161. } GTY((desc ("%1.reason == LC_ENTER_MACRO"))) d;
  162. };
  163. #define MAP_START_LOCATION(MAP) (MAP)->start_location
  164. #define ORDINARY_MAP_FILE_NAME(MAP) \
  165. linemap_check_ordinary (MAP)->d.ordinary.to_file
  166. #define ORDINARY_MAP_STARTING_LINE_NUMBER(MAP) \
  167. linemap_check_ordinary (MAP)->d.ordinary.to_line
  168. #define ORDINARY_MAP_INCLUDER_FILE_INDEX(MAP) \
  169. linemap_check_ordinary (MAP)->d.ordinary.included_from
  170. #define ORDINARY_MAP_IN_SYSTEM_HEADER_P(MAP) \
  171. linemap_check_ordinary (MAP)->d.ordinary.sysp
  172. #define ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(MAP) \
  173. linemap_check_ordinary (MAP)->d.ordinary.column_bits
  174. #define MACRO_MAP_MACRO(MAP) (MAP)->d.macro.macro
  175. #define MACRO_MAP_NUM_MACRO_TOKENS(MAP) (MAP)->d.macro.n_tokens
  176. #define MACRO_MAP_LOCATIONS(MAP) (MAP)->d.macro.macro_locations
  177. #define MACRO_MAP_EXPANSION_POINT_LOCATION(MAP) (MAP)->d.macro.expansion
  178. /* The abstraction of a set of location maps. There can be several
  179. types of location maps. This abstraction contains the attributes
  180. that are independent from the type of the map. */
  181. struct GTY(()) maps_info {
  182. /* This array contains the different line maps.
  183. A line map is created for the following events:
  184. - when a new preprocessing unit start.
  185. - when a preprocessing unit ends.
  186. - when a macro expansion occurs. */
  187. struct line_map * GTY ((length ("%h.used"))) maps;
  188. /* The total number of allocated maps. */
  189. unsigned int allocated;
  190. /* The number of elements used in maps. This number is smaller
  191. or equal to ALLOCATED. */
  192. unsigned int used;
  193. unsigned int cache;
  194. };
  195. /* Data structure to associate an arbitrary data to a source location. */
  196. struct GTY(()) location_adhoc_data {
  197. source_location locus;
  198. void * GTY((skip)) data;
  199. };
  200. struct htab;
  201. /* The following data structure encodes a location with some adhoc data
  202. and maps it to a new unsigned integer (called an adhoc location)
  203. that replaces the original location to represent the mapping.
  204. The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
  205. highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
  206. the original location. Once identified as the adhoc_loc, the lower 31
  207. bits of the integer is used to index the location_adhoc_data array,
  208. in which the locus and associated data is stored. */
  209. struct GTY(()) location_adhoc_data_map {
  210. struct htab * GTY((skip)) htab;
  211. source_location curr_loc;
  212. unsigned int allocated;
  213. struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
  214. };
  215. /* A set of chronological line_map structures. */
  216. struct GTY(()) line_maps {
  217. struct maps_info info_ordinary;
  218. struct maps_info info_macro;
  219. /* Depth of the include stack, including the current file. */
  220. unsigned int depth;
  221. /* If true, prints an include trace a la -H. */
  222. bool trace_includes;
  223. /* Highest source_location "given out". */
  224. source_location highest_location;
  225. /* Start of line of highest source_location "given out". */
  226. source_location highest_line;
  227. /* The maximum column number we can quickly allocate. Higher numbers
  228. may require allocating a new line_map. */
  229. unsigned int max_column_hint;
  230. /* If non-null, the allocator to use when resizing 'maps'. If null,
  231. xrealloc is used. */
  232. line_map_realloc reallocator;
  233. /* The allocators' function used to know the actual size it
  234. allocated, for a certain allocation size requested. */
  235. line_map_round_alloc_size_func round_alloc_size;
  236. struct location_adhoc_data_map location_adhoc_data_map;
  237. /* The special location value that is used as spelling location for
  238. built-in tokens. */
  239. source_location builtin_location;
  240. };
  241. /* Returns the pointer to the memory region where information about
  242. maps are stored in the line table SET. MACRO_MAP_P is a flag
  243. telling if we want macro or ordinary maps. */
  244. #define LINEMAPS_MAP_INFO(SET, MACRO_MAP_P) \
  245. ((MACRO_MAP_P) \
  246. ? &((SET)->info_macro) \
  247. : &((SET)->info_ordinary))
  248. /* Returns the pointer to the memory region where maps are stored in
  249. the line table SET. MAP_KIND shall be TRUE if we are interested in
  250. macro maps false otherwise. */
  251. #define LINEMAPS_MAPS(SET, MAP_KIND) \
  252. (LINEMAPS_MAP_INFO (SET, MAP_KIND))->maps
  253. /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
  254. if we are interested in macro maps, FALSE otherwise. */
  255. #define LINEMAPS_ALLOCATED(SET, MAP_KIND) \
  256. (LINEMAPS_MAP_INFO (SET, MAP_KIND))->allocated
  257. /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
  258. we are interested in macro maps, FALSE otherwise.*/
  259. #define LINEMAPS_USED(SET, MAP_KIND) \
  260. (LINEMAPS_MAP_INFO (SET, MAP_KIND))->used
  261. /* Returns the index of the last map that was looked up with
  262. linemap_lookup. MAP_KIND shall be TRUE if we are interested in
  263. macro maps, FALSE otherwise. */
  264. #define LINEMAPS_CACHE(SET, MAP_KIND) \
  265. (LINEMAPS_MAP_INFO (SET, MAP_KIND))->cache
  266. /* Return the map at a given index. */
  267. #define LINEMAPS_MAP_AT(SET, MAP_KIND, INDEX) \
  268. (&((LINEMAPS_MAPS (SET, MAP_KIND))[(INDEX)]))
  269. /* Returns the last map used in the line table SET. MAP_KIND
  270. shall be TRUE if we are interested in macro maps, FALSE
  271. otherwise.*/
  272. #define LINEMAPS_LAST_MAP(SET, MAP_KIND) \
  273. LINEMAPS_MAP_AT (SET, MAP_KIND, (LINEMAPS_USED (SET, MAP_KIND) - 1))
  274. /* Returns the last map that was allocated in the line table SET.
  275. MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
  276. otherwise.*/
  277. #define LINEMAPS_LAST_ALLOCATED_MAP(SET, MAP_KIND) \
  278. LINEMAPS_MAP_AT (SET, MAP_KIND, LINEMAPS_ALLOCATED (SET, MAP_KIND) - 1)
  279. /* Returns a pointer to the memory region where ordinary maps are
  280. allocated in the line table SET. */
  281. #define LINEMAPS_ORDINARY_MAPS(SET) \
  282. LINEMAPS_MAPS (SET, false)
  283. /* Returns the INDEXth ordinary map. */
  284. #define LINEMAPS_ORDINARY_MAP_AT(SET, INDEX) \
  285. LINEMAPS_MAP_AT (SET, false, INDEX)
  286. /* Return the number of ordinary maps allocated in the line table
  287. SET. */
  288. #define LINEMAPS_ORDINARY_ALLOCATED(SET) \
  289. LINEMAPS_ALLOCATED(SET, false)
  290. /* Return the number of ordinary maps used in the line table SET. */
  291. #define LINEMAPS_ORDINARY_USED(SET) \
  292. LINEMAPS_USED(SET, false)
  293. /* Return the index of the last ordinary map that was looked up with
  294. linemap_lookup. */
  295. #define LINEMAPS_ORDINARY_CACHE(SET) \
  296. LINEMAPS_CACHE(SET, false)
  297. /* Returns a pointer to the last ordinary map used in the line table
  298. SET. */
  299. #define LINEMAPS_LAST_ORDINARY_MAP(SET) \
  300. LINEMAPS_LAST_MAP(SET, false)
  301. /* Returns a pointer to the last ordinary map allocated the line table
  302. SET. */
  303. #define LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP(SET) \
  304. LINEMAPS_LAST_ALLOCATED_MAP(SET, false)
  305. /* Returns a pointer to the beginning of the region where macro maps
  306. are allcoated. */
  307. #define LINEMAPS_MACRO_MAPS(SET) \
  308. LINEMAPS_MAPS(SET, true)
  309. /* Returns the INDEXth macro map. */
  310. #define LINEMAPS_MACRO_MAP_AT(SET, INDEX) \
  311. LINEMAPS_MAP_AT (SET, true, INDEX)
  312. /* Returns the number of macro maps that were allocated in the line
  313. table SET. */
  314. #define LINEMAPS_MACRO_ALLOCATED(SET) \
  315. LINEMAPS_ALLOCATED(SET, true)
  316. /* Returns the number of macro maps used in the line table SET. */
  317. #define LINEMAPS_MACRO_USED(SET) \
  318. LINEMAPS_USED(SET, true)
  319. /* Returns the index of the last macro map looked up with
  320. linemap_lookup. */
  321. #define LINEMAPS_MACRO_CACHE(SET) \
  322. LINEMAPS_CACHE(SET, true)
  323. /* Returns the lowest location [of a token resulting from macro
  324. expansion] encoded in this line table. */
  325. #define LINEMAPS_MACRO_LOWEST_LOCATION(SET) \
  326. (LINEMAPS_MACRO_USED (set) \
  327. ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set)) \
  328. : MAX_SOURCE_LOCATION)
  329. /* Returns the last macro map used in the line table SET. */
  330. #define LINEMAPS_LAST_MACRO_MAP(SET) \
  331. LINEMAPS_LAST_MAP (SET, true)
  332. /* Returns the last macro map allocated in the line table SET. */
  333. #define LINEMAPS_LAST_ALLOCATED_MACRO_MAP(SET) \
  334. LINEMAPS_LAST_ALLOCATED_MAP (SET, true)
  335. extern void location_adhoc_data_fini (struct line_maps *);
  336. extern source_location get_combined_adhoc_loc (struct line_maps *,
  337. source_location, void *);
  338. extern void *get_data_from_adhoc_loc (struct line_maps *, source_location);
  339. extern source_location get_location_from_adhoc_loc (struct line_maps *,
  340. source_location);
  341. #define IS_ADHOC_LOC(LOC) (((LOC) & MAX_SOURCE_LOCATION) != (LOC))
  342. #define COMBINE_LOCATION_DATA(SET, LOC, BLOCK) \
  343. get_combined_adhoc_loc ((SET), (LOC), (BLOCK))
  344. extern void rebuild_location_adhoc_htab (struct line_maps *);
  345. /* Initialize a line map set. SET is the line map set to initialize
  346. and BUILTIN_LOCATION is the special location value to be used as
  347. spelling location for built-in tokens. This BUILTIN_LOCATION has
  348. to be strictly less than RESERVED_LOCATION_COUNT. */
  349. extern void linemap_init (struct line_maps *set,
  350. source_location builtin_location);
  351. /* Check for and warn about line_maps entered but not exited. */
  352. extern void linemap_check_files_exited (struct line_maps *);
  353. /* Return a source_location for the start (i.e. column==0) of
  354. (physical) line TO_LINE in the current source file (as in the
  355. most recent linemap_add). MAX_COLUMN_HINT is the highest column
  356. number we expect to use in this line (but it does not change
  357. the highest_location). */
  358. extern source_location linemap_line_start
  359. (struct line_maps *set, linenum_type to_line, unsigned int max_column_hint);
  360. /* Add a mapping of logical source line to physical source file and
  361. line number. This function creates an "ordinary map", which is a
  362. map that records locations of tokens that are not part of macro
  363. replacement-lists present at a macro expansion point.
  364. The text pointed to by TO_FILE must have a lifetime
  365. at least as long as the lifetime of SET. An empty
  366. TO_FILE means standard input. If reason is LC_LEAVE, and
  367. TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
  368. natural values considering the file we are returning to.
  369. A call to this function can relocate the previous set of
  370. maps, so any stored line_map pointers should not be used. */
  371. extern const struct line_map *linemap_add
  372. (struct line_maps *, enum lc_reason, unsigned int sysp,
  373. const char *to_file, linenum_type to_line);
  374. /* Given a logical source location, returns the map which the
  375. corresponding (source file, line, column) triplet can be deduced
  376. from. Since the set is built chronologically, the logical lines are
  377. monotonic increasing, and so the list is sorted and we can use a
  378. binary search. If no line map have been allocated yet, this
  379. function returns NULL. */
  380. extern const struct line_map *linemap_lookup
  381. (struct line_maps *, source_location);
  382. /* Returns TRUE if the line table set tracks token locations across
  383. macro expansion, FALSE otherwise. */
  384. bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
  385. /* Return TRUE if MAP encodes locations coming from a macro
  386. replacement-list at macro expansion point. */
  387. bool linemap_macro_expansion_map_p (const struct line_map *);
  388. /* Return the name of the macro associated to MACRO_MAP. */
  389. const char* linemap_map_get_macro_name (const struct line_map*);
  390. /* Return a positive value if LOCATION is the locus of a token that is
  391. located in a system header, O otherwise. It returns 1 if LOCATION
  392. is the locus of a token that is located in a system header, and 2
  393. if LOCATION is the locus of a token located in a C system header
  394. that therefore needs to be extern "C" protected in C++.
  395. Note that this function returns 1 if LOCATION belongs to a token
  396. that is part of a macro replacement-list defined in a system
  397. header, but expanded in a non-system file. */
  398. int linemap_location_in_system_header_p (struct line_maps *,
  399. source_location);
  400. /* Return TRUE if LOCATION is a source code location of a token coming
  401. from a macro replacement-list at a macro expansion point, FALSE
  402. otherwise. */
  403. bool linemap_location_from_macro_expansion_p (const struct line_maps *,
  404. source_location);
  405. /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
  406. be reserved for libcpp user as special values, no token from libcpp
  407. will contain any of those locations. */
  408. #define RESERVED_LOCATION_COUNT 2
  409. /* Converts a map and a source_location to source line. */
  410. #define SOURCE_LINE(MAP, LOC) \
  411. (((((LOC) - linemap_check_ordinary (MAP)->start_location) \
  412. >> (MAP)->d.ordinary.column_bits) + (MAP)->d.ordinary.to_line))
  413. /* Convert a map and source_location to source column number. */
  414. #define SOURCE_COLUMN(MAP, LOC) \
  415. ((((LOC) - linemap_check_ordinary (MAP)->start_location) \
  416. & ((1 << (MAP)->d.ordinary.column_bits) - 1)))
  417. /* Returns the last source line number within an ordinary map. This
  418. is the (last) line of the #include, or other directive, that caused
  419. a map change. */
  420. #define LAST_SOURCE_LINE(MAP) \
  421. SOURCE_LINE (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
  422. /* Return the last column number within an ordinary map. */
  423. #define LAST_SOURCE_COLUMN(MAP) \
  424. SOURCE_COLUMN (MAP, LAST_SOURCE_LINE_LOCATION (MAP))
  425. /* Return the location of the last source line within an ordinary
  426. map. */
  427. #define LAST_SOURCE_LINE_LOCATION(MAP) \
  428. ((((linemap_check_ordinary (MAP)[1].start_location - 1 \
  429. - (MAP)->start_location) \
  430. & ~((1 << (MAP)->d.ordinary.column_bits) - 1)) \
  431. + (MAP)->start_location))
  432. /* Returns the map a given map was included from, or NULL if the map
  433. belongs to the main file, i.e, a file that wasn't included by
  434. another one. */
  435. #define INCLUDED_FROM(SET, MAP) \
  436. ((linemap_check_ordinary (MAP)->d.ordinary.included_from == -1) \
  437. ? NULL \
  438. : (&LINEMAPS_ORDINARY_MAPS (SET)[(MAP)->d.ordinary.included_from]))
  439. /* Nonzero if the map is at the bottom of the include stack. */
  440. #define MAIN_FILE_P(MAP) \
  441. ((linemap_check_ordinary (MAP)->d.ordinary.included_from < 0))
  442. #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
  443. /* Assertion macro to be used in line-map code. */
  444. #define linemap_assert(EXPR) \
  445. do { \
  446. if (! (EXPR)) \
  447. abort (); \
  448. } while (0)
  449. /* Assert that becomes a conditional expression when checking is disabled at
  450. compilation time. Use this for conditions that should not happen but if
  451. they happen, it is better to handle them gracefully rather than crash
  452. randomly later.
  453. Usage:
  454. if (linemap_assert_fails(EXPR)) handle_error(); */
  455. #define linemap_assert_fails(EXPR) __extension__ \
  456. ({linemap_assert (EXPR); false;})
  457. /* Assert that MAP encodes locations of tokens that are not part of
  458. the replacement-list of a macro expansion. */
  459. #define linemap_check_ordinary(LINE_MAP) __extension__ \
  460. ({linemap_assert (!linemap_macro_expansion_map_p (LINE_MAP)); \
  461. (LINE_MAP);})
  462. #else
  463. /* Include EXPR, so that unused variable warnings do not occur. */
  464. #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
  465. #define linemap_assert_fails(EXPR) (! (EXPR))
  466. #define linemap_check_ordinary(LINE_MAP) (LINE_MAP)
  467. #endif
  468. /* Encode and return a source_location from a column number. The
  469. source line considered is the last source line used to call
  470. linemap_line_start, i.e, the last source line which a location was
  471. encoded from. */
  472. extern source_location
  473. linemap_position_for_column (struct line_maps *, unsigned int);
  474. /* Encode and return a source location from a given line and
  475. column. */
  476. source_location
  477. linemap_position_for_line_and_column (const struct line_map *,
  478. linenum_type, unsigned int);
  479. /* Encode and return a source_location starting from location LOC and
  480. shifting it by OFFSET columns. This function does not support
  481. virtual locations. */
  482. source_location
  483. linemap_position_for_loc_and_offset (struct line_maps *set,
  484. source_location loc,
  485. unsigned int offset);
  486. /* Return the file this map is for. */
  487. #define LINEMAP_FILE(MAP) \
  488. (linemap_check_ordinary (MAP)->d.ordinary.to_file)
  489. /* Return the line number this map started encoding location from. */
  490. #define LINEMAP_LINE(MAP) \
  491. (linemap_check_ordinary (MAP)->d.ordinary.to_line)
  492. /* Return a positive value if map encodes locations from a system
  493. header, 0 otherwise. Returns 1 if MAP encodes locations in a
  494. system header and 2 if it encodes locations in a C system header
  495. that therefore needs to be extern "C" protected in C++. */
  496. #define LINEMAP_SYSP(MAP) \
  497. (linemap_check_ordinary (MAP)->d.ordinary.sysp)
  498. /* Return a positive value if PRE denotes the location of a token that
  499. comes before the token of POST, 0 if PRE denotes the location of
  500. the same token as the token for POST, and a negative value
  501. otherwise. */
  502. int linemap_compare_locations (struct line_maps *set,
  503. source_location pre,
  504. source_location post);
  505. /* Return TRUE if LOC_A denotes the location a token that comes
  506. topogically before the token denoted by location LOC_B, or if they
  507. are equal. */
  508. #define linemap_location_before_p(SET, LOC_A, LOC_B) \
  509. (linemap_compare_locations ((SET), (LOC_A), (LOC_B)) >= 0)
  510. typedef struct
  511. {
  512. /* The name of the source file involved. */
  513. const char *file;
  514. /* The line-location in the source file. */
  515. int line;
  516. int column;
  517. void *data;
  518. /* In a system header?. */
  519. bool sysp;
  520. } expanded_location;
  521. /* This is enum is used by the function linemap_resolve_location
  522. below. The meaning of the values is explained in the comment of
  523. that function. */
  524. enum location_resolution_kind
  525. {
  526. LRK_MACRO_EXPANSION_POINT,
  527. LRK_SPELLING_LOCATION,
  528. LRK_MACRO_DEFINITION_LOCATION
  529. };
  530. /* Resolve a virtual location into either a spelling location, an
  531. expansion point location or a token argument replacement point
  532. location. Return the map that encodes the virtual location as well
  533. as the resolved location.
  534. If LOC is *NOT* the location of a token resulting from the
  535. expansion of a macro, then the parameter LRK (which stands for
  536. Location Resolution Kind) is ignored and the resulting location
  537. just equals the one given in argument.
  538. Now if LOC *IS* the location of a token resulting from the
  539. expansion of a macro, this is what happens.
  540. * If LRK is set to LRK_MACRO_EXPANSION_POINT
  541. -------------------------------
  542. The virtual location is resolved to the first macro expansion point
  543. that led to this macro expansion.
  544. * If LRK is set to LRK_SPELLING_LOCATION
  545. -------------------------------------
  546. The virtual location is resolved to the locus where the token has
  547. been spelled in the source. This can follow through all the macro
  548. expansions that led to the token.
  549. * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
  550. --------------------------------------
  551. The virtual location is resolved to the locus of the token in the
  552. context of the macro definition.
  553. If LOC is the locus of a token that is an argument of a
  554. function-like macro [replacing a parameter in the replacement list
  555. of the macro] the virtual location is resolved to the locus of the
  556. parameter that is replaced, in the context of the definition of the
  557. macro.
  558. If LOC is the locus of a token that is not an argument of a
  559. function-like macro, then the function behaves as if LRK was set to
  560. LRK_SPELLING_LOCATION.
  561. If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
  562. returned location. Note that if the returned location wasn't originally
  563. encoded by a map, the *MAP is set to NULL. This can happen if LOC
  564. resolves to a location reserved for the client code, like
  565. UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
  566. source_location linemap_resolve_location (struct line_maps *,
  567. source_location loc,
  568. enum location_resolution_kind lrk,
  569. const struct line_map **loc_map);
  570. /* Suppose that LOC is the virtual location of a token coming from the
  571. expansion of a macro M. This function then steps up to get the
  572. location L of the point where M got expanded. If L is a spelling
  573. location inside a macro expansion M', then this function returns
  574. the point where M' was expanded. LOC_MAP is an output parameter.
  575. When non-NULL, *LOC_MAP is set to the map of the returned
  576. location. */
  577. source_location linemap_unwind_toward_expansion (struct line_maps *,
  578. source_location loc,
  579. const struct line_map **loc_map);
  580. /* If LOC is the virtual location of a token coming from the expansion
  581. of a macro M and if its spelling location is reserved (e.g, a
  582. location for a built-in token), then this function unwinds (using
  583. linemap_unwind_toward_expansion) the location until a location that
  584. is not reserved and is not in a system header is reached. In other
  585. words, this unwinds the reserved location until a location that is
  586. in real source code is reached.
  587. Otherwise, if the spelling location for LOC is not reserved or if
  588. LOC doesn't come from the expansion of a macro, the function
  589. returns LOC as is and *MAP is not touched.
  590. *MAP is set to the map of the returned location if the later is
  591. different from LOC. */
  592. source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
  593. source_location loc,
  594. const struct line_map **map);
  595. /* Expand source code location LOC and return a user readable source
  596. code location. LOC must be a spelling (non-virtual) location. If
  597. it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
  598. location is returned. */
  599. expanded_location linemap_expand_location (struct line_maps *,
  600. const struct line_map *,
  601. source_location loc);
  602. /* Statistics about maps allocation and usage as returned by
  603. linemap_get_statistics. */
  604. struct linemap_stats
  605. {
  606. long num_ordinary_maps_allocated;
  607. long num_ordinary_maps_used;
  608. long ordinary_maps_allocated_size;
  609. long ordinary_maps_used_size;
  610. long num_expanded_macros;
  611. long num_macro_tokens;
  612. long num_macro_maps_used;
  613. long macro_maps_allocated_size;
  614. long macro_maps_used_size;
  615. long macro_maps_locations_size;
  616. long duplicated_macro_maps_locations_size;
  617. };
  618. /* Return the highest location emitted for a given file for which
  619. there is a line map in SET. FILE_NAME is the file name to
  620. consider. If the function returns TRUE, *LOC is set to the highest
  621. location emitted for that file. */
  622. bool linemap_get_file_highest_location (struct line_maps * set,
  623. const char *file_name,
  624. source_location *loc);
  625. /* Compute and return statistics about the memory consumption of some
  626. parts of the line table SET. */
  627. void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
  628. /* Dump debugging information about source location LOC into the file
  629. stream STREAM. SET is the line map set LOC comes from. */
  630. void linemap_dump_location (struct line_maps *, source_location, FILE *);
  631. /* Dump line map at index IX in line table SET to STREAM. If STREAM
  632. is NULL, use stderr. IS_MACRO is true if the caller wants to
  633. dump a macro map, false otherwise. */
  634. void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
  635. /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
  636. NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
  637. specifies how many macro maps to dump. */
  638. void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
  639. #endif /* !LIBCPP_LINE_MAP_H */