directives.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633
  1. /* CPP Library. (Directive handling.)
  2. Copyright (C) 1986-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. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 3, or (at your option) any
  9. later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "cpplib.h"
  20. #include "internal.h"
  21. #include "mkdeps.h"
  22. #include "obstack.h"
  23. /* Stack of conditionals currently in progress
  24. (including both successful and failing conditionals). */
  25. struct if_stack
  26. {
  27. struct if_stack *next;
  28. source_location line; /* Line where condition started. */
  29. const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
  30. bool skip_elses; /* Can future #else / #elif be skipped? */
  31. bool was_skipping; /* If were skipping on entry. */
  32. int type; /* Most recent conditional for diagnostics. */
  33. };
  34. /* Contains a registered pragma or pragma namespace. */
  35. typedef void (*pragma_cb) (cpp_reader *);
  36. struct pragma_entry
  37. {
  38. struct pragma_entry *next;
  39. const cpp_hashnode *pragma; /* Name and length. */
  40. bool is_nspace;
  41. bool is_internal;
  42. bool is_deferred;
  43. bool allow_expansion;
  44. union {
  45. pragma_cb handler;
  46. struct pragma_entry *space;
  47. unsigned int ident;
  48. } u;
  49. };
  50. /* Values for the origin field of struct directive. KANDR directives
  51. come from traditional (K&R) C. STDC89 directives come from the
  52. 1989 C standard. EXTENSION directives are extensions. */
  53. #define KANDR 0
  54. #define STDC89 1
  55. #define EXTENSION 2
  56. /* Values for the flags field of struct directive. COND indicates a
  57. conditional; IF_COND an opening conditional. INCL means to treat
  58. "..." and <...> as q-char and h-char sequences respectively. IN_I
  59. means this directive should be handled even if -fpreprocessed is in
  60. effect (these are the directives with callback hooks).
  61. EXPAND is set on directives that are always macro-expanded. */
  62. #define COND (1 << 0)
  63. #define IF_COND (1 << 1)
  64. #define INCL (1 << 2)
  65. #define IN_I (1 << 3)
  66. #define EXPAND (1 << 4)
  67. #define DEPRECATED (1 << 5)
  68. /* Defines one #-directive, including how to handle it. */
  69. typedef void (*directive_handler) (cpp_reader *);
  70. typedef struct directive directive;
  71. struct directive
  72. {
  73. directive_handler handler; /* Function to handle directive. */
  74. const uchar *name; /* Name of directive. */
  75. unsigned short length; /* Length of name. */
  76. unsigned char origin; /* Origin of directive. */
  77. unsigned char flags; /* Flags describing this directive. */
  78. };
  79. /* Forward declarations. */
  80. static void skip_rest_of_line (cpp_reader *);
  81. static void check_eol (cpp_reader *, bool);
  82. static void start_directive (cpp_reader *);
  83. static void prepare_directive_trad (cpp_reader *);
  84. static void end_directive (cpp_reader *, int);
  85. static void directive_diagnostics (cpp_reader *, const directive *, int);
  86. static void run_directive (cpp_reader *, int, const char *, size_t);
  87. static char *glue_header_name (cpp_reader *);
  88. static const char *parse_include (cpp_reader *, int *, const cpp_token ***,
  89. source_location *);
  90. static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
  91. static unsigned int read_flag (cpp_reader *, unsigned int);
  92. static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
  93. static void do_diagnostic (cpp_reader *, int, int, int);
  94. static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
  95. static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
  96. static void do_include_common (cpp_reader *, enum include_type);
  97. static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
  98. const cpp_hashnode *);
  99. static int count_registered_pragmas (struct pragma_entry *);
  100. static char ** save_registered_pragmas (struct pragma_entry *, char **);
  101. static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
  102. char **);
  103. static void do_pragma_once (cpp_reader *);
  104. static void do_pragma_poison (cpp_reader *);
  105. static void do_pragma_system_header (cpp_reader *);
  106. static void do_pragma_dependency (cpp_reader *);
  107. static void do_pragma_warning_or_error (cpp_reader *, bool error);
  108. static void do_pragma_warning (cpp_reader *);
  109. static void do_pragma_error (cpp_reader *);
  110. static void do_linemarker (cpp_reader *);
  111. static const cpp_token *get_token_no_padding (cpp_reader *);
  112. static const cpp_token *get__Pragma_string (cpp_reader *);
  113. static void destringize_and_run (cpp_reader *, const cpp_string *);
  114. static int parse_answer (cpp_reader *, struct answer **, int, source_location);
  115. static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
  116. static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
  117. static void handle_assertion (cpp_reader *, const char *, int);
  118. static void do_pragma_push_macro (cpp_reader *);
  119. static void do_pragma_pop_macro (cpp_reader *);
  120. static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *);
  121. /* This is the table of directive handlers. It is ordered by
  122. frequency of occurrence; the numbers at the end are directive
  123. counts from all the source code I have lying around (egcs and libc
  124. CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
  125. pcmcia-cs-3.0.9). This is no longer important as directive lookup
  126. is now O(1). All extensions other than #warning, #include_next,
  127. and #import are deprecated. The name is where the extension
  128. appears to have come from. */
  129. #define DIRECTIVE_TABLE \
  130. D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
  131. D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
  132. D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
  133. D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
  134. D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
  135. D(else, T_ELSE, KANDR, COND) /* 9863 */ \
  136. D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
  137. D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
  138. D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
  139. D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
  140. D(error, T_ERROR, STDC89, 0) /* 475 */ \
  141. D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
  142. D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
  143. D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
  144. D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
  145. D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
  146. D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
  147. D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
  148. D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
  149. /* #sccs is synonymous with #ident. */
  150. #define do_sccs do_ident
  151. /* Use the table to generate a series of prototypes, an enum for the
  152. directive names, and an array of directive handlers. */
  153. #define D(name, t, o, f) static void do_##name (cpp_reader *);
  154. DIRECTIVE_TABLE
  155. #undef D
  156. #define D(n, tag, o, f) tag,
  157. enum
  158. {
  159. DIRECTIVE_TABLE
  160. N_DIRECTIVES
  161. };
  162. #undef D
  163. #define D(name, t, origin, flags) \
  164. { do_##name, (const uchar *) #name, \
  165. sizeof #name - 1, origin, flags },
  166. static const directive dtable[] =
  167. {
  168. DIRECTIVE_TABLE
  169. };
  170. #undef D
  171. #undef DIRECTIVE_TABLE
  172. /* Wrapper struct directive for linemarkers.
  173. The origin is more or less true - the original K+R cpp
  174. did use this notation in its preprocessed output. */
  175. static const directive linemarker_dir =
  176. {
  177. do_linemarker, UC"#", 1, KANDR, IN_I
  178. };
  179. #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
  180. /* Skip any remaining tokens in a directive. */
  181. static void
  182. skip_rest_of_line (cpp_reader *pfile)
  183. {
  184. /* Discard all stacked contexts. */
  185. while (pfile->context->prev)
  186. _cpp_pop_context (pfile);
  187. /* Sweep up all tokens remaining on the line. */
  188. if (! SEEN_EOL ())
  189. while (_cpp_lex_token (pfile)->type != CPP_EOF)
  190. ;
  191. }
  192. /* Helper function for check_oel. */
  193. static void
  194. check_eol_1 (cpp_reader *pfile, bool expand, int reason)
  195. {
  196. if (! SEEN_EOL () && (expand
  197. ? cpp_get_token (pfile)
  198. : _cpp_lex_token (pfile))->type != CPP_EOF)
  199. cpp_pedwarning (pfile, reason, "extra tokens at end of #%s directive",
  200. pfile->directive->name);
  201. }
  202. /* Variant of check_eol used for Wendif-labels warnings. */
  203. static void
  204. check_eol_endif_labels (cpp_reader *pfile)
  205. {
  206. check_eol_1 (pfile, false, CPP_W_ENDIF_LABELS);
  207. }
  208. /* Ensure there are no stray tokens at the end of a directive. If
  209. EXPAND is true, tokens macro-expanding to nothing are allowed. */
  210. static void
  211. check_eol (cpp_reader *pfile, bool expand)
  212. {
  213. check_eol_1 (pfile, expand, CPP_W_NONE);
  214. }
  215. /* Ensure there are no stray tokens other than comments at the end of
  216. a directive, and gather the comments. */
  217. static const cpp_token **
  218. check_eol_return_comments (cpp_reader *pfile)
  219. {
  220. size_t c;
  221. size_t capacity = 8;
  222. const cpp_token **buf;
  223. buf = XNEWVEC (const cpp_token *, capacity);
  224. c = 0;
  225. if (! SEEN_EOL ())
  226. {
  227. while (1)
  228. {
  229. const cpp_token *tok;
  230. tok = _cpp_lex_token (pfile);
  231. if (tok->type == CPP_EOF)
  232. break;
  233. if (tok->type != CPP_COMMENT)
  234. cpp_error (pfile, CPP_DL_PEDWARN,
  235. "extra tokens at end of #%s directive",
  236. pfile->directive->name);
  237. else
  238. {
  239. if (c + 1 >= capacity)
  240. {
  241. capacity *= 2;
  242. buf = XRESIZEVEC (const cpp_token *, buf, capacity);
  243. }
  244. buf[c] = tok;
  245. ++c;
  246. }
  247. }
  248. }
  249. buf[c] = NULL;
  250. return buf;
  251. }
  252. /* Called when entering a directive, _Pragma or command-line directive. */
  253. static void
  254. start_directive (cpp_reader *pfile)
  255. {
  256. /* Setup in-directive state. */
  257. pfile->state.in_directive = 1;
  258. pfile->state.save_comments = 0;
  259. pfile->directive_result.type = CPP_PADDING;
  260. /* Some handlers need the position of the # for diagnostics. */
  261. pfile->directive_line = pfile->line_table->highest_line;
  262. }
  263. /* Called when leaving a directive, _Pragma or command-line directive. */
  264. static void
  265. end_directive (cpp_reader *pfile, int skip_line)
  266. {
  267. if (CPP_OPTION (pfile, traditional))
  268. {
  269. /* Revert change of prepare_directive_trad. */
  270. if (!pfile->state.in_deferred_pragma)
  271. pfile->state.prevent_expansion--;
  272. if (pfile->directive != &dtable[T_DEFINE])
  273. _cpp_remove_overlay (pfile);
  274. }
  275. else if (pfile->state.in_deferred_pragma)
  276. ;
  277. /* We don't skip for an assembler #. */
  278. else if (skip_line)
  279. {
  280. skip_rest_of_line (pfile);
  281. if (!pfile->keep_tokens)
  282. {
  283. pfile->cur_run = &pfile->base_run;
  284. pfile->cur_token = pfile->base_run.base;
  285. }
  286. }
  287. /* Restore state. */
  288. pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
  289. pfile->state.in_directive = 0;
  290. pfile->state.in_expression = 0;
  291. pfile->state.angled_headers = 0;
  292. pfile->directive = 0;
  293. }
  294. /* Prepare to handle the directive in pfile->directive. */
  295. static void
  296. prepare_directive_trad (cpp_reader *pfile)
  297. {
  298. if (pfile->directive != &dtable[T_DEFINE])
  299. {
  300. bool no_expand = (pfile->directive
  301. && ! (pfile->directive->flags & EXPAND));
  302. bool was_skipping = pfile->state.skipping;
  303. pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
  304. || pfile->directive == &dtable[T_ELIF]);
  305. if (pfile->state.in_expression)
  306. pfile->state.skipping = false;
  307. if (no_expand)
  308. pfile->state.prevent_expansion++;
  309. _cpp_scan_out_logical_line (pfile, NULL, false);
  310. if (no_expand)
  311. pfile->state.prevent_expansion--;
  312. pfile->state.skipping = was_skipping;
  313. _cpp_overlay_buffer (pfile, pfile->out.base,
  314. pfile->out.cur - pfile->out.base);
  315. }
  316. /* Stop ISO C from expanding anything. */
  317. pfile->state.prevent_expansion++;
  318. }
  319. /* Output diagnostics for a directive DIR. INDENTED is nonzero if
  320. the '#' was indented. */
  321. static void
  322. directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
  323. {
  324. /* Issue -pedantic or deprecated warnings for extensions. We let
  325. -pedantic take precedence if both are applicable. */
  326. if (! pfile->state.skipping)
  327. {
  328. if (dir->origin == EXTENSION
  329. && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
  330. && CPP_PEDANTIC (pfile))
  331. cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
  332. else if (((dir->flags & DEPRECATED) != 0
  333. || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
  334. && CPP_OPTION (pfile, cpp_warn_deprecated))
  335. cpp_warning (pfile, CPP_W_DEPRECATED,
  336. "#%s is a deprecated GCC extension", dir->name);
  337. }
  338. /* Traditionally, a directive is ignored unless its # is in
  339. column 1. Therefore in code intended to work with K+R
  340. compilers, directives added by C89 must have their #
  341. indented, and directives present in traditional C must not.
  342. This is true even of directives in skipped conditional
  343. blocks. #elif cannot be used at all. */
  344. if (CPP_WTRADITIONAL (pfile))
  345. {
  346. if (dir == &dtable[T_ELIF])
  347. cpp_warning (pfile, CPP_W_TRADITIONAL,
  348. "suggest not using #elif in traditional C");
  349. else if (indented && dir->origin == KANDR)
  350. cpp_warning (pfile, CPP_W_TRADITIONAL,
  351. "traditional C ignores #%s with the # indented",
  352. dir->name);
  353. else if (!indented && dir->origin != KANDR)
  354. cpp_warning (pfile, CPP_W_TRADITIONAL,
  355. "suggest hiding #%s from traditional C with an indented #",
  356. dir->name);
  357. }
  358. }
  359. /* Check if we have a known directive. INDENTED is nonzero if the
  360. '#' of the directive was indented. This function is in this file
  361. to save unnecessarily exporting dtable etc. to lex.c. Returns
  362. nonzero if the line of tokens has been handled, zero if we should
  363. continue processing the line. */
  364. int
  365. _cpp_handle_directive (cpp_reader *pfile, int indented)
  366. {
  367. const directive *dir = 0;
  368. const cpp_token *dname;
  369. bool was_parsing_args = pfile->state.parsing_args;
  370. bool was_discarding_output = pfile->state.discarding_output;
  371. int skip = 1;
  372. if (was_discarding_output)
  373. pfile->state.prevent_expansion = 0;
  374. if (was_parsing_args)
  375. {
  376. if (CPP_OPTION (pfile, cpp_pedantic))
  377. cpp_error (pfile, CPP_DL_PEDWARN,
  378. "embedding a directive within macro arguments is not portable");
  379. pfile->state.parsing_args = 0;
  380. pfile->state.prevent_expansion = 0;
  381. }
  382. start_directive (pfile);
  383. dname = _cpp_lex_token (pfile);
  384. if (dname->type == CPP_NAME)
  385. {
  386. if (dname->val.node.node->is_directive)
  387. dir = &dtable[dname->val.node.node->directive_index];
  388. }
  389. /* We do not recognize the # followed by a number extension in
  390. assembler code. */
  391. else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
  392. {
  393. dir = &linemarker_dir;
  394. if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
  395. && ! pfile->state.skipping)
  396. cpp_error (pfile, CPP_DL_PEDWARN,
  397. "style of line directive is a GCC extension");
  398. }
  399. if (dir)
  400. {
  401. /* If we have a directive that is not an opening conditional,
  402. invalidate any control macro. */
  403. if (! (dir->flags & IF_COND))
  404. pfile->mi_valid = false;
  405. /* Kluge alert. In order to be sure that code like this
  406. #define HASH #
  407. HASH define foo bar
  408. does not cause '#define foo bar' to get executed when
  409. compiled with -save-temps, we recognize directives in
  410. -fpreprocessed mode only if the # is in column 1. macro.c
  411. puts a space in front of any '#' at the start of a macro.
  412. We exclude the -fdirectives-only case because macro expansion
  413. has not been performed yet, and block comments can cause spaces
  414. to precede the directive. */
  415. if (CPP_OPTION (pfile, preprocessed)
  416. && !CPP_OPTION (pfile, directives_only)
  417. && (indented || !(dir->flags & IN_I)))
  418. {
  419. skip = 0;
  420. dir = 0;
  421. }
  422. else
  423. {
  424. /* In failed conditional groups, all non-conditional
  425. directives are ignored. Before doing that, whether
  426. skipping or not, we should lex angle-bracketed headers
  427. correctly, and maybe output some diagnostics. */
  428. pfile->state.angled_headers = dir->flags & INCL;
  429. pfile->state.directive_wants_padding = dir->flags & INCL;
  430. if (! CPP_OPTION (pfile, preprocessed))
  431. directive_diagnostics (pfile, dir, indented);
  432. if (pfile->state.skipping && !(dir->flags & COND))
  433. dir = 0;
  434. }
  435. }
  436. else if (dname->type == CPP_EOF)
  437. ; /* CPP_EOF is the "null directive". */
  438. else
  439. {
  440. /* An unknown directive. Don't complain about it in assembly
  441. source: we don't know where the comments are, and # may
  442. introduce assembler pseudo-ops. Don't complain about invalid
  443. directives in skipped conditional groups (6.10 p4). */
  444. if (CPP_OPTION (pfile, lang) == CLK_ASM)
  445. skip = 0;
  446. else if (!pfile->state.skipping)
  447. cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
  448. cpp_token_as_text (pfile, dname));
  449. }
  450. pfile->directive = dir;
  451. if (CPP_OPTION (pfile, traditional))
  452. prepare_directive_trad (pfile);
  453. if (dir)
  454. pfile->directive->handler (pfile);
  455. else if (skip == 0)
  456. _cpp_backup_tokens (pfile, 1);
  457. end_directive (pfile, skip);
  458. if (was_parsing_args && !pfile->state.in_deferred_pragma)
  459. {
  460. /* Restore state when within macro args. */
  461. pfile->state.parsing_args = 2;
  462. pfile->state.prevent_expansion = 1;
  463. }
  464. if (was_discarding_output)
  465. pfile->state.prevent_expansion = 1;
  466. return skip;
  467. }
  468. /* Directive handler wrapper used by the command line option
  469. processor. BUF is \n terminated. */
  470. static void
  471. run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
  472. {
  473. cpp_push_buffer (pfile, (const uchar *) buf, count,
  474. /* from_stage3 */ true);
  475. start_directive (pfile);
  476. /* This is a short-term fix to prevent a leading '#' being
  477. interpreted as a directive. */
  478. _cpp_clean_line (pfile);
  479. pfile->directive = &dtable[dir_no];
  480. if (CPP_OPTION (pfile, traditional))
  481. prepare_directive_trad (pfile);
  482. pfile->directive->handler (pfile);
  483. end_directive (pfile, 1);
  484. _cpp_pop_buffer (pfile);
  485. }
  486. /* Checks for validity the macro name in #define, #undef, #ifdef and
  487. #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
  488. processing a #define or #undefine directive, and false
  489. otherwise. */
  490. static cpp_hashnode *
  491. lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
  492. {
  493. const cpp_token *token = _cpp_lex_token (pfile);
  494. /* The token immediately after #define must be an identifier. That
  495. identifier may not be "defined", per C99 6.10.8p4.
  496. In C++, it may not be any of the "named operators" either,
  497. per C++98 [lex.digraph], [lex.key].
  498. Finally, the identifier may not have been poisoned. (In that case
  499. the lexer has issued the error message for us.) */
  500. if (token->type == CPP_NAME)
  501. {
  502. cpp_hashnode *node = token->val.node.node;
  503. if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
  504. cpp_error (pfile, CPP_DL_ERROR,
  505. "\"defined\" cannot be used as a macro name");
  506. else if (is_def_or_undef
  507. && (node == pfile->spec_nodes.n__has_include__
  508. || node == pfile->spec_nodes.n__has_include_next__))
  509. cpp_error (pfile, CPP_DL_ERROR,
  510. "\"__has_include__\" cannot be used as a macro name");
  511. else if (! (node->flags & NODE_POISONED))
  512. return node;
  513. }
  514. else if (token->flags & NAMED_OP)
  515. cpp_error (pfile, CPP_DL_ERROR,
  516. "\"%s\" cannot be used as a macro name as it is an operator in C++",
  517. NODE_NAME (token->val.node.node));
  518. else if (token->type == CPP_EOF)
  519. cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
  520. pfile->directive->name);
  521. else
  522. cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
  523. return NULL;
  524. }
  525. /* Process a #define directive. Most work is done in macro.c. */
  526. static void
  527. do_define (cpp_reader *pfile)
  528. {
  529. cpp_hashnode *node = lex_macro_node (pfile, true);
  530. if (node)
  531. {
  532. /* If we have been requested to expand comments into macros,
  533. then re-enable saving of comments. */
  534. pfile->state.save_comments =
  535. ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
  536. if (pfile->cb.before_define)
  537. pfile->cb.before_define (pfile);
  538. if (_cpp_create_definition (pfile, node))
  539. if (pfile->cb.define)
  540. pfile->cb.define (pfile, pfile->directive_line, node);
  541. node->flags &= ~NODE_USED;
  542. }
  543. }
  544. /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
  545. static void
  546. do_undef (cpp_reader *pfile)
  547. {
  548. cpp_hashnode *node = lex_macro_node (pfile, true);
  549. if (node)
  550. {
  551. if (pfile->cb.before_define)
  552. pfile->cb.before_define (pfile);
  553. if (pfile->cb.undef)
  554. pfile->cb.undef (pfile, pfile->directive_line, node);
  555. /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
  556. identifier is not currently defined as a macro name. */
  557. if (node->type == NT_MACRO)
  558. {
  559. if (node->flags & NODE_WARN)
  560. cpp_error (pfile, CPP_DL_WARNING,
  561. "undefining \"%s\"", NODE_NAME (node));
  562. else if ((node->flags & NODE_BUILTIN)
  563. && CPP_OPTION (pfile, warn_builtin_macro_redefined))
  564. cpp_warning_with_line (pfile, CPP_W_BUILTIN_MACRO_REDEFINED,
  565. pfile->directive_line, 0,
  566. "undefining \"%s\"", NODE_NAME (node));
  567. if (CPP_OPTION (pfile, warn_unused_macros))
  568. _cpp_warn_if_unused_macro (pfile, node, NULL);
  569. _cpp_free_definition (node);
  570. }
  571. }
  572. check_eol (pfile, false);
  573. }
  574. /* Undefine a single macro/assertion/whatever. */
  575. static int
  576. undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
  577. void *data_p ATTRIBUTE_UNUSED)
  578. {
  579. /* Body of _cpp_free_definition inlined here for speed.
  580. Macros and assertions no longer have anything to free. */
  581. h->type = NT_VOID;
  582. h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
  583. return 1;
  584. }
  585. /* Undefine all macros and assertions. */
  586. void
  587. cpp_undef_all (cpp_reader *pfile)
  588. {
  589. cpp_forall_identifiers (pfile, undefine_macros, NULL);
  590. }
  591. /* Helper routine used by parse_include. Reinterpret the current line
  592. as an h-char-sequence (< ... >); we are looking at the first token
  593. after the <. Returns a malloced filename. */
  594. static char *
  595. glue_header_name (cpp_reader *pfile)
  596. {
  597. const cpp_token *token;
  598. char *buffer;
  599. size_t len, total_len = 0, capacity = 1024;
  600. /* To avoid lexed tokens overwriting our glued name, we can only
  601. allocate from the string pool once we've lexed everything. */
  602. buffer = XNEWVEC (char, capacity);
  603. for (;;)
  604. {
  605. token = get_token_no_padding (pfile);
  606. if (token->type == CPP_GREATER)
  607. break;
  608. if (token->type == CPP_EOF)
  609. {
  610. cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
  611. break;
  612. }
  613. len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
  614. if (total_len + len > capacity)
  615. {
  616. capacity = (capacity + len) * 2;
  617. buffer = XRESIZEVEC (char, buffer, capacity);
  618. }
  619. if (token->flags & PREV_WHITE)
  620. buffer[total_len++] = ' ';
  621. total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
  622. true)
  623. - (uchar *) buffer);
  624. }
  625. buffer[total_len] = '\0';
  626. return buffer;
  627. }
  628. /* Returns the file name of #include, #include_next, #import and
  629. #pragma dependency. The string is malloced and the caller should
  630. free it. Returns NULL on error. LOCATION is the source location
  631. of the file name. */
  632. static const char *
  633. parse_include (cpp_reader *pfile, int *pangle_brackets,
  634. const cpp_token ***buf, source_location *location)
  635. {
  636. char *fname;
  637. const cpp_token *header;
  638. /* Allow macro expansion. */
  639. header = get_token_no_padding (pfile);
  640. *location = header->src_loc;
  641. if ((header->type == CPP_STRING && header->val.str.text[0] != 'R')
  642. || header->type == CPP_HEADER_NAME)
  643. {
  644. fname = XNEWVEC (char, header->val.str.len - 1);
  645. memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
  646. fname[header->val.str.len - 2] = '\0';
  647. *pangle_brackets = header->type == CPP_HEADER_NAME;
  648. }
  649. else if (header->type == CPP_LESS)
  650. {
  651. fname = glue_header_name (pfile);
  652. *pangle_brackets = 1;
  653. }
  654. else
  655. {
  656. const unsigned char *dir;
  657. if (pfile->directive == &dtable[T_PRAGMA])
  658. dir = UC"pragma dependency";
  659. else
  660. dir = pfile->directive->name;
  661. cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
  662. dir);
  663. return NULL;
  664. }
  665. if (pfile->directive == &dtable[T_PRAGMA])
  666. {
  667. /* This pragma allows extra tokens after the file name. */
  668. }
  669. else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
  670. check_eol (pfile, true);
  671. else
  672. {
  673. /* If we are not discarding comments, then gather them while
  674. doing the eol check. */
  675. *buf = check_eol_return_comments (pfile);
  676. }
  677. return fname;
  678. }
  679. /* Handle #include, #include_next and #import. */
  680. static void
  681. do_include_common (cpp_reader *pfile, enum include_type type)
  682. {
  683. const char *fname;
  684. int angle_brackets;
  685. const cpp_token **buf = NULL;
  686. source_location location;
  687. /* Re-enable saving of comments if requested, so that the include
  688. callback can dump comments which follow #include. */
  689. pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
  690. fname = parse_include (pfile, &angle_brackets, &buf, &location);
  691. if (!fname)
  692. {
  693. if (buf)
  694. XDELETEVEC (buf);
  695. return;
  696. }
  697. if (!*fname)
  698. {
  699. cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0,
  700. "empty filename in #%s",
  701. pfile->directive->name);
  702. XDELETEVEC (fname);
  703. if (buf)
  704. XDELETEVEC (buf);
  705. return;
  706. }
  707. /* Prevent #include recursion. */
  708. if (pfile->line_table->depth >= CPP_STACK_MAX)
  709. cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
  710. else
  711. {
  712. /* Get out of macro context, if we are. */
  713. skip_rest_of_line (pfile);
  714. if (pfile->cb.include)
  715. pfile->cb.include (pfile, pfile->directive_line,
  716. pfile->directive->name, fname, angle_brackets,
  717. buf);
  718. _cpp_stack_include (pfile, fname, angle_brackets, type);
  719. }
  720. XDELETEVEC (fname);
  721. if (buf)
  722. XDELETEVEC (buf);
  723. }
  724. static void
  725. do_include (cpp_reader *pfile)
  726. {
  727. do_include_common (pfile, IT_INCLUDE);
  728. }
  729. static void
  730. do_import (cpp_reader *pfile)
  731. {
  732. do_include_common (pfile, IT_IMPORT);
  733. }
  734. static void
  735. do_include_next (cpp_reader *pfile)
  736. {
  737. enum include_type type = IT_INCLUDE_NEXT;
  738. /* If this is the primary source file, warn and use the normal
  739. search logic. */
  740. if (cpp_in_primary_file (pfile))
  741. {
  742. cpp_error (pfile, CPP_DL_WARNING,
  743. "#include_next in primary source file");
  744. type = IT_INCLUDE;
  745. }
  746. do_include_common (pfile, type);
  747. }
  748. /* Subroutine of do_linemarker. Read possible flags after file name.
  749. LAST is the last flag seen; 0 if this is the first flag. Return the
  750. flag if it is valid, 0 at the end of the directive. Otherwise
  751. complain. */
  752. static unsigned int
  753. read_flag (cpp_reader *pfile, unsigned int last)
  754. {
  755. const cpp_token *token = _cpp_lex_token (pfile);
  756. if (token->type == CPP_NUMBER && token->val.str.len == 1)
  757. {
  758. unsigned int flag = token->val.str.text[0] - '0';
  759. if (flag > last && flag <= 4
  760. && (flag != 4 || last == 3)
  761. && (flag != 2 || last == 0))
  762. return flag;
  763. }
  764. if (token->type != CPP_EOF)
  765. cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
  766. cpp_token_as_text (pfile, token));
  767. return 0;
  768. }
  769. /* Subroutine of do_line and do_linemarker. Convert a number in STR,
  770. of length LEN, to binary; store it in NUMP, and return false if the
  771. number was well-formed, true if not. WRAPPED is set to true if the
  772. number did not fit into 'unsigned long'. */
  773. static bool
  774. strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
  775. {
  776. linenum_type reg = 0;
  777. linenum_type reg_prev = 0;
  778. uchar c;
  779. *wrapped = false;
  780. while (len--)
  781. {
  782. c = *str++;
  783. if (!ISDIGIT (c))
  784. return true;
  785. reg *= 10;
  786. reg += c - '0';
  787. if (reg < reg_prev)
  788. *wrapped = true;
  789. reg_prev = reg;
  790. }
  791. *nump = reg;
  792. return false;
  793. }
  794. /* Interpret #line command.
  795. Note that the filename string (if any) is a true string constant
  796. (escapes are interpreted), unlike in #line. */
  797. static void
  798. do_line (cpp_reader *pfile)
  799. {
  800. const struct line_maps *line_table = pfile->line_table;
  801. const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
  802. /* skip_rest_of_line() may cause line table to be realloc()ed so note down
  803. sysp right now. */
  804. unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
  805. const cpp_token *token;
  806. const char *new_file = ORDINARY_MAP_FILE_NAME (map);
  807. linenum_type new_lineno;
  808. /* C99 raised the minimum limit on #line numbers. */
  809. linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
  810. bool wrapped;
  811. /* #line commands expand macros. */
  812. token = cpp_get_token (pfile);
  813. if (token->type != CPP_NUMBER
  814. || strtolinenum (token->val.str.text, token->val.str.len,
  815. &new_lineno, &wrapped))
  816. {
  817. if (token->type == CPP_EOF)
  818. cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
  819. else
  820. cpp_error (pfile, CPP_DL_ERROR,
  821. "\"%s\" after #line is not a positive integer",
  822. cpp_token_as_text (pfile, token));
  823. return;
  824. }
  825. if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
  826. cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
  827. else if (wrapped)
  828. cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
  829. token = cpp_get_token (pfile);
  830. if (token->type == CPP_STRING)
  831. {
  832. cpp_string s = { 0, 0 };
  833. if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
  834. &s, CPP_STRING))
  835. new_file = (const char *)s.text;
  836. check_eol (pfile, true);
  837. }
  838. else if (token->type != CPP_EOF)
  839. {
  840. cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
  841. cpp_token_as_text (pfile, token));
  842. return;
  843. }
  844. skip_rest_of_line (pfile);
  845. _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno,
  846. map_sysp);
  847. }
  848. /* Interpret the # 44 "file" [flags] notation, which has slightly
  849. different syntax and semantics from #line: Flags are allowed,
  850. and we never complain about the line number being too big. */
  851. static void
  852. do_linemarker (cpp_reader *pfile)
  853. {
  854. const struct line_maps *line_table = pfile->line_table;
  855. const struct line_map *map = LINEMAPS_LAST_ORDINARY_MAP (line_table);
  856. const cpp_token *token;
  857. const char *new_file = ORDINARY_MAP_FILE_NAME (map);
  858. linenum_type new_lineno;
  859. unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map);
  860. enum lc_reason reason = LC_RENAME_VERBATIM;
  861. int flag;
  862. bool wrapped;
  863. /* Back up so we can get the number again. Putting this in
  864. _cpp_handle_directive risks two calls to _cpp_backup_tokens in
  865. some circumstances, which can segfault. */
  866. _cpp_backup_tokens (pfile, 1);
  867. /* #line commands expand macros. */
  868. token = cpp_get_token (pfile);
  869. if (token->type != CPP_NUMBER
  870. || strtolinenum (token->val.str.text, token->val.str.len,
  871. &new_lineno, &wrapped))
  872. {
  873. /* Unlike #line, there does not seem to be a way to get an EOF
  874. here. So, it should be safe to always spell the token. */
  875. cpp_error (pfile, CPP_DL_ERROR,
  876. "\"%s\" after # is not a positive integer",
  877. cpp_token_as_text (pfile, token));
  878. return;
  879. }
  880. token = cpp_get_token (pfile);
  881. if (token->type == CPP_STRING)
  882. {
  883. cpp_string s = { 0, 0 };
  884. if (cpp_interpret_string_notranslate (pfile, &token->val.str,
  885. 1, &s, CPP_STRING))
  886. new_file = (const char *)s.text;
  887. new_sysp = 0;
  888. flag = read_flag (pfile, 0);
  889. if (flag == 1)
  890. {
  891. reason = LC_ENTER;
  892. /* Fake an include for cpp_included (). */
  893. _cpp_fake_include (pfile, new_file);
  894. flag = read_flag (pfile, flag);
  895. }
  896. else if (flag == 2)
  897. {
  898. reason = LC_LEAVE;
  899. flag = read_flag (pfile, flag);
  900. }
  901. if (flag == 3)
  902. {
  903. new_sysp = 1;
  904. flag = read_flag (pfile, flag);
  905. if (flag == 4)
  906. new_sysp = 2;
  907. }
  908. pfile->buffer->sysp = new_sysp;
  909. check_eol (pfile, false);
  910. }
  911. else if (token->type != CPP_EOF)
  912. {
  913. cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
  914. cpp_token_as_text (pfile, token));
  915. return;
  916. }
  917. skip_rest_of_line (pfile);
  918. /* Compensate for the increment in linemap_add that occurs in
  919. _cpp_do_file_change. We're currently at the start of the line
  920. *following* the #line directive. A separate source_location for this
  921. location makes no sense (until we do the LC_LEAVE), and
  922. complicates LAST_SOURCE_LINE_LOCATION. */
  923. pfile->line_table->highest_location--;
  924. _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
  925. }
  926. /* Arrange the file_change callback. pfile->line has changed to
  927. FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
  928. header, 2 for a system header that needs to be extern "C" protected,
  929. and zero otherwise. */
  930. void
  931. _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
  932. const char *to_file, linenum_type file_line,
  933. unsigned int sysp)
  934. {
  935. const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
  936. to_file, file_line);
  937. if (map != NULL)
  938. linemap_line_start (pfile->line_table,
  939. ORDINARY_MAP_STARTING_LINE_NUMBER (map),
  940. 127);
  941. if (pfile->cb.file_change)
  942. pfile->cb.file_change (pfile, map);
  943. }
  944. /* Report a warning or error detected by the program we are
  945. processing. Use the directive's tokens in the error message. */
  946. static void
  947. do_diagnostic (cpp_reader *pfile, int code, int reason, int print_dir)
  948. {
  949. const unsigned char *dir_name;
  950. unsigned char *line;
  951. source_location src_loc = pfile->cur_token[-1].src_loc;
  952. if (print_dir)
  953. dir_name = pfile->directive->name;
  954. else
  955. dir_name = NULL;
  956. pfile->state.prevent_expansion++;
  957. line = cpp_output_line_to_string (pfile, dir_name);
  958. pfile->state.prevent_expansion--;
  959. if (code == CPP_DL_WARNING_SYSHDR && reason)
  960. cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line);
  961. else if (code == CPP_DL_WARNING && reason)
  962. cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line);
  963. else
  964. cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
  965. free (line);
  966. }
  967. static void
  968. do_error (cpp_reader *pfile)
  969. {
  970. do_diagnostic (pfile, CPP_DL_ERROR, 0, 1);
  971. }
  972. static void
  973. do_warning (cpp_reader *pfile)
  974. {
  975. /* We want #warning diagnostics to be emitted in system headers too. */
  976. do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1);
  977. }
  978. /* Report program identification. */
  979. static void
  980. do_ident (cpp_reader *pfile)
  981. {
  982. const cpp_token *str = cpp_get_token (pfile);
  983. if (str->type != CPP_STRING)
  984. cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
  985. pfile->directive->name);
  986. else if (pfile->cb.ident)
  987. pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
  988. check_eol (pfile, false);
  989. }
  990. /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
  991. matching entry, or NULL if none is found. The returned entry could
  992. be the start of a namespace chain, or a pragma. */
  993. static struct pragma_entry *
  994. lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
  995. {
  996. while (chain && chain->pragma != pragma)
  997. chain = chain->next;
  998. return chain;
  999. }
  1000. /* Create and insert a blank pragma entry at the beginning of a
  1001. singly-linked CHAIN. */
  1002. static struct pragma_entry *
  1003. new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
  1004. {
  1005. struct pragma_entry *new_entry;
  1006. new_entry = (struct pragma_entry *)
  1007. _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
  1008. memset (new_entry, 0, sizeof (struct pragma_entry));
  1009. new_entry->next = *chain;
  1010. *chain = new_entry;
  1011. return new_entry;
  1012. }
  1013. /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
  1014. goes in the global namespace. */
  1015. static struct pragma_entry *
  1016. register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
  1017. bool allow_name_expansion)
  1018. {
  1019. struct pragma_entry **chain = &pfile->pragmas;
  1020. struct pragma_entry *entry;
  1021. const cpp_hashnode *node;
  1022. if (space)
  1023. {
  1024. node = cpp_lookup (pfile, UC space, strlen (space));
  1025. entry = lookup_pragma_entry (*chain, node);
  1026. if (!entry)
  1027. {
  1028. entry = new_pragma_entry (pfile, chain);
  1029. entry->pragma = node;
  1030. entry->is_nspace = true;
  1031. entry->allow_expansion = allow_name_expansion;
  1032. }
  1033. else if (!entry->is_nspace)
  1034. goto clash;
  1035. else if (entry->allow_expansion != allow_name_expansion)
  1036. {
  1037. cpp_error (pfile, CPP_DL_ICE,
  1038. "registering pragmas in namespace \"%s\" with mismatched "
  1039. "name expansion", space);
  1040. return NULL;
  1041. }
  1042. chain = &entry->u.space;
  1043. }
  1044. else if (allow_name_expansion)
  1045. {
  1046. cpp_error (pfile, CPP_DL_ICE,
  1047. "registering pragma \"%s\" with name expansion "
  1048. "and no namespace", name);
  1049. return NULL;
  1050. }
  1051. /* Check for duplicates. */
  1052. node = cpp_lookup (pfile, UC name, strlen (name));
  1053. entry = lookup_pragma_entry (*chain, node);
  1054. if (entry == NULL)
  1055. {
  1056. entry = new_pragma_entry (pfile, chain);
  1057. entry->pragma = node;
  1058. return entry;
  1059. }
  1060. if (entry->is_nspace)
  1061. clash:
  1062. cpp_error (pfile, CPP_DL_ICE,
  1063. "registering \"%s\" as both a pragma and a pragma namespace",
  1064. NODE_NAME (node));
  1065. else if (space)
  1066. cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
  1067. space, name);
  1068. else
  1069. cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
  1070. return NULL;
  1071. }
  1072. /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
  1073. static void
  1074. register_pragma_internal (cpp_reader *pfile, const char *space,
  1075. const char *name, pragma_cb handler)
  1076. {
  1077. struct pragma_entry *entry;
  1078. entry = register_pragma_1 (pfile, space, name, false);
  1079. entry->is_internal = true;
  1080. entry->u.handler = handler;
  1081. }
  1082. /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
  1083. goes in the global namespace. HANDLER is the handler it will call,
  1084. which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
  1085. expansion while parsing pragma NAME. This function is exported
  1086. from libcpp. */
  1087. void
  1088. cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
  1089. pragma_cb handler, bool allow_expansion)
  1090. {
  1091. struct pragma_entry *entry;
  1092. if (!handler)
  1093. {
  1094. cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
  1095. return;
  1096. }
  1097. entry = register_pragma_1 (pfile, space, name, false);
  1098. if (entry)
  1099. {
  1100. entry->allow_expansion = allow_expansion;
  1101. entry->u.handler = handler;
  1102. }
  1103. }
  1104. /* Similarly, but create mark the pragma for deferred processing.
  1105. When found, a CPP_PRAGMA token will be insertted into the stream
  1106. with IDENT in the token->u.pragma slot. */
  1107. void
  1108. cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
  1109. const char *name, unsigned int ident,
  1110. bool allow_expansion, bool allow_name_expansion)
  1111. {
  1112. struct pragma_entry *entry;
  1113. entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
  1114. if (entry)
  1115. {
  1116. entry->is_deferred = true;
  1117. entry->allow_expansion = allow_expansion;
  1118. entry->u.ident = ident;
  1119. }
  1120. }
  1121. /* Register the pragmas the preprocessor itself handles. */
  1122. void
  1123. _cpp_init_internal_pragmas (cpp_reader *pfile)
  1124. {
  1125. /* Pragmas in the global namespace. */
  1126. register_pragma_internal (pfile, 0, "once", do_pragma_once);
  1127. register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
  1128. register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
  1129. /* New GCC-specific pragmas should be put in the GCC namespace. */
  1130. register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
  1131. register_pragma_internal (pfile, "GCC", "system_header",
  1132. do_pragma_system_header);
  1133. register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
  1134. register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning);
  1135. register_pragma_internal (pfile, "GCC", "error", do_pragma_error);
  1136. }
  1137. /* Return the number of registered pragmas in PE. */
  1138. static int
  1139. count_registered_pragmas (struct pragma_entry *pe)
  1140. {
  1141. int ct = 0;
  1142. for (; pe != NULL; pe = pe->next)
  1143. {
  1144. if (pe->is_nspace)
  1145. ct += count_registered_pragmas (pe->u.space);
  1146. ct++;
  1147. }
  1148. return ct;
  1149. }
  1150. /* Save into SD the names of the registered pragmas referenced by PE,
  1151. and return a pointer to the next free space in SD. */
  1152. static char **
  1153. save_registered_pragmas (struct pragma_entry *pe, char **sd)
  1154. {
  1155. for (; pe != NULL; pe = pe->next)
  1156. {
  1157. if (pe->is_nspace)
  1158. sd = save_registered_pragmas (pe->u.space, sd);
  1159. *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
  1160. HT_LEN (&pe->pragma->ident),
  1161. HT_LEN (&pe->pragma->ident) + 1);
  1162. }
  1163. return sd;
  1164. }
  1165. /* Return a newly-allocated array which saves the names of the
  1166. registered pragmas. */
  1167. char **
  1168. _cpp_save_pragma_names (cpp_reader *pfile)
  1169. {
  1170. int ct = count_registered_pragmas (pfile->pragmas);
  1171. char **result = XNEWVEC (char *, ct);
  1172. (void) save_registered_pragmas (pfile->pragmas, result);
  1173. return result;
  1174. }
  1175. /* Restore from SD the names of the registered pragmas referenced by PE,
  1176. and return a pointer to the next unused name in SD. */
  1177. static char **
  1178. restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
  1179. char **sd)
  1180. {
  1181. for (; pe != NULL; pe = pe->next)
  1182. {
  1183. if (pe->is_nspace)
  1184. sd = restore_registered_pragmas (pfile, pe->u.space, sd);
  1185. pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
  1186. free (*sd);
  1187. sd++;
  1188. }
  1189. return sd;
  1190. }
  1191. /* Restore the names of the registered pragmas from SAVED. */
  1192. void
  1193. _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
  1194. {
  1195. (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
  1196. free (saved);
  1197. }
  1198. /* Pragmata handling. We handle some, and pass the rest on to the
  1199. front end. C99 defines three pragmas and says that no macro
  1200. expansion is to be performed on them; whether or not macro
  1201. expansion happens for other pragmas is implementation defined.
  1202. This implementation allows for a mix of both, since GCC did not
  1203. traditionally macro expand its (few) pragmas, whereas OpenMP
  1204. specifies that macro expansion should happen. */
  1205. static void
  1206. do_pragma (cpp_reader *pfile)
  1207. {
  1208. const struct pragma_entry *p = NULL;
  1209. const cpp_token *token, *pragma_token;
  1210. source_location pragma_token_virt_loc = 0;
  1211. cpp_token ns_token;
  1212. unsigned int count = 1;
  1213. pfile->state.prevent_expansion++;
  1214. pragma_token = token = cpp_get_token_with_location (pfile,
  1215. &pragma_token_virt_loc);
  1216. ns_token = *token;
  1217. if (token->type == CPP_NAME)
  1218. {
  1219. p = lookup_pragma_entry (pfile->pragmas, token->val.node.node);
  1220. if (p && p->is_nspace)
  1221. {
  1222. bool allow_name_expansion = p->allow_expansion;
  1223. if (allow_name_expansion)
  1224. pfile->state.prevent_expansion--;
  1225. token = cpp_get_token (pfile);
  1226. if (token->type == CPP_NAME)
  1227. p = lookup_pragma_entry (p->u.space, token->val.node.node);
  1228. else
  1229. p = NULL;
  1230. if (allow_name_expansion)
  1231. pfile->state.prevent_expansion++;
  1232. count = 2;
  1233. }
  1234. }
  1235. if (p)
  1236. {
  1237. if (p->is_deferred)
  1238. {
  1239. pfile->directive_result.src_loc = pragma_token_virt_loc;
  1240. pfile->directive_result.type = CPP_PRAGMA;
  1241. pfile->directive_result.flags = pragma_token->flags;
  1242. pfile->directive_result.val.pragma = p->u.ident;
  1243. pfile->state.in_deferred_pragma = true;
  1244. pfile->state.pragma_allow_expansion = p->allow_expansion;
  1245. if (!p->allow_expansion)
  1246. pfile->state.prevent_expansion++;
  1247. }
  1248. else
  1249. {
  1250. /* Since the handler below doesn't get the line number, that
  1251. it might need for diagnostics, make sure it has the right
  1252. numbers in place. */
  1253. if (pfile->cb.line_change)
  1254. (*pfile->cb.line_change) (pfile, pragma_token, false);
  1255. if (p->allow_expansion)
  1256. pfile->state.prevent_expansion--;
  1257. (*p->u.handler) (pfile);
  1258. if (p->allow_expansion)
  1259. pfile->state.prevent_expansion++;
  1260. }
  1261. }
  1262. else if (pfile->cb.def_pragma)
  1263. {
  1264. if (count == 1 || pfile->context->prev == NULL)
  1265. _cpp_backup_tokens (pfile, count);
  1266. else
  1267. {
  1268. /* Invalid name comes from macro expansion, _cpp_backup_tokens
  1269. won't allow backing 2 tokens. */
  1270. /* ??? The token buffer is leaked. Perhaps if def_pragma hook
  1271. reads both tokens, we could perhaps free it, but if it doesn't,
  1272. we don't know the exact lifespan. */
  1273. cpp_token *toks = XNEWVEC (cpp_token, 2);
  1274. toks[0] = ns_token;
  1275. toks[0].flags |= NO_EXPAND;
  1276. toks[1] = *token;
  1277. toks[1].flags |= NO_EXPAND;
  1278. _cpp_push_token_context (pfile, NULL, toks, 2);
  1279. }
  1280. pfile->cb.def_pragma (pfile, pfile->directive_line);
  1281. }
  1282. pfile->state.prevent_expansion--;
  1283. }
  1284. /* Handle #pragma once. */
  1285. static void
  1286. do_pragma_once (cpp_reader *pfile)
  1287. {
  1288. if (cpp_in_primary_file (pfile))
  1289. cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
  1290. check_eol (pfile, false);
  1291. _cpp_mark_file_once_only (pfile, pfile->buffer->file);
  1292. }
  1293. /* Handle #pragma push_macro(STRING). */
  1294. static void
  1295. do_pragma_push_macro (cpp_reader *pfile)
  1296. {
  1297. cpp_hashnode *node;
  1298. size_t defnlen;
  1299. const uchar *defn = NULL;
  1300. char *macroname, *dest;
  1301. const char *limit, *src;
  1302. const cpp_token *txt;
  1303. struct def_pragma_macro *c;
  1304. txt = get__Pragma_string (pfile);
  1305. if (!txt)
  1306. {
  1307. source_location src_loc = pfile->cur_token[-1].src_loc;
  1308. cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
  1309. "invalid #pragma push_macro directive");
  1310. check_eol (pfile, false);
  1311. skip_rest_of_line (pfile);
  1312. return;
  1313. }
  1314. dest = macroname = (char *) alloca (txt->val.str.len + 2);
  1315. src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
  1316. limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
  1317. while (src < limit)
  1318. {
  1319. /* We know there is a character following the backslash. */
  1320. if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
  1321. src++;
  1322. *dest++ = *src++;
  1323. }
  1324. *dest = 0;
  1325. check_eol (pfile, false);
  1326. skip_rest_of_line (pfile);
  1327. c = XNEW (struct def_pragma_macro);
  1328. memset (c, 0, sizeof (struct def_pragma_macro));
  1329. c->name = XNEWVAR (char, strlen (macroname) + 1);
  1330. strcpy (c->name, macroname);
  1331. c->next = pfile->pushed_macros;
  1332. node = _cpp_lex_identifier (pfile, c->name);
  1333. if (node->type == NT_VOID)
  1334. c->is_undef = 1;
  1335. else
  1336. {
  1337. defn = cpp_macro_definition (pfile, node);
  1338. defnlen = ustrlen (defn);
  1339. c->definition = XNEWVEC (uchar, defnlen + 2);
  1340. c->definition[defnlen] = '\n';
  1341. c->definition[defnlen + 1] = 0;
  1342. c->line = node->value.macro->line;
  1343. c->syshdr = node->value.macro->syshdr;
  1344. c->used = node->value.macro->used;
  1345. memcpy (c->definition, defn, defnlen);
  1346. }
  1347. pfile->pushed_macros = c;
  1348. }
  1349. /* Handle #pragma pop_macro(STRING). */
  1350. static void
  1351. do_pragma_pop_macro (cpp_reader *pfile)
  1352. {
  1353. char *macroname, *dest;
  1354. const char *limit, *src;
  1355. const cpp_token *txt;
  1356. struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
  1357. txt = get__Pragma_string (pfile);
  1358. if (!txt)
  1359. {
  1360. source_location src_loc = pfile->cur_token[-1].src_loc;
  1361. cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
  1362. "invalid #pragma pop_macro directive");
  1363. check_eol (pfile, false);
  1364. skip_rest_of_line (pfile);
  1365. return;
  1366. }
  1367. dest = macroname = (char *) alloca (txt->val.str.len + 2);
  1368. src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
  1369. limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
  1370. while (src < limit)
  1371. {
  1372. /* We know there is a character following the backslash. */
  1373. if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
  1374. src++;
  1375. *dest++ = *src++;
  1376. }
  1377. *dest = 0;
  1378. check_eol (pfile, false);
  1379. skip_rest_of_line (pfile);
  1380. while (c != NULL)
  1381. {
  1382. if (!strcmp (c->name, macroname))
  1383. {
  1384. if (!l)
  1385. pfile->pushed_macros = c->next;
  1386. else
  1387. l->next = c->next;
  1388. cpp_pop_definition (pfile, c);
  1389. free (c->definition);
  1390. free (c->name);
  1391. free (c);
  1392. break;
  1393. }
  1394. l = c;
  1395. c = c->next;
  1396. }
  1397. }
  1398. /* Handle #pragma GCC poison, to poison one or more identifiers so
  1399. that the lexer produces a hard error for each subsequent usage. */
  1400. static void
  1401. do_pragma_poison (cpp_reader *pfile)
  1402. {
  1403. const cpp_token *tok;
  1404. cpp_hashnode *hp;
  1405. pfile->state.poisoned_ok = 1;
  1406. for (;;)
  1407. {
  1408. tok = _cpp_lex_token (pfile);
  1409. if (tok->type == CPP_EOF)
  1410. break;
  1411. if (tok->type != CPP_NAME)
  1412. {
  1413. cpp_error (pfile, CPP_DL_ERROR,
  1414. "invalid #pragma GCC poison directive");
  1415. break;
  1416. }
  1417. hp = tok->val.node.node;
  1418. if (hp->flags & NODE_POISONED)
  1419. continue;
  1420. if (hp->type == NT_MACRO)
  1421. cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
  1422. NODE_NAME (hp));
  1423. _cpp_free_definition (hp);
  1424. hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
  1425. }
  1426. pfile->state.poisoned_ok = 0;
  1427. }
  1428. /* Mark the current header as a system header. This will suppress
  1429. some categories of warnings (notably those from -pedantic). It is
  1430. intended for use in system libraries that cannot be implemented in
  1431. conforming C, but cannot be certain that their headers appear in a
  1432. system include directory. To prevent abuse, it is rejected in the
  1433. primary source file. */
  1434. static void
  1435. do_pragma_system_header (cpp_reader *pfile)
  1436. {
  1437. if (cpp_in_primary_file (pfile))
  1438. cpp_error (pfile, CPP_DL_WARNING,
  1439. "#pragma system_header ignored outside include file");
  1440. else
  1441. {
  1442. check_eol (pfile, false);
  1443. skip_rest_of_line (pfile);
  1444. cpp_make_system_header (pfile, 1, 0);
  1445. }
  1446. }
  1447. /* Check the modified date of the current include file against a specified
  1448. file. Issue a diagnostic, if the specified file is newer. We use this to
  1449. determine if a fixed header should be refixed. */
  1450. static void
  1451. do_pragma_dependency (cpp_reader *pfile)
  1452. {
  1453. const char *fname;
  1454. int angle_brackets, ordering;
  1455. source_location location;
  1456. fname = parse_include (pfile, &angle_brackets, NULL, &location);
  1457. if (!fname)
  1458. return;
  1459. ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
  1460. if (ordering < 0)
  1461. cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
  1462. else if (ordering > 0)
  1463. {
  1464. cpp_error (pfile, CPP_DL_WARNING,
  1465. "current file is older than %s", fname);
  1466. if (cpp_get_token (pfile)->type != CPP_EOF)
  1467. {
  1468. _cpp_backup_tokens (pfile, 1);
  1469. do_diagnostic (pfile, CPP_DL_WARNING, 0, 0);
  1470. }
  1471. }
  1472. free ((void *) fname);
  1473. }
  1474. /* Issue a diagnostic with the message taken from the pragma. If
  1475. ERROR is true, the diagnostic is a warning, otherwise, it is an
  1476. error. */
  1477. static void
  1478. do_pragma_warning_or_error (cpp_reader *pfile, bool error)
  1479. {
  1480. const cpp_token *tok = _cpp_lex_token (pfile);
  1481. cpp_string str;
  1482. if (tok->type != CPP_STRING
  1483. || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str,
  1484. CPP_STRING)
  1485. || str.len == 0)
  1486. {
  1487. cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive",
  1488. error ? "error" : "warning");
  1489. return;
  1490. }
  1491. cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING,
  1492. "%s", str.text);
  1493. free ((void *)str.text);
  1494. }
  1495. /* Issue a warning diagnostic. */
  1496. static void
  1497. do_pragma_warning (cpp_reader *pfile)
  1498. {
  1499. do_pragma_warning_or_error (pfile, false);
  1500. }
  1501. /* Issue an error diagnostic. */
  1502. static void
  1503. do_pragma_error (cpp_reader *pfile)
  1504. {
  1505. do_pragma_warning_or_error (pfile, true);
  1506. }
  1507. /* Get a token but skip padding. */
  1508. static const cpp_token *
  1509. get_token_no_padding (cpp_reader *pfile)
  1510. {
  1511. for (;;)
  1512. {
  1513. const cpp_token *result = cpp_get_token (pfile);
  1514. if (result->type != CPP_PADDING)
  1515. return result;
  1516. }
  1517. }
  1518. /* Check syntax is "(string-literal)". Returns the string on success,
  1519. or NULL on failure. */
  1520. static const cpp_token *
  1521. get__Pragma_string (cpp_reader *pfile)
  1522. {
  1523. const cpp_token *string;
  1524. const cpp_token *paren;
  1525. paren = get_token_no_padding (pfile);
  1526. if (paren->type == CPP_EOF)
  1527. _cpp_backup_tokens (pfile, 1);
  1528. if (paren->type != CPP_OPEN_PAREN)
  1529. return NULL;
  1530. string = get_token_no_padding (pfile);
  1531. if (string->type == CPP_EOF)
  1532. _cpp_backup_tokens (pfile, 1);
  1533. if (string->type != CPP_STRING && string->type != CPP_WSTRING
  1534. && string->type != CPP_STRING32 && string->type != CPP_STRING16
  1535. && string->type != CPP_UTF8STRING)
  1536. return NULL;
  1537. paren = get_token_no_padding (pfile);
  1538. if (paren->type == CPP_EOF)
  1539. _cpp_backup_tokens (pfile, 1);
  1540. if (paren->type != CPP_CLOSE_PAREN)
  1541. return NULL;
  1542. return string;
  1543. }
  1544. /* Destringize IN into a temporary buffer, by removing the first \ of
  1545. \" and \\ sequences, and process the result as a #pragma directive. */
  1546. static void
  1547. destringize_and_run (cpp_reader *pfile, const cpp_string *in)
  1548. {
  1549. const unsigned char *src, *limit;
  1550. char *dest, *result;
  1551. cpp_context *saved_context;
  1552. cpp_token *saved_cur_token;
  1553. tokenrun *saved_cur_run;
  1554. cpp_token *toks;
  1555. int count;
  1556. const struct directive *save_directive;
  1557. dest = result = (char *) alloca (in->len - 1);
  1558. src = in->text + 1 + (in->text[0] == 'L');
  1559. limit = in->text + in->len - 1;
  1560. while (src < limit)
  1561. {
  1562. /* We know there is a character following the backslash. */
  1563. if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
  1564. src++;
  1565. *dest++ = *src++;
  1566. }
  1567. *dest = '\n';
  1568. /* Ugh; an awful kludge. We are really not set up to be lexing
  1569. tokens when in the middle of a macro expansion. Use a new
  1570. context to force cpp_get_token to lex, and so skip_rest_of_line
  1571. doesn't go beyond the end of the text. Also, remember the
  1572. current lexing position so we can return to it later.
  1573. Something like line-at-a-time lexing should remove the need for
  1574. this. */
  1575. saved_context = pfile->context;
  1576. saved_cur_token = pfile->cur_token;
  1577. saved_cur_run = pfile->cur_run;
  1578. pfile->context = XCNEW (cpp_context);
  1579. /* Inline run_directive, since we need to delay the _cpp_pop_buffer
  1580. until we've read all of the tokens that we want. */
  1581. cpp_push_buffer (pfile, (const uchar *) result, dest - result,
  1582. /* from_stage3 */ true);
  1583. /* ??? Antique Disgusting Hack. What does this do? */
  1584. if (pfile->buffer->prev)
  1585. pfile->buffer->file = pfile->buffer->prev->file;
  1586. start_directive (pfile);
  1587. _cpp_clean_line (pfile);
  1588. save_directive = pfile->directive;
  1589. pfile->directive = &dtable[T_PRAGMA];
  1590. do_pragma (pfile);
  1591. end_directive (pfile, 1);
  1592. pfile->directive = save_directive;
  1593. /* We always insert at least one token, the directive result. It'll
  1594. either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
  1595. need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
  1596. /* If we're not handling the pragma internally, read all of the tokens from
  1597. the string buffer now, while the string buffer is still installed. */
  1598. /* ??? Note that the token buffer allocated here is leaked. It's not clear
  1599. to me what the true lifespan of the tokens are. It would appear that
  1600. the lifespan is the entire parse of the main input stream, in which case
  1601. this may not be wrong. */
  1602. if (pfile->directive_result.type == CPP_PRAGMA)
  1603. {
  1604. int maxcount;
  1605. count = 1;
  1606. maxcount = 50;
  1607. toks = XNEWVEC (cpp_token, maxcount);
  1608. toks[0] = pfile->directive_result;
  1609. do
  1610. {
  1611. if (count == maxcount)
  1612. {
  1613. maxcount = maxcount * 3 / 2;
  1614. toks = XRESIZEVEC (cpp_token, toks, maxcount);
  1615. }
  1616. toks[count] = *cpp_get_token (pfile);
  1617. /* Macros have been already expanded by cpp_get_token
  1618. if the pragma allowed expansion. */
  1619. toks[count++].flags |= NO_EXPAND;
  1620. }
  1621. while (toks[count-1].type != CPP_PRAGMA_EOL);
  1622. }
  1623. else
  1624. {
  1625. count = 1;
  1626. toks = XNEW (cpp_token);
  1627. toks[0] = pfile->directive_result;
  1628. /* If we handled the entire pragma internally, make sure we get the
  1629. line number correct for the next token. */
  1630. if (pfile->cb.line_change)
  1631. pfile->cb.line_change (pfile, pfile->cur_token, false);
  1632. }
  1633. /* Finish inlining run_directive. */
  1634. pfile->buffer->file = NULL;
  1635. _cpp_pop_buffer (pfile);
  1636. /* Reset the old macro state before ... */
  1637. XDELETE (pfile->context);
  1638. pfile->context = saved_context;
  1639. pfile->cur_token = saved_cur_token;
  1640. pfile->cur_run = saved_cur_run;
  1641. /* ... inserting the new tokens we collected. */
  1642. _cpp_push_token_context (pfile, NULL, toks, count);
  1643. }
  1644. /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
  1645. int
  1646. _cpp_do__Pragma (cpp_reader *pfile)
  1647. {
  1648. const cpp_token *string = get__Pragma_string (pfile);
  1649. pfile->directive_result.type = CPP_PADDING;
  1650. if (string)
  1651. {
  1652. destringize_and_run (pfile, &string->val.str);
  1653. return 1;
  1654. }
  1655. cpp_error (pfile, CPP_DL_ERROR,
  1656. "_Pragma takes a parenthesized string literal");
  1657. return 0;
  1658. }
  1659. /* Handle #ifdef. */
  1660. static void
  1661. do_ifdef (cpp_reader *pfile)
  1662. {
  1663. int skip = 1;
  1664. if (! pfile->state.skipping)
  1665. {
  1666. cpp_hashnode *node = lex_macro_node (pfile, false);
  1667. if (node)
  1668. {
  1669. /* Do not treat conditional macros as being defined. This is due to
  1670. the powerpc and spu ports using conditional macros for 'vector',
  1671. 'bool', and 'pixel' to act as conditional keywords. This messes
  1672. up tests like #ifndef bool. */
  1673. skip = (node->type != NT_MACRO
  1674. || ((node->flags & NODE_CONDITIONAL) != 0));
  1675. _cpp_mark_macro_used (node);
  1676. if (!(node->flags & NODE_USED))
  1677. {
  1678. node->flags |= NODE_USED;
  1679. if (node->type == NT_MACRO)
  1680. {
  1681. if ((node->flags & NODE_BUILTIN)
  1682. && pfile->cb.user_builtin_macro)
  1683. pfile->cb.user_builtin_macro (pfile, node);
  1684. if (pfile->cb.used_define)
  1685. pfile->cb.used_define (pfile, pfile->directive_line, node);
  1686. }
  1687. else
  1688. {
  1689. if (pfile->cb.used_undef)
  1690. pfile->cb.used_undef (pfile, pfile->directive_line, node);
  1691. }
  1692. }
  1693. if (pfile->cb.used)
  1694. pfile->cb.used (pfile, pfile->directive_line, node);
  1695. check_eol (pfile, false);
  1696. }
  1697. }
  1698. push_conditional (pfile, skip, T_IFDEF, 0);
  1699. }
  1700. /* Handle #ifndef. */
  1701. static void
  1702. do_ifndef (cpp_reader *pfile)
  1703. {
  1704. int skip = 1;
  1705. cpp_hashnode *node = 0;
  1706. if (! pfile->state.skipping)
  1707. {
  1708. node = lex_macro_node (pfile, false);
  1709. if (node)
  1710. {
  1711. /* Do not treat conditional macros as being defined. This is due to
  1712. the powerpc and spu ports using conditional macros for 'vector',
  1713. 'bool', and 'pixel' to act as conditional keywords. This messes
  1714. up tests like #ifndef bool. */
  1715. skip = (node->type == NT_MACRO
  1716. && ((node->flags & NODE_CONDITIONAL) == 0));
  1717. _cpp_mark_macro_used (node);
  1718. if (!(node->flags & NODE_USED))
  1719. {
  1720. node->flags |= NODE_USED;
  1721. if (node->type == NT_MACRO)
  1722. {
  1723. if ((node->flags & NODE_BUILTIN)
  1724. && pfile->cb.user_builtin_macro)
  1725. pfile->cb.user_builtin_macro (pfile, node);
  1726. if (pfile->cb.used_define)
  1727. pfile->cb.used_define (pfile, pfile->directive_line, node);
  1728. }
  1729. else
  1730. {
  1731. if (pfile->cb.used_undef)
  1732. pfile->cb.used_undef (pfile, pfile->directive_line, node);
  1733. }
  1734. }
  1735. if (pfile->cb.used)
  1736. pfile->cb.used (pfile, pfile->directive_line, node);
  1737. check_eol (pfile, false);
  1738. }
  1739. }
  1740. push_conditional (pfile, skip, T_IFNDEF, node);
  1741. }
  1742. /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
  1743. pfile->mi_ind_cmacro so we can handle multiple-include
  1744. optimizations. If macro expansion occurs in the expression, we
  1745. cannot treat it as a controlling conditional, since the expansion
  1746. could change in the future. That is handled by cpp_get_token. */
  1747. static void
  1748. do_if (cpp_reader *pfile)
  1749. {
  1750. int skip = 1;
  1751. if (! pfile->state.skipping)
  1752. skip = _cpp_parse_expr (pfile, true) == false;
  1753. push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
  1754. }
  1755. /* Flip skipping state if appropriate and continue without changing
  1756. if_stack; this is so that the error message for missing #endif's
  1757. etc. will point to the original #if. */
  1758. static void
  1759. do_else (cpp_reader *pfile)
  1760. {
  1761. cpp_buffer *buffer = pfile->buffer;
  1762. struct if_stack *ifs = buffer->if_stack;
  1763. if (ifs == NULL)
  1764. cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
  1765. else
  1766. {
  1767. if (ifs->type == T_ELSE)
  1768. {
  1769. cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
  1770. cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
  1771. "the conditional began here");
  1772. }
  1773. ifs->type = T_ELSE;
  1774. /* Skip any future (erroneous) #elses or #elifs. */
  1775. pfile->state.skipping = ifs->skip_elses;
  1776. ifs->skip_elses = true;
  1777. /* Invalidate any controlling macro. */
  1778. ifs->mi_cmacro = 0;
  1779. /* Only check EOL if was not originally skipping. */
  1780. if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
  1781. check_eol_endif_labels (pfile);
  1782. }
  1783. }
  1784. /* Handle a #elif directive by not changing if_stack either. See the
  1785. comment above do_else. */
  1786. static void
  1787. do_elif (cpp_reader *pfile)
  1788. {
  1789. cpp_buffer *buffer = pfile->buffer;
  1790. struct if_stack *ifs = buffer->if_stack;
  1791. if (ifs == NULL)
  1792. cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
  1793. else
  1794. {
  1795. if (ifs->type == T_ELSE)
  1796. {
  1797. cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
  1798. cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
  1799. "the conditional began here");
  1800. }
  1801. ifs->type = T_ELIF;
  1802. /* See DR#412: "Only the first group whose control condition
  1803. evaluates to true (nonzero) is processed; any following groups
  1804. are skipped and their controlling directives are processed as
  1805. if they were in a group that is skipped." */
  1806. if (ifs->skip_elses)
  1807. pfile->state.skipping = 1;
  1808. else
  1809. {
  1810. pfile->state.skipping = ! _cpp_parse_expr (pfile, false);
  1811. ifs->skip_elses = ! pfile->state.skipping;
  1812. }
  1813. /* Invalidate any controlling macro. */
  1814. ifs->mi_cmacro = 0;
  1815. }
  1816. }
  1817. /* #endif pops the if stack and resets pfile->state.skipping. */
  1818. static void
  1819. do_endif (cpp_reader *pfile)
  1820. {
  1821. cpp_buffer *buffer = pfile->buffer;
  1822. struct if_stack *ifs = buffer->if_stack;
  1823. if (ifs == NULL)
  1824. cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
  1825. else
  1826. {
  1827. /* Only check EOL if was not originally skipping. */
  1828. if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
  1829. check_eol_endif_labels (pfile);
  1830. /* If potential control macro, we go back outside again. */
  1831. if (ifs->next == 0 && ifs->mi_cmacro)
  1832. {
  1833. pfile->mi_valid = true;
  1834. pfile->mi_cmacro = ifs->mi_cmacro;
  1835. }
  1836. buffer->if_stack = ifs->next;
  1837. pfile->state.skipping = ifs->was_skipping;
  1838. obstack_free (&pfile->buffer_ob, ifs);
  1839. }
  1840. }
  1841. /* Push an if_stack entry for a preprocessor conditional, and set
  1842. pfile->state.skipping to SKIP. If TYPE indicates the conditional
  1843. is #if or #ifndef, CMACRO is a potentially controlling macro, and
  1844. we need to check here that we are at the top of the file. */
  1845. static void
  1846. push_conditional (cpp_reader *pfile, int skip, int type,
  1847. const cpp_hashnode *cmacro)
  1848. {
  1849. struct if_stack *ifs;
  1850. cpp_buffer *buffer = pfile->buffer;
  1851. ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
  1852. ifs->line = pfile->directive_line;
  1853. ifs->next = buffer->if_stack;
  1854. ifs->skip_elses = pfile->state.skipping || !skip;
  1855. ifs->was_skipping = pfile->state.skipping;
  1856. ifs->type = type;
  1857. /* This condition is effectively a test for top-of-file. */
  1858. if (pfile->mi_valid && pfile->mi_cmacro == 0)
  1859. ifs->mi_cmacro = cmacro;
  1860. else
  1861. ifs->mi_cmacro = 0;
  1862. pfile->state.skipping = skip;
  1863. buffer->if_stack = ifs;
  1864. }
  1865. /* Read the tokens of the answer into the macro pool, in a directive
  1866. of type TYPE. Only commit the memory if we intend it as permanent
  1867. storage, i.e. the #assert case. Returns 0 on success, and sets
  1868. ANSWERP to point to the answer. PRED_LOC is the location of the
  1869. predicate. */
  1870. static int
  1871. parse_answer (cpp_reader *pfile, struct answer **answerp, int type,
  1872. source_location pred_loc)
  1873. {
  1874. const cpp_token *paren;
  1875. struct answer *answer;
  1876. unsigned int acount;
  1877. /* In a conditional, it is legal to not have an open paren. We
  1878. should save the following token in this case. */
  1879. paren = cpp_get_token (pfile);
  1880. /* If not a paren, see if we're OK. */
  1881. if (paren->type != CPP_OPEN_PAREN)
  1882. {
  1883. /* In a conditional no answer is a test for any answer. It
  1884. could be followed by any token. */
  1885. if (type == T_IF)
  1886. {
  1887. _cpp_backup_tokens (pfile, 1);
  1888. return 0;
  1889. }
  1890. /* #unassert with no answer is valid - it removes all answers. */
  1891. if (type == T_UNASSERT && paren->type == CPP_EOF)
  1892. return 0;
  1893. cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0,
  1894. "missing '(' after predicate");
  1895. return 1;
  1896. }
  1897. for (acount = 0;; acount++)
  1898. {
  1899. size_t room_needed;
  1900. const cpp_token *token = cpp_get_token (pfile);
  1901. cpp_token *dest;
  1902. if (token->type == CPP_CLOSE_PAREN)
  1903. break;
  1904. if (token->type == CPP_EOF)
  1905. {
  1906. cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
  1907. return 1;
  1908. }
  1909. /* struct answer includes the space for one token. */
  1910. room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
  1911. if (BUFF_ROOM (pfile->a_buff) < room_needed)
  1912. _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
  1913. dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
  1914. *dest = *token;
  1915. /* Drop whitespace at start, for answer equivalence purposes. */
  1916. if (acount == 0)
  1917. dest->flags &= ~PREV_WHITE;
  1918. }
  1919. if (acount == 0)
  1920. {
  1921. cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
  1922. return 1;
  1923. }
  1924. answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
  1925. answer->count = acount;
  1926. answer->next = NULL;
  1927. *answerp = answer;
  1928. return 0;
  1929. }
  1930. /* Parses an assertion directive of type TYPE, returning a pointer to
  1931. the hash node of the predicate, or 0 on error. If an answer was
  1932. supplied, it is placed in ANSWERP, otherwise it is set to 0. */
  1933. static cpp_hashnode *
  1934. parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
  1935. {
  1936. cpp_hashnode *result = 0;
  1937. const cpp_token *predicate;
  1938. /* We don't expand predicates or answers. */
  1939. pfile->state.prevent_expansion++;
  1940. *answerp = 0;
  1941. predicate = cpp_get_token (pfile);
  1942. if (predicate->type == CPP_EOF)
  1943. cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
  1944. else if (predicate->type != CPP_NAME)
  1945. cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0,
  1946. "predicate must be an identifier");
  1947. else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0)
  1948. {
  1949. unsigned int len = NODE_LEN (predicate->val.node.node);
  1950. unsigned char *sym = (unsigned char *) alloca (len + 1);
  1951. /* Prefix '#' to get it out of macro namespace. */
  1952. sym[0] = '#';
  1953. memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len);
  1954. result = cpp_lookup (pfile, sym, len + 1);
  1955. }
  1956. pfile->state.prevent_expansion--;
  1957. return result;
  1958. }
  1959. /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
  1960. or a pointer to NULL if the answer is not in the chain. */
  1961. static struct answer **
  1962. find_answer (cpp_hashnode *node, const struct answer *candidate)
  1963. {
  1964. unsigned int i;
  1965. struct answer **result;
  1966. for (result = &node->value.answers; *result; result = &(*result)->next)
  1967. {
  1968. struct answer *answer = *result;
  1969. if (answer->count == candidate->count)
  1970. {
  1971. for (i = 0; i < answer->count; i++)
  1972. if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
  1973. break;
  1974. if (i == answer->count)
  1975. break;
  1976. }
  1977. }
  1978. return result;
  1979. }
  1980. /* Test an assertion within a preprocessor conditional. Returns
  1981. nonzero on failure, zero on success. On success, the result of
  1982. the test is written into VALUE, otherwise the value 0. */
  1983. int
  1984. _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
  1985. {
  1986. struct answer *answer;
  1987. cpp_hashnode *node;
  1988. node = parse_assertion (pfile, &answer, T_IF);
  1989. /* For recovery, an erroneous assertion expression is handled as a
  1990. failing assertion. */
  1991. *value = 0;
  1992. if (node)
  1993. *value = (node->type == NT_ASSERTION &&
  1994. (answer == 0 || *find_answer (node, answer) != 0));
  1995. else if (pfile->cur_token[-1].type == CPP_EOF)
  1996. _cpp_backup_tokens (pfile, 1);
  1997. /* We don't commit the memory for the answer - it's temporary only. */
  1998. return node == 0;
  1999. }
  2000. /* Handle #assert. */
  2001. static void
  2002. do_assert (cpp_reader *pfile)
  2003. {
  2004. struct answer *new_answer;
  2005. cpp_hashnode *node;
  2006. node = parse_assertion (pfile, &new_answer, T_ASSERT);
  2007. if (node)
  2008. {
  2009. size_t answer_size;
  2010. /* Place the new answer in the answer list. First check there
  2011. is not a duplicate. */
  2012. new_answer->next = 0;
  2013. if (node->type == NT_ASSERTION)
  2014. {
  2015. if (*find_answer (node, new_answer))
  2016. {
  2017. cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
  2018. NODE_NAME (node) + 1);
  2019. return;
  2020. }
  2021. new_answer->next = node->value.answers;
  2022. }
  2023. answer_size = sizeof (struct answer) + ((new_answer->count - 1)
  2024. * sizeof (cpp_token));
  2025. /* Commit or allocate storage for the object. */
  2026. if (pfile->hash_table->alloc_subobject)
  2027. {
  2028. struct answer *temp_answer = new_answer;
  2029. new_answer = (struct answer *) pfile->hash_table->alloc_subobject
  2030. (answer_size);
  2031. memcpy (new_answer, temp_answer, answer_size);
  2032. }
  2033. else
  2034. BUFF_FRONT (pfile->a_buff) += answer_size;
  2035. node->type = NT_ASSERTION;
  2036. node->value.answers = new_answer;
  2037. check_eol (pfile, false);
  2038. }
  2039. }
  2040. /* Handle #unassert. */
  2041. static void
  2042. do_unassert (cpp_reader *pfile)
  2043. {
  2044. cpp_hashnode *node;
  2045. struct answer *answer;
  2046. node = parse_assertion (pfile, &answer, T_UNASSERT);
  2047. /* It isn't an error to #unassert something that isn't asserted. */
  2048. if (node && node->type == NT_ASSERTION)
  2049. {
  2050. if (answer)
  2051. {
  2052. struct answer **p = find_answer (node, answer), *temp;
  2053. /* Remove the answer from the list. */
  2054. temp = *p;
  2055. if (temp)
  2056. *p = temp->next;
  2057. /* Did we free the last answer? */
  2058. if (node->value.answers == 0)
  2059. node->type = NT_VOID;
  2060. check_eol (pfile, false);
  2061. }
  2062. else
  2063. _cpp_free_definition (node);
  2064. }
  2065. /* We don't commit the memory for the answer - it's temporary only. */
  2066. }
  2067. /* These are for -D, -U, -A. */
  2068. /* Process the string STR as if it appeared as the body of a #define.
  2069. If STR is just an identifier, define it with value 1.
  2070. If STR has anything after the identifier, then it should
  2071. be identifier=definition. */
  2072. void
  2073. cpp_define (cpp_reader *pfile, const char *str)
  2074. {
  2075. char *buf;
  2076. const char *p;
  2077. size_t count;
  2078. /* Copy the entire option so we can modify it.
  2079. Change the first "=" in the string to a space. If there is none,
  2080. tack " 1" on the end. */
  2081. count = strlen (str);
  2082. buf = (char *) alloca (count + 3);
  2083. memcpy (buf, str, count);
  2084. p = strchr (str, '=');
  2085. if (p)
  2086. buf[p - str] = ' ';
  2087. else
  2088. {
  2089. buf[count++] = ' ';
  2090. buf[count++] = '1';
  2091. }
  2092. buf[count] = '\n';
  2093. run_directive (pfile, T_DEFINE, buf, count);
  2094. }
  2095. /* Use to build macros to be run through cpp_define() as
  2096. described above.
  2097. Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
  2098. void
  2099. cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
  2100. {
  2101. char *ptr;
  2102. va_list ap;
  2103. va_start (ap, fmt);
  2104. ptr = xvasprintf (fmt, ap);
  2105. va_end (ap);
  2106. cpp_define (pfile, ptr);
  2107. free (ptr);
  2108. }
  2109. /* Slight variant of the above for use by initialize_builtins. */
  2110. void
  2111. _cpp_define_builtin (cpp_reader *pfile, const char *str)
  2112. {
  2113. size_t len = strlen (str);
  2114. char *buf = (char *) alloca (len + 1);
  2115. memcpy (buf, str, len);
  2116. buf[len] = '\n';
  2117. run_directive (pfile, T_DEFINE, buf, len);
  2118. }
  2119. /* Process MACRO as if it appeared as the body of an #undef. */
  2120. void
  2121. cpp_undef (cpp_reader *pfile, const char *macro)
  2122. {
  2123. size_t len = strlen (macro);
  2124. char *buf = (char *) alloca (len + 1);
  2125. memcpy (buf, macro, len);
  2126. buf[len] = '\n';
  2127. run_directive (pfile, T_UNDEF, buf, len);
  2128. }
  2129. /* Replace a previous definition DEF of the macro STR. If DEF is NULL,
  2130. or first element is zero, then the macro should be undefined. */
  2131. static void
  2132. cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
  2133. {
  2134. cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name);
  2135. if (node == NULL)
  2136. return;
  2137. if (pfile->cb.before_define)
  2138. pfile->cb.before_define (pfile);
  2139. if (node->type == NT_MACRO)
  2140. {
  2141. if (pfile->cb.undef)
  2142. pfile->cb.undef (pfile, pfile->directive_line, node);
  2143. if (CPP_OPTION (pfile, warn_unused_macros))
  2144. _cpp_warn_if_unused_macro (pfile, node, NULL);
  2145. }
  2146. if (node->type != NT_VOID)
  2147. _cpp_free_definition (node);
  2148. if (c->is_undef)
  2149. return;
  2150. {
  2151. size_t namelen;
  2152. const uchar *dn;
  2153. cpp_hashnode *h = NULL;
  2154. cpp_buffer *nbuf;
  2155. namelen = ustrcspn (c->definition, "( \n");
  2156. h = cpp_lookup (pfile, c->definition, namelen);
  2157. dn = c->definition + namelen;
  2158. h->type = NT_VOID;
  2159. h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
  2160. nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true);
  2161. if (nbuf != NULL)
  2162. {
  2163. _cpp_clean_line (pfile);
  2164. nbuf->sysp = 1;
  2165. if (!_cpp_create_definition (pfile, h))
  2166. abort ();
  2167. _cpp_pop_buffer (pfile);
  2168. }
  2169. else
  2170. abort ();
  2171. h->value.macro->line = c->line;
  2172. h->value.macro->syshdr = c->syshdr;
  2173. h->value.macro->used = c->used;
  2174. }
  2175. }
  2176. /* Process the string STR as if it appeared as the body of a #assert. */
  2177. void
  2178. cpp_assert (cpp_reader *pfile, const char *str)
  2179. {
  2180. handle_assertion (pfile, str, T_ASSERT);
  2181. }
  2182. /* Process STR as if it appeared as the body of an #unassert. */
  2183. void
  2184. cpp_unassert (cpp_reader *pfile, const char *str)
  2185. {
  2186. handle_assertion (pfile, str, T_UNASSERT);
  2187. }
  2188. /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
  2189. static void
  2190. handle_assertion (cpp_reader *pfile, const char *str, int type)
  2191. {
  2192. size_t count = strlen (str);
  2193. const char *p = strchr (str, '=');
  2194. /* Copy the entire option so we can modify it. Change the first
  2195. "=" in the string to a '(', and tack a ')' on the end. */
  2196. char *buf = (char *) alloca (count + 2);
  2197. memcpy (buf, str, count);
  2198. if (p)
  2199. {
  2200. buf[p - str] = '(';
  2201. buf[count++] = ')';
  2202. }
  2203. buf[count] = '\n';
  2204. str = buf;
  2205. run_directive (pfile, type, str, count);
  2206. }
  2207. /* The options structure. */
  2208. cpp_options *
  2209. cpp_get_options (cpp_reader *pfile)
  2210. {
  2211. return &pfile->opts;
  2212. }
  2213. /* The callbacks structure. */
  2214. cpp_callbacks *
  2215. cpp_get_callbacks (cpp_reader *pfile)
  2216. {
  2217. return &pfile->cb;
  2218. }
  2219. /* Copy the given callbacks structure to our own. */
  2220. void
  2221. cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
  2222. {
  2223. pfile->cb = *cb;
  2224. }
  2225. /* The dependencies structure. (Creates one if it hasn't already been.) */
  2226. struct deps *
  2227. cpp_get_deps (cpp_reader *pfile)
  2228. {
  2229. if (!pfile->deps)
  2230. pfile->deps = deps_init ();
  2231. return pfile->deps;
  2232. }
  2233. /* Push a new buffer on the buffer stack. Returns the new buffer; it
  2234. doesn't fail. It does not generate a file change call back; that
  2235. is the responsibility of the caller. */
  2236. cpp_buffer *
  2237. cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
  2238. int from_stage3)
  2239. {
  2240. cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
  2241. /* Clears, amongst other things, if_stack and mi_cmacro. */
  2242. memset (new_buffer, 0, sizeof (cpp_buffer));
  2243. new_buffer->next_line = new_buffer->buf = buffer;
  2244. new_buffer->rlimit = buffer + len;
  2245. new_buffer->from_stage3 = from_stage3;
  2246. new_buffer->prev = pfile->buffer;
  2247. new_buffer->need_line = true;
  2248. pfile->buffer = new_buffer;
  2249. return new_buffer;
  2250. }
  2251. /* Pops a single buffer, with a file change call-back if appropriate.
  2252. Then pushes the next -include file, if any remain. */
  2253. void
  2254. _cpp_pop_buffer (cpp_reader *pfile)
  2255. {
  2256. cpp_buffer *buffer = pfile->buffer;
  2257. struct _cpp_file *inc = buffer->file;
  2258. struct if_stack *ifs;
  2259. const unsigned char *to_free;
  2260. /* Walk back up the conditional stack till we reach its level at
  2261. entry to this file, issuing error messages. */
  2262. for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
  2263. cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
  2264. "unterminated #%s", dtable[ifs->type].name);
  2265. /* In case of a missing #endif. */
  2266. pfile->state.skipping = 0;
  2267. /* _cpp_do_file_change expects pfile->buffer to be the new one. */
  2268. pfile->buffer = buffer->prev;
  2269. to_free = buffer->to_free;
  2270. free (buffer->notes);
  2271. /* Free the buffer object now; we may want to push a new buffer
  2272. in _cpp_push_next_include_file. */
  2273. obstack_free (&pfile->buffer_ob, buffer);
  2274. if (inc)
  2275. {
  2276. _cpp_pop_file_buffer (pfile, inc, to_free);
  2277. _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
  2278. }
  2279. }
  2280. /* Enter all recognized directives in the hash table. */
  2281. void
  2282. _cpp_init_directives (cpp_reader *pfile)
  2283. {
  2284. unsigned int i;
  2285. cpp_hashnode *node;
  2286. for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
  2287. {
  2288. node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
  2289. node->is_directive = 1;
  2290. node->directive_index = i;
  2291. }
  2292. }
  2293. /* Extract header file from a bracket include. Parsing starts after '<'.
  2294. The string is malloced and must be freed by the caller. */
  2295. char *
  2296. _cpp_bracket_include(cpp_reader *pfile)
  2297. {
  2298. return glue_header_name (pfile);
  2299. }