line-map.h 67 KB

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