line-map.h 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  1. /* Map (unsigned int) keys to (source file, line, column) triples.
  2. Copyright (C) 2001-2018 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. /* Both gcc and emacs number source *lines* starting at 1, but
  23. they have differing conventions for *columns*.
  24. GCC uses a 1-based convention for source columns,
  25. whereas Emacs's M-x column-number-mode uses a 0-based convention.
  26. For example, an error in the initial, left-hand
  27. column of source line 3 is reported by GCC as:
  28. some-file.c:3:1: error: ...etc...
  29. On navigating to the location of that error in Emacs
  30. (e.g. via "next-error"),
  31. the locus is reported in the Mode Line
  32. (assuming M-x column-number-mode) as:
  33. some-file.c 10% (3, 0)
  34. i.e. "3:1:" in GCC corresponds to "(3, 0)" in Emacs. */
  35. /* The type of line numbers. */
  36. typedef unsigned int linenum_type;
  37. /* A function for for use by qsort for comparing line numbers. */
  38. inline int compare (linenum_type lhs, linenum_type rhs)
  39. {
  40. /* Avoid truncation issues by using long long for the comparison,
  41. and only consider the sign of the result. */
  42. long long diff = (long long)lhs - (long long)rhs;
  43. if (diff)
  44. return diff > 0 ? 1 : -1;
  45. return 0;
  46. }
  47. /* Reason for creating a new line map with linemap_add. LC_ENTER is
  48. when including a new file, e.g. a #include directive in C.
  49. LC_LEAVE is when reaching a file's end. LC_RENAME is when a file
  50. name or line number changes for neither of the above reasons
  51. (e.g. a #line directive in C); LC_RENAME_VERBATIM is like LC_RENAME
  52. but a filename of "" is not specially interpreted as standard
  53. input. LC_ENTER_MACRO is when a macro expansion is about to start. */
  54. enum lc_reason
  55. {
  56. LC_ENTER = 0,
  57. LC_LEAVE,
  58. LC_RENAME,
  59. LC_RENAME_VERBATIM,
  60. LC_ENTER_MACRO
  61. /* FIXME: add support for stringize and paste. */
  62. };
  63. /* The typedef "source_location" is a key within the location database,
  64. identifying a source location or macro expansion, along with range
  65. information, and (optionally) a pointer for use by gcc.
  66. This key only has meaning in relation to a line_maps instance. Within
  67. gcc there is a single line_maps instance: "line_table", declared in
  68. gcc/input.h and defined in gcc/input.c.
  69. The values of the keys are intended to be internal to libcpp,
  70. but for ease-of-understanding the implementation, they are currently
  71. assigned as follows:
  72. Actual | Value | Meaning
  73. -----------+-------------------------------+-------------------------------
  74. 0x00000000 | UNKNOWN_LOCATION (gcc/input.h)| Unknown/invalid location.
  75. -----------+-------------------------------+-------------------------------
  76. 0x00000001 | BUILTINS_LOCATION | The location for declarations
  77. | (gcc/input.h) | in "<built-in>"
  78. -----------+-------------------------------+-------------------------------
  79. 0x00000002 | RESERVED_LOCATION_COUNT | The first location to be
  80. | (also | handed out, and the
  81. | ordmap[0]->start_location) | first line in ordmap 0
  82. -----------+-------------------------------+-------------------------------
  83. | ordmap[1]->start_location | First line in ordmap 1
  84. | ordmap[1]->start_location+32 | First column in that line
  85. | (assuming range_bits == 5) |
  86. | ordmap[1]->start_location+64 | 2nd column in that line
  87. | ordmap[1]->start_location+4096| Second line in ordmap 1
  88. | (assuming column_bits == 12)
  89. |
  90. | Subsequent lines are offset by (1 << column_bits),
  91. | e.g. 4096 for 12 bits, with a column value of 0 representing
  92. | "the whole line".
  93. |
  94. | Within a line, the low "range_bits" (typically 5) are used for
  95. | storing short ranges, so that there's an offset of
  96. | (1 << range_bits) between individual columns within a line,
  97. | typically 32.
  98. | The low range_bits store the offset of the end point from the
  99. | start point, and the start point is found by masking away
  100. | the range bits.
  101. |
  102. | For example:
  103. | ordmap[1]->start_location+64 "2nd column in that line"
  104. | above means a caret at that location, with a range
  105. | starting and finishing at the same place (the range bits
  106. | are 0), a range of length 1.
  107. |
  108. | By contrast:
  109. | ordmap[1]->start_location+68
  110. | has range bits 0x4, meaning a caret with a range starting at
  111. | that location, but with endpoint 4 columns further on: a range
  112. | of length 5.
  113. |
  114. | Ranges that have caret != start, or have an endpoint too
  115. | far away to fit in range_bits are instead stored as ad-hoc
  116. | locations. Hence for range_bits == 5 we can compactly store
  117. | tokens of length <= 32 without needing to use the ad-hoc
  118. | table.
  119. |
  120. | This packing scheme means we effectively have
  121. | (column_bits - range_bits)
  122. | of bits for the columns, typically (12 - 5) = 7, for 128
  123. | columns; longer line widths are accomodated by starting a
  124. | new ordmap with a higher column_bits.
  125. |
  126. | ordmap[2]->start_location-1 | Final location in ordmap 1
  127. -----------+-------------------------------+-------------------------------
  128. | ordmap[2]->start_location | First line in ordmap 2
  129. | ordmap[3]->start_location-1 | Final location in ordmap 2
  130. -----------+-------------------------------+-------------------------------
  131. | | (etc)
  132. -----------+-------------------------------+-------------------------------
  133. | ordmap[n-1]->start_location | First line in final ord map
  134. | | (etc)
  135. | set->highest_location - 1 | Final location in that ordmap
  136. -----------+-------------------------------+-------------------------------
  137. | set->highest_location | Location of the where the next
  138. | | ordinary linemap would start
  139. -----------+-------------------------------+-------------------------------
  140. | |
  141. | VVVVVVVVVVVVVVVVVVVVVVVVVVV
  142. | Ordinary maps grow this way
  143. |
  144. | (unallocated integers)
  145. |
  146. 0x60000000 | LINE_MAP_MAX_LOCATION_WITH_COLS
  147. | Beyond this point, ordinary linemaps have 0 bits per column:
  148. | each increment of the value corresponds to a new source line.
  149. |
  150. 0x70000000 | LINE_MAP_MAX_SOURCE_LOCATION
  151. | Beyond the point, we give up on ordinary maps; attempts to
  152. | create locations in them lead to UNKNOWN_LOCATION (0).
  153. |
  154. | (unallocated integers)
  155. |
  156. | Macro maps grow this way
  157. | ^^^^^^^^^^^^^^^^^^^^^^^^
  158. | |
  159. -----------+-------------------------------+-------------------------------
  160. | LINEMAPS_MACRO_LOWEST_LOCATION| Locations within macro maps
  161. | macromap[m-1]->start_location | Start of last macro map
  162. | |
  163. -----------+-------------------------------+-------------------------------
  164. | macromap[m-2]->start_location | Start of penultimate macro map
  165. -----------+-------------------------------+-------------------------------
  166. | macromap[1]->start_location | Start of macro map 1
  167. -----------+-------------------------------+-------------------------------
  168. | macromap[0]->start_location | Start of macro map 0
  169. 0x7fffffff | MAX_SOURCE_LOCATION | Also used as a mask for
  170. | | accessing the ad-hoc data table
  171. -----------+-------------------------------+-------------------------------
  172. 0x80000000 | Start of ad-hoc values; the lower 31 bits are used as an index
  173. ... | into the line_table->location_adhoc_data_map.data array.
  174. 0xffffffff | UINT_MAX |
  175. -----------+-------------------------------+-------------------------------
  176. Examples of location encoding.
  177. Packed ranges
  178. =============
  179. Consider encoding the location of a token "foo", seen underlined here
  180. on line 523, within an ordinary line_map that starts at line 500:
  181. 11111111112
  182. 12345678901234567890
  183. 522
  184. 523 return foo + bar;
  185. ^~~
  186. 524
  187. The location's caret and start are both at line 523, column 11; the
  188. location's finish is on the same line, at column 13 (an offset of 2
  189. columns, for length 3).
  190. Line 523 is offset 23 from the starting line of the ordinary line_map.
  191. caret == start, and the offset of the finish fits within 5 bits, so
  192. this can be stored as a packed range.
  193. This is encoded as:
  194. ordmap->start
  195. + (line_offset << ordmap->m_column_and_range_bits)
  196. + (column << ordmap->m_range_bits)
  197. + (range_offset);
  198. i.e. (for line offset 23, column 11, range offset 2):
  199. ordmap->start
  200. + (23 << 12)
  201. + (11 << 5)
  202. + 2;
  203. i.e.:
  204. ordmap->start + 0x17162
  205. assuming that the line_map uses the default of 7 bits for columns and
  206. 5 bits for packed range (giving 12 bits for m_column_and_range_bits).
  207. "Pure" locations
  208. ================
  209. These are a special case of the above, where
  210. caret == start == finish
  211. They are stored as packed ranges with offset == 0.
  212. For example, the location of the "f" of "foo" could be stored
  213. as above, but with range offset 0, giving:
  214. ordmap->start
  215. + (23 << 12)
  216. + (11 << 5)
  217. + 0;
  218. i.e.:
  219. ordmap->start + 0x17160
  220. Unoptimized ranges
  221. ==================
  222. Consider encoding the location of the binary expression
  223. below:
  224. 11111111112
  225. 12345678901234567890
  226. 522
  227. 523 return foo + bar;
  228. ~~~~^~~~~
  229. 524
  230. The location's caret is at the "+", line 523 column 15, but starts
  231. earlier, at the "f" of "foo" at column 11. The finish is at the "r"
  232. of "bar" at column 19.
  233. This can't be stored as a packed range since start != caret.
  234. Hence it is stored as an ad-hoc location e.g. 0x80000003.
  235. Stripping off the top bit gives us an index into the ad-hoc
  236. lookaside table:
  237. line_table->location_adhoc_data_map.data[0x3]
  238. from which the caret, start and finish can be looked up,
  239. encoded as "pure" locations:
  240. start == ordmap->start + (23 << 12) + (11 << 5)
  241. == ordmap->start + 0x17160 (as above; the "f" of "foo")
  242. caret == ordmap->start + (23 << 12) + (15 << 5)
  243. == ordmap->start + 0x171e0
  244. finish == ordmap->start + (23 << 12) + (19 << 5)
  245. == ordmap->start + 0x17260
  246. To further see how source_location works in practice, see the
  247. worked example in libcpp/location-example.txt. */
  248. typedef unsigned int source_location;
  249. /* Do not track column numbers higher than this one. As a result, the
  250. range of column_bits is [12, 18] (or 0 if column numbers are
  251. disabled). */
  252. const unsigned int LINE_MAP_MAX_COLUMN_NUMBER = (1U << 12);
  253. /* Do not pack ranges if locations get higher than this.
  254. If you change this, update:
  255. gcc.dg/plugin/location-overflow-test-*.c. */
  256. const source_location LINE_MAP_MAX_LOCATION_WITH_PACKED_RANGES = 0x50000000;
  257. /* Do not track column numbers if locations get higher than this.
  258. If you change this, update:
  259. gcc.dg/plugin/location-overflow-test-*.c. */
  260. const source_location LINE_MAP_MAX_LOCATION_WITH_COLS = 0x60000000;
  261. /* A range of source locations.
  262. Ranges are closed:
  263. m_start is the first location within the range,
  264. m_finish is the last location within the range.
  265. We may need a more compact way to store these, but for now,
  266. let's do it the simple way, as a pair. */
  267. struct GTY(()) source_range
  268. {
  269. source_location m_start;
  270. source_location m_finish;
  271. /* We avoid using constructors, since various structs that
  272. don't yet have constructors will embed instances of
  273. source_range. */
  274. /* Make a source_range from a source_location. */
  275. static source_range from_location (source_location loc)
  276. {
  277. source_range result;
  278. result.m_start = loc;
  279. result.m_finish = loc;
  280. return result;
  281. }
  282. /* Make a source_range from a pair of source_location. */
  283. static source_range from_locations (source_location start,
  284. source_location finish)
  285. {
  286. source_range result;
  287. result.m_start = start;
  288. result.m_finish = finish;
  289. return result;
  290. }
  291. };
  292. /* Memory allocation function typedef. Works like xrealloc. */
  293. typedef void *(*line_map_realloc) (void *, size_t);
  294. /* Memory allocator function that returns the actual allocated size,
  295. for a given requested allocation. */
  296. typedef size_t (*line_map_round_alloc_size_func) (size_t);
  297. /* A line_map encodes a sequence of locations.
  298. There are two kinds of maps. Ordinary maps and macro expansion
  299. maps, a.k.a macro maps.
  300. A macro map encodes source locations of tokens that are part of a
  301. macro replacement-list, at a macro expansion point. E.g, in:
  302. #define PLUS(A,B) A + B
  303. No macro map is going to be created there, because we are not at a
  304. macro expansion point. We are at a macro /definition/ point. So the
  305. locations of the tokens of the macro replacement-list (i.e, A + B)
  306. will be locations in an ordinary map, not a macro map.
  307. On the other hand, if we later do:
  308. int a = PLUS (1,2);
  309. The invocation of PLUS here is a macro expansion. So we are at a
  310. macro expansion point. The preprocessor expands PLUS (1,2) and
  311. replaces it with the tokens of its replacement-list: 1 + 2. A macro
  312. map is going to be created to hold (or rather to map, haha ...) the
  313. locations of the tokens 1, + and 2. The macro map also records the
  314. location of the expansion point of PLUS. That location is mapped in
  315. the map that is active right before the location of the invocation
  316. of PLUS. */
  317. struct GTY((tag ("0"), desc ("%h.reason == LC_ENTER_MACRO ? 2 : 1"))) line_map {
  318. source_location start_location;
  319. /* The reason for creation of this line map. */
  320. ENUM_BITFIELD (lc_reason) reason : CHAR_BIT;
  321. };
  322. /* An ordinary line map encodes physical source locations. Those
  323. physical source locations are called "spelling locations".
  324. Physical source file TO_FILE at line TO_LINE at column 0 is represented
  325. by the logical START_LOCATION. TO_LINE+L at column C is represented by
  326. START_LOCATION+(L*(1<<m_column_and_range_bits))+(C*1<<m_range_bits), as
  327. long as C<(1<<effective range bits), and the result_location is less than
  328. the next line_map's start_location.
  329. (The top line is line 1 and the leftmost column is column 1; line/column 0
  330. means "entire file/line" or "unknown line/column" or "not applicable".)
  331. The highest possible source location is MAX_SOURCE_LOCATION. */
  332. struct GTY((tag ("1"))) line_map_ordinary : public line_map {
  333. const char *to_file;
  334. linenum_type to_line;
  335. /* An index into the set that gives the line mapping at whose end
  336. the current one was included. File(s) at the bottom of the
  337. include stack have this set to -1. */
  338. int included_from;
  339. /* SYSP is one for a system header, two for a C system header file
  340. that therefore needs to be extern "C" protected in C++, and zero
  341. otherwise. This field isn't really needed now that it's in
  342. cpp_buffer. */
  343. unsigned char sysp;
  344. /* Number of the low-order source_location bits used for column numbers
  345. and ranges. */
  346. unsigned int m_column_and_range_bits : 8;
  347. /* Number of the low-order "column" bits used for storing short ranges
  348. inline, rather than in the ad-hoc table.
  349. MSB LSB
  350. 31 0
  351. +-------------------------+-------------------------------------------+
  352. | |<---map->column_and_range_bits (e.g. 12)-->|
  353. +-------------------------+-----------------------+-------------------+
  354. | | column_and_range_bits | map->range_bits |
  355. | | - range_bits | |
  356. +-------------------------+-----------------------+-------------------+
  357. | row bits | effective column bits | short range bits |
  358. | | (e.g. 7) | (e.g. 5) |
  359. +-------------------------+-----------------------+-------------------+ */
  360. unsigned int m_range_bits : 8;
  361. };
  362. /* This is the highest possible source location encoded within an
  363. ordinary or macro map. */
  364. const source_location MAX_SOURCE_LOCATION = 0x7FFFFFFF;
  365. struct cpp_hashnode;
  366. /* A macro line map encodes location of tokens coming from a macro
  367. expansion.
  368. The offset from START_LOCATION is used to index into
  369. MACRO_LOCATIONS; this holds the original location of the token. */
  370. struct GTY((tag ("2"))) line_map_macro : public line_map {
  371. /* The cpp macro which expansion gave birth to this macro map. */
  372. struct cpp_hashnode * GTY ((nested_ptr (union tree_node,
  373. "%h ? CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (%h)) : NULL",
  374. "%h ? HT_IDENT_TO_GCC_IDENT (HT_NODE (%h)) : NULL")))
  375. macro;
  376. /* The number of tokens inside the replacement-list of MACRO. */
  377. unsigned int n_tokens;
  378. /* This array of location is actually an array of pairs of
  379. locations. The elements inside it thus look like:
  380. x0,y0, x1,y1, x2,y2, ...., xn,yn.
  381. where n == n_tokens;
  382. Remember that these xI,yI are collected when libcpp is about to
  383. expand a given macro.
  384. yI is the location in the macro definition, either of the token
  385. itself or of a macro parameter that it replaces.
  386. Imagine this:
  387. #define PLUS(A, B) A + B <--- #1
  388. int a = PLUS (1,2); <--- #2
  389. There is a macro map for the expansion of PLUS in #2. PLUS is
  390. expanded into its expansion-list. The expansion-list is the
  391. replacement-list of PLUS where the macro parameters are replaced
  392. with their arguments. So the replacement-list of PLUS is made of
  393. the tokens:
  394. A, +, B
  395. and the expansion-list is made of the tokens:
  396. 1, +, 2
  397. Let's consider the case of token "+". Its y1 [yI for I == 1] is
  398. its spelling location in #1.
  399. y0 (thus for token "1") is the spelling location of A in #1.
  400. And y2 (of token "2") is the spelling location of B in #1.
  401. When the token is /not/ an argument for a macro, xI is the same
  402. location as yI. Otherwise, xI is the location of the token
  403. outside this macro expansion. If this macro was expanded from
  404. another macro expansion, xI is a virtual location representing
  405. the token in that macro expansion; otherwise, it is the spelling
  406. location of the token.
  407. Note that a virtual location is a location returned by
  408. linemap_add_macro_token. It encodes the relevant locations (x,y
  409. pairs) of that token across the macro expansions from which it
  410. (the token) might come from.
  411. In the example above x1 (for token "+") is going to be the same
  412. as y1. x0 is the spelling location for the argument token "1",
  413. and x2 is the spelling location for the argument token "2". */
  414. source_location * GTY((atomic)) macro_locations;
  415. /* This is the location of the expansion point of the current macro
  416. map. It's the location of the macro name. That location is held
  417. by the map that was current right before the current one. It
  418. could have been either a macro or an ordinary map, depending on
  419. if we are in a nested expansion context not. */
  420. source_location expansion;
  421. };
  422. #if CHECKING_P && (GCC_VERSION >= 2007)
  423. /* Assertion macro to be used in line-map code. */
  424. #define linemap_assert(EXPR) \
  425. do { \
  426. if (! (EXPR)) \
  427. abort (); \
  428. } while (0)
  429. /* Assert that becomes a conditional expression when checking is disabled at
  430. compilation time. Use this for conditions that should not happen but if
  431. they happen, it is better to handle them gracefully rather than crash
  432. randomly later.
  433. Usage:
  434. if (linemap_assert_fails(EXPR)) handle_error(); */
  435. #define linemap_assert_fails(EXPR) __extension__ \
  436. ({linemap_assert (EXPR); false;})
  437. #else
  438. /* Include EXPR, so that unused variable warnings do not occur. */
  439. #define linemap_assert(EXPR) ((void)(0 && (EXPR)))
  440. #define linemap_assert_fails(EXPR) (! (EXPR))
  441. #endif
  442. /* Return TRUE if MAP encodes locations coming from a macro
  443. replacement-list at macro expansion point. */
  444. bool
  445. linemap_macro_expansion_map_p (const struct line_map *);
  446. /* Assert that MAP encodes locations of tokens that are not part of
  447. the replacement-list of a macro expansion, downcasting from
  448. line_map * to line_map_ordinary *. */
  449. inline line_map_ordinary *
  450. linemap_check_ordinary (struct line_map *map)
  451. {
  452. linemap_assert (!linemap_macro_expansion_map_p (map));
  453. return (line_map_ordinary *)map;
  454. }
  455. /* Assert that MAP encodes locations of tokens that are not part of
  456. the replacement-list of a macro expansion, downcasting from
  457. const line_map * to const line_map_ordinary *. */
  458. inline const line_map_ordinary *
  459. linemap_check_ordinary (const struct line_map *map)
  460. {
  461. linemap_assert (!linemap_macro_expansion_map_p (map));
  462. return (const line_map_ordinary *)map;
  463. }
  464. /* Assert that MAP is a macro expansion and downcast to the appropriate
  465. subclass. */
  466. inline line_map_macro *linemap_check_macro (line_map *map)
  467. {
  468. linemap_assert (linemap_macro_expansion_map_p (map));
  469. return (line_map_macro *)map;
  470. }
  471. /* Assert that MAP is a macro expansion and downcast to the appropriate
  472. subclass. */
  473. inline const line_map_macro *
  474. linemap_check_macro (const line_map *map)
  475. {
  476. linemap_assert (linemap_macro_expansion_map_p (map));
  477. return (const line_map_macro *)map;
  478. }
  479. /* Read the start location of MAP. */
  480. inline source_location
  481. MAP_START_LOCATION (const line_map *map)
  482. {
  483. return map->start_location;
  484. }
  485. /* Get the starting line number of ordinary map MAP. */
  486. inline linenum_type
  487. ORDINARY_MAP_STARTING_LINE_NUMBER (const line_map_ordinary *ord_map)
  488. {
  489. return ord_map->to_line;
  490. }
  491. /* Get the index of the ordinary map at whose end
  492. ordinary map MAP was included.
  493. File(s) at the bottom of the include stack have this set. */
  494. inline int
  495. ORDINARY_MAP_INCLUDER_FILE_INDEX (const line_map_ordinary *ord_map)
  496. {
  497. return ord_map->included_from;
  498. }
  499. /* Return a positive value if map encodes locations from a system
  500. header, 0 otherwise. Returns 1 if ordinary map MAP encodes locations
  501. in a system header and 2 if it encodes locations in a C system header
  502. that therefore needs to be extern "C" protected in C++. */
  503. inline unsigned char
  504. ORDINARY_MAP_IN_SYSTEM_HEADER_P (const line_map_ordinary *ord_map)
  505. {
  506. return ord_map->sysp;
  507. }
  508. /* Get the filename of ordinary map MAP. */
  509. inline const char *
  510. ORDINARY_MAP_FILE_NAME (const line_map_ordinary *ord_map)
  511. {
  512. return ord_map->to_file;
  513. }
  514. /* Get the cpp macro whose expansion gave birth to macro map MAP. */
  515. inline cpp_hashnode *
  516. MACRO_MAP_MACRO (const line_map_macro *macro_map)
  517. {
  518. return macro_map->macro;
  519. }
  520. /* Get the number of tokens inside the replacement-list of the macro
  521. that led to macro map MAP. */
  522. inline unsigned int
  523. MACRO_MAP_NUM_MACRO_TOKENS (const line_map_macro *macro_map)
  524. {
  525. return macro_map->n_tokens;
  526. }
  527. /* Get the array of pairs of locations within macro map MAP.
  528. See the declaration of line_map_macro for more information. */
  529. inline source_location *
  530. MACRO_MAP_LOCATIONS (const line_map_macro *macro_map)
  531. {
  532. return macro_map->macro_locations;
  533. }
  534. /* Get the location of the expansion point of the macro map MAP. */
  535. inline source_location
  536. MACRO_MAP_EXPANSION_POINT_LOCATION (const line_map_macro *macro_map)
  537. {
  538. return macro_map->expansion;
  539. }
  540. /* The abstraction of a set of location maps. There can be several
  541. types of location maps. This abstraction contains the attributes
  542. that are independent from the type of the map.
  543. Essentially this is just a vector of T_linemap_subclass,
  544. which can only ever grow in size. */
  545. struct GTY(()) maps_info_ordinary {
  546. /* This array contains the "ordinary" line maps, for all
  547. events other than macro expansion
  548. (e.g. when a new preprocessing unit starts or ends). */
  549. line_map_ordinary * GTY ((length ("%h.used"))) maps;
  550. /* The total number of allocated maps. */
  551. unsigned int allocated;
  552. /* The number of elements used in maps. This number is smaller
  553. or equal to ALLOCATED. */
  554. unsigned int used;
  555. unsigned int cache;
  556. };
  557. struct GTY(()) maps_info_macro {
  558. /* This array contains the macro line maps.
  559. A macro line map is created whenever a macro expansion occurs. */
  560. line_map_macro * GTY ((length ("%h.used"))) maps;
  561. /* The total number of allocated maps. */
  562. unsigned int allocated;
  563. /* The number of elements used in maps. This number is smaller
  564. or equal to ALLOCATED. */
  565. unsigned int used;
  566. unsigned int cache;
  567. };
  568. /* Data structure to associate a source_range together with an arbitrary
  569. data pointer with a source location. */
  570. struct GTY(()) location_adhoc_data {
  571. source_location locus;
  572. source_range src_range;
  573. void * GTY((skip)) data;
  574. };
  575. struct htab;
  576. /* The following data structure encodes a location with some adhoc data
  577. and maps it to a new unsigned integer (called an adhoc location)
  578. that replaces the original location to represent the mapping.
  579. The new adhoc_loc uses the highest bit as the enabling bit, i.e. if the
  580. highest bit is 1, then the number is adhoc_loc. Otherwise, it serves as
  581. the original location. Once identified as the adhoc_loc, the lower 31
  582. bits of the integer is used to index the location_adhoc_data array,
  583. in which the locus and associated data is stored. */
  584. struct GTY(()) location_adhoc_data_map {
  585. struct htab * GTY((skip)) htab;
  586. source_location curr_loc;
  587. unsigned int allocated;
  588. struct location_adhoc_data GTY((length ("%h.allocated"))) *data;
  589. };
  590. /* A set of chronological line_map structures. */
  591. struct GTY(()) line_maps {
  592. ~line_maps ();
  593. maps_info_ordinary info_ordinary;
  594. maps_info_macro info_macro;
  595. /* Depth of the include stack, including the current file. */
  596. unsigned int depth;
  597. /* If true, prints an include trace a la -H. */
  598. bool trace_includes;
  599. /* Highest source_location "given out". */
  600. source_location highest_location;
  601. /* Start of line of highest source_location "given out". */
  602. source_location highest_line;
  603. /* The maximum column number we can quickly allocate. Higher numbers
  604. may require allocating a new line_map. */
  605. unsigned int max_column_hint;
  606. /* If non-null, the allocator to use when resizing 'maps'. If null,
  607. xrealloc is used. */
  608. line_map_realloc reallocator;
  609. /* The allocators' function used to know the actual size it
  610. allocated, for a certain allocation size requested. */
  611. line_map_round_alloc_size_func round_alloc_size;
  612. struct location_adhoc_data_map location_adhoc_data_map;
  613. /* The special location value that is used as spelling location for
  614. built-in tokens. */
  615. source_location builtin_location;
  616. /* True if we've seen a #line or # 44 "file" directive. */
  617. bool seen_line_directive;
  618. /* The default value of range_bits in ordinary line maps. */
  619. unsigned int default_range_bits;
  620. unsigned int num_optimized_ranges;
  621. unsigned int num_unoptimized_ranges;
  622. };
  623. /* Returns the number of allocated maps so far. MAP_KIND shall be TRUE
  624. if we are interested in macro maps, FALSE otherwise. */
  625. inline unsigned int
  626. LINEMAPS_ALLOCATED (const line_maps *set, bool map_kind)
  627. {
  628. if (map_kind)
  629. return set->info_macro.allocated;
  630. else
  631. return set->info_ordinary.allocated;
  632. }
  633. /* As above, but by reference (e.g. as an lvalue). */
  634. inline unsigned int &
  635. LINEMAPS_ALLOCATED (line_maps *set, bool map_kind)
  636. {
  637. if (map_kind)
  638. return set->info_macro.allocated;
  639. else
  640. return set->info_ordinary.allocated;
  641. }
  642. /* Returns the number of used maps so far. MAP_KIND shall be TRUE if
  643. we are interested in macro maps, FALSE otherwise.*/
  644. inline unsigned int
  645. LINEMAPS_USED (const line_maps *set, bool map_kind)
  646. {
  647. if (map_kind)
  648. return set->info_macro.used;
  649. else
  650. return set->info_ordinary.used;
  651. }
  652. /* As above, but by reference (e.g. as an lvalue). */
  653. inline unsigned int &
  654. LINEMAPS_USED (line_maps *set, bool map_kind)
  655. {
  656. if (map_kind)
  657. return set->info_macro.used;
  658. else
  659. return set->info_ordinary.used;
  660. }
  661. /* Returns the index of the last map that was looked up with
  662. linemap_lookup. MAP_KIND shall be TRUE if we are interested in
  663. macro maps, FALSE otherwise. */
  664. inline unsigned int
  665. LINEMAPS_CACHE (const line_maps *set, bool map_kind)
  666. {
  667. if (map_kind)
  668. return set->info_macro.cache;
  669. else
  670. return set->info_ordinary.cache;
  671. }
  672. /* As above, but by reference (e.g. as an lvalue). */
  673. inline unsigned int &
  674. LINEMAPS_CACHE (line_maps *set, bool map_kind)
  675. {
  676. if (map_kind)
  677. return set->info_macro.cache;
  678. else
  679. return set->info_ordinary.cache;
  680. }
  681. /* Return the map at a given index. */
  682. inline line_map *
  683. LINEMAPS_MAP_AT (const line_maps *set, bool map_kind, int index)
  684. {
  685. if (map_kind)
  686. return &set->info_macro.maps[index];
  687. else
  688. return &set->info_ordinary.maps[index];
  689. }
  690. /* Returns the last map used in the line table SET. MAP_KIND
  691. shall be TRUE if we are interested in macro maps, FALSE
  692. otherwise.*/
  693. inline line_map *
  694. LINEMAPS_LAST_MAP (const line_maps *set, bool map_kind)
  695. {
  696. return LINEMAPS_MAP_AT (set, map_kind,
  697. LINEMAPS_USED (set, map_kind) - 1);
  698. }
  699. /* Returns the last map that was allocated in the line table SET.
  700. MAP_KIND shall be TRUE if we are interested in macro maps, FALSE
  701. otherwise.*/
  702. inline line_map *
  703. LINEMAPS_LAST_ALLOCATED_MAP (const line_maps *set, bool map_kind)
  704. {
  705. return LINEMAPS_MAP_AT (set, map_kind,
  706. LINEMAPS_ALLOCATED (set, map_kind) - 1);
  707. }
  708. /* Returns a pointer to the memory region where ordinary maps are
  709. allocated in the line table SET. */
  710. inline line_map_ordinary *
  711. LINEMAPS_ORDINARY_MAPS (const line_maps *set)
  712. {
  713. return set->info_ordinary.maps;
  714. }
  715. /* Returns the INDEXth ordinary map. */
  716. inline line_map_ordinary *
  717. LINEMAPS_ORDINARY_MAP_AT (const line_maps *set, int index)
  718. {
  719. linemap_assert (index >= 0);
  720. linemap_assert ((unsigned int)index < set->info_ordinary.used);
  721. return &set->info_ordinary.maps[index];
  722. }
  723. /* Return the number of ordinary maps allocated in the line table
  724. SET. */
  725. inline unsigned int
  726. LINEMAPS_ORDINARY_ALLOCATED (const line_maps *set)
  727. {
  728. return LINEMAPS_ALLOCATED (set, false);
  729. }
  730. /* Return the number of ordinary maps used in the line table SET. */
  731. inline unsigned int
  732. LINEMAPS_ORDINARY_USED (const line_maps *set)
  733. {
  734. return LINEMAPS_USED (set, false);
  735. }
  736. /* Return the index of the last ordinary map that was looked up with
  737. linemap_lookup. */
  738. inline unsigned int
  739. LINEMAPS_ORDINARY_CACHE (const line_maps *set)
  740. {
  741. return LINEMAPS_CACHE (set, false);
  742. }
  743. /* As above, but by reference (e.g. as an lvalue). */
  744. inline unsigned int &
  745. LINEMAPS_ORDINARY_CACHE (line_maps *set)
  746. {
  747. return LINEMAPS_CACHE (set, false);
  748. }
  749. /* Returns a pointer to the last ordinary map used in the line table
  750. SET. */
  751. inline line_map_ordinary *
  752. LINEMAPS_LAST_ORDINARY_MAP (const line_maps *set)
  753. {
  754. return (line_map_ordinary *)LINEMAPS_LAST_MAP (set, false);
  755. }
  756. /* Returns a pointer to the last ordinary map allocated the line table
  757. SET. */
  758. inline line_map_ordinary *
  759. LINEMAPS_LAST_ALLOCATED_ORDINARY_MAP (const line_maps *set)
  760. {
  761. return (line_map_ordinary *)LINEMAPS_LAST_ALLOCATED_MAP (set, false);
  762. }
  763. /* Returns a pointer to the beginning of the region where macro maps
  764. are allocated. */
  765. inline line_map_macro *
  766. LINEMAPS_MACRO_MAPS (const line_maps *set)
  767. {
  768. return set->info_macro.maps;
  769. }
  770. /* Returns the INDEXth macro map. */
  771. inline line_map_macro *
  772. LINEMAPS_MACRO_MAP_AT (const line_maps *set, int index)
  773. {
  774. linemap_assert (index >= 0);
  775. linemap_assert ((unsigned int)index < set->info_macro.used);
  776. return &set->info_macro.maps[index];
  777. }
  778. /* Returns the number of macro maps that were allocated in the line
  779. table SET. */
  780. inline unsigned int
  781. LINEMAPS_MACRO_ALLOCATED (const line_maps *set)
  782. {
  783. return LINEMAPS_ALLOCATED (set, true);
  784. }
  785. /* Returns the number of macro maps used in the line table SET. */
  786. inline unsigned int
  787. LINEMAPS_MACRO_USED (const line_maps *set)
  788. {
  789. return LINEMAPS_USED (set, true);
  790. }
  791. /* Returns the index of the last macro map looked up with
  792. linemap_lookup. */
  793. inline unsigned int
  794. LINEMAPS_MACRO_CACHE (const line_maps *set)
  795. {
  796. return LINEMAPS_CACHE (set, true);
  797. }
  798. /* As above, but by reference (e.g. as an lvalue). */
  799. inline unsigned int &
  800. LINEMAPS_MACRO_CACHE (line_maps *set)
  801. {
  802. return LINEMAPS_CACHE (set, true);
  803. }
  804. /* Returns the last macro map used in the line table SET. */
  805. inline line_map_macro *
  806. LINEMAPS_LAST_MACRO_MAP (const line_maps *set)
  807. {
  808. return (line_map_macro *)LINEMAPS_LAST_MAP (set, true);
  809. }
  810. /* Returns the lowest location [of a token resulting from macro
  811. expansion] encoded in this line table. */
  812. inline source_location
  813. LINEMAPS_MACRO_LOWEST_LOCATION (const line_maps *set)
  814. {
  815. return LINEMAPS_MACRO_USED (set)
  816. ? MAP_START_LOCATION (LINEMAPS_LAST_MACRO_MAP (set))
  817. : MAX_SOURCE_LOCATION;
  818. }
  819. /* Returns the last macro map allocated in the line table SET. */
  820. inline line_map_macro *
  821. LINEMAPS_LAST_ALLOCATED_MACRO_MAP (const line_maps *set)
  822. {
  823. return (line_map_macro *)LINEMAPS_LAST_ALLOCATED_MAP (set, true);
  824. }
  825. extern source_location get_combined_adhoc_loc (struct line_maps *,
  826. source_location,
  827. source_range,
  828. void *);
  829. extern void *get_data_from_adhoc_loc (struct line_maps *, source_location);
  830. extern source_location get_location_from_adhoc_loc (struct line_maps *,
  831. source_location);
  832. extern source_range get_range_from_loc (line_maps *set, source_location loc);
  833. /* Get whether location LOC is an ad-hoc location. */
  834. inline bool
  835. IS_ADHOC_LOC (source_location loc)
  836. {
  837. return (loc & MAX_SOURCE_LOCATION) != loc;
  838. }
  839. /* Get whether location LOC is a "pure" location, or
  840. whether it is an ad-hoc location, or embeds range information. */
  841. bool
  842. pure_location_p (line_maps *set, source_location loc);
  843. /* Given location LOC within SET, strip away any packed range information
  844. or ad-hoc information. */
  845. extern source_location get_pure_location (line_maps *set,
  846. source_location loc);
  847. /* Combine LOC and BLOCK, giving a combined adhoc location. */
  848. inline source_location
  849. COMBINE_LOCATION_DATA (struct line_maps *set,
  850. source_location loc,
  851. source_range src_range,
  852. void *block)
  853. {
  854. return get_combined_adhoc_loc (set, loc, src_range, block);
  855. }
  856. extern void rebuild_location_adhoc_htab (struct line_maps *);
  857. /* Initialize a line map set. SET is the line map set to initialize
  858. and BUILTIN_LOCATION is the special location value to be used as
  859. spelling location for built-in tokens. This BUILTIN_LOCATION has
  860. to be strictly less than RESERVED_LOCATION_COUNT. */
  861. extern void linemap_init (struct line_maps *set,
  862. source_location builtin_location);
  863. /* Check for and warn about line_maps entered but not exited. */
  864. extern void linemap_check_files_exited (struct line_maps *);
  865. /* Return a source_location for the start (i.e. column==0) of
  866. (physical) line TO_LINE in the current source file (as in the
  867. most recent linemap_add). MAX_COLUMN_HINT is the highest column
  868. number we expect to use in this line (but it does not change
  869. the highest_location). */
  870. extern source_location linemap_line_start
  871. (struct line_maps *set, linenum_type to_line, unsigned int max_column_hint);
  872. /* Add a mapping of logical source line to physical source file and
  873. line number. This function creates an "ordinary map", which is a
  874. map that records locations of tokens that are not part of macro
  875. replacement-lists present at a macro expansion point.
  876. The text pointed to by TO_FILE must have a lifetime
  877. at least as long as the lifetime of SET. An empty
  878. TO_FILE means standard input. If reason is LC_LEAVE, and
  879. TO_FILE is NULL, then TO_FILE, TO_LINE and SYSP are given their
  880. natural values considering the file we are returning to.
  881. A call to this function can relocate the previous set of
  882. maps, so any stored line_map pointers should not be used. */
  883. extern const struct line_map *linemap_add
  884. (struct line_maps *, enum lc_reason, unsigned int sysp,
  885. const char *to_file, linenum_type to_line);
  886. /* Given a logical source location, returns the map which the
  887. corresponding (source file, line, column) triplet can be deduced
  888. from. Since the set is built chronologically, the logical lines are
  889. monotonic increasing, and so the list is sorted and we can use a
  890. binary search. If no line map have been allocated yet, this
  891. function returns NULL. */
  892. extern const struct line_map *linemap_lookup
  893. (struct line_maps *, source_location);
  894. /* Returns TRUE if the line table set tracks token locations across
  895. macro expansion, FALSE otherwise. */
  896. bool linemap_tracks_macro_expansion_locs_p (struct line_maps *);
  897. /* Return the name of the macro associated to MACRO_MAP. */
  898. const char* linemap_map_get_macro_name (const line_map_macro *);
  899. /* Return a positive value if LOCATION is the locus of a token that is
  900. located in a system header, O otherwise. It returns 1 if LOCATION
  901. is the locus of a token that is located in a system header, and 2
  902. if LOCATION is the locus of a token located in a C system header
  903. that therefore needs to be extern "C" protected in C++.
  904. Note that this function returns 1 if LOCATION belongs to a token
  905. that is part of a macro replacement-list defined in a system
  906. header, but expanded in a non-system file. */
  907. int linemap_location_in_system_header_p (struct line_maps *,
  908. source_location);
  909. /* Return TRUE if LOCATION is a source code location of a token that is part of
  910. a macro expansion, FALSE otherwise. */
  911. bool linemap_location_from_macro_expansion_p (const struct line_maps *,
  912. source_location);
  913. /* TRUE if LOCATION is a source code location of a token that is part of the
  914. definition of a macro, FALSE otherwise. */
  915. bool linemap_location_from_macro_definition_p (struct line_maps *,
  916. source_location);
  917. /* With the precondition that LOCATION is the locus of a token that is
  918. an argument of a function-like macro MACRO_MAP and appears in the
  919. expansion of MACRO_MAP, return the locus of that argument in the
  920. context of the caller of MACRO_MAP. */
  921. extern source_location linemap_macro_map_loc_unwind_toward_spelling
  922. (line_maps *set, const line_map_macro *macro_map, source_location location);
  923. /* source_location values from 0 to RESERVED_LOCATION_COUNT-1 will
  924. be reserved for libcpp user as special values, no token from libcpp
  925. will contain any of those locations. */
  926. const source_location RESERVED_LOCATION_COUNT = 2;
  927. /* Converts a map and a source_location to source line. */
  928. inline linenum_type
  929. SOURCE_LINE (const line_map_ordinary *ord_map, source_location loc)
  930. {
  931. return ((loc - ord_map->start_location)
  932. >> ord_map->m_column_and_range_bits) + ord_map->to_line;
  933. }
  934. /* Convert a map and source_location to source column number. */
  935. inline linenum_type
  936. SOURCE_COLUMN (const line_map_ordinary *ord_map, source_location loc)
  937. {
  938. return ((loc - ord_map->start_location)
  939. & ((1 << ord_map->m_column_and_range_bits) - 1)) >> ord_map->m_range_bits;
  940. }
  941. /* Return the location of the last source line within an ordinary
  942. map. */
  943. inline source_location
  944. LAST_SOURCE_LINE_LOCATION (const line_map_ordinary *map)
  945. {
  946. return (((map[1].start_location - 1
  947. - map->start_location)
  948. & ~((1 << map->m_column_and_range_bits) - 1))
  949. + map->start_location);
  950. }
  951. /* Returns the last source line number within an ordinary map. This
  952. is the (last) line of the #include, or other directive, that caused
  953. a map change. */
  954. inline linenum_type
  955. LAST_SOURCE_LINE (const line_map_ordinary *map)
  956. {
  957. return SOURCE_LINE (map, LAST_SOURCE_LINE_LOCATION (map));
  958. }
  959. /* Return the last column number within an ordinary map. */
  960. inline linenum_type
  961. LAST_SOURCE_COLUMN (const line_map_ordinary *map)
  962. {
  963. return SOURCE_COLUMN (map, LAST_SOURCE_LINE_LOCATION (map));
  964. }
  965. /* Returns the map a given map was included from, or NULL if the map
  966. belongs to the main file, i.e, a file that wasn't included by
  967. another one. */
  968. inline line_map_ordinary *
  969. INCLUDED_FROM (struct line_maps *set, const line_map_ordinary *ord_map)
  970. {
  971. return ((ord_map->included_from == -1)
  972. ? NULL
  973. : LINEMAPS_ORDINARY_MAP_AT (set, ord_map->included_from));
  974. }
  975. /* True if the map is at the bottom of the include stack. */
  976. inline bool
  977. MAIN_FILE_P (const line_map_ordinary *ord_map)
  978. {
  979. return ord_map->included_from < 0;
  980. }
  981. /* Encode and return a source_location from a column number. The
  982. source line considered is the last source line used to call
  983. linemap_line_start, i.e, the last source line which a location was
  984. encoded from. */
  985. extern source_location
  986. linemap_position_for_column (struct line_maps *, unsigned int);
  987. /* Encode and return a source location from a given line and
  988. column. */
  989. source_location
  990. linemap_position_for_line_and_column (line_maps *set,
  991. const line_map_ordinary *,
  992. linenum_type, unsigned int);
  993. /* Encode and return a source_location starting from location LOC and
  994. shifting it by OFFSET columns. This function does not support
  995. virtual locations. */
  996. source_location
  997. linemap_position_for_loc_and_offset (struct line_maps *set,
  998. source_location loc,
  999. unsigned int offset);
  1000. /* Return the file this map is for. */
  1001. inline const char *
  1002. LINEMAP_FILE (const line_map_ordinary *ord_map)
  1003. {
  1004. return ord_map->to_file;
  1005. }
  1006. /* Return the line number this map started encoding location from. */
  1007. inline linenum_type
  1008. LINEMAP_LINE (const line_map_ordinary *ord_map)
  1009. {
  1010. return ord_map->to_line;
  1011. }
  1012. /* Return a positive value if map encodes locations from a system
  1013. header, 0 otherwise. Returns 1 if MAP encodes locations in a
  1014. system header and 2 if it encodes locations in a C system header
  1015. that therefore needs to be extern "C" protected in C++. */
  1016. inline unsigned char
  1017. LINEMAP_SYSP (const line_map_ordinary *ord_map)
  1018. {
  1019. return ord_map->sysp;
  1020. }
  1021. /* Return a positive value if PRE denotes the location of a token that
  1022. comes before the token of POST, 0 if PRE denotes the location of
  1023. the same token as the token for POST, and a negative value
  1024. otherwise. */
  1025. int linemap_compare_locations (struct line_maps *set,
  1026. source_location pre,
  1027. source_location post);
  1028. /* Return TRUE if LOC_A denotes the location a token that comes
  1029. topogically before the token denoted by location LOC_B, or if they
  1030. are equal. */
  1031. inline bool
  1032. linemap_location_before_p (struct line_maps *set,
  1033. source_location loc_a,
  1034. source_location loc_b)
  1035. {
  1036. return linemap_compare_locations (set, loc_a, loc_b) >= 0;
  1037. }
  1038. typedef struct
  1039. {
  1040. /* The name of the source file involved. */
  1041. const char *file;
  1042. /* The line-location in the source file. */
  1043. int line;
  1044. int column;
  1045. void *data;
  1046. /* In a system header?. */
  1047. bool sysp;
  1048. } expanded_location;
  1049. /* A location within a rich_location: a caret&range, with
  1050. the caret potentially flagged for display. */
  1051. struct location_range
  1052. {
  1053. source_location m_loc;
  1054. /* Should a caret be drawn for this range? Typically this is
  1055. true for the 0th range, and false for subsequent ranges,
  1056. but the Fortran frontend overrides this for rendering things like:
  1057. x = x + y
  1058. 1 2
  1059. Error: Shapes for operands at (1) and (2) are not conformable
  1060. where "1" and "2" are notionally carets. */
  1061. bool m_show_caret_p;
  1062. };
  1063. /* A partially-embedded vec for use within rich_location for storing
  1064. ranges and fix-it hints.
  1065. Elements [0..NUM_EMBEDDED) are allocated within m_embed, after
  1066. that they are within the dynamically-allocated m_extra.
  1067. This allows for static allocation in the common case, whilst
  1068. supporting the rarer case of an arbitrary number of elements.
  1069. Dynamic allocation is not performed unless it's needed. */
  1070. template <typename T, int NUM_EMBEDDED>
  1071. class semi_embedded_vec
  1072. {
  1073. public:
  1074. semi_embedded_vec ();
  1075. ~semi_embedded_vec ();
  1076. unsigned int count () const { return m_num; }
  1077. T& operator[] (int idx);
  1078. const T& operator[] (int idx) const;
  1079. void push (const T&);
  1080. void truncate (int len);
  1081. private:
  1082. int m_num;
  1083. T m_embedded[NUM_EMBEDDED];
  1084. int m_alloc;
  1085. T *m_extra;
  1086. };
  1087. /* Constructor for semi_embedded_vec. In particular, no dynamic allocation
  1088. is done. */
  1089. template <typename T, int NUM_EMBEDDED>
  1090. semi_embedded_vec<T, NUM_EMBEDDED>::semi_embedded_vec ()
  1091. : m_num (0), m_alloc (0), m_extra (NULL)
  1092. {
  1093. }
  1094. /* semi_embedded_vec's dtor. Release any dynamically-allocated memory. */
  1095. template <typename T, int NUM_EMBEDDED>
  1096. semi_embedded_vec<T, NUM_EMBEDDED>::~semi_embedded_vec ()
  1097. {
  1098. XDELETEVEC (m_extra);
  1099. }
  1100. /* Look up element IDX, mutably. */
  1101. template <typename T, int NUM_EMBEDDED>
  1102. T&
  1103. semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx)
  1104. {
  1105. linemap_assert (idx < m_num);
  1106. if (idx < NUM_EMBEDDED)
  1107. return m_embedded[idx];
  1108. else
  1109. {
  1110. linemap_assert (m_extra != NULL);
  1111. return m_extra[idx - NUM_EMBEDDED];
  1112. }
  1113. }
  1114. /* Look up element IDX (const). */
  1115. template <typename T, int NUM_EMBEDDED>
  1116. const T&
  1117. semi_embedded_vec<T, NUM_EMBEDDED>::operator[] (int idx) const
  1118. {
  1119. linemap_assert (idx < m_num);
  1120. if (idx < NUM_EMBEDDED)
  1121. return m_embedded[idx];
  1122. else
  1123. {
  1124. linemap_assert (m_extra != NULL);
  1125. return m_extra[idx - NUM_EMBEDDED];
  1126. }
  1127. }
  1128. /* Append VALUE to the end of the semi_embedded_vec. */
  1129. template <typename T, int NUM_EMBEDDED>
  1130. void
  1131. semi_embedded_vec<T, NUM_EMBEDDED>::push (const T& value)
  1132. {
  1133. int idx = m_num++;
  1134. if (idx < NUM_EMBEDDED)
  1135. m_embedded[idx] = value;
  1136. else
  1137. {
  1138. /* Offset "idx" to be an index within m_extra. */
  1139. idx -= NUM_EMBEDDED;
  1140. if (NULL == m_extra)
  1141. {
  1142. linemap_assert (m_alloc == 0);
  1143. m_alloc = 16;
  1144. m_extra = XNEWVEC (T, m_alloc);
  1145. }
  1146. else if (idx >= m_alloc)
  1147. {
  1148. linemap_assert (m_alloc > 0);
  1149. m_alloc *= 2;
  1150. m_extra = XRESIZEVEC (T, m_extra, m_alloc);
  1151. }
  1152. linemap_assert (m_extra);
  1153. linemap_assert (idx < m_alloc);
  1154. m_extra[idx] = value;
  1155. }
  1156. }
  1157. /* Truncate to length LEN. No deallocation is performed. */
  1158. template <typename T, int NUM_EMBEDDED>
  1159. void
  1160. semi_embedded_vec<T, NUM_EMBEDDED>::truncate (int len)
  1161. {
  1162. linemap_assert (len <= m_num);
  1163. m_num = len;
  1164. }
  1165. class fixit_hint;
  1166. /* A "rich" source code location, for use when printing diagnostics.
  1167. A rich_location has one or more carets&ranges, where the carets
  1168. are optional. These are referred to as "ranges" from here.
  1169. Typically the zeroth range has a caret; other ranges sometimes
  1170. have carets.
  1171. The "primary" location of a rich_location is the caret of range 0,
  1172. used for determining the line/column when printing diagnostic
  1173. text, such as:
  1174. some-file.c:3:1: error: ...etc...
  1175. Additional ranges may be added to help the user identify other
  1176. pertinent clauses in a diagnostic.
  1177. rich_location instances are intended to be allocated on the stack
  1178. when generating diagnostics, and to be short-lived.
  1179. Examples of rich locations
  1180. --------------------------
  1181. Example A
  1182. *********
  1183. int i = "foo";
  1184. ^
  1185. This "rich" location is simply a single range (range 0), with
  1186. caret = start = finish at the given point.
  1187. Example B
  1188. *********
  1189. a = (foo && bar)
  1190. ~~~~~^~~~~~~
  1191. This rich location has a single range (range 0), with the caret
  1192. at the first "&", and the start/finish at the parentheses.
  1193. Compare with example C below.
  1194. Example C
  1195. *********
  1196. a = (foo && bar)
  1197. ~~~ ^~ ~~~
  1198. This rich location has three ranges:
  1199. - Range 0 has its caret and start location at the first "&" and
  1200. end at the second "&.
  1201. - Range 1 has its start and finish at the "f" and "o" of "foo";
  1202. the caret is not flagged for display, but is perhaps at the "f"
  1203. of "foo".
  1204. - Similarly, range 2 has its start and finish at the "b" and "r" of
  1205. "bar"; the caret is not flagged for display, but is perhaps at the
  1206. "b" of "bar".
  1207. Compare with example B above.
  1208. Example D (Fortran frontend)
  1209. ****************************
  1210. x = x + y
  1211. 1 2
  1212. This rich location has range 0 at "1", and range 1 at "2".
  1213. Both are flagged for caret display. Both ranges have start/finish
  1214. equal to their caret point. The frontend overrides the diagnostic
  1215. context's default caret character for these ranges.
  1216. Example E
  1217. *********
  1218. printf ("arg0: %i arg1: %s arg2: %i",
  1219. ^~
  1220. 100, 101, 102);
  1221. ~~~
  1222. This rich location has two ranges:
  1223. - range 0 is at the "%s" with start = caret = "%" and finish at
  1224. the "s".
  1225. - range 1 has start/finish covering the "101" and is not flagged for
  1226. caret printing; it is perhaps at the start of "101".
  1227. Fix-it hints
  1228. ------------
  1229. Rich locations can also contain "fix-it hints", giving suggestions
  1230. for the user on how to edit their code to fix a problem. These
  1231. can be expressed as insertions, replacements, and removals of text.
  1232. The edits by default are relative to the zeroth range within the
  1233. rich_location, but optionally they can be expressed relative to
  1234. other locations (using various overloaded methods of the form
  1235. rich_location::add_fixit_*).
  1236. For example:
  1237. Example F: fix-it hint: insert_before
  1238. *************************************
  1239. ptr = arr[0];
  1240. ^~~~~~
  1241. &
  1242. This rich location has a single range (range 0) covering "arr[0]",
  1243. with the caret at the start. The rich location has a single
  1244. insertion fix-it hint, inserted before range 0, added via
  1245. richloc.add_fixit_insert_before ("&");
  1246. Example G: multiple fix-it hints: insert_before and insert_after
  1247. ****************************************************************
  1248. #define FN(ARG0, ARG1, ARG2) fn(ARG0, ARG1, ARG2)
  1249. ^~~~ ^~~~ ^~~~
  1250. ( ) ( ) ( )
  1251. This rich location has three ranges, covering "arg0", "arg1",
  1252. and "arg2", all with caret-printing enabled.
  1253. The rich location has 6 insertion fix-it hints: each arg
  1254. has a pair of insertion fix-it hints, suggesting wrapping
  1255. them with parentheses: one a '(' inserted before,
  1256. the other a ')' inserted after, added via
  1257. richloc.add_fixit_insert_before (LOC, "(");
  1258. and
  1259. richloc.add_fixit_insert_after (LOC, ")");
  1260. Example H: fix-it hint: removal
  1261. *******************************
  1262. struct s {int i};;
  1263. ^
  1264. -
  1265. This rich location has a single range at the stray trailing
  1266. semicolon, along with a single removal fix-it hint, covering
  1267. the same range, added via:
  1268. richloc.add_fixit_remove ();
  1269. Example I: fix-it hint: replace
  1270. *******************************
  1271. c = s.colour;
  1272. ^~~~~~
  1273. color
  1274. This rich location has a single range (range 0) covering "colour",
  1275. and a single "replace" fix-it hint, covering the same range,
  1276. added via
  1277. richloc.add_fixit_replace ("color");
  1278. Adding a fix-it hint can fail: for example, attempts to insert content
  1279. at the transition between two line maps may fail due to there being no
  1280. source_location (aka location_t) value to express the new location.
  1281. Attempts to add a fix-it hint within a macro expansion will fail.
  1282. There is only limited support for newline characters in fix-it hints:
  1283. only hints with newlines which insert an entire new line are permitted,
  1284. inserting at the start of a line, and finishing with a newline
  1285. (with no interior newline characters). Other attempts to add
  1286. fix-it hints containing newline characters will fail.
  1287. Similarly, attempts to delete or replace a range *affecting* multiple
  1288. lines will fail.
  1289. The rich_location API handles these failures gracefully, so that
  1290. diagnostics can attempt to add fix-it hints without each needing
  1291. extensive checking.
  1292. Fix-it hints within a rich_location are "atomic": if any hints can't
  1293. be applied, none of them will be (tracked by the m_seen_impossible_fixit
  1294. flag), and no fix-its hints will be displayed for that rich_location.
  1295. This implies that diagnostic messages need to be worded in such a way
  1296. that they make sense whether or not the fix-it hints are displayed,
  1297. or that richloc.seen_impossible_fixit_p () should be checked before
  1298. issuing the diagnostics. */
  1299. class rich_location
  1300. {
  1301. public:
  1302. /* Constructors. */
  1303. /* Constructing from a location. */
  1304. rich_location (line_maps *set, source_location loc);
  1305. /* Destructor. */
  1306. ~rich_location ();
  1307. /* Accessors. */
  1308. source_location get_loc () const { return get_loc (0); }
  1309. source_location get_loc (unsigned int idx) const;
  1310. void
  1311. add_range (source_location loc, bool show_caret_p);
  1312. void
  1313. set_range (line_maps *set, unsigned int idx, source_location loc,
  1314. bool show_caret_p);
  1315. unsigned int get_num_locations () const { return m_ranges.count (); }
  1316. const location_range *get_range (unsigned int idx) const;
  1317. location_range *get_range (unsigned int idx);
  1318. expanded_location get_expanded_location (unsigned int idx);
  1319. void
  1320. override_column (int column);
  1321. /* Fix-it hints. */
  1322. /* Methods for adding insertion fix-it hints. */
  1323. /* Suggest inserting NEW_CONTENT immediately before the primary
  1324. range's start. */
  1325. void
  1326. add_fixit_insert_before (const char *new_content);
  1327. /* Suggest inserting NEW_CONTENT immediately before the start of WHERE. */
  1328. void
  1329. add_fixit_insert_before (source_location where,
  1330. const char *new_content);
  1331. /* Suggest inserting NEW_CONTENT immediately after the end of the primary
  1332. range. */
  1333. void
  1334. add_fixit_insert_after (const char *new_content);
  1335. /* Suggest inserting NEW_CONTENT immediately after the end of WHERE. */
  1336. void
  1337. add_fixit_insert_after (source_location where,
  1338. const char *new_content);
  1339. /* Methods for adding removal fix-it hints. */
  1340. /* Suggest removing the content covered by range 0. */
  1341. void
  1342. add_fixit_remove ();
  1343. /* Suggest removing the content covered between the start and finish
  1344. of WHERE. */
  1345. void
  1346. add_fixit_remove (source_location where);
  1347. /* Suggest removing the content covered by SRC_RANGE. */
  1348. void
  1349. add_fixit_remove (source_range src_range);
  1350. /* Methods for adding "replace" fix-it hints. */
  1351. /* Suggest replacing the content covered by range 0 with NEW_CONTENT. */
  1352. void
  1353. add_fixit_replace (const char *new_content);
  1354. /* Suggest replacing the content between the start and finish of
  1355. WHERE with NEW_CONTENT. */
  1356. void
  1357. add_fixit_replace (source_location where,
  1358. const char *new_content);
  1359. /* Suggest replacing the content covered by SRC_RANGE with
  1360. NEW_CONTENT. */
  1361. void
  1362. add_fixit_replace (source_range src_range,
  1363. const char *new_content);
  1364. unsigned int get_num_fixit_hints () const { return m_fixit_hints.count (); }
  1365. fixit_hint *get_fixit_hint (int idx) const { return m_fixit_hints[idx]; }
  1366. fixit_hint *get_last_fixit_hint () const;
  1367. bool seen_impossible_fixit_p () const { return m_seen_impossible_fixit; }
  1368. /* Set this if the fix-it hints are not suitable to be
  1369. automatically applied.
  1370. For example, if you are suggesting more than one
  1371. mutually exclusive solution to a problem, then
  1372. it doesn't make sense to apply all of the solutions;
  1373. manual intervention is required.
  1374. If set, then the fix-it hints in the rich_location will
  1375. be printed, but will not be added to generated patches,
  1376. or affect the modified version of the file. */
  1377. void fixits_cannot_be_auto_applied ()
  1378. {
  1379. m_fixits_cannot_be_auto_applied = true;
  1380. }
  1381. bool fixits_can_be_auto_applied_p () const
  1382. {
  1383. return !m_fixits_cannot_be_auto_applied;
  1384. }
  1385. private:
  1386. bool reject_impossible_fixit (source_location where);
  1387. void stop_supporting_fixits ();
  1388. void maybe_add_fixit (source_location start,
  1389. source_location next_loc,
  1390. const char *new_content);
  1391. public:
  1392. static const int STATICALLY_ALLOCATED_RANGES = 3;
  1393. protected:
  1394. line_maps *m_line_table;
  1395. semi_embedded_vec <location_range, STATICALLY_ALLOCATED_RANGES> m_ranges;
  1396. int m_column_override;
  1397. bool m_have_expanded_location;
  1398. expanded_location m_expanded_location;
  1399. static const int MAX_STATIC_FIXIT_HINTS = 2;
  1400. semi_embedded_vec <fixit_hint *, MAX_STATIC_FIXIT_HINTS> m_fixit_hints;
  1401. bool m_seen_impossible_fixit;
  1402. bool m_fixits_cannot_be_auto_applied;
  1403. };
  1404. /* A fix-it hint: a suggested insertion, replacement, or deletion of text.
  1405. We handle these three types of edit with one class, by representing
  1406. them as replacement of a half-open range:
  1407. [start, next_loc)
  1408. Insertions have start == next_loc: "replace" the empty string at the
  1409. start location with the new string.
  1410. Deletions are replacement with the empty string.
  1411. There is only limited support for newline characters in fix-it hints
  1412. as noted above in the comment for class rich_location.
  1413. A fixit_hint instance can have at most one newline character; if
  1414. present, the newline character must be the final character of
  1415. the content (preventing e.g. fix-its that split a pre-existing line). */
  1416. class fixit_hint
  1417. {
  1418. public:
  1419. fixit_hint (source_location start,
  1420. source_location next_loc,
  1421. const char *new_content);
  1422. ~fixit_hint () { free (m_bytes); }
  1423. bool affects_line_p (const char *file, int line) const;
  1424. source_location get_start_loc () const { return m_start; }
  1425. source_location get_next_loc () const { return m_next_loc; }
  1426. bool maybe_append (source_location start,
  1427. source_location next_loc,
  1428. const char *new_content);
  1429. const char *get_string () const { return m_bytes; }
  1430. size_t get_length () const { return m_len; }
  1431. bool insertion_p () const { return m_start == m_next_loc; }
  1432. bool ends_with_newline_p () const;
  1433. private:
  1434. /* We don't use source_range here since, unlike most places,
  1435. this is a half-open/half-closed range:
  1436. [start, next_loc)
  1437. so that we can support insertion via start == next_loc. */
  1438. source_location m_start;
  1439. source_location m_next_loc;
  1440. char *m_bytes;
  1441. size_t m_len;
  1442. };
  1443. /* This is enum is used by the function linemap_resolve_location
  1444. below. The meaning of the values is explained in the comment of
  1445. that function. */
  1446. enum location_resolution_kind
  1447. {
  1448. LRK_MACRO_EXPANSION_POINT,
  1449. LRK_SPELLING_LOCATION,
  1450. LRK_MACRO_DEFINITION_LOCATION
  1451. };
  1452. /* Resolve a virtual location into either a spelling location, an
  1453. expansion point location or a token argument replacement point
  1454. location. Return the map that encodes the virtual location as well
  1455. as the resolved location.
  1456. If LOC is *NOT* the location of a token resulting from the
  1457. expansion of a macro, then the parameter LRK (which stands for
  1458. Location Resolution Kind) is ignored and the resulting location
  1459. just equals the one given in argument.
  1460. Now if LOC *IS* the location of a token resulting from the
  1461. expansion of a macro, this is what happens.
  1462. * If LRK is set to LRK_MACRO_EXPANSION_POINT
  1463. -------------------------------
  1464. The virtual location is resolved to the first macro expansion point
  1465. that led to this macro expansion.
  1466. * If LRK is set to LRK_SPELLING_LOCATION
  1467. -------------------------------------
  1468. The virtual location is resolved to the locus where the token has
  1469. been spelled in the source. This can follow through all the macro
  1470. expansions that led to the token.
  1471. * If LRK is set to LRK_MACRO_DEFINITION_LOCATION
  1472. --------------------------------------
  1473. The virtual location is resolved to the locus of the token in the
  1474. context of the macro definition.
  1475. If LOC is the locus of a token that is an argument of a
  1476. function-like macro [replacing a parameter in the replacement list
  1477. of the macro] the virtual location is resolved to the locus of the
  1478. parameter that is replaced, in the context of the definition of the
  1479. macro.
  1480. If LOC is the locus of a token that is not an argument of a
  1481. function-like macro, then the function behaves as if LRK was set to
  1482. LRK_SPELLING_LOCATION.
  1483. If LOC_MAP is not NULL, *LOC_MAP is set to the map encoding the
  1484. returned location. Note that if the returned location wasn't originally
  1485. encoded by a map, the *MAP is set to NULL. This can happen if LOC
  1486. resolves to a location reserved for the client code, like
  1487. UNKNOWN_LOCATION or BUILTINS_LOCATION in GCC. */
  1488. source_location linemap_resolve_location (struct line_maps *,
  1489. source_location loc,
  1490. enum location_resolution_kind lrk,
  1491. const line_map_ordinary **loc_map);
  1492. /* Suppose that LOC is the virtual location of a token coming from the
  1493. expansion of a macro M. This function then steps up to get the
  1494. location L of the point where M got expanded. If L is a spelling
  1495. location inside a macro expansion M', then this function returns
  1496. the point where M' was expanded. LOC_MAP is an output parameter.
  1497. When non-NULL, *LOC_MAP is set to the map of the returned
  1498. location. */
  1499. source_location linemap_unwind_toward_expansion (struct line_maps *,
  1500. source_location loc,
  1501. const struct line_map **loc_map);
  1502. /* If LOC is the virtual location of a token coming from the expansion
  1503. of a macro M and if its spelling location is reserved (e.g, a
  1504. location for a built-in token), then this function unwinds (using
  1505. linemap_unwind_toward_expansion) the location until a location that
  1506. is not reserved and is not in a system header is reached. In other
  1507. words, this unwinds the reserved location until a location that is
  1508. in real source code is reached.
  1509. Otherwise, if the spelling location for LOC is not reserved or if
  1510. LOC doesn't come from the expansion of a macro, the function
  1511. returns LOC as is and *MAP is not touched.
  1512. *MAP is set to the map of the returned location if the later is
  1513. different from LOC. */
  1514. source_location linemap_unwind_to_first_non_reserved_loc (struct line_maps *,
  1515. source_location loc,
  1516. const struct line_map **map);
  1517. /* Expand source code location LOC and return a user readable source
  1518. code location. LOC must be a spelling (non-virtual) location. If
  1519. it's a location < RESERVED_LOCATION_COUNT a zeroed expanded source
  1520. location is returned. */
  1521. expanded_location linemap_expand_location (struct line_maps *,
  1522. const struct line_map *,
  1523. source_location loc);
  1524. /* Statistics about maps allocation and usage as returned by
  1525. linemap_get_statistics. */
  1526. struct linemap_stats
  1527. {
  1528. long num_ordinary_maps_allocated;
  1529. long num_ordinary_maps_used;
  1530. long ordinary_maps_allocated_size;
  1531. long ordinary_maps_used_size;
  1532. long num_expanded_macros;
  1533. long num_macro_tokens;
  1534. long num_macro_maps_used;
  1535. long macro_maps_allocated_size;
  1536. long macro_maps_used_size;
  1537. long macro_maps_locations_size;
  1538. long duplicated_macro_maps_locations_size;
  1539. long adhoc_table_size;
  1540. long adhoc_table_entries_used;
  1541. };
  1542. /* Return the highest location emitted for a given file for which
  1543. there is a line map in SET. FILE_NAME is the file name to
  1544. consider. If the function returns TRUE, *LOC is set to the highest
  1545. location emitted for that file. */
  1546. bool linemap_get_file_highest_location (struct line_maps * set,
  1547. const char *file_name,
  1548. source_location *loc);
  1549. /* Compute and return statistics about the memory consumption of some
  1550. parts of the line table SET. */
  1551. void linemap_get_statistics (struct line_maps *, struct linemap_stats *);
  1552. /* Dump debugging information about source location LOC into the file
  1553. stream STREAM. SET is the line map set LOC comes from. */
  1554. void linemap_dump_location (struct line_maps *, source_location, FILE *);
  1555. /* Dump line map at index IX in line table SET to STREAM. If STREAM
  1556. is NULL, use stderr. IS_MACRO is true if the caller wants to
  1557. dump a macro map, false otherwise. */
  1558. void linemap_dump (FILE *, struct line_maps *, unsigned, bool);
  1559. /* Dump line table SET to STREAM. If STREAM is NULL, stderr is used.
  1560. NUM_ORDINARY specifies how many ordinary maps to dump. NUM_MACRO
  1561. specifies how many macro maps to dump. */
  1562. void line_table_dump (FILE *, struct line_maps *, unsigned int, unsigned int);
  1563. /* An enum for distinguishing the various parts within a source_location. */
  1564. enum location_aspect
  1565. {
  1566. LOCATION_ASPECT_CARET,
  1567. LOCATION_ASPECT_START,
  1568. LOCATION_ASPECT_FINISH
  1569. };
  1570. /* The rich_location class requires a way to expand source_location instances.
  1571. We would directly use expand_location_to_spelling_point, which is
  1572. implemented in gcc/input.c, but we also need to use it for rich_location
  1573. within genmatch.c.
  1574. Hence we require client code of libcpp to implement the following
  1575. symbol. */
  1576. extern expanded_location
  1577. linemap_client_expand_location_to_spelling_point (source_location,
  1578. enum location_aspect);
  1579. #endif /* !LIBCPP_LINE_MAP_H */