lex.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308
  1. /* CPP Library - lexical analysis.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Contributed by Per Bothner, 1994-95.
  4. Based on CCCP program by Paul Rubin, June 1986
  5. Adapted to ANSI C, Richard Stallman, Jan 1987
  6. Broken out to separate file, Zack Weinberg, Mar 2000
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 3, or (at your option) any
  10. later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "cpplib.h"
  21. #include "internal.h"
  22. enum spell_type
  23. {
  24. SPELL_OPERATOR = 0,
  25. SPELL_IDENT,
  26. SPELL_LITERAL,
  27. SPELL_NONE
  28. };
  29. struct token_spelling
  30. {
  31. enum spell_type category;
  32. const unsigned char *name;
  33. };
  34. static const unsigned char *const digraph_spellings[] =
  35. { UC"%:", UC"%:%:", UC"<:", UC":>", UC"<%", UC"%>" };
  36. #define OP(e, s) { SPELL_OPERATOR, UC s },
  37. #define TK(e, s) { SPELL_ ## s, UC #e },
  38. static const struct token_spelling token_spellings[N_TTYPES] = { TTYPE_TABLE };
  39. #undef OP
  40. #undef TK
  41. #define TOKEN_SPELL(token) (token_spellings[(token)->type].category)
  42. #define TOKEN_NAME(token) (token_spellings[(token)->type].name)
  43. static void add_line_note (cpp_buffer *, const uchar *, unsigned int);
  44. static int skip_line_comment (cpp_reader *);
  45. static void skip_whitespace (cpp_reader *, cppchar_t);
  46. static void lex_string (cpp_reader *, cpp_token *, const uchar *);
  47. static void save_comment (cpp_reader *, cpp_token *, const uchar *, cppchar_t);
  48. static void store_comment (cpp_reader *, cpp_token *);
  49. static void create_literal (cpp_reader *, cpp_token *, const uchar *,
  50. unsigned int, enum cpp_ttype);
  51. static bool warn_in_comment (cpp_reader *, _cpp_line_note *);
  52. static int name_p (cpp_reader *, const cpp_string *);
  53. static tokenrun *next_tokenrun (tokenrun *);
  54. static _cpp_buff *new_buff (size_t);
  55. /* Utility routine:
  56. Compares, the token TOKEN to the NUL-terminated string STRING.
  57. TOKEN must be a CPP_NAME. Returns 1 for equal, 0 for unequal. */
  58. int
  59. cpp_ideq (const cpp_token *token, const char *string)
  60. {
  61. if (token->type != CPP_NAME)
  62. return 0;
  63. return !ustrcmp (NODE_NAME (token->val.node.node), (const uchar *) string);
  64. }
  65. /* Record a note TYPE at byte POS into the current cleaned logical
  66. line. */
  67. static void
  68. add_line_note (cpp_buffer *buffer, const uchar *pos, unsigned int type)
  69. {
  70. if (buffer->notes_used == buffer->notes_cap)
  71. {
  72. buffer->notes_cap = buffer->notes_cap * 2 + 200;
  73. buffer->notes = XRESIZEVEC (_cpp_line_note, buffer->notes,
  74. buffer->notes_cap);
  75. }
  76. buffer->notes[buffer->notes_used].pos = pos;
  77. buffer->notes[buffer->notes_used].type = type;
  78. buffer->notes_used++;
  79. }
  80. /* Fast path to find line special characters using optimized character
  81. scanning algorithms. Anything complicated falls back to the slow
  82. path below. Since this loop is very hot it's worth doing these kinds
  83. of optimizations.
  84. One of the paths through the ifdefs should provide
  85. const uchar *search_line_fast (const uchar *s, const uchar *end);
  86. Between S and END, search for \n, \r, \\, ?. Return a pointer to
  87. the found character.
  88. Note that the last character of the buffer is *always* a newline,
  89. as forced by _cpp_convert_input. This fact can be used to avoid
  90. explicitly looking for the end of the buffer. */
  91. /* Configure gives us an ifdef test. */
  92. #ifndef WORDS_BIGENDIAN
  93. #define WORDS_BIGENDIAN 0
  94. #endif
  95. /* We'd like the largest integer that fits into a register. There's nothing
  96. in <stdint.h> that gives us that. For most hosts this is unsigned long,
  97. but MS decided on an LLP64 model. Thankfully when building with GCC we
  98. can get the "real" word size. */
  99. #ifdef __GNUC__
  100. typedef unsigned int word_type __attribute__((__mode__(__word__)));
  101. #else
  102. typedef unsigned long word_type;
  103. #endif
  104. /* The code below is only expecting sizes 4 or 8.
  105. Die at compile-time if this expectation is violated. */
  106. typedef char check_word_type_size
  107. [(sizeof(word_type) == 8 || sizeof(word_type) == 4) * 2 - 1];
  108. /* Return X with the first N bytes forced to values that won't match one
  109. of the interesting characters. Note that NUL is not interesting. */
  110. static inline word_type
  111. acc_char_mask_misalign (word_type val, unsigned int n)
  112. {
  113. word_type mask = -1;
  114. if (WORDS_BIGENDIAN)
  115. mask >>= n * 8;
  116. else
  117. mask <<= n * 8;
  118. return val & mask;
  119. }
  120. /* Return X replicated to all byte positions within WORD_TYPE. */
  121. static inline word_type
  122. acc_char_replicate (uchar x)
  123. {
  124. word_type ret;
  125. ret = (x << 24) | (x << 16) | (x << 8) | x;
  126. if (sizeof(word_type) == 8)
  127. ret = (ret << 16 << 16) | ret;
  128. return ret;
  129. }
  130. /* Return non-zero if some byte of VAL is (probably) C. */
  131. static inline word_type
  132. acc_char_cmp (word_type val, word_type c)
  133. {
  134. #if defined(__GNUC__) && defined(__alpha__)
  135. /* We can get exact results using a compare-bytes instruction.
  136. Get (val == c) via (0 >= (val ^ c)). */
  137. return __builtin_alpha_cmpbge (0, val ^ c);
  138. #else
  139. word_type magic = 0x7efefefeU;
  140. if (sizeof(word_type) == 8)
  141. magic = (magic << 16 << 16) | 0xfefefefeU;
  142. magic |= 1;
  143. val ^= c;
  144. return ((val + magic) ^ ~val) & ~magic;
  145. #endif
  146. }
  147. /* Given the result of acc_char_cmp is non-zero, return the index of
  148. the found character. If this was a false positive, return -1. */
  149. static inline int
  150. acc_char_index (word_type cmp ATTRIBUTE_UNUSED,
  151. word_type val ATTRIBUTE_UNUSED)
  152. {
  153. #if defined(__GNUC__) && defined(__alpha__) && !WORDS_BIGENDIAN
  154. /* The cmpbge instruction sets *bits* of the result corresponding to
  155. matches in the bytes with no false positives. */
  156. return __builtin_ctzl (cmp);
  157. #else
  158. unsigned int i;
  159. /* ??? It would be nice to force unrolling here,
  160. and have all of these constants folded. */
  161. for (i = 0; i < sizeof(word_type); ++i)
  162. {
  163. uchar c;
  164. if (WORDS_BIGENDIAN)
  165. c = (val >> (sizeof(word_type) - i - 1) * 8) & 0xff;
  166. else
  167. c = (val >> i * 8) & 0xff;
  168. if (c == '\n' || c == '\r' || c == '\\' || c == '?')
  169. return i;
  170. }
  171. return -1;
  172. #endif
  173. }
  174. /* A version of the fast scanner using bit fiddling techniques.
  175. For 32-bit words, one would normally perform 16 comparisons and
  176. 16 branches. With this algorithm one performs 24 arithmetic
  177. operations and one branch. Whether this is faster with a 32-bit
  178. word size is going to be somewhat system dependent.
  179. For 64-bit words, we eliminate twice the number of comparisons
  180. and branches without increasing the number of arithmetic operations.
  181. It's almost certainly going to be a win with 64-bit word size. */
  182. static const uchar * search_line_acc_char (const uchar *, const uchar *)
  183. ATTRIBUTE_UNUSED;
  184. static const uchar *
  185. search_line_acc_char (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
  186. {
  187. const word_type repl_nl = acc_char_replicate ('\n');
  188. const word_type repl_cr = acc_char_replicate ('\r');
  189. const word_type repl_bs = acc_char_replicate ('\\');
  190. const word_type repl_qm = acc_char_replicate ('?');
  191. unsigned int misalign;
  192. const word_type *p;
  193. word_type val, t;
  194. /* Align the buffer. Mask out any bytes from before the beginning. */
  195. p = (word_type *)((uintptr_t)s & -sizeof(word_type));
  196. val = *p;
  197. misalign = (uintptr_t)s & (sizeof(word_type) - 1);
  198. if (misalign)
  199. val = acc_char_mask_misalign (val, misalign);
  200. /* Main loop. */
  201. while (1)
  202. {
  203. t = acc_char_cmp (val, repl_nl);
  204. t |= acc_char_cmp (val, repl_cr);
  205. t |= acc_char_cmp (val, repl_bs);
  206. t |= acc_char_cmp (val, repl_qm);
  207. if (__builtin_expect (t != 0, 0))
  208. {
  209. int i = acc_char_index (t, val);
  210. if (i >= 0)
  211. return (const uchar *)p + i;
  212. }
  213. val = *++p;
  214. }
  215. }
  216. /* Disable on Solaris 2/x86 until the following problem can be properly
  217. autoconfed:
  218. The Solaris 10+ assembler tags objects with the instruction set
  219. extensions used, so SSE4.2 executables cannot run on machines that
  220. don't support that extension. */
  221. #if (GCC_VERSION >= 4005) && (__GNUC__ >= 5 || !defined(__PIC__)) && (defined(__i386__) || defined(__x86_64__)) && !(defined(__sun__) && defined(__svr4__))
  222. /* Replicated character data to be shared between implementations.
  223. Recall that outside of a context with vector support we can't
  224. define compatible vector types, therefore these are all defined
  225. in terms of raw characters. */
  226. static const char repl_chars[4][16] __attribute__((aligned(16))) = {
  227. { '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n',
  228. '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n' },
  229. { '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r',
  230. '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r' },
  231. { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\',
  232. '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' },
  233. { '?', '?', '?', '?', '?', '?', '?', '?',
  234. '?', '?', '?', '?', '?', '?', '?', '?' },
  235. };
  236. /* A version of the fast scanner using MMX vectorized byte compare insns.
  237. This uses the PMOVMSKB instruction which was introduced with "MMX2",
  238. which was packaged into SSE1; it is also present in the AMD MMX
  239. extension. Mark the function as using "sse" so that we emit a real
  240. "emms" instruction, rather than the 3dNOW "femms" instruction. */
  241. static const uchar *
  242. #ifndef __SSE__
  243. __attribute__((__target__("sse")))
  244. #endif
  245. search_line_mmx (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
  246. {
  247. typedef char v8qi __attribute__ ((__vector_size__ (8)));
  248. typedef int __m64 __attribute__ ((__vector_size__ (8), __may_alias__));
  249. const v8qi repl_nl = *(const v8qi *)repl_chars[0];
  250. const v8qi repl_cr = *(const v8qi *)repl_chars[1];
  251. const v8qi repl_bs = *(const v8qi *)repl_chars[2];
  252. const v8qi repl_qm = *(const v8qi *)repl_chars[3];
  253. unsigned int misalign, found, mask;
  254. const v8qi *p;
  255. v8qi data, t, c;
  256. /* Align the source pointer. While MMX doesn't generate unaligned data
  257. faults, this allows us to safely scan to the end of the buffer without
  258. reading beyond the end of the last page. */
  259. misalign = (uintptr_t)s & 7;
  260. p = (const v8qi *)((uintptr_t)s & -8);
  261. data = *p;
  262. /* Create a mask for the bytes that are valid within the first
  263. 16-byte block. The Idea here is that the AND with the mask
  264. within the loop is "free", since we need some AND or TEST
  265. insn in order to set the flags for the branch anyway. */
  266. mask = -1u << misalign;
  267. /* Main loop processing 8 bytes at a time. */
  268. goto start;
  269. do
  270. {
  271. data = *++p;
  272. mask = -1;
  273. start:
  274. t = __builtin_ia32_pcmpeqb(data, repl_nl);
  275. c = __builtin_ia32_pcmpeqb(data, repl_cr);
  276. t = (v8qi) __builtin_ia32_por ((__m64)t, (__m64)c);
  277. c = __builtin_ia32_pcmpeqb(data, repl_bs);
  278. t = (v8qi) __builtin_ia32_por ((__m64)t, (__m64)c);
  279. c = __builtin_ia32_pcmpeqb(data, repl_qm);
  280. t = (v8qi) __builtin_ia32_por ((__m64)t, (__m64)c);
  281. found = __builtin_ia32_pmovmskb (t);
  282. found &= mask;
  283. }
  284. while (!found);
  285. __builtin_ia32_emms ();
  286. /* FOUND contains 1 in bits for which we matched a relevant
  287. character. Conversion to the byte index is trivial. */
  288. found = __builtin_ctz(found);
  289. return (const uchar *)p + found;
  290. }
  291. /* A version of the fast scanner using SSE2 vectorized byte compare insns. */
  292. static const uchar *
  293. #ifndef __SSE2__
  294. __attribute__((__target__("sse2")))
  295. #endif
  296. search_line_sse2 (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
  297. {
  298. typedef char v16qi __attribute__ ((__vector_size__ (16)));
  299. const v16qi repl_nl = *(const v16qi *)repl_chars[0];
  300. const v16qi repl_cr = *(const v16qi *)repl_chars[1];
  301. const v16qi repl_bs = *(const v16qi *)repl_chars[2];
  302. const v16qi repl_qm = *(const v16qi *)repl_chars[3];
  303. unsigned int misalign, found, mask;
  304. const v16qi *p;
  305. v16qi data, t;
  306. /* Align the source pointer. */
  307. misalign = (uintptr_t)s & 15;
  308. p = (const v16qi *)((uintptr_t)s & -16);
  309. data = *p;
  310. /* Create a mask for the bytes that are valid within the first
  311. 16-byte block. The Idea here is that the AND with the mask
  312. within the loop is "free", since we need some AND or TEST
  313. insn in order to set the flags for the branch anyway. */
  314. mask = -1u << misalign;
  315. /* Main loop processing 16 bytes at a time. */
  316. goto start;
  317. do
  318. {
  319. data = *++p;
  320. mask = -1;
  321. start:
  322. t = __builtin_ia32_pcmpeqb128(data, repl_nl);
  323. t |= __builtin_ia32_pcmpeqb128(data, repl_cr);
  324. t |= __builtin_ia32_pcmpeqb128(data, repl_bs);
  325. t |= __builtin_ia32_pcmpeqb128(data, repl_qm);
  326. found = __builtin_ia32_pmovmskb128 (t);
  327. found &= mask;
  328. }
  329. while (!found);
  330. /* FOUND contains 1 in bits for which we matched a relevant
  331. character. Conversion to the byte index is trivial. */
  332. found = __builtin_ctz(found);
  333. return (const uchar *)p + found;
  334. }
  335. #ifdef HAVE_SSE4
  336. /* A version of the fast scanner using SSE 4.2 vectorized string insns. */
  337. static const uchar *
  338. #ifndef __SSE4_2__
  339. __attribute__((__target__("sse4.2")))
  340. #endif
  341. search_line_sse42 (const uchar *s, const uchar *end)
  342. {
  343. typedef char v16qi __attribute__ ((__vector_size__ (16)));
  344. static const v16qi search = { '\n', '\r', '?', '\\' };
  345. uintptr_t si = (uintptr_t)s;
  346. uintptr_t index;
  347. /* Check for unaligned input. */
  348. if (si & 15)
  349. {
  350. v16qi sv;
  351. if (__builtin_expect (end - s < 16, 0)
  352. && __builtin_expect ((si & 0xfff) > 0xff0, 0))
  353. {
  354. /* There are less than 16 bytes left in the buffer, and less
  355. than 16 bytes left on the page. Reading 16 bytes at this
  356. point might generate a spurious page fault. Defer to the
  357. SSE2 implementation, which already handles alignment. */
  358. return search_line_sse2 (s, end);
  359. }
  360. /* ??? The builtin doesn't understand that the PCMPESTRI read from
  361. memory need not be aligned. */
  362. sv = __builtin_ia32_loaddqu ((const char *) s);
  363. index = __builtin_ia32_pcmpestri128 (search, 4, sv, 16, 0);
  364. if (__builtin_expect (index < 16, 0))
  365. goto found;
  366. /* Advance the pointer to an aligned address. We will re-scan a
  367. few bytes, but we no longer need care for reading past the
  368. end of a page, since we're guaranteed a match. */
  369. s = (const uchar *)((si + 16) & -16);
  370. }
  371. /* Main loop, processing 16 bytes at a time. By doing the whole loop
  372. in inline assembly, we can make proper use of the flags set. */
  373. __asm ( "sub $16, %1\n"
  374. " .balign 16\n"
  375. "0: add $16, %1\n"
  376. " %vpcmpestri $0, (%1), %2\n"
  377. " jnc 0b"
  378. : "=&c"(index), "+r"(s)
  379. : "x"(search), "a"(4), "d"(16));
  380. found:
  381. return s + index;
  382. }
  383. #else
  384. /* Work around out-dated assemblers without sse4 support. */
  385. #define search_line_sse42 search_line_sse2
  386. #endif
  387. /* Check the CPU capabilities. */
  388. #include "../gcc/config/i386/cpuid.h"
  389. typedef const uchar * (*search_line_fast_type) (const uchar *, const uchar *);
  390. static search_line_fast_type search_line_fast;
  391. #define HAVE_init_vectorized_lexer 1
  392. static inline void
  393. init_vectorized_lexer (void)
  394. {
  395. unsigned dummy, ecx = 0, edx = 0;
  396. search_line_fast_type impl = search_line_acc_char;
  397. int minimum = 0;
  398. #if defined(__SSE4_2__)
  399. minimum = 3;
  400. #elif defined(__SSE2__)
  401. minimum = 2;
  402. #elif defined(__SSE__)
  403. minimum = 1;
  404. #endif
  405. if (minimum == 3)
  406. impl = search_line_sse42;
  407. else if (__get_cpuid (1, &dummy, &dummy, &ecx, &edx) || minimum == 2)
  408. {
  409. if (minimum == 3 || (ecx & bit_SSE4_2))
  410. impl = search_line_sse42;
  411. else if (minimum == 2 || (edx & bit_SSE2))
  412. impl = search_line_sse2;
  413. else if (minimum == 1 || (edx & bit_SSE))
  414. impl = search_line_mmx;
  415. }
  416. else if (__get_cpuid (0x80000001, &dummy, &dummy, &dummy, &edx))
  417. {
  418. if (minimum == 1
  419. || (edx & (bit_MMXEXT | bit_CMOV)) == (bit_MMXEXT | bit_CMOV))
  420. impl = search_line_mmx;
  421. }
  422. search_line_fast = impl;
  423. }
  424. #elif defined(_ARCH_PWR8) && defined(__ALTIVEC__)
  425. /* A vection of the fast scanner using AltiVec vectorized byte compares
  426. and VSX unaligned loads (when VSX is available). This is otherwise
  427. the same as the pre-GCC 5 version. */
  428. ATTRIBUTE_NO_SANITIZE_UNDEFINED
  429. static const uchar *
  430. search_line_fast (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
  431. {
  432. typedef __attribute__((altivec(vector))) unsigned char vc;
  433. const vc repl_nl = {
  434. '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n',
  435. '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'
  436. };
  437. const vc repl_cr = {
  438. '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r',
  439. '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r'
  440. };
  441. const vc repl_bs = {
  442. '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\',
  443. '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\'
  444. };
  445. const vc repl_qm = {
  446. '?', '?', '?', '?', '?', '?', '?', '?',
  447. '?', '?', '?', '?', '?', '?', '?', '?',
  448. };
  449. const vc zero = { 0 };
  450. vc data, t;
  451. /* Main loop processing 16 bytes at a time. */
  452. do
  453. {
  454. vc m_nl, m_cr, m_bs, m_qm;
  455. data = *((const vc *)s);
  456. s += 16;
  457. m_nl = (vc) __builtin_vec_cmpeq(data, repl_nl);
  458. m_cr = (vc) __builtin_vec_cmpeq(data, repl_cr);
  459. m_bs = (vc) __builtin_vec_cmpeq(data, repl_bs);
  460. m_qm = (vc) __builtin_vec_cmpeq(data, repl_qm);
  461. t = (m_nl | m_cr) | (m_bs | m_qm);
  462. /* T now contains 0xff in bytes for which we matched one of the relevant
  463. characters. We want to exit the loop if any byte in T is non-zero.
  464. Below is the expansion of vec_any_ne(t, zero). */
  465. }
  466. while (!__builtin_vec_vcmpeq_p(/*__CR6_LT_REV*/3, t, zero));
  467. /* Restore s to to point to the 16 bytes we just processed. */
  468. s -= 16;
  469. {
  470. #define N (sizeof(vc) / sizeof(long))
  471. union {
  472. vc v;
  473. /* Statically assert that N is 2 or 4. */
  474. unsigned long l[(N == 2 || N == 4) ? N : -1];
  475. } u;
  476. unsigned long l, i = 0;
  477. u.v = t;
  478. /* Find the first word of T that is non-zero. */
  479. switch (N)
  480. {
  481. case 4:
  482. l = u.l[i++];
  483. if (l != 0)
  484. break;
  485. s += sizeof(unsigned long);
  486. l = u.l[i++];
  487. if (l != 0)
  488. break;
  489. s += sizeof(unsigned long);
  490. case 2:
  491. l = u.l[i++];
  492. if (l != 0)
  493. break;
  494. s += sizeof(unsigned long);
  495. l = u.l[i];
  496. }
  497. /* L now contains 0xff in bytes for which we matched one of the
  498. relevant characters. We can find the byte index by finding
  499. its bit index and dividing by 8. */
  500. #ifdef __BIG_ENDIAN__
  501. l = __builtin_clzl(l) >> 3;
  502. #else
  503. l = __builtin_ctzl(l) >> 3;
  504. #endif
  505. return s + l;
  506. #undef N
  507. }
  508. }
  509. #elif (GCC_VERSION >= 4005) && defined(__ALTIVEC__) && defined (__BIG_ENDIAN__)
  510. /* A vection of the fast scanner using AltiVec vectorized byte compares.
  511. This cannot be used for little endian because vec_lvsl/lvsr are
  512. deprecated for little endian and the code won't work properly. */
  513. /* ??? Unfortunately, attribute(target("altivec")) is not yet supported,
  514. so we can't compile this function without -maltivec on the command line
  515. (or implied by some other switch). */
  516. static const uchar *
  517. search_line_fast (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
  518. {
  519. typedef __attribute__((altivec(vector))) unsigned char vc;
  520. const vc repl_nl = {
  521. '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n',
  522. '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'
  523. };
  524. const vc repl_cr = {
  525. '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r',
  526. '\r', '\r', '\r', '\r', '\r', '\r', '\r', '\r'
  527. };
  528. const vc repl_bs = {
  529. '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\',
  530. '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\'
  531. };
  532. const vc repl_qm = {
  533. '?', '?', '?', '?', '?', '?', '?', '?',
  534. '?', '?', '?', '?', '?', '?', '?', '?',
  535. };
  536. const vc ones = {
  537. -1, -1, -1, -1, -1, -1, -1, -1,
  538. -1, -1, -1, -1, -1, -1, -1, -1,
  539. };
  540. const vc zero = { 0 };
  541. vc data, mask, t;
  542. /* Altivec loads automatically mask addresses with -16. This lets us
  543. issue the first load as early as possible. */
  544. data = __builtin_vec_ld(0, (const vc *)s);
  545. /* Discard bytes before the beginning of the buffer. Do this by
  546. beginning with all ones and shifting in zeros according to the
  547. mis-alignment. The LVSR instruction pulls the exact shift we
  548. want from the address. */
  549. mask = __builtin_vec_lvsr(0, s);
  550. mask = __builtin_vec_perm(zero, ones, mask);
  551. data &= mask;
  552. /* While altivec loads mask addresses, we still need to align S so
  553. that the offset we compute at the end is correct. */
  554. s = (const uchar *)((uintptr_t)s & -16);
  555. /* Main loop processing 16 bytes at a time. */
  556. goto start;
  557. do
  558. {
  559. vc m_nl, m_cr, m_bs, m_qm;
  560. s += 16;
  561. data = __builtin_vec_ld(0, (const vc *)s);
  562. start:
  563. m_nl = (vc) __builtin_vec_cmpeq(data, repl_nl);
  564. m_cr = (vc) __builtin_vec_cmpeq(data, repl_cr);
  565. m_bs = (vc) __builtin_vec_cmpeq(data, repl_bs);
  566. m_qm = (vc) __builtin_vec_cmpeq(data, repl_qm);
  567. t = (m_nl | m_cr) | (m_bs | m_qm);
  568. /* T now contains 0xff in bytes for which we matched one of the relevant
  569. characters. We want to exit the loop if any byte in T is non-zero.
  570. Below is the expansion of vec_any_ne(t, zero). */
  571. }
  572. while (!__builtin_vec_vcmpeq_p(/*__CR6_LT_REV*/3, t, zero));
  573. {
  574. #define N (sizeof(vc) / sizeof(long))
  575. union {
  576. vc v;
  577. /* Statically assert that N is 2 or 4. */
  578. unsigned long l[(N == 2 || N == 4) ? N : -1];
  579. } u;
  580. unsigned long l, i = 0;
  581. u.v = t;
  582. /* Find the first word of T that is non-zero. */
  583. switch (N)
  584. {
  585. case 4:
  586. l = u.l[i++];
  587. if (l != 0)
  588. break;
  589. s += sizeof(unsigned long);
  590. l = u.l[i++];
  591. if (l != 0)
  592. break;
  593. s += sizeof(unsigned long);
  594. case 2:
  595. l = u.l[i++];
  596. if (l != 0)
  597. break;
  598. s += sizeof(unsigned long);
  599. l = u.l[i];
  600. }
  601. /* L now contains 0xff in bytes for which we matched one of the
  602. relevant characters. We can find the byte index by finding
  603. its bit index and dividing by 8. */
  604. l = __builtin_clzl(l) >> 3;
  605. return s + l;
  606. #undef N
  607. }
  608. }
  609. #elif defined (__ARM_NEON)
  610. #include "arm_neon.h"
  611. static const uchar *
  612. search_line_fast (const uchar *s, const uchar *end ATTRIBUTE_UNUSED)
  613. {
  614. const uint8x16_t repl_nl = vdupq_n_u8 ('\n');
  615. const uint8x16_t repl_cr = vdupq_n_u8 ('\r');
  616. const uint8x16_t repl_bs = vdupq_n_u8 ('\\');
  617. const uint8x16_t repl_qm = vdupq_n_u8 ('?');
  618. const uint8x16_t xmask = (uint8x16_t) vdupq_n_u64 (0x8040201008040201ULL);
  619. unsigned int misalign, found, mask;
  620. const uint8_t *p;
  621. uint8x16_t data;
  622. /* Align the source pointer. */
  623. misalign = (uintptr_t)s & 15;
  624. p = (const uint8_t *)((uintptr_t)s & -16);
  625. data = vld1q_u8 (p);
  626. /* Create a mask for the bytes that are valid within the first
  627. 16-byte block. The Idea here is that the AND with the mask
  628. within the loop is "free", since we need some AND or TEST
  629. insn in order to set the flags for the branch anyway. */
  630. mask = (-1u << misalign) & 0xffff;
  631. /* Main loop, processing 16 bytes at a time. */
  632. goto start;
  633. do
  634. {
  635. uint8x8_t l;
  636. uint16x4_t m;
  637. uint32x2_t n;
  638. uint8x16_t t, u, v, w;
  639. p += 16;
  640. data = vld1q_u8 (p);
  641. mask = 0xffff;
  642. start:
  643. t = vceqq_u8 (data, repl_nl);
  644. u = vceqq_u8 (data, repl_cr);
  645. v = vorrq_u8 (t, vceqq_u8 (data, repl_bs));
  646. w = vorrq_u8 (u, vceqq_u8 (data, repl_qm));
  647. t = vandq_u8 (vorrq_u8 (v, w), xmask);
  648. l = vpadd_u8 (vget_low_u8 (t), vget_high_u8 (t));
  649. m = vpaddl_u8 (l);
  650. n = vpaddl_u16 (m);
  651. found = vget_lane_u32 ((uint32x2_t) vorr_u64 ((uint64x1_t) n,
  652. vshr_n_u64 ((uint64x1_t) n, 24)), 0);
  653. found &= mask;
  654. }
  655. while (!found);
  656. /* FOUND contains 1 in bits for which we matched a relevant
  657. character. Conversion to the byte index is trivial. */
  658. found = __builtin_ctz (found);
  659. return (const uchar *)p + found;
  660. }
  661. #else
  662. /* We only have one accellerated alternative. Use a direct call so that
  663. we encourage inlining. */
  664. #define search_line_fast search_line_acc_char
  665. #endif
  666. /* Initialize the lexer if needed. */
  667. void
  668. _cpp_init_lexer (void)
  669. {
  670. #ifdef HAVE_init_vectorized_lexer
  671. init_vectorized_lexer ();
  672. #endif
  673. }
  674. /* Returns with a logical line that contains no escaped newlines or
  675. trigraphs. This is a time-critical inner loop. */
  676. void
  677. _cpp_clean_line (cpp_reader *pfile)
  678. {
  679. cpp_buffer *buffer;
  680. const uchar *s;
  681. uchar c, *d, *p;
  682. buffer = pfile->buffer;
  683. buffer->cur_note = buffer->notes_used = 0;
  684. buffer->cur = buffer->line_base = buffer->next_line;
  685. buffer->need_line = false;
  686. s = buffer->next_line;
  687. if (!buffer->from_stage3)
  688. {
  689. const uchar *pbackslash = NULL;
  690. /* Fast path. This is the common case of an un-escaped line with
  691. no trigraphs. The primary win here is by not writing any
  692. data back to memory until we have to. */
  693. while (1)
  694. {
  695. /* Perform an optimized search for \n, \r, \\, ?. */
  696. s = search_line_fast (s, buffer->rlimit);
  697. c = *s;
  698. if (c == '\\')
  699. {
  700. /* Record the location of the backslash and continue. */
  701. pbackslash = s++;
  702. }
  703. else if (__builtin_expect (c == '?', 0))
  704. {
  705. if (__builtin_expect (s[1] == '?', false)
  706. && _cpp_trigraph_map[s[2]])
  707. {
  708. /* Have a trigraph. We may or may not have to convert
  709. it. Add a line note regardless, for -Wtrigraphs. */
  710. add_line_note (buffer, s, s[2]);
  711. if (CPP_OPTION (pfile, trigraphs))
  712. {
  713. /* We do, and that means we have to switch to the
  714. slow path. */
  715. d = (uchar *) s;
  716. *d = _cpp_trigraph_map[s[2]];
  717. s += 2;
  718. goto slow_path;
  719. }
  720. }
  721. /* Not a trigraph. Continue on fast-path. */
  722. s++;
  723. }
  724. else
  725. break;
  726. }
  727. /* This must be \r or \n. We're either done, or we'll be forced
  728. to write back to the buffer and continue on the slow path. */
  729. d = (uchar *) s;
  730. if (__builtin_expect (s == buffer->rlimit, false))
  731. goto done;
  732. /* DOS line ending? */
  733. if (__builtin_expect (c == '\r', false) && s[1] == '\n')
  734. {
  735. s++;
  736. if (s == buffer->rlimit)
  737. goto done;
  738. }
  739. if (__builtin_expect (pbackslash == NULL, true))
  740. goto done;
  741. /* Check for escaped newline. */
  742. p = d;
  743. while (is_nvspace (p[-1]))
  744. p--;
  745. if (p - 1 != pbackslash)
  746. goto done;
  747. /* Have an escaped newline; process it and proceed to
  748. the slow path. */
  749. add_line_note (buffer, p - 1, p != d ? ' ' : '\\');
  750. d = p - 2;
  751. buffer->next_line = p - 1;
  752. slow_path:
  753. while (1)
  754. {
  755. c = *++s;
  756. *++d = c;
  757. if (c == '\n' || c == '\r')
  758. {
  759. /* Handle DOS line endings. */
  760. if (c == '\r' && s != buffer->rlimit && s[1] == '\n')
  761. s++;
  762. if (s == buffer->rlimit)
  763. break;
  764. /* Escaped? */
  765. p = d;
  766. while (p != buffer->next_line && is_nvspace (p[-1]))
  767. p--;
  768. if (p == buffer->next_line || p[-1] != '\\')
  769. break;
  770. add_line_note (buffer, p - 1, p != d ? ' ': '\\');
  771. d = p - 2;
  772. buffer->next_line = p - 1;
  773. }
  774. else if (c == '?' && s[1] == '?' && _cpp_trigraph_map[s[2]])
  775. {
  776. /* Add a note regardless, for the benefit of -Wtrigraphs. */
  777. add_line_note (buffer, d, s[2]);
  778. if (CPP_OPTION (pfile, trigraphs))
  779. {
  780. *d = _cpp_trigraph_map[s[2]];
  781. s += 2;
  782. }
  783. }
  784. }
  785. }
  786. else
  787. {
  788. while (*s != '\n' && *s != '\r')
  789. s++;
  790. d = (uchar *) s;
  791. /* Handle DOS line endings. */
  792. if (*s == '\r' && s != buffer->rlimit && s[1] == '\n')
  793. s++;
  794. }
  795. done:
  796. *d = '\n';
  797. /* A sentinel note that should never be processed. */
  798. add_line_note (buffer, d + 1, '\n');
  799. buffer->next_line = s + 1;
  800. }
  801. /* Return true if the trigraph indicated by NOTE should be warned
  802. about in a comment. */
  803. static bool
  804. warn_in_comment (cpp_reader *pfile, _cpp_line_note *note)
  805. {
  806. const uchar *p;
  807. /* Within comments we don't warn about trigraphs, unless the
  808. trigraph forms an escaped newline, as that may change
  809. behavior. */
  810. if (note->type != '/')
  811. return false;
  812. /* If -trigraphs, then this was an escaped newline iff the next note
  813. is coincident. */
  814. if (CPP_OPTION (pfile, trigraphs))
  815. return note[1].pos == note->pos;
  816. /* Otherwise, see if this forms an escaped newline. */
  817. p = note->pos + 3;
  818. while (is_nvspace (*p))
  819. p++;
  820. /* There might have been escaped newlines between the trigraph and the
  821. newline we found. Hence the position test. */
  822. return (*p == '\n' && p < note[1].pos);
  823. }
  824. /* Process the notes created by add_line_note as far as the current
  825. location. */
  826. void
  827. _cpp_process_line_notes (cpp_reader *pfile, int in_comment)
  828. {
  829. cpp_buffer *buffer = pfile->buffer;
  830. for (;;)
  831. {
  832. _cpp_line_note *note = &buffer->notes[buffer->cur_note];
  833. unsigned int col;
  834. if (note->pos > buffer->cur)
  835. break;
  836. buffer->cur_note++;
  837. col = CPP_BUF_COLUMN (buffer, note->pos + 1);
  838. if (note->type == '\\' || note->type == ' ')
  839. {
  840. if (note->type == ' ' && !in_comment)
  841. cpp_error_with_line (pfile, CPP_DL_WARNING, pfile->line_table->highest_line, col,
  842. "backslash and newline separated by space");
  843. if (buffer->next_line > buffer->rlimit)
  844. {
  845. cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line, col,
  846. "backslash-newline at end of file");
  847. /* Prevent "no newline at end of file" warning. */
  848. buffer->next_line = buffer->rlimit;
  849. }
  850. buffer->line_base = note->pos;
  851. CPP_INCREMENT_LINE (pfile, 0);
  852. }
  853. else if (_cpp_trigraph_map[note->type])
  854. {
  855. if (CPP_OPTION (pfile, warn_trigraphs)
  856. && (!in_comment || warn_in_comment (pfile, note)))
  857. {
  858. if (CPP_OPTION (pfile, trigraphs))
  859. cpp_warning_with_line (pfile, CPP_W_TRIGRAPHS,
  860. pfile->line_table->highest_line, col,
  861. "trigraph ??%c converted to %c",
  862. note->type,
  863. (int) _cpp_trigraph_map[note->type]);
  864. else
  865. {
  866. cpp_warning_with_line
  867. (pfile, CPP_W_TRIGRAPHS,
  868. pfile->line_table->highest_line, col,
  869. "trigraph ??%c ignored, use -trigraphs to enable",
  870. note->type);
  871. }
  872. }
  873. }
  874. else if (note->type == 0)
  875. /* Already processed in lex_raw_string. */;
  876. else
  877. abort ();
  878. }
  879. }
  880. /* Skip a C-style block comment. We find the end of the comment by
  881. seeing if an asterisk is before every '/' we encounter. Returns
  882. nonzero if comment terminated by EOF, zero otherwise.
  883. Buffer->cur points to the initial asterisk of the comment. */
  884. bool
  885. _cpp_skip_block_comment (cpp_reader *pfile)
  886. {
  887. cpp_buffer *buffer = pfile->buffer;
  888. const uchar *cur = buffer->cur;
  889. uchar c;
  890. cur++;
  891. if (*cur == '/')
  892. cur++;
  893. for (;;)
  894. {
  895. /* People like decorating comments with '*', so check for '/'
  896. instead for efficiency. */
  897. c = *cur++;
  898. if (c == '/')
  899. {
  900. if (cur[-2] == '*')
  901. break;
  902. /* Warn about potential nested comments, but not if the '/'
  903. comes immediately before the true comment delimiter.
  904. Don't bother to get it right across escaped newlines. */
  905. if (CPP_OPTION (pfile, warn_comments)
  906. && cur[0] == '*' && cur[1] != '/')
  907. {
  908. buffer->cur = cur;
  909. cpp_warning_with_line (pfile, CPP_W_COMMENTS,
  910. pfile->line_table->highest_line,
  911. CPP_BUF_COL (buffer),
  912. "\"/*\" within comment");
  913. }
  914. }
  915. else if (c == '\n')
  916. {
  917. unsigned int cols;
  918. buffer->cur = cur - 1;
  919. _cpp_process_line_notes (pfile, true);
  920. if (buffer->next_line >= buffer->rlimit)
  921. return true;
  922. _cpp_clean_line (pfile);
  923. cols = buffer->next_line - buffer->line_base;
  924. CPP_INCREMENT_LINE (pfile, cols);
  925. cur = buffer->cur;
  926. }
  927. }
  928. buffer->cur = cur;
  929. _cpp_process_line_notes (pfile, true);
  930. return false;
  931. }
  932. /* Skip a C++ line comment, leaving buffer->cur pointing to the
  933. terminating newline. Handles escaped newlines. Returns nonzero
  934. if a multiline comment. */
  935. static int
  936. skip_line_comment (cpp_reader *pfile)
  937. {
  938. cpp_buffer *buffer = pfile->buffer;
  939. source_location orig_line = pfile->line_table->highest_line;
  940. while (*buffer->cur != '\n')
  941. buffer->cur++;
  942. _cpp_process_line_notes (pfile, true);
  943. return orig_line != pfile->line_table->highest_line;
  944. }
  945. /* Skips whitespace, saving the next non-whitespace character. */
  946. static void
  947. skip_whitespace (cpp_reader *pfile, cppchar_t c)
  948. {
  949. cpp_buffer *buffer = pfile->buffer;
  950. bool saw_NUL = false;
  951. do
  952. {
  953. /* Horizontal space always OK. */
  954. if (c == ' ' || c == '\t')
  955. ;
  956. /* Just \f \v or \0 left. */
  957. else if (c == '\0')
  958. saw_NUL = true;
  959. else if (pfile->state.in_directive && CPP_PEDANTIC (pfile))
  960. cpp_error_with_line (pfile, CPP_DL_PEDWARN, pfile->line_table->highest_line,
  961. CPP_BUF_COL (buffer),
  962. "%s in preprocessing directive",
  963. c == '\f' ? "form feed" : "vertical tab");
  964. c = *buffer->cur++;
  965. }
  966. /* We only want non-vertical space, i.e. ' ' \t \f \v \0. */
  967. while (is_nvspace (c));
  968. if (saw_NUL)
  969. cpp_error (pfile, CPP_DL_WARNING, "null character(s) ignored");
  970. buffer->cur--;
  971. }
  972. /* See if the characters of a number token are valid in a name (no
  973. '.', '+' or '-'). */
  974. static int
  975. name_p (cpp_reader *pfile, const cpp_string *string)
  976. {
  977. unsigned int i;
  978. for (i = 0; i < string->len; i++)
  979. if (!is_idchar (string->text[i]))
  980. return 0;
  981. return 1;
  982. }
  983. /* After parsing an identifier or other sequence, produce a warning about
  984. sequences not in NFC/NFKC. */
  985. static void
  986. warn_about_normalization (cpp_reader *pfile,
  987. const cpp_token *token,
  988. const struct normalize_state *s)
  989. {
  990. if (CPP_OPTION (pfile, warn_normalize) < NORMALIZE_STATE_RESULT (s)
  991. && !pfile->state.skipping)
  992. {
  993. /* Make sure that the token is printed using UCNs, even
  994. if we'd otherwise happily print UTF-8. */
  995. unsigned char *buf = XNEWVEC (unsigned char, cpp_token_len (token));
  996. size_t sz;
  997. sz = cpp_spell_token (pfile, token, buf, false) - buf;
  998. if (NORMALIZE_STATE_RESULT (s) == normalized_C)
  999. cpp_warning_with_line (pfile, CPP_W_NORMALIZE, token->src_loc, 0,
  1000. "`%.*s' is not in NFKC", (int) sz, buf);
  1001. else
  1002. cpp_warning_with_line (pfile, CPP_W_NORMALIZE, token->src_loc, 0,
  1003. "`%.*s' is not in NFC", (int) sz, buf);
  1004. free (buf);
  1005. }
  1006. }
  1007. /* Returns TRUE if the sequence starting at buffer->cur is invalid in
  1008. an identifier. FIRST is TRUE if this starts an identifier. */
  1009. static bool
  1010. forms_identifier_p (cpp_reader *pfile, int first,
  1011. struct normalize_state *state)
  1012. {
  1013. cpp_buffer *buffer = pfile->buffer;
  1014. if (*buffer->cur == '$')
  1015. {
  1016. if (!CPP_OPTION (pfile, dollars_in_ident))
  1017. return false;
  1018. buffer->cur++;
  1019. if (CPP_OPTION (pfile, warn_dollars) && !pfile->state.skipping)
  1020. {
  1021. CPP_OPTION (pfile, warn_dollars) = 0;
  1022. cpp_error (pfile, CPP_DL_PEDWARN, "'$' in identifier or number");
  1023. }
  1024. return true;
  1025. }
  1026. /* Is this a syntactically valid UCN? */
  1027. if (CPP_OPTION (pfile, extended_identifiers)
  1028. && *buffer->cur == '\\'
  1029. && (buffer->cur[1] == 'u' || buffer->cur[1] == 'U'))
  1030. {
  1031. buffer->cur += 2;
  1032. if (_cpp_valid_ucn (pfile, &buffer->cur, buffer->rlimit, 1 + !first,
  1033. state))
  1034. return true;
  1035. buffer->cur -= 2;
  1036. }
  1037. return false;
  1038. }
  1039. /* Helper function to get the cpp_hashnode of the identifier BASE. */
  1040. static cpp_hashnode *
  1041. lex_identifier_intern (cpp_reader *pfile, const uchar *base)
  1042. {
  1043. cpp_hashnode *result;
  1044. const uchar *cur;
  1045. unsigned int len;
  1046. unsigned int hash = HT_HASHSTEP (0, *base);
  1047. cur = base + 1;
  1048. while (ISIDNUM (*cur))
  1049. {
  1050. hash = HT_HASHSTEP (hash, *cur);
  1051. cur++;
  1052. }
  1053. len = cur - base;
  1054. hash = HT_HASHFINISH (hash, len);
  1055. result = CPP_HASHNODE (ht_lookup_with_hash (pfile->hash_table,
  1056. base, len, hash, HT_ALLOC));
  1057. /* Rarely, identifiers require diagnostics when lexed. */
  1058. if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
  1059. && !pfile->state.skipping, 0))
  1060. {
  1061. /* It is allowed to poison the same identifier twice. */
  1062. if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
  1063. cpp_error (pfile, CPP_DL_ERROR, "attempt to use poisoned \"%s\"",
  1064. NODE_NAME (result));
  1065. /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
  1066. replacement list of a variadic macro. */
  1067. if (result == pfile->spec_nodes.n__VA_ARGS__
  1068. && !pfile->state.va_args_ok)
  1069. {
  1070. if (CPP_OPTION (pfile, cplusplus))
  1071. cpp_error (pfile, CPP_DL_PEDWARN,
  1072. "__VA_ARGS__ can only appear in the expansion"
  1073. " of a C++11 variadic macro");
  1074. else
  1075. cpp_error (pfile, CPP_DL_PEDWARN,
  1076. "__VA_ARGS__ can only appear in the expansion"
  1077. " of a C99 variadic macro");
  1078. }
  1079. /* For -Wc++-compat, warn about use of C++ named operators. */
  1080. if (result->flags & NODE_WARN_OPERATOR)
  1081. cpp_warning (pfile, CPP_W_CXX_OPERATOR_NAMES,
  1082. "identifier \"%s\" is a special operator name in C++",
  1083. NODE_NAME (result));
  1084. }
  1085. return result;
  1086. }
  1087. /* Get the cpp_hashnode of an identifier specified by NAME in
  1088. the current cpp_reader object. If none is found, NULL is returned. */
  1089. cpp_hashnode *
  1090. _cpp_lex_identifier (cpp_reader *pfile, const char *name)
  1091. {
  1092. cpp_hashnode *result;
  1093. result = lex_identifier_intern (pfile, (uchar *) name);
  1094. return result;
  1095. }
  1096. /* Lex an identifier starting at BUFFER->CUR - 1. */
  1097. static cpp_hashnode *
  1098. lex_identifier (cpp_reader *pfile, const uchar *base, bool starts_ucn,
  1099. struct normalize_state *nst, cpp_hashnode **spelling)
  1100. {
  1101. cpp_hashnode *result;
  1102. const uchar *cur;
  1103. unsigned int len;
  1104. unsigned int hash = HT_HASHSTEP (0, *base);
  1105. cur = pfile->buffer->cur;
  1106. if (! starts_ucn)
  1107. {
  1108. while (ISIDNUM (*cur))
  1109. {
  1110. hash = HT_HASHSTEP (hash, *cur);
  1111. cur++;
  1112. }
  1113. NORMALIZE_STATE_UPDATE_IDNUM (nst, *(cur - 1));
  1114. }
  1115. pfile->buffer->cur = cur;
  1116. if (starts_ucn || forms_identifier_p (pfile, false, nst))
  1117. {
  1118. /* Slower version for identifiers containing UCNs (or $). */
  1119. do {
  1120. while (ISIDNUM (*pfile->buffer->cur))
  1121. {
  1122. NORMALIZE_STATE_UPDATE_IDNUM (nst, *pfile->buffer->cur);
  1123. pfile->buffer->cur++;
  1124. }
  1125. } while (forms_identifier_p (pfile, false, nst));
  1126. result = _cpp_interpret_identifier (pfile, base,
  1127. pfile->buffer->cur - base);
  1128. *spelling = cpp_lookup (pfile, base, pfile->buffer->cur - base);
  1129. }
  1130. else
  1131. {
  1132. len = cur - base;
  1133. hash = HT_HASHFINISH (hash, len);
  1134. result = CPP_HASHNODE (ht_lookup_with_hash (pfile->hash_table,
  1135. base, len, hash, HT_ALLOC));
  1136. *spelling = result;
  1137. }
  1138. /* Rarely, identifiers require diagnostics when lexed. */
  1139. if (__builtin_expect ((result->flags & NODE_DIAGNOSTIC)
  1140. && !pfile->state.skipping, 0))
  1141. {
  1142. /* It is allowed to poison the same identifier twice. */
  1143. if ((result->flags & NODE_POISONED) && !pfile->state.poisoned_ok)
  1144. cpp_error (pfile, CPP_DL_ERROR, "attempt to use poisoned \"%s\"",
  1145. NODE_NAME (result));
  1146. /* Constraint 6.10.3.5: __VA_ARGS__ should only appear in the
  1147. replacement list of a variadic macro. */
  1148. if (result == pfile->spec_nodes.n__VA_ARGS__
  1149. && !pfile->state.va_args_ok)
  1150. {
  1151. if (CPP_OPTION (pfile, cplusplus))
  1152. cpp_error (pfile, CPP_DL_PEDWARN,
  1153. "__VA_ARGS__ can only appear in the expansion"
  1154. " of a C++11 variadic macro");
  1155. else
  1156. cpp_error (pfile, CPP_DL_PEDWARN,
  1157. "__VA_ARGS__ can only appear in the expansion"
  1158. " of a C99 variadic macro");
  1159. }
  1160. /* For -Wc++-compat, warn about use of C++ named operators. */
  1161. if (result->flags & NODE_WARN_OPERATOR)
  1162. cpp_warning (pfile, CPP_W_CXX_OPERATOR_NAMES,
  1163. "identifier \"%s\" is a special operator name in C++",
  1164. NODE_NAME (result));
  1165. }
  1166. return result;
  1167. }
  1168. /* Lex a number to NUMBER starting at BUFFER->CUR - 1. */
  1169. static void
  1170. lex_number (cpp_reader *pfile, cpp_string *number,
  1171. struct normalize_state *nst)
  1172. {
  1173. const uchar *cur;
  1174. const uchar *base;
  1175. uchar *dest;
  1176. base = pfile->buffer->cur - 1;
  1177. do
  1178. {
  1179. cur = pfile->buffer->cur;
  1180. /* N.B. ISIDNUM does not include $. */
  1181. while (ISIDNUM (*cur) || *cur == '.' || DIGIT_SEP (*cur)
  1182. || VALID_SIGN (*cur, cur[-1]))
  1183. {
  1184. NORMALIZE_STATE_UPDATE_IDNUM (nst, *cur);
  1185. cur++;
  1186. }
  1187. /* A number can't end with a digit separator. */
  1188. while (cur > pfile->buffer->cur && DIGIT_SEP (cur[-1]))
  1189. --cur;
  1190. pfile->buffer->cur = cur;
  1191. }
  1192. while (forms_identifier_p (pfile, false, nst));
  1193. number->len = cur - base;
  1194. dest = _cpp_unaligned_alloc (pfile, number->len + 1);
  1195. memcpy (dest, base, number->len);
  1196. dest[number->len] = '\0';
  1197. number->text = dest;
  1198. }
  1199. /* Create a token of type TYPE with a literal spelling. */
  1200. static void
  1201. create_literal (cpp_reader *pfile, cpp_token *token, const uchar *base,
  1202. unsigned int len, enum cpp_ttype type)
  1203. {
  1204. uchar *dest = _cpp_unaligned_alloc (pfile, len + 1);
  1205. memcpy (dest, base, len);
  1206. dest[len] = '\0';
  1207. token->type = type;
  1208. token->val.str.len = len;
  1209. token->val.str.text = dest;
  1210. }
  1211. /* Subroutine of lex_raw_string: Append LEN chars from BASE to the buffer
  1212. sequence from *FIRST_BUFF_P to LAST_BUFF_P. */
  1213. static void
  1214. bufring_append (cpp_reader *pfile, const uchar *base, size_t len,
  1215. _cpp_buff **first_buff_p, _cpp_buff **last_buff_p)
  1216. {
  1217. _cpp_buff *first_buff = *first_buff_p;
  1218. _cpp_buff *last_buff = *last_buff_p;
  1219. if (first_buff == NULL)
  1220. first_buff = last_buff = _cpp_get_buff (pfile, len);
  1221. else if (len > BUFF_ROOM (last_buff))
  1222. {
  1223. size_t room = BUFF_ROOM (last_buff);
  1224. memcpy (BUFF_FRONT (last_buff), base, room);
  1225. BUFF_FRONT (last_buff) += room;
  1226. base += room;
  1227. len -= room;
  1228. last_buff = _cpp_append_extend_buff (pfile, last_buff, len);
  1229. }
  1230. memcpy (BUFF_FRONT (last_buff), base, len);
  1231. BUFF_FRONT (last_buff) += len;
  1232. *first_buff_p = first_buff;
  1233. *last_buff_p = last_buff;
  1234. }
  1235. /* Returns true if a macro has been defined.
  1236. This might not work if compile with -save-temps,
  1237. or preprocess separately from compilation. */
  1238. static bool
  1239. is_macro(cpp_reader *pfile, const uchar *base)
  1240. {
  1241. const uchar *cur = base;
  1242. if (! ISIDST (*cur))
  1243. return false;
  1244. unsigned int hash = HT_HASHSTEP (0, *cur);
  1245. ++cur;
  1246. while (ISIDNUM (*cur))
  1247. {
  1248. hash = HT_HASHSTEP (hash, *cur);
  1249. ++cur;
  1250. }
  1251. hash = HT_HASHFINISH (hash, cur - base);
  1252. cpp_hashnode *result = CPP_HASHNODE (ht_lookup_with_hash (pfile->hash_table,
  1253. base, cur - base, hash, HT_NO_INSERT));
  1254. return !result ? false : (result->type == NT_MACRO);
  1255. }
  1256. /* Lexes a raw string. The stored string contains the spelling, including
  1257. double quotes, delimiter string, '(' and ')', any leading
  1258. 'L', 'u', 'U' or 'u8' and 'R' modifier. It returns the type of the
  1259. literal, or CPP_OTHER if it was not properly terminated.
  1260. The spelling is NUL-terminated, but it is not guaranteed that this
  1261. is the first NUL since embedded NULs are preserved. */
  1262. static void
  1263. lex_raw_string (cpp_reader *pfile, cpp_token *token, const uchar *base,
  1264. const uchar *cur)
  1265. {
  1266. uchar raw_prefix[17];
  1267. uchar temp_buffer[18];
  1268. const uchar *orig_base;
  1269. unsigned int raw_prefix_len = 0, raw_suffix_len = 0;
  1270. enum raw_str_phase { RAW_STR_PREFIX, RAW_STR, RAW_STR_SUFFIX };
  1271. raw_str_phase phase = RAW_STR_PREFIX;
  1272. enum cpp_ttype type;
  1273. size_t total_len = 0;
  1274. /* Index into temp_buffer during phases other than RAW_STR,
  1275. during RAW_STR phase 17 to tell BUF_APPEND that nothing should
  1276. be appended to temp_buffer. */
  1277. size_t temp_buffer_len = 0;
  1278. _cpp_buff *first_buff = NULL, *last_buff = NULL;
  1279. size_t raw_prefix_start;
  1280. _cpp_line_note *note = &pfile->buffer->notes[pfile->buffer->cur_note];
  1281. type = (*base == 'L' ? CPP_WSTRING :
  1282. *base == 'U' ? CPP_STRING32 :
  1283. *base == 'u' ? (base[1] == '8' ? CPP_UTF8STRING : CPP_STRING16)
  1284. : CPP_STRING);
  1285. #define BUF_APPEND(STR,LEN) \
  1286. do { \
  1287. bufring_append (pfile, (const uchar *)(STR), (LEN), \
  1288. &first_buff, &last_buff); \
  1289. total_len += (LEN); \
  1290. if (__builtin_expect (temp_buffer_len < 17, 0) \
  1291. && (const uchar *)(STR) != base \
  1292. && (LEN) <= 2) \
  1293. { \
  1294. memcpy (temp_buffer + temp_buffer_len, \
  1295. (const uchar *)(STR), (LEN)); \
  1296. temp_buffer_len += (LEN); \
  1297. } \
  1298. } while (0);
  1299. orig_base = base;
  1300. ++cur;
  1301. raw_prefix_start = cur - base;
  1302. for (;;)
  1303. {
  1304. cppchar_t c;
  1305. /* If we previously performed any trigraph or line splicing
  1306. transformations, undo them in between the opening and closing
  1307. double quote. */
  1308. while (note->pos < cur)
  1309. ++note;
  1310. for (; note->pos == cur; ++note)
  1311. {
  1312. switch (note->type)
  1313. {
  1314. case '\\':
  1315. case ' ':
  1316. /* Restore backslash followed by newline. */
  1317. BUF_APPEND (base, cur - base);
  1318. base = cur;
  1319. BUF_APPEND ("\\", 1);
  1320. after_backslash:
  1321. if (note->type == ' ')
  1322. {
  1323. /* GNU backslash whitespace newline extension. FIXME
  1324. could be any sequence of non-vertical space. When we
  1325. can properly restore any such sequence, we should mark
  1326. this note as handled so _cpp_process_line_notes
  1327. doesn't warn. */
  1328. BUF_APPEND (" ", 1);
  1329. }
  1330. BUF_APPEND ("\n", 1);
  1331. break;
  1332. case 0:
  1333. /* Already handled. */
  1334. break;
  1335. default:
  1336. if (_cpp_trigraph_map[note->type])
  1337. {
  1338. /* Don't warn about this trigraph in
  1339. _cpp_process_line_notes, since trigraphs show up as
  1340. trigraphs in raw strings. */
  1341. uchar type = note->type;
  1342. note->type = 0;
  1343. if (!CPP_OPTION (pfile, trigraphs))
  1344. /* If we didn't convert the trigraph in the first
  1345. place, don't do anything now either. */
  1346. break;
  1347. BUF_APPEND (base, cur - base);
  1348. base = cur;
  1349. BUF_APPEND ("??", 2);
  1350. /* ??/ followed by newline gets two line notes, one for
  1351. the trigraph and one for the backslash/newline. */
  1352. if (type == '/' && note[1].pos == cur)
  1353. {
  1354. if (note[1].type != '\\'
  1355. && note[1].type != ' ')
  1356. abort ();
  1357. BUF_APPEND ("/", 1);
  1358. ++note;
  1359. goto after_backslash;
  1360. }
  1361. else
  1362. {
  1363. /* Skip the replacement character. */
  1364. base = ++cur;
  1365. BUF_APPEND (&type, 1);
  1366. c = type;
  1367. goto check_c;
  1368. }
  1369. }
  1370. else
  1371. abort ();
  1372. break;
  1373. }
  1374. }
  1375. c = *cur++;
  1376. if (__builtin_expect (temp_buffer_len < 17, 0))
  1377. temp_buffer[temp_buffer_len++] = c;
  1378. check_c:
  1379. if (phase == RAW_STR_PREFIX)
  1380. {
  1381. while (raw_prefix_len < temp_buffer_len)
  1382. {
  1383. raw_prefix[raw_prefix_len] = temp_buffer[raw_prefix_len];
  1384. switch (raw_prefix[raw_prefix_len])
  1385. {
  1386. case ' ': case '(': case ')': case '\\': case '\t':
  1387. case '\v': case '\f': case '\n': default:
  1388. break;
  1389. /* Basic source charset except the above chars. */
  1390. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  1391. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  1392. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  1393. case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  1394. case 'y': case 'z':
  1395. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  1396. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  1397. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  1398. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  1399. case 'Y': case 'Z':
  1400. case '0': case '1': case '2': case '3': case '4': case '5':
  1401. case '6': case '7': case '8': case '9':
  1402. case '_': case '{': case '}': case '#': case '[': case ']':
  1403. case '<': case '>': case '%': case ':': case ';': case '.':
  1404. case '?': case '*': case '+': case '-': case '/': case '^':
  1405. case '&': case '|': case '~': case '!': case '=': case ',':
  1406. case '"': case '\'':
  1407. if (raw_prefix_len < 16)
  1408. {
  1409. raw_prefix_len++;
  1410. continue;
  1411. }
  1412. break;
  1413. }
  1414. if (raw_prefix[raw_prefix_len] != '(')
  1415. {
  1416. int col = CPP_BUF_COLUMN (pfile->buffer, cur) + 1;
  1417. if (raw_prefix_len == 16)
  1418. cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc,
  1419. col, "raw string delimiter longer "
  1420. "than 16 characters");
  1421. else if (raw_prefix[raw_prefix_len] == '\n')
  1422. cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc,
  1423. col, "invalid new-line in raw "
  1424. "string delimiter");
  1425. else
  1426. cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc,
  1427. col, "invalid character '%c' in "
  1428. "raw string delimiter",
  1429. (int) raw_prefix[raw_prefix_len]);
  1430. pfile->buffer->cur = orig_base + raw_prefix_start - 1;
  1431. create_literal (pfile, token, orig_base,
  1432. raw_prefix_start - 1, CPP_OTHER);
  1433. if (first_buff)
  1434. _cpp_release_buff (pfile, first_buff);
  1435. return;
  1436. }
  1437. raw_prefix[raw_prefix_len] = '"';
  1438. phase = RAW_STR;
  1439. /* Nothing should be appended to temp_buffer during
  1440. RAW_STR phase. */
  1441. temp_buffer_len = 17;
  1442. break;
  1443. }
  1444. continue;
  1445. }
  1446. else if (phase == RAW_STR_SUFFIX)
  1447. {
  1448. while (raw_suffix_len <= raw_prefix_len
  1449. && raw_suffix_len < temp_buffer_len
  1450. && temp_buffer[raw_suffix_len] == raw_prefix[raw_suffix_len])
  1451. raw_suffix_len++;
  1452. if (raw_suffix_len > raw_prefix_len)
  1453. break;
  1454. if (raw_suffix_len == temp_buffer_len)
  1455. continue;
  1456. phase = RAW_STR;
  1457. /* Nothing should be appended to temp_buffer during
  1458. RAW_STR phase. */
  1459. temp_buffer_len = 17;
  1460. }
  1461. if (c == ')')
  1462. {
  1463. phase = RAW_STR_SUFFIX;
  1464. raw_suffix_len = 0;
  1465. temp_buffer_len = 0;
  1466. }
  1467. else if (c == '\n')
  1468. {
  1469. if (pfile->state.in_directive
  1470. || (pfile->state.parsing_args
  1471. && pfile->buffer->next_line >= pfile->buffer->rlimit))
  1472. {
  1473. cur--;
  1474. type = CPP_OTHER;
  1475. cpp_error_with_line (pfile, CPP_DL_ERROR, token->src_loc, 0,
  1476. "unterminated raw string");
  1477. break;
  1478. }
  1479. BUF_APPEND (base, cur - base);
  1480. if (pfile->buffer->cur < pfile->buffer->rlimit)
  1481. CPP_INCREMENT_LINE (pfile, 0);
  1482. pfile->buffer->need_line = true;
  1483. pfile->buffer->cur = cur-1;
  1484. _cpp_process_line_notes (pfile, false);
  1485. if (!_cpp_get_fresh_line (pfile))
  1486. {
  1487. source_location src_loc = token->src_loc;
  1488. token->type = CPP_EOF;
  1489. /* Tell the compiler the line number of the EOF token. */
  1490. token->src_loc = pfile->line_table->highest_line;
  1491. token->flags = BOL;
  1492. if (first_buff != NULL)
  1493. _cpp_release_buff (pfile, first_buff);
  1494. cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
  1495. "unterminated raw string");
  1496. return;
  1497. }
  1498. cur = base = pfile->buffer->cur;
  1499. note = &pfile->buffer->notes[pfile->buffer->cur_note];
  1500. }
  1501. }
  1502. if (CPP_OPTION (pfile, user_literals))
  1503. {
  1504. /* If a string format macro, say from inttypes.h, is placed touching
  1505. a string literal it could be parsed as a C++11 user-defined string
  1506. literal thus breaking the program.
  1507. Try to identify macros with is_macro. A warning is issued. */
  1508. if (is_macro (pfile, cur))
  1509. {
  1510. /* Raise a warning, but do not consume subsequent tokens. */
  1511. if (CPP_OPTION (pfile, warn_literal_suffix) && !pfile->state.skipping)
  1512. cpp_warning_with_line (pfile, CPP_W_LITERAL_SUFFIX,
  1513. token->src_loc, 0,
  1514. "invalid suffix on literal; C++11 requires "
  1515. "a space between literal and string macro");
  1516. }
  1517. /* Grab user defined literal suffix. */
  1518. else if (ISIDST (*cur))
  1519. {
  1520. type = cpp_userdef_string_add_type (type);
  1521. ++cur;
  1522. while (ISIDNUM (*cur))
  1523. ++cur;
  1524. }
  1525. }
  1526. pfile->buffer->cur = cur;
  1527. if (first_buff == NULL)
  1528. create_literal (pfile, token, base, cur - base, type);
  1529. else
  1530. {
  1531. uchar *dest = _cpp_unaligned_alloc (pfile, total_len + (cur - base) + 1);
  1532. token->type = type;
  1533. token->val.str.len = total_len + (cur - base);
  1534. token->val.str.text = dest;
  1535. last_buff = first_buff;
  1536. while (last_buff != NULL)
  1537. {
  1538. memcpy (dest, last_buff->base,
  1539. BUFF_FRONT (last_buff) - last_buff->base);
  1540. dest += BUFF_FRONT (last_buff) - last_buff->base;
  1541. last_buff = last_buff->next;
  1542. }
  1543. _cpp_release_buff (pfile, first_buff);
  1544. memcpy (dest, base, cur - base);
  1545. dest[cur - base] = '\0';
  1546. }
  1547. }
  1548. /* Lexes a string, character constant, or angle-bracketed header file
  1549. name. The stored string contains the spelling, including opening
  1550. quote and any leading 'L', 'u', 'U' or 'u8' and optional
  1551. 'R' modifier. It returns the type of the literal, or CPP_OTHER
  1552. if it was not properly terminated, or CPP_LESS for an unterminated
  1553. header name which must be relexed as normal tokens.
  1554. The spelling is NUL-terminated, but it is not guaranteed that this
  1555. is the first NUL since embedded NULs are preserved. */
  1556. static void
  1557. lex_string (cpp_reader *pfile, cpp_token *token, const uchar *base)
  1558. {
  1559. bool saw_NUL = false;
  1560. const uchar *cur;
  1561. cppchar_t terminator;
  1562. enum cpp_ttype type;
  1563. cur = base;
  1564. terminator = *cur++;
  1565. if (terminator == 'L' || terminator == 'U')
  1566. terminator = *cur++;
  1567. else if (terminator == 'u')
  1568. {
  1569. terminator = *cur++;
  1570. if (terminator == '8')
  1571. terminator = *cur++;
  1572. }
  1573. if (terminator == 'R')
  1574. {
  1575. lex_raw_string (pfile, token, base, cur);
  1576. return;
  1577. }
  1578. if (terminator == '"')
  1579. type = (*base == 'L' ? CPP_WSTRING :
  1580. *base == 'U' ? CPP_STRING32 :
  1581. *base == 'u' ? (base[1] == '8' ? CPP_UTF8STRING : CPP_STRING16)
  1582. : CPP_STRING);
  1583. else if (terminator == '\'')
  1584. type = (*base == 'L' ? CPP_WCHAR :
  1585. *base == 'U' ? CPP_CHAR32 :
  1586. *base == 'u' ? CPP_CHAR16 : CPP_CHAR);
  1587. else
  1588. terminator = '>', type = CPP_HEADER_NAME;
  1589. for (;;)
  1590. {
  1591. cppchar_t c = *cur++;
  1592. /* In #include-style directives, terminators are not escapable. */
  1593. if (c == '\\' && !pfile->state.angled_headers && *cur != '\n')
  1594. cur++;
  1595. else if (c == terminator)
  1596. break;
  1597. else if (c == '\n')
  1598. {
  1599. cur--;
  1600. /* Unmatched quotes always yield undefined behavior, but
  1601. greedy lexing means that what appears to be an unterminated
  1602. header name may actually be a legitimate sequence of tokens. */
  1603. if (terminator == '>')
  1604. {
  1605. token->type = CPP_LESS;
  1606. return;
  1607. }
  1608. type = CPP_OTHER;
  1609. break;
  1610. }
  1611. else if (c == '\0')
  1612. saw_NUL = true;
  1613. }
  1614. if (saw_NUL && !pfile->state.skipping)
  1615. cpp_error (pfile, CPP_DL_WARNING,
  1616. "null character(s) preserved in literal");
  1617. if (type == CPP_OTHER && CPP_OPTION (pfile, lang) != CLK_ASM)
  1618. cpp_error (pfile, CPP_DL_PEDWARN, "missing terminating %c character",
  1619. (int) terminator);
  1620. if (CPP_OPTION (pfile, user_literals))
  1621. {
  1622. /* If a string format macro, say from inttypes.h, is placed touching
  1623. a string literal it could be parsed as a C++11 user-defined string
  1624. literal thus breaking the program.
  1625. Try to identify macros with is_macro. A warning is issued. */
  1626. if (is_macro (pfile, cur))
  1627. {
  1628. /* Raise a warning, but do not consume subsequent tokens. */
  1629. if (CPP_OPTION (pfile, warn_literal_suffix) && !pfile->state.skipping)
  1630. cpp_warning_with_line (pfile, CPP_W_LITERAL_SUFFIX,
  1631. token->src_loc, 0,
  1632. "invalid suffix on literal; C++11 requires "
  1633. "a space between literal and string macro");
  1634. }
  1635. /* Grab user defined literal suffix. */
  1636. else if (ISIDST (*cur))
  1637. {
  1638. type = cpp_userdef_char_add_type (type);
  1639. type = cpp_userdef_string_add_type (type);
  1640. ++cur;
  1641. while (ISIDNUM (*cur))
  1642. ++cur;
  1643. }
  1644. }
  1645. pfile->buffer->cur = cur;
  1646. create_literal (pfile, token, base, cur - base, type);
  1647. }
  1648. /* Return the comment table. The client may not make any assumption
  1649. about the ordering of the table. */
  1650. cpp_comment_table *
  1651. cpp_get_comments (cpp_reader *pfile)
  1652. {
  1653. return &pfile->comments;
  1654. }
  1655. /* Append a comment to the end of the comment table. */
  1656. static void
  1657. store_comment (cpp_reader *pfile, cpp_token *token)
  1658. {
  1659. int len;
  1660. if (pfile->comments.allocated == 0)
  1661. {
  1662. pfile->comments.allocated = 256;
  1663. pfile->comments.entries = (cpp_comment *) xmalloc
  1664. (pfile->comments.allocated * sizeof (cpp_comment));
  1665. }
  1666. if (pfile->comments.count == pfile->comments.allocated)
  1667. {
  1668. pfile->comments.allocated *= 2;
  1669. pfile->comments.entries = (cpp_comment *) xrealloc
  1670. (pfile->comments.entries,
  1671. pfile->comments.allocated * sizeof (cpp_comment));
  1672. }
  1673. len = token->val.str.len;
  1674. /* Copy comment. Note, token may not be NULL terminated. */
  1675. pfile->comments.entries[pfile->comments.count].comment =
  1676. (char *) xmalloc (sizeof (char) * (len + 1));
  1677. memcpy (pfile->comments.entries[pfile->comments.count].comment,
  1678. token->val.str.text, len);
  1679. pfile->comments.entries[pfile->comments.count].comment[len] = '\0';
  1680. /* Set source location. */
  1681. pfile->comments.entries[pfile->comments.count].sloc = token->src_loc;
  1682. /* Increment the count of entries in the comment table. */
  1683. pfile->comments.count++;
  1684. }
  1685. /* The stored comment includes the comment start and any terminator. */
  1686. static void
  1687. save_comment (cpp_reader *pfile, cpp_token *token, const unsigned char *from,
  1688. cppchar_t type)
  1689. {
  1690. unsigned char *buffer;
  1691. unsigned int len, clen, i;
  1692. len = pfile->buffer->cur - from + 1; /* + 1 for the initial '/'. */
  1693. /* C++ comments probably (not definitely) have moved past a new
  1694. line, which we don't want to save in the comment. */
  1695. if (is_vspace (pfile->buffer->cur[-1]))
  1696. len--;
  1697. /* If we are currently in a directive or in argument parsing, then
  1698. we need to store all C++ comments as C comments internally, and
  1699. so we need to allocate a little extra space in that case.
  1700. Note that the only time we encounter a directive here is
  1701. when we are saving comments in a "#define". */
  1702. clen = ((pfile->state.in_directive || pfile->state.parsing_args)
  1703. && type == '/') ? len + 2 : len;
  1704. buffer = _cpp_unaligned_alloc (pfile, clen);
  1705. token->type = CPP_COMMENT;
  1706. token->val.str.len = clen;
  1707. token->val.str.text = buffer;
  1708. buffer[0] = '/';
  1709. memcpy (buffer + 1, from, len - 1);
  1710. /* Finish conversion to a C comment, if necessary. */
  1711. if ((pfile->state.in_directive || pfile->state.parsing_args) && type == '/')
  1712. {
  1713. buffer[1] = '*';
  1714. buffer[clen - 2] = '*';
  1715. buffer[clen - 1] = '/';
  1716. /* As there can be in a C++ comments illegal sequences for C comments
  1717. we need to filter them out. */
  1718. for (i = 2; i < (clen - 2); i++)
  1719. if (buffer[i] == '/' && (buffer[i - 1] == '*' || buffer[i + 1] == '*'))
  1720. buffer[i] = '|';
  1721. }
  1722. /* Finally store this comment for use by clients of libcpp. */
  1723. store_comment (pfile, token);
  1724. }
  1725. /* Allocate COUNT tokens for RUN. */
  1726. void
  1727. _cpp_init_tokenrun (tokenrun *run, unsigned int count)
  1728. {
  1729. run->base = XNEWVEC (cpp_token, count);
  1730. run->limit = run->base + count;
  1731. run->next = NULL;
  1732. }
  1733. /* Returns the next tokenrun, or creates one if there is none. */
  1734. static tokenrun *
  1735. next_tokenrun (tokenrun *run)
  1736. {
  1737. if (run->next == NULL)
  1738. {
  1739. run->next = XNEW (tokenrun);
  1740. run->next->prev = run;
  1741. _cpp_init_tokenrun (run->next, 250);
  1742. }
  1743. return run->next;
  1744. }
  1745. /* Return the number of not yet processed token in a given
  1746. context. */
  1747. int
  1748. _cpp_remaining_tokens_num_in_context (cpp_context *context)
  1749. {
  1750. if (context->tokens_kind == TOKENS_KIND_DIRECT)
  1751. return (LAST (context).token - FIRST (context).token);
  1752. else if (context->tokens_kind == TOKENS_KIND_INDIRECT
  1753. || context->tokens_kind == TOKENS_KIND_EXTENDED)
  1754. return (LAST (context).ptoken - FIRST (context).ptoken);
  1755. else
  1756. abort ();
  1757. }
  1758. /* Returns the token present at index INDEX in a given context. If
  1759. INDEX is zero, the next token to be processed is returned. */
  1760. static const cpp_token*
  1761. _cpp_token_from_context_at (cpp_context *context, int index)
  1762. {
  1763. if (context->tokens_kind == TOKENS_KIND_DIRECT)
  1764. return &(FIRST (context).token[index]);
  1765. else if (context->tokens_kind == TOKENS_KIND_INDIRECT
  1766. || context->tokens_kind == TOKENS_KIND_EXTENDED)
  1767. return FIRST (context).ptoken[index];
  1768. else
  1769. abort ();
  1770. }
  1771. /* Look ahead in the input stream. */
  1772. const cpp_token *
  1773. cpp_peek_token (cpp_reader *pfile, int index)
  1774. {
  1775. cpp_context *context = pfile->context;
  1776. const cpp_token *peektok;
  1777. int count;
  1778. /* First, scan through any pending cpp_context objects. */
  1779. while (context->prev)
  1780. {
  1781. ptrdiff_t sz = _cpp_remaining_tokens_num_in_context (context);
  1782. if (index < (int) sz)
  1783. return _cpp_token_from_context_at (context, index);
  1784. index -= (int) sz;
  1785. context = context->prev;
  1786. }
  1787. /* We will have to read some new tokens after all (and do so
  1788. without invalidating preceding tokens). */
  1789. count = index;
  1790. pfile->keep_tokens++;
  1791. /* For peeked tokens temporarily disable line_change reporting,
  1792. until the tokens are parsed for real. */
  1793. void (*line_change) (cpp_reader *, const cpp_token *, int)
  1794. = pfile->cb.line_change;
  1795. pfile->cb.line_change = NULL;
  1796. do
  1797. {
  1798. peektok = _cpp_lex_token (pfile);
  1799. if (peektok->type == CPP_EOF)
  1800. {
  1801. index--;
  1802. break;
  1803. }
  1804. }
  1805. while (index--);
  1806. _cpp_backup_tokens_direct (pfile, count - index);
  1807. pfile->keep_tokens--;
  1808. pfile->cb.line_change = line_change;
  1809. return peektok;
  1810. }
  1811. /* Allocate a single token that is invalidated at the same time as the
  1812. rest of the tokens on the line. Has its line and col set to the
  1813. same as the last lexed token, so that diagnostics appear in the
  1814. right place. */
  1815. cpp_token *
  1816. _cpp_temp_token (cpp_reader *pfile)
  1817. {
  1818. cpp_token *old, *result;
  1819. ptrdiff_t sz = pfile->cur_run->limit - pfile->cur_token;
  1820. ptrdiff_t la = (ptrdiff_t) pfile->lookaheads;
  1821. old = pfile->cur_token - 1;
  1822. /* Any pre-existing lookaheads must not be clobbered. */
  1823. if (la)
  1824. {
  1825. if (sz <= la)
  1826. {
  1827. tokenrun *next = next_tokenrun (pfile->cur_run);
  1828. if (sz < la)
  1829. memmove (next->base + 1, next->base,
  1830. (la - sz) * sizeof (cpp_token));
  1831. next->base[0] = pfile->cur_run->limit[-1];
  1832. }
  1833. if (sz > 1)
  1834. memmove (pfile->cur_token + 1, pfile->cur_token,
  1835. MIN (la, sz - 1) * sizeof (cpp_token));
  1836. }
  1837. if (!sz && pfile->cur_token == pfile->cur_run->limit)
  1838. {
  1839. pfile->cur_run = next_tokenrun (pfile->cur_run);
  1840. pfile->cur_token = pfile->cur_run->base;
  1841. }
  1842. result = pfile->cur_token++;
  1843. result->src_loc = old->src_loc;
  1844. return result;
  1845. }
  1846. /* Lex a token into RESULT (external interface). Takes care of issues
  1847. like directive handling, token lookahead, multiple include
  1848. optimization and skipping. */
  1849. const cpp_token *
  1850. _cpp_lex_token (cpp_reader *pfile)
  1851. {
  1852. cpp_token *result;
  1853. for (;;)
  1854. {
  1855. if (pfile->cur_token == pfile->cur_run->limit)
  1856. {
  1857. pfile->cur_run = next_tokenrun (pfile->cur_run);
  1858. pfile->cur_token = pfile->cur_run->base;
  1859. }
  1860. /* We assume that the current token is somewhere in the current
  1861. run. */
  1862. if (pfile->cur_token < pfile->cur_run->base
  1863. || pfile->cur_token >= pfile->cur_run->limit)
  1864. abort ();
  1865. if (pfile->lookaheads)
  1866. {
  1867. pfile->lookaheads--;
  1868. result = pfile->cur_token++;
  1869. }
  1870. else
  1871. result = _cpp_lex_direct (pfile);
  1872. if (result->flags & BOL)
  1873. {
  1874. /* Is this a directive. If _cpp_handle_directive returns
  1875. false, it is an assembler #. */
  1876. if (result->type == CPP_HASH
  1877. /* 6.10.3 p 11: Directives in a list of macro arguments
  1878. gives undefined behavior. This implementation
  1879. handles the directive as normal. */
  1880. && pfile->state.parsing_args != 1)
  1881. {
  1882. if (_cpp_handle_directive (pfile, result->flags & PREV_WHITE))
  1883. {
  1884. if (pfile->directive_result.type == CPP_PADDING)
  1885. continue;
  1886. result = &pfile->directive_result;
  1887. }
  1888. }
  1889. else if (pfile->state.in_deferred_pragma)
  1890. result = &pfile->directive_result;
  1891. if (pfile->cb.line_change && !pfile->state.skipping)
  1892. pfile->cb.line_change (pfile, result, pfile->state.parsing_args);
  1893. }
  1894. /* We don't skip tokens in directives. */
  1895. if (pfile->state.in_directive || pfile->state.in_deferred_pragma)
  1896. break;
  1897. /* Outside a directive, invalidate controlling macros. At file
  1898. EOF, _cpp_lex_direct takes care of popping the buffer, so we never
  1899. get here and MI optimization works. */
  1900. pfile->mi_valid = false;
  1901. if (!pfile->state.skipping || result->type == CPP_EOF)
  1902. break;
  1903. }
  1904. return result;
  1905. }
  1906. /* Returns true if a fresh line has been loaded. */
  1907. bool
  1908. _cpp_get_fresh_line (cpp_reader *pfile)
  1909. {
  1910. int return_at_eof;
  1911. /* We can't get a new line until we leave the current directive. */
  1912. if (pfile->state.in_directive)
  1913. return false;
  1914. for (;;)
  1915. {
  1916. cpp_buffer *buffer = pfile->buffer;
  1917. if (!buffer->need_line)
  1918. return true;
  1919. if (buffer->next_line < buffer->rlimit)
  1920. {
  1921. _cpp_clean_line (pfile);
  1922. return true;
  1923. }
  1924. /* First, get out of parsing arguments state. */
  1925. if (pfile->state.parsing_args)
  1926. return false;
  1927. /* End of buffer. Non-empty files should end in a newline. */
  1928. if (buffer->buf != buffer->rlimit
  1929. && buffer->next_line > buffer->rlimit
  1930. && !buffer->from_stage3)
  1931. {
  1932. /* Clip to buffer size. */
  1933. buffer->next_line = buffer->rlimit;
  1934. }
  1935. return_at_eof = buffer->return_at_eof;
  1936. _cpp_pop_buffer (pfile);
  1937. if (pfile->buffer == NULL || return_at_eof)
  1938. return false;
  1939. }
  1940. }
  1941. #define IF_NEXT_IS(CHAR, THEN_TYPE, ELSE_TYPE) \
  1942. do \
  1943. { \
  1944. result->type = ELSE_TYPE; \
  1945. if (*buffer->cur == CHAR) \
  1946. buffer->cur++, result->type = THEN_TYPE; \
  1947. } \
  1948. while (0)
  1949. /* Lex a token into pfile->cur_token, which is also incremented, to
  1950. get diagnostics pointing to the correct location.
  1951. Does not handle issues such as token lookahead, multiple-include
  1952. optimization, directives, skipping etc. This function is only
  1953. suitable for use by _cpp_lex_token, and in special cases like
  1954. lex_expansion_token which doesn't care for any of these issues.
  1955. When meeting a newline, returns CPP_EOF if parsing a directive,
  1956. otherwise returns to the start of the token buffer if permissible.
  1957. Returns the location of the lexed token. */
  1958. cpp_token *
  1959. _cpp_lex_direct (cpp_reader *pfile)
  1960. {
  1961. cppchar_t c;
  1962. cpp_buffer *buffer;
  1963. const unsigned char *comment_start;
  1964. cpp_token *result = pfile->cur_token++;
  1965. fresh_line:
  1966. result->flags = 0;
  1967. buffer = pfile->buffer;
  1968. if (buffer->need_line)
  1969. {
  1970. if (pfile->state.in_deferred_pragma)
  1971. {
  1972. result->type = CPP_PRAGMA_EOL;
  1973. pfile->state.in_deferred_pragma = false;
  1974. if (!pfile->state.pragma_allow_expansion)
  1975. pfile->state.prevent_expansion--;
  1976. return result;
  1977. }
  1978. if (!_cpp_get_fresh_line (pfile))
  1979. {
  1980. result->type = CPP_EOF;
  1981. if (!pfile->state.in_directive)
  1982. {
  1983. /* Tell the compiler the line number of the EOF token. */
  1984. result->src_loc = pfile->line_table->highest_line;
  1985. result->flags = BOL;
  1986. }
  1987. return result;
  1988. }
  1989. if (!pfile->keep_tokens)
  1990. {
  1991. pfile->cur_run = &pfile->base_run;
  1992. result = pfile->base_run.base;
  1993. pfile->cur_token = result + 1;
  1994. }
  1995. result->flags = BOL;
  1996. if (pfile->state.parsing_args == 2)
  1997. result->flags |= PREV_WHITE;
  1998. }
  1999. buffer = pfile->buffer;
  2000. update_tokens_line:
  2001. result->src_loc = pfile->line_table->highest_line;
  2002. skipped_white:
  2003. if (buffer->cur >= buffer->notes[buffer->cur_note].pos
  2004. && !pfile->overlaid_buffer)
  2005. {
  2006. _cpp_process_line_notes (pfile, false);
  2007. result->src_loc = pfile->line_table->highest_line;
  2008. }
  2009. c = *buffer->cur++;
  2010. if (pfile->forced_token_location_p)
  2011. result->src_loc = *pfile->forced_token_location_p;
  2012. else
  2013. result->src_loc = linemap_position_for_column (pfile->line_table,
  2014. CPP_BUF_COLUMN (buffer, buffer->cur));
  2015. switch (c)
  2016. {
  2017. case ' ': case '\t': case '\f': case '\v': case '\0':
  2018. result->flags |= PREV_WHITE;
  2019. skip_whitespace (pfile, c);
  2020. goto skipped_white;
  2021. case '\n':
  2022. if (buffer->cur < buffer->rlimit)
  2023. CPP_INCREMENT_LINE (pfile, 0);
  2024. buffer->need_line = true;
  2025. goto fresh_line;
  2026. case '0': case '1': case '2': case '3': case '4':
  2027. case '5': case '6': case '7': case '8': case '9':
  2028. {
  2029. struct normalize_state nst = INITIAL_NORMALIZE_STATE;
  2030. result->type = CPP_NUMBER;
  2031. lex_number (pfile, &result->val.str, &nst);
  2032. warn_about_normalization (pfile, result, &nst);
  2033. break;
  2034. }
  2035. case 'L':
  2036. case 'u':
  2037. case 'U':
  2038. case 'R':
  2039. /* 'L', 'u', 'U', 'u8' or 'R' may introduce wide characters,
  2040. wide strings or raw strings. */
  2041. if (c == 'L' || CPP_OPTION (pfile, rliterals)
  2042. || (c != 'R' && CPP_OPTION (pfile, uliterals)))
  2043. {
  2044. if ((*buffer->cur == '\'' && c != 'R')
  2045. || *buffer->cur == '"'
  2046. || (*buffer->cur == 'R'
  2047. && c != 'R'
  2048. && buffer->cur[1] == '"'
  2049. && CPP_OPTION (pfile, rliterals))
  2050. || (*buffer->cur == '8'
  2051. && c == 'u'
  2052. && (buffer->cur[1] == '"'
  2053. || (buffer->cur[1] == 'R' && buffer->cur[2] == '"'
  2054. && CPP_OPTION (pfile, rliterals)))))
  2055. {
  2056. lex_string (pfile, result, buffer->cur - 1);
  2057. break;
  2058. }
  2059. }
  2060. /* Fall through. */
  2061. case '_':
  2062. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  2063. case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  2064. case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  2065. case 's': case 't': case 'v': case 'w': case 'x':
  2066. case 'y': case 'z':
  2067. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  2068. case 'G': case 'H': case 'I': case 'J': case 'K':
  2069. case 'M': case 'N': case 'O': case 'P': case 'Q':
  2070. case 'S': case 'T': case 'V': case 'W': case 'X':
  2071. case 'Y': case 'Z':
  2072. result->type = CPP_NAME;
  2073. {
  2074. struct normalize_state nst = INITIAL_NORMALIZE_STATE;
  2075. result->val.node.node = lex_identifier (pfile, buffer->cur - 1, false,
  2076. &nst,
  2077. &result->val.node.spelling);
  2078. warn_about_normalization (pfile, result, &nst);
  2079. }
  2080. /* Convert named operators to their proper types. */
  2081. if (result->val.node.node->flags & NODE_OPERATOR)
  2082. {
  2083. result->flags |= NAMED_OP;
  2084. result->type = (enum cpp_ttype) result->val.node.node->directive_index;
  2085. }
  2086. break;
  2087. case '\'':
  2088. case '"':
  2089. lex_string (pfile, result, buffer->cur - 1);
  2090. break;
  2091. case '/':
  2092. /* A potential block or line comment. */
  2093. comment_start = buffer->cur;
  2094. c = *buffer->cur;
  2095. if (c == '*')
  2096. {
  2097. if (_cpp_skip_block_comment (pfile))
  2098. cpp_error (pfile, CPP_DL_ERROR, "unterminated comment");
  2099. }
  2100. else if (c == '/' && ! CPP_OPTION (pfile, traditional))
  2101. {
  2102. /* Don't warn for system headers. */
  2103. if (cpp_in_system_header (pfile))
  2104. ;
  2105. /* Warn about comments if pedantically GNUC89, and not
  2106. in system headers. */
  2107. else if (CPP_OPTION (pfile, lang) == CLK_GNUC89
  2108. && CPP_PEDANTIC (pfile)
  2109. && ! buffer->warned_cplusplus_comments)
  2110. {
  2111. cpp_error (pfile, CPP_DL_PEDWARN,
  2112. "C++ style comments are not allowed in ISO C90");
  2113. cpp_error (pfile, CPP_DL_PEDWARN,
  2114. "(this will be reported only once per input file)");
  2115. buffer->warned_cplusplus_comments = 1;
  2116. }
  2117. /* Or if specifically desired via -Wc90-c99-compat. */
  2118. else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
  2119. && ! CPP_OPTION (pfile, cplusplus)
  2120. && ! buffer->warned_cplusplus_comments)
  2121. {
  2122. cpp_error (pfile, CPP_DL_WARNING,
  2123. "C++ style comments are incompatible with C90");
  2124. cpp_error (pfile, CPP_DL_WARNING,
  2125. "(this will be reported only once per input file)");
  2126. buffer->warned_cplusplus_comments = 1;
  2127. }
  2128. /* In C89/C94, C++ style comments are forbidden. */
  2129. else if ((CPP_OPTION (pfile, lang) == CLK_STDC89
  2130. || CPP_OPTION (pfile, lang) == CLK_STDC94))
  2131. {
  2132. /* But don't be confused about valid code such as
  2133. - // immediately followed by *,
  2134. - // in a preprocessing directive,
  2135. - // in an #if 0 block. */
  2136. if (buffer->cur[1] == '*'
  2137. || pfile->state.in_directive
  2138. || pfile->state.skipping)
  2139. {
  2140. result->type = CPP_DIV;
  2141. break;
  2142. }
  2143. else if (! buffer->warned_cplusplus_comments)
  2144. {
  2145. cpp_error (pfile, CPP_DL_ERROR,
  2146. "C++ style comments are not allowed in ISO C90");
  2147. cpp_error (pfile, CPP_DL_ERROR,
  2148. "(this will be reported only once per input "
  2149. "file)");
  2150. buffer->warned_cplusplus_comments = 1;
  2151. }
  2152. }
  2153. if (skip_line_comment (pfile) && CPP_OPTION (pfile, warn_comments))
  2154. cpp_warning (pfile, CPP_W_COMMENTS, "multi-line comment");
  2155. }
  2156. else if (c == '=')
  2157. {
  2158. buffer->cur++;
  2159. result->type = CPP_DIV_EQ;
  2160. break;
  2161. }
  2162. else
  2163. {
  2164. result->type = CPP_DIV;
  2165. break;
  2166. }
  2167. if (!pfile->state.save_comments)
  2168. {
  2169. result->flags |= PREV_WHITE;
  2170. goto update_tokens_line;
  2171. }
  2172. /* Save the comment as a token in its own right. */
  2173. save_comment (pfile, result, comment_start, c);
  2174. break;
  2175. case '<':
  2176. if (pfile->state.angled_headers)
  2177. {
  2178. lex_string (pfile, result, buffer->cur - 1);
  2179. if (result->type != CPP_LESS)
  2180. break;
  2181. }
  2182. result->type = CPP_LESS;
  2183. if (*buffer->cur == '=')
  2184. buffer->cur++, result->type = CPP_LESS_EQ;
  2185. else if (*buffer->cur == '<')
  2186. {
  2187. buffer->cur++;
  2188. IF_NEXT_IS ('=', CPP_LSHIFT_EQ, CPP_LSHIFT);
  2189. }
  2190. else if (CPP_OPTION (pfile, digraphs))
  2191. {
  2192. if (*buffer->cur == ':')
  2193. {
  2194. /* C++11 [2.5/3 lex.pptoken], "Otherwise, if the next
  2195. three characters are <:: and the subsequent character
  2196. is neither : nor >, the < is treated as a preprocessor
  2197. token by itself". */
  2198. if (CPP_OPTION (pfile, cplusplus)
  2199. && CPP_OPTION (pfile, lang) != CLK_CXX98
  2200. && CPP_OPTION (pfile, lang) != CLK_GNUCXX
  2201. && buffer->cur[1] == ':'
  2202. && buffer->cur[2] != ':' && buffer->cur[2] != '>')
  2203. break;
  2204. buffer->cur++;
  2205. result->flags |= DIGRAPH;
  2206. result->type = CPP_OPEN_SQUARE;
  2207. }
  2208. else if (*buffer->cur == '%')
  2209. {
  2210. buffer->cur++;
  2211. result->flags |= DIGRAPH;
  2212. result->type = CPP_OPEN_BRACE;
  2213. }
  2214. }
  2215. break;
  2216. case '>':
  2217. result->type = CPP_GREATER;
  2218. if (*buffer->cur == '=')
  2219. buffer->cur++, result->type = CPP_GREATER_EQ;
  2220. else if (*buffer->cur == '>')
  2221. {
  2222. buffer->cur++;
  2223. IF_NEXT_IS ('=', CPP_RSHIFT_EQ, CPP_RSHIFT);
  2224. }
  2225. break;
  2226. case '%':
  2227. result->type = CPP_MOD;
  2228. if (*buffer->cur == '=')
  2229. buffer->cur++, result->type = CPP_MOD_EQ;
  2230. else if (CPP_OPTION (pfile, digraphs))
  2231. {
  2232. if (*buffer->cur == ':')
  2233. {
  2234. buffer->cur++;
  2235. result->flags |= DIGRAPH;
  2236. result->type = CPP_HASH;
  2237. if (*buffer->cur == '%' && buffer->cur[1] == ':')
  2238. buffer->cur += 2, result->type = CPP_PASTE, result->val.token_no = 0;
  2239. }
  2240. else if (*buffer->cur == '>')
  2241. {
  2242. buffer->cur++;
  2243. result->flags |= DIGRAPH;
  2244. result->type = CPP_CLOSE_BRACE;
  2245. }
  2246. }
  2247. break;
  2248. case '.':
  2249. result->type = CPP_DOT;
  2250. if (ISDIGIT (*buffer->cur))
  2251. {
  2252. struct normalize_state nst = INITIAL_NORMALIZE_STATE;
  2253. result->type = CPP_NUMBER;
  2254. lex_number (pfile, &result->val.str, &nst);
  2255. warn_about_normalization (pfile, result, &nst);
  2256. }
  2257. else if (*buffer->cur == '.' && buffer->cur[1] == '.')
  2258. buffer->cur += 2, result->type = CPP_ELLIPSIS;
  2259. else if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
  2260. buffer->cur++, result->type = CPP_DOT_STAR;
  2261. break;
  2262. case '+':
  2263. result->type = CPP_PLUS;
  2264. if (*buffer->cur == '+')
  2265. buffer->cur++, result->type = CPP_PLUS_PLUS;
  2266. else if (*buffer->cur == '=')
  2267. buffer->cur++, result->type = CPP_PLUS_EQ;
  2268. break;
  2269. case '-':
  2270. result->type = CPP_MINUS;
  2271. if (*buffer->cur == '>')
  2272. {
  2273. buffer->cur++;
  2274. result->type = CPP_DEREF;
  2275. if (*buffer->cur == '*' && CPP_OPTION (pfile, cplusplus))
  2276. buffer->cur++, result->type = CPP_DEREF_STAR;
  2277. }
  2278. else if (*buffer->cur == '-')
  2279. buffer->cur++, result->type = CPP_MINUS_MINUS;
  2280. else if (*buffer->cur == '=')
  2281. buffer->cur++, result->type = CPP_MINUS_EQ;
  2282. break;
  2283. case '&':
  2284. result->type = CPP_AND;
  2285. if (*buffer->cur == '&')
  2286. buffer->cur++, result->type = CPP_AND_AND;
  2287. else if (*buffer->cur == '=')
  2288. buffer->cur++, result->type = CPP_AND_EQ;
  2289. break;
  2290. case '|':
  2291. result->type = CPP_OR;
  2292. if (*buffer->cur == '|')
  2293. buffer->cur++, result->type = CPP_OR_OR;
  2294. else if (*buffer->cur == '=')
  2295. buffer->cur++, result->type = CPP_OR_EQ;
  2296. break;
  2297. case ':':
  2298. result->type = CPP_COLON;
  2299. if (*buffer->cur == ':' && CPP_OPTION (pfile, cplusplus))
  2300. buffer->cur++, result->type = CPP_SCOPE;
  2301. else if (*buffer->cur == '>' && CPP_OPTION (pfile, digraphs))
  2302. {
  2303. buffer->cur++;
  2304. result->flags |= DIGRAPH;
  2305. result->type = CPP_CLOSE_SQUARE;
  2306. }
  2307. break;
  2308. case '*': IF_NEXT_IS ('=', CPP_MULT_EQ, CPP_MULT); break;
  2309. case '=': IF_NEXT_IS ('=', CPP_EQ_EQ, CPP_EQ); break;
  2310. case '!': IF_NEXT_IS ('=', CPP_NOT_EQ, CPP_NOT); break;
  2311. case '^': IF_NEXT_IS ('=', CPP_XOR_EQ, CPP_XOR); break;
  2312. case '#': IF_NEXT_IS ('#', CPP_PASTE, CPP_HASH); result->val.token_no = 0; break;
  2313. case '?': result->type = CPP_QUERY; break;
  2314. case '~': result->type = CPP_COMPL; break;
  2315. case ',': result->type = CPP_COMMA; break;
  2316. case '(': result->type = CPP_OPEN_PAREN; break;
  2317. case ')': result->type = CPP_CLOSE_PAREN; break;
  2318. case '[': result->type = CPP_OPEN_SQUARE; break;
  2319. case ']': result->type = CPP_CLOSE_SQUARE; break;
  2320. case '{': result->type = CPP_OPEN_BRACE; break;
  2321. case '}': result->type = CPP_CLOSE_BRACE; break;
  2322. case ';': result->type = CPP_SEMICOLON; break;
  2323. /* @ is a punctuator in Objective-C. */
  2324. case '@': result->type = CPP_ATSIGN; break;
  2325. case '$':
  2326. case '\\':
  2327. {
  2328. const uchar *base = --buffer->cur;
  2329. struct normalize_state nst = INITIAL_NORMALIZE_STATE;
  2330. if (forms_identifier_p (pfile, true, &nst))
  2331. {
  2332. result->type = CPP_NAME;
  2333. result->val.node.node = lex_identifier (pfile, base, true, &nst,
  2334. &result->val.node.spelling);
  2335. warn_about_normalization (pfile, result, &nst);
  2336. break;
  2337. }
  2338. buffer->cur++;
  2339. }
  2340. default:
  2341. create_literal (pfile, result, buffer->cur - 1, 1, CPP_OTHER);
  2342. break;
  2343. }
  2344. return result;
  2345. }
  2346. /* An upper bound on the number of bytes needed to spell TOKEN.
  2347. Does not include preceding whitespace. */
  2348. unsigned int
  2349. cpp_token_len (const cpp_token *token)
  2350. {
  2351. unsigned int len;
  2352. switch (TOKEN_SPELL (token))
  2353. {
  2354. default: len = 6; break;
  2355. case SPELL_LITERAL: len = token->val.str.len; break;
  2356. case SPELL_IDENT: len = NODE_LEN (token->val.node.node) * 10; break;
  2357. }
  2358. return len;
  2359. }
  2360. /* Parse UTF-8 out of NAMEP and place a \U escape in BUFFER.
  2361. Return the number of bytes read out of NAME. (There are always
  2362. 10 bytes written to BUFFER.) */
  2363. static size_t
  2364. utf8_to_ucn (unsigned char *buffer, const unsigned char *name)
  2365. {
  2366. int j;
  2367. int ucn_len = 0;
  2368. int ucn_len_c;
  2369. unsigned t;
  2370. unsigned long utf32;
  2371. /* Compute the length of the UTF-8 sequence. */
  2372. for (t = *name; t & 0x80; t <<= 1)
  2373. ucn_len++;
  2374. utf32 = *name & (0x7F >> ucn_len);
  2375. for (ucn_len_c = 1; ucn_len_c < ucn_len; ucn_len_c++)
  2376. {
  2377. utf32 = (utf32 << 6) | (*++name & 0x3F);
  2378. /* Ill-formed UTF-8. */
  2379. if ((*name & ~0x3F) != 0x80)
  2380. abort ();
  2381. }
  2382. *buffer++ = '\\';
  2383. *buffer++ = 'U';
  2384. for (j = 7; j >= 0; j--)
  2385. *buffer++ = "0123456789abcdef"[(utf32 >> (4 * j)) & 0xF];
  2386. return ucn_len;
  2387. }
  2388. /* Given a token TYPE corresponding to a digraph, return a pointer to
  2389. the spelling of the digraph. */
  2390. static const unsigned char *
  2391. cpp_digraph2name (enum cpp_ttype type)
  2392. {
  2393. return digraph_spellings[(int) type - (int) CPP_FIRST_DIGRAPH];
  2394. }
  2395. /* Write the spelling of an identifier IDENT, using UCNs, to BUFFER.
  2396. The buffer must already contain the enough space to hold the
  2397. token's spelling. Returns a pointer to the character after the
  2398. last character written. */
  2399. unsigned char *
  2400. _cpp_spell_ident_ucns (unsigned char *buffer, cpp_hashnode *ident)
  2401. {
  2402. size_t i;
  2403. const unsigned char *name = NODE_NAME (ident);
  2404. for (i = 0; i < NODE_LEN (ident); i++)
  2405. if (name[i] & ~0x7F)
  2406. {
  2407. i += utf8_to_ucn (buffer, name + i) - 1;
  2408. buffer += 10;
  2409. }
  2410. else
  2411. *buffer++ = name[i];
  2412. return buffer;
  2413. }
  2414. /* Write the spelling of a token TOKEN to BUFFER. The buffer must
  2415. already contain the enough space to hold the token's spelling.
  2416. Returns a pointer to the character after the last character written.
  2417. FORSTRING is true if this is to be the spelling after translation
  2418. phase 1 (with the original spelling of extended identifiers), false
  2419. if extended identifiers should always be written using UCNs (there is
  2420. no option for always writing them in the internal UTF-8 form).
  2421. FIXME: Would be nice if we didn't need the PFILE argument. */
  2422. unsigned char *
  2423. cpp_spell_token (cpp_reader *pfile, const cpp_token *token,
  2424. unsigned char *buffer, bool forstring)
  2425. {
  2426. switch (TOKEN_SPELL (token))
  2427. {
  2428. case SPELL_OPERATOR:
  2429. {
  2430. const unsigned char *spelling;
  2431. unsigned char c;
  2432. if (token->flags & DIGRAPH)
  2433. spelling = cpp_digraph2name (token->type);
  2434. else if (token->flags & NAMED_OP)
  2435. goto spell_ident;
  2436. else
  2437. spelling = TOKEN_NAME (token);
  2438. while ((c = *spelling++) != '\0')
  2439. *buffer++ = c;
  2440. }
  2441. break;
  2442. spell_ident:
  2443. case SPELL_IDENT:
  2444. if (forstring)
  2445. {
  2446. memcpy (buffer, NODE_NAME (token->val.node.spelling),
  2447. NODE_LEN (token->val.node.spelling));
  2448. buffer += NODE_LEN (token->val.node.spelling);
  2449. }
  2450. else
  2451. buffer = _cpp_spell_ident_ucns (buffer, token->val.node.node);
  2452. break;
  2453. case SPELL_LITERAL:
  2454. memcpy (buffer, token->val.str.text, token->val.str.len);
  2455. buffer += token->val.str.len;
  2456. break;
  2457. case SPELL_NONE:
  2458. cpp_error (pfile, CPP_DL_ICE,
  2459. "unspellable token %s", TOKEN_NAME (token));
  2460. break;
  2461. }
  2462. return buffer;
  2463. }
  2464. /* Returns TOKEN spelt as a null-terminated string. The string is
  2465. freed when the reader is destroyed. Useful for diagnostics. */
  2466. unsigned char *
  2467. cpp_token_as_text (cpp_reader *pfile, const cpp_token *token)
  2468. {
  2469. unsigned int len = cpp_token_len (token) + 1;
  2470. unsigned char *start = _cpp_unaligned_alloc (pfile, len), *end;
  2471. end = cpp_spell_token (pfile, token, start, false);
  2472. end[0] = '\0';
  2473. return start;
  2474. }
  2475. /* Returns a pointer to a string which spells the token defined by
  2476. TYPE and FLAGS. Used by C front ends, which really should move to
  2477. using cpp_token_as_text. */
  2478. const char *
  2479. cpp_type2name (enum cpp_ttype type, unsigned char flags)
  2480. {
  2481. if (flags & DIGRAPH)
  2482. return (const char *) cpp_digraph2name (type);
  2483. else if (flags & NAMED_OP)
  2484. return cpp_named_operator2name (type);
  2485. return (const char *) token_spellings[type].name;
  2486. }
  2487. /* Writes the spelling of token to FP, without any preceding space.
  2488. Separated from cpp_spell_token for efficiency - to avoid stdio
  2489. double-buffering. */
  2490. void
  2491. cpp_output_token (const cpp_token *token, FILE *fp)
  2492. {
  2493. switch (TOKEN_SPELL (token))
  2494. {
  2495. case SPELL_OPERATOR:
  2496. {
  2497. const unsigned char *spelling;
  2498. int c;
  2499. if (token->flags & DIGRAPH)
  2500. spelling = cpp_digraph2name (token->type);
  2501. else if (token->flags & NAMED_OP)
  2502. goto spell_ident;
  2503. else
  2504. spelling = TOKEN_NAME (token);
  2505. c = *spelling;
  2506. do
  2507. putc (c, fp);
  2508. while ((c = *++spelling) != '\0');
  2509. }
  2510. break;
  2511. spell_ident:
  2512. case SPELL_IDENT:
  2513. {
  2514. size_t i;
  2515. const unsigned char * name = NODE_NAME (token->val.node.node);
  2516. for (i = 0; i < NODE_LEN (token->val.node.node); i++)
  2517. if (name[i] & ~0x7F)
  2518. {
  2519. unsigned char buffer[10];
  2520. i += utf8_to_ucn (buffer, name + i) - 1;
  2521. fwrite (buffer, 1, 10, fp);
  2522. }
  2523. else
  2524. fputc (NODE_NAME (token->val.node.node)[i], fp);
  2525. }
  2526. break;
  2527. case SPELL_LITERAL:
  2528. fwrite (token->val.str.text, 1, token->val.str.len, fp);
  2529. break;
  2530. case SPELL_NONE:
  2531. /* An error, most probably. */
  2532. break;
  2533. }
  2534. }
  2535. /* Compare two tokens. */
  2536. int
  2537. _cpp_equiv_tokens (const cpp_token *a, const cpp_token *b)
  2538. {
  2539. if (a->type == b->type && a->flags == b->flags)
  2540. switch (TOKEN_SPELL (a))
  2541. {
  2542. default: /* Keep compiler happy. */
  2543. case SPELL_OPERATOR:
  2544. /* token_no is used to track where multiple consecutive ##
  2545. tokens were originally located. */
  2546. return (a->type != CPP_PASTE || a->val.token_no == b->val.token_no);
  2547. case SPELL_NONE:
  2548. return (a->type != CPP_MACRO_ARG
  2549. || (a->val.macro_arg.arg_no == b->val.macro_arg.arg_no
  2550. && a->val.macro_arg.spelling == b->val.macro_arg.spelling));
  2551. case SPELL_IDENT:
  2552. return (a->val.node.node == b->val.node.node
  2553. && a->val.node.spelling == b->val.node.spelling);
  2554. case SPELL_LITERAL:
  2555. return (a->val.str.len == b->val.str.len
  2556. && !memcmp (a->val.str.text, b->val.str.text,
  2557. a->val.str.len));
  2558. }
  2559. return 0;
  2560. }
  2561. /* Returns nonzero if a space should be inserted to avoid an
  2562. accidental token paste for output. For simplicity, it is
  2563. conservative, and occasionally advises a space where one is not
  2564. needed, e.g. "." and ".2". */
  2565. int
  2566. cpp_avoid_paste (cpp_reader *pfile, const cpp_token *token1,
  2567. const cpp_token *token2)
  2568. {
  2569. enum cpp_ttype a = token1->type, b = token2->type;
  2570. cppchar_t c;
  2571. if (token1->flags & NAMED_OP)
  2572. a = CPP_NAME;
  2573. if (token2->flags & NAMED_OP)
  2574. b = CPP_NAME;
  2575. c = EOF;
  2576. if (token2->flags & DIGRAPH)
  2577. c = digraph_spellings[(int) b - (int) CPP_FIRST_DIGRAPH][0];
  2578. else if (token_spellings[b].category == SPELL_OPERATOR)
  2579. c = token_spellings[b].name[0];
  2580. /* Quickly get everything that can paste with an '='. */
  2581. if ((int) a <= (int) CPP_LAST_EQ && c == '=')
  2582. return 1;
  2583. switch (a)
  2584. {
  2585. case CPP_GREATER: return c == '>';
  2586. case CPP_LESS: return c == '<' || c == '%' || c == ':';
  2587. case CPP_PLUS: return c == '+';
  2588. case CPP_MINUS: return c == '-' || c == '>';
  2589. case CPP_DIV: return c == '/' || c == '*'; /* Comments. */
  2590. case CPP_MOD: return c == ':' || c == '>';
  2591. case CPP_AND: return c == '&';
  2592. case CPP_OR: return c == '|';
  2593. case CPP_COLON: return c == ':' || c == '>';
  2594. case CPP_DEREF: return c == '*';
  2595. case CPP_DOT: return c == '.' || c == '%' || b == CPP_NUMBER;
  2596. case CPP_HASH: return c == '#' || c == '%'; /* Digraph form. */
  2597. case CPP_NAME: return ((b == CPP_NUMBER
  2598. && name_p (pfile, &token2->val.str))
  2599. || b == CPP_NAME
  2600. || b == CPP_CHAR || b == CPP_STRING); /* L */
  2601. case CPP_NUMBER: return (b == CPP_NUMBER || b == CPP_NAME
  2602. || c == '.' || c == '+' || c == '-');
  2603. /* UCNs */
  2604. case CPP_OTHER: return ((token1->val.str.text[0] == '\\'
  2605. && b == CPP_NAME)
  2606. || (CPP_OPTION (pfile, objc)
  2607. && token1->val.str.text[0] == '@'
  2608. && (b == CPP_NAME || b == CPP_STRING)));
  2609. case CPP_STRING:
  2610. case CPP_WSTRING:
  2611. case CPP_UTF8STRING:
  2612. case CPP_STRING16:
  2613. case CPP_STRING32: return (CPP_OPTION (pfile, user_literals)
  2614. && (b == CPP_NAME
  2615. || (TOKEN_SPELL (token2) == SPELL_LITERAL
  2616. && ISIDST (token2->val.str.text[0]))));
  2617. default: break;
  2618. }
  2619. return 0;
  2620. }
  2621. /* Output all the remaining tokens on the current line, and a newline
  2622. character, to FP. Leading whitespace is removed. If there are
  2623. macros, special token padding is not performed. */
  2624. void
  2625. cpp_output_line (cpp_reader *pfile, FILE *fp)
  2626. {
  2627. const cpp_token *token;
  2628. token = cpp_get_token (pfile);
  2629. while (token->type != CPP_EOF)
  2630. {
  2631. cpp_output_token (token, fp);
  2632. token = cpp_get_token (pfile);
  2633. if (token->flags & PREV_WHITE)
  2634. putc (' ', fp);
  2635. }
  2636. putc ('\n', fp);
  2637. }
  2638. /* Return a string representation of all the remaining tokens on the
  2639. current line. The result is allocated using xmalloc and must be
  2640. freed by the caller. */
  2641. unsigned char *
  2642. cpp_output_line_to_string (cpp_reader *pfile, const unsigned char *dir_name)
  2643. {
  2644. const cpp_token *token;
  2645. unsigned int out = dir_name ? ustrlen (dir_name) : 0;
  2646. unsigned int alloced = 120 + out;
  2647. unsigned char *result = (unsigned char *) xmalloc (alloced);
  2648. /* If DIR_NAME is empty, there are no initial contents. */
  2649. if (dir_name)
  2650. {
  2651. sprintf ((char *) result, "#%s ", dir_name);
  2652. out += 2;
  2653. }
  2654. token = cpp_get_token (pfile);
  2655. while (token->type != CPP_EOF)
  2656. {
  2657. unsigned char *last;
  2658. /* Include room for a possible space and the terminating nul. */
  2659. unsigned int len = cpp_token_len (token) + 2;
  2660. if (out + len > alloced)
  2661. {
  2662. alloced *= 2;
  2663. if (out + len > alloced)
  2664. alloced = out + len;
  2665. result = (unsigned char *) xrealloc (result, alloced);
  2666. }
  2667. last = cpp_spell_token (pfile, token, &result[out], 0);
  2668. out = last - result;
  2669. token = cpp_get_token (pfile);
  2670. if (token->flags & PREV_WHITE)
  2671. result[out++] = ' ';
  2672. }
  2673. result[out] = '\0';
  2674. return result;
  2675. }
  2676. /* Memory buffers. Changing these three constants can have a dramatic
  2677. effect on performance. The values here are reasonable defaults,
  2678. but might be tuned. If you adjust them, be sure to test across a
  2679. range of uses of cpplib, including heavy nested function-like macro
  2680. expansion. Also check the change in peak memory usage (NJAMD is a
  2681. good tool for this). */
  2682. #define MIN_BUFF_SIZE 8000
  2683. #define BUFF_SIZE_UPPER_BOUND(MIN_SIZE) (MIN_BUFF_SIZE + (MIN_SIZE) * 3 / 2)
  2684. #define EXTENDED_BUFF_SIZE(BUFF, MIN_EXTRA) \
  2685. (MIN_EXTRA + ((BUFF)->limit - (BUFF)->cur) * 2)
  2686. #if MIN_BUFF_SIZE > BUFF_SIZE_UPPER_BOUND (0)
  2687. #error BUFF_SIZE_UPPER_BOUND must be at least as large as MIN_BUFF_SIZE!
  2688. #endif
  2689. /* Create a new allocation buffer. Place the control block at the end
  2690. of the buffer, so that buffer overflows will cause immediate chaos. */
  2691. static _cpp_buff *
  2692. new_buff (size_t len)
  2693. {
  2694. _cpp_buff *result;
  2695. unsigned char *base;
  2696. if (len < MIN_BUFF_SIZE)
  2697. len = MIN_BUFF_SIZE;
  2698. len = CPP_ALIGN (len);
  2699. #ifdef ENABLE_VALGRIND_CHECKING
  2700. /* Valgrind warns about uses of interior pointers, so put _cpp_buff
  2701. struct first. */
  2702. size_t slen = CPP_ALIGN2 (sizeof (_cpp_buff), 2 * DEFAULT_ALIGNMENT);
  2703. base = XNEWVEC (unsigned char, len + slen);
  2704. result = (_cpp_buff *) base;
  2705. base += slen;
  2706. #else
  2707. base = XNEWVEC (unsigned char, len + sizeof (_cpp_buff));
  2708. result = (_cpp_buff *) (base + len);
  2709. #endif
  2710. result->base = base;
  2711. result->cur = base;
  2712. result->limit = base + len;
  2713. result->next = NULL;
  2714. return result;
  2715. }
  2716. /* Place a chain of unwanted allocation buffers on the free list. */
  2717. void
  2718. _cpp_release_buff (cpp_reader *pfile, _cpp_buff *buff)
  2719. {
  2720. _cpp_buff *end = buff;
  2721. while (end->next)
  2722. end = end->next;
  2723. end->next = pfile->free_buffs;
  2724. pfile->free_buffs = buff;
  2725. }
  2726. /* Return a free buffer of size at least MIN_SIZE. */
  2727. _cpp_buff *
  2728. _cpp_get_buff (cpp_reader *pfile, size_t min_size)
  2729. {
  2730. _cpp_buff *result, **p;
  2731. for (p = &pfile->free_buffs;; p = &(*p)->next)
  2732. {
  2733. size_t size;
  2734. if (*p == NULL)
  2735. return new_buff (min_size);
  2736. result = *p;
  2737. size = result->limit - result->base;
  2738. /* Return a buffer that's big enough, but don't waste one that's
  2739. way too big. */
  2740. if (size >= min_size && size <= BUFF_SIZE_UPPER_BOUND (min_size))
  2741. break;
  2742. }
  2743. *p = result->next;
  2744. result->next = NULL;
  2745. result->cur = result->base;
  2746. return result;
  2747. }
  2748. /* Creates a new buffer with enough space to hold the uncommitted
  2749. remaining bytes of BUFF, and at least MIN_EXTRA more bytes. Copies
  2750. the excess bytes to the new buffer. Chains the new buffer after
  2751. BUFF, and returns the new buffer. */
  2752. _cpp_buff *
  2753. _cpp_append_extend_buff (cpp_reader *pfile, _cpp_buff *buff, size_t min_extra)
  2754. {
  2755. size_t size = EXTENDED_BUFF_SIZE (buff, min_extra);
  2756. _cpp_buff *new_buff = _cpp_get_buff (pfile, size);
  2757. buff->next = new_buff;
  2758. memcpy (new_buff->base, buff->cur, BUFF_ROOM (buff));
  2759. return new_buff;
  2760. }
  2761. /* Creates a new buffer with enough space to hold the uncommitted
  2762. remaining bytes of the buffer pointed to by BUFF, and at least
  2763. MIN_EXTRA more bytes. Copies the excess bytes to the new buffer.
  2764. Chains the new buffer before the buffer pointed to by BUFF, and
  2765. updates the pointer to point to the new buffer. */
  2766. void
  2767. _cpp_extend_buff (cpp_reader *pfile, _cpp_buff **pbuff, size_t min_extra)
  2768. {
  2769. _cpp_buff *new_buff, *old_buff = *pbuff;
  2770. size_t size = EXTENDED_BUFF_SIZE (old_buff, min_extra);
  2771. new_buff = _cpp_get_buff (pfile, size);
  2772. memcpy (new_buff->base, old_buff->cur, BUFF_ROOM (old_buff));
  2773. new_buff->next = old_buff;
  2774. *pbuff = new_buff;
  2775. }
  2776. /* Free a chain of buffers starting at BUFF. */
  2777. void
  2778. _cpp_free_buff (_cpp_buff *buff)
  2779. {
  2780. _cpp_buff *next;
  2781. for (; buff; buff = next)
  2782. {
  2783. next = buff->next;
  2784. #ifdef ENABLE_VALGRIND_CHECKING
  2785. free (buff);
  2786. #else
  2787. free (buff->base);
  2788. #endif
  2789. }
  2790. }
  2791. /* Allocate permanent, unaligned storage of length LEN. */
  2792. unsigned char *
  2793. _cpp_unaligned_alloc (cpp_reader *pfile, size_t len)
  2794. {
  2795. _cpp_buff *buff = pfile->u_buff;
  2796. unsigned char *result = buff->cur;
  2797. if (len > (size_t) (buff->limit - result))
  2798. {
  2799. buff = _cpp_get_buff (pfile, len);
  2800. buff->next = pfile->u_buff;
  2801. pfile->u_buff = buff;
  2802. result = buff->cur;
  2803. }
  2804. buff->cur = result + len;
  2805. return result;
  2806. }
  2807. /* Allocate permanent, unaligned storage of length LEN from a_buff.
  2808. That buffer is used for growing allocations when saving macro
  2809. replacement lists in a #define, and when parsing an answer to an
  2810. assertion in #assert, #unassert or #if (and therefore possibly
  2811. whilst expanding macros). It therefore must not be used by any
  2812. code that they might call: specifically the lexer and the guts of
  2813. the macro expander.
  2814. All existing other uses clearly fit this restriction: storing
  2815. registered pragmas during initialization. */
  2816. unsigned char *
  2817. _cpp_aligned_alloc (cpp_reader *pfile, size_t len)
  2818. {
  2819. _cpp_buff *buff = pfile->a_buff;
  2820. unsigned char *result = buff->cur;
  2821. if (len > (size_t) (buff->limit - result))
  2822. {
  2823. buff = _cpp_get_buff (pfile, len);
  2824. buff->next = pfile->a_buff;
  2825. pfile->a_buff = buff;
  2826. result = buff->cur;
  2827. }
  2828. buff->cur = result + len;
  2829. return result;
  2830. }
  2831. /* Say which field of TOK is in use. */
  2832. enum cpp_token_fld_kind
  2833. cpp_token_val_index (const cpp_token *tok)
  2834. {
  2835. switch (TOKEN_SPELL (tok))
  2836. {
  2837. case SPELL_IDENT:
  2838. return CPP_TOKEN_FLD_NODE;
  2839. case SPELL_LITERAL:
  2840. return CPP_TOKEN_FLD_STR;
  2841. case SPELL_OPERATOR:
  2842. if (tok->type == CPP_PASTE)
  2843. return CPP_TOKEN_FLD_TOKEN_NO;
  2844. else
  2845. return CPP_TOKEN_FLD_NONE;
  2846. case SPELL_NONE:
  2847. if (tok->type == CPP_MACRO_ARG)
  2848. return CPP_TOKEN_FLD_ARG_NO;
  2849. else if (tok->type == CPP_PADDING)
  2850. return CPP_TOKEN_FLD_SOURCE;
  2851. else if (tok->type == CPP_PRAGMA)
  2852. return CPP_TOKEN_FLD_PRAGMA;
  2853. /* else fall through */
  2854. default:
  2855. return CPP_TOKEN_FLD_NONE;
  2856. }
  2857. }
  2858. /* All tokens lexed in R after calling this function will be forced to have
  2859. their source_location the same as the location referenced by P, until
  2860. cpp_stop_forcing_token_locations is called for R. */
  2861. void
  2862. cpp_force_token_locations (cpp_reader *r, source_location *p)
  2863. {
  2864. r->forced_token_location_p = p;
  2865. }
  2866. /* Go back to assigning locations naturally for lexed tokens. */
  2867. void
  2868. cpp_stop_forcing_token_locations (cpp_reader *r)
  2869. {
  2870. r->forced_token_location_p = NULL;
  2871. }