directive.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. /*-
  2. * Copyright (c) 1998, 2002-2008 Kiyoshi Matsui <kmatsui@t3.rim.or.jp>
  3. * All rights reserved.
  4. *
  5. * Some parts of this code are derived from the public domain software
  6. * DECUS cpp (1984,1985) written by Martin Minow.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * D I R E C T I V E . C
  31. * P r o c e s s D i r e c t i v e L i n e s
  32. *
  33. * The routines to handle directives other than #include and #pragma
  34. * are placed here.
  35. */
  36. #if PREPROCESSED
  37. #include "mcpp.H"
  38. #else
  39. #include "system.H"
  40. #include "internal.H"
  41. #endif
  42. static int do_if( int hash, const char * directive_name);
  43. /* #if, #elif, #ifdef, #ifndef */
  44. static void sync_linenum( void);
  45. /* Synchronize number of newlines */
  46. static long do_line( void);
  47. /* Process #line directive */
  48. static int get_parm( void);
  49. /* Get parameters of macro, its nargs, names, lengths */
  50. static int get_repl( const char * macroname);
  51. /* Get replacement text embedding parameter number */
  52. static char * is_formal( const char * name, int conv);
  53. /* If formal parameter, save the number */
  54. static char * def_stringization( char * repl_cur);
  55. /* Define stringization */
  56. static char * mgtoken_save( const char * macroname);
  57. /* Prefix DEF_MAGIC to macro name in repl-text */
  58. static char * str_parm_scan( char * string_end);
  59. /* Scan the parameter in quote */
  60. static void do_undef( void);
  61. /* Process #undef directive */
  62. static void dump_repl( const DEFBUF * dp, FILE * fp, int gcc2_va);
  63. /* Dump replacement text */
  64. /*
  65. * Generate (by hand-inspection) a set of unique values for each directive.
  66. * MCPP won't compile if there are hash conflicts.
  67. */
  68. #define L_if ('i' ^ (EOS << 1))
  69. #define L_ifdef ('i' ^ ('d' << 1))
  70. #define L_ifndef ('i' ^ ('n' << 1))
  71. #define L_elif ('e' ^ ('i' << 1))
  72. #define L_else ('e' ^ ('s' << 1))
  73. #define L_endif ('e' ^ ('d' << 1))
  74. #define L_define ('d' ^ ('f' << 1))
  75. #define L_undef ('u' ^ ('d' << 1))
  76. #define L_line ('l' ^ ('n' << 1))
  77. #define L_include ('i' ^ ('c' << 1))
  78. #if COMPILER == GNUC
  79. #define L_include_next ('i' ^ ('c' << 1) ^ ('_' << 1))
  80. #endif
  81. #if SYSTEM == SYS_MAC
  82. #define L_import ('i' ^ ('p' << 1))
  83. #endif
  84. #define L_error ('e' ^ ('r' << 1))
  85. #define L_pragma ('p' ^ ('a' << 1))
  86. static const char * const not_ident
  87. = "Not an identifier \"%s\""; /* _E_ */
  88. static const char * const no_arg = "No argument"; /* _E_ */
  89. static const char * const excess
  90. = "Excessive token sequence \"%s\""; /* _E_ _W1_ */
  91. void directive( void)
  92. /*
  93. * Process #directive lines. Each directive have their own subroutines.
  94. */
  95. {
  96. const char * const many_nesting =
  97. "More than %.0s%ld nesting of #if (#ifdef) sections%s"; /* _F_ _W4_ _W8_ */
  98. const char * const not_in_section
  99. = "Not in a #if (#ifdef) section in a source file"; /* _E_ _W1_ */
  100. const char * const illeg_dir
  101. = "Illegal #directive \"%s%.0ld%s\""; /* _E_ _W1_ _W8_ */
  102. const char * const in_skipped = " (in skipped block)"; /* _W8_ */
  103. FILEINFO * file;
  104. int token_type;
  105. int hash;
  106. int c;
  107. char * tp;
  108. in_directive = TRUE;
  109. if (keep_comments) {
  110. mcpp_fputc( '\n', OUT); /* Possibly flush out comments */
  111. newlines--;
  112. }
  113. c = skip_ws();
  114. if (c == '\n') /* 'null' directive */
  115. goto ret;
  116. token_type = scan_token( c, (workp = work_buf, &workp), work_end);
  117. if (in_asm && (token_type != NAM
  118. || (! str_eq( identifier, "asm")
  119. && ! str_eq( identifier, "endasm"))))
  120. /* In #asm block, ignore #anything other than #asm or #endasm */
  121. goto skip_line;
  122. if (token_type != NAM) {
  123. if (mcpp_mode == OLD_PREP && token_type == NUM) { /* # 123 [fname]*/
  124. strcpy( identifier, "line");
  125. } else {
  126. if (compiling) {
  127. if (option_flags.lang_asm) {
  128. if (warn_level & 1)
  129. cwarn( illeg_dir, work_buf, 0L, NULL);
  130. } else {
  131. cerror( illeg_dir, work_buf, 0L, NULL);
  132. }
  133. } else if (warn_level & 8) {
  134. cwarn( illeg_dir, work_buf, 0L, in_skipped);
  135. }
  136. goto skip_line;
  137. }
  138. }
  139. hash = (identifier[ 1] == EOS) ? identifier[ 0]
  140. : (identifier[ 0] ^ (identifier[ 2] << 1));
  141. if (strlen( identifier) > 7)
  142. hash ^= (identifier[ 7] << 1);
  143. /* hash is set to a unique value corresponding to the directive.*/
  144. switch (hash) {
  145. case L_if: tp = "if"; break;
  146. case L_ifdef: tp = "ifdef"; break;
  147. case L_ifndef: tp = "ifndef"; break;
  148. case L_elif: tp = "elif"; break;
  149. case L_else: tp = "else"; break;
  150. case L_endif: tp = "endif"; break;
  151. case L_define: tp = "define"; break;
  152. case L_undef: tp = "undef"; break;
  153. case L_line: tp = "line"; break;
  154. case L_include: tp = "include"; break;
  155. #if COMPILER == GNUC
  156. case L_include_next: tp = "include_next"; break;
  157. #endif
  158. #if SYSTEM == SYS_MAC
  159. case L_import: tp = "import"; break;
  160. #endif
  161. case L_error: tp = "error"; break;
  162. case L_pragma: tp = "pragma"; break;
  163. default: tp = NULL; break;
  164. }
  165. if (tp != NULL && ! str_eq( identifier, tp)) { /* Hash conflict*/
  166. hash = 0; /* Unknown directive, will */
  167. tp = NULL; /* be handled by do_old() */
  168. }
  169. if (! compiling) { /* Not compiling now */
  170. switch (hash) {
  171. case L_elif :
  172. if (! standard) {
  173. if (warn_level & 8)
  174. do_old(); /* Unknown directive */
  175. goto skip_line; /* Skip the line */
  176. } /* Else fall through */
  177. case L_else : /* Test the #if's nest, if 0, compile */
  178. case L_endif: /* Un-nest #if */
  179. break;
  180. case L_if : /* These can't turn */
  181. case L_ifdef: /* compilation on, but */
  182. case L_ifndef : /* we must nest #if's.*/
  183. if (&ifstack[ BLK_NEST] < ++ifptr)
  184. goto if_nest_err;
  185. if (standard && (warn_level & 8)
  186. && &ifstack[ std_limits.blk_nest + 1] == ifptr)
  187. cwarn( many_nesting, NULL, (long) std_limits.blk_nest
  188. , in_skipped);
  189. ifptr->stat = 0; /* !WAS_COMPILING */
  190. ifptr->ifline = src_line; /* Line at section start*/
  191. goto skip_line;
  192. default : /* Other directives */
  193. if (tp == NULL && (warn_level & 8))
  194. do_old(); /* Unknown directive ? */
  195. goto skip_line; /* Skip the line */
  196. }
  197. }
  198. macro_line = 0; /* Reset error flag */
  199. file = infile; /* Remember the current file */
  200. switch (hash) {
  201. case L_if:
  202. case L_ifdef:
  203. case L_ifndef:
  204. if (&ifstack[ BLK_NEST] < ++ifptr)
  205. goto if_nest_err;
  206. if (standard && (warn_level & 4) &&
  207. &ifstack[ std_limits.blk_nest + 1] == ifptr)
  208. cwarn( many_nesting, NULL , (long) std_limits.blk_nest, NULL);
  209. ifptr->stat = WAS_COMPILING;
  210. ifptr->ifline = src_line;
  211. goto ifdo;
  212. case L_elif:
  213. if (! standard) {
  214. do_old(); /* Unrecognized directive */
  215. break;
  216. }
  217. if (ifptr == &ifstack[0])
  218. goto nest_err;
  219. if (ifptr == infile->initif) {
  220. goto in_file_nest_err;
  221. }
  222. if (ifptr->stat & ELSE_SEEN)
  223. goto else_seen_err;
  224. if ((ifptr->stat & (WAS_COMPILING | TRUE_SEEN)) != WAS_COMPILING) {
  225. compiling = FALSE; /* Done compiling stuff */
  226. goto skip_line; /* Skip this group */
  227. }
  228. hash = L_if;
  229. ifdo:
  230. c = do_if( hash, tp);
  231. if (mcpp_debug & IF) {
  232. mcpp_fprintf( DBG
  233. , "#if (#elif, #ifdef, #ifndef) evaluate to %s.\n"
  234. , compiling ? "TRUE" : "FALSE");
  235. mcpp_fprintf( DBG, "line %ld: %s", src_line, infile->buffer);
  236. }
  237. if (c == FALSE) { /* Error */
  238. compiling = FALSE; /* Skip this group */
  239. goto skip_line; /* Prevent an extra error message */
  240. }
  241. break;
  242. case L_else:
  243. if (ifptr == &ifstack[0])
  244. goto nest_err;
  245. if (ifptr == infile->initif) {
  246. if (standard)
  247. goto in_file_nest_err;
  248. else if (warn_level & 1)
  249. cwarn( not_in_section, NULL, 0L, NULL);
  250. }
  251. if (ifptr->stat & ELSE_SEEN)
  252. goto else_seen_err;
  253. ifptr->stat |= ELSE_SEEN;
  254. ifptr->elseline = src_line;
  255. if (ifptr->stat & WAS_COMPILING) {
  256. if (compiling || (ifptr->stat & TRUE_SEEN) != 0)
  257. compiling = FALSE;
  258. else
  259. compiling = TRUE;
  260. }
  261. if ((mcpp_debug & MACRO_CALL) && (ifptr->stat & WAS_COMPILING)) {
  262. sync_linenum();
  263. mcpp_fprintf( OUT, "/*else %ld:%c*/\n", src_line
  264. , compiling ? 'T' : 'F'); /* Show that #else is seen */
  265. }
  266. break;
  267. case L_endif:
  268. if (ifptr == &ifstack[0])
  269. goto nest_err;
  270. if (ifptr <= infile->initif) {
  271. if (standard)
  272. goto in_file_nest_err;
  273. else if (warn_level & 1)
  274. cwarn( not_in_section, NULL, 0L, NULL);
  275. }
  276. if (! compiling && (ifptr->stat & WAS_COMPILING))
  277. wrong_line = TRUE;
  278. compiling = (ifptr->stat & WAS_COMPILING);
  279. if ((mcpp_debug & MACRO_CALL) && compiling) {
  280. sync_linenum();
  281. mcpp_fprintf( OUT, "/*endif %ld*/\n", src_line);
  282. /* Show that #if block has ended */
  283. }
  284. --ifptr;
  285. break;
  286. case L_define:
  287. do_define( FALSE, 0);
  288. break;
  289. case L_undef:
  290. do_undef();
  291. break;
  292. case L_line:
  293. if ((c = do_line()) > 0) {
  294. src_line = c;
  295. sharp( NULL, 0); /* Putout the new line number and file name */
  296. infile->line = --src_line; /* Next line number is 'src_line' */
  297. newlines = -1;
  298. } else { /* Error already diagnosed by do_line() */
  299. skip_nl();
  300. }
  301. break;
  302. case L_include:
  303. in_include = TRUE;
  304. if (do_include( FALSE) == TRUE && file != infile)
  305. newlines = -1; /* File has been included. Clear blank lines */
  306. in_include = FALSE;
  307. break;
  308. case L_error:
  309. if (! standard) {
  310. do_old(); /* Unrecognized directive */
  311. break;
  312. }
  313. cerror( infile->buffer, NULL, 0L, NULL); /* _E_ */
  314. break;
  315. case L_pragma:
  316. if (! standard) {
  317. do_old(); /* Unrecognized directive */
  318. break;
  319. }
  320. do_pragma();
  321. newlines = -1; /* Do not putout excessive '\n' */
  322. break;
  323. default: /* Non-Standard or unknown directives */
  324. do_old();
  325. break;
  326. }
  327. switch (hash) {
  328. case L_if :
  329. case L_elif :
  330. case L_define :
  331. case L_line :
  332. goto skip_line; /* To prevent duplicate error message */
  333. #if COMPILER == GNUC
  334. case L_include_next :
  335. if (file != infile) /* File has been included */
  336. newlines = -1;
  337. #endif
  338. #if SYSTEM == SYS_MAC
  339. case L_import :
  340. if (file != infile) /* File has been included */
  341. newlines = -1;
  342. #endif
  343. case L_error :
  344. if (standard)
  345. goto skip_line;
  346. /* Else fall through */
  347. case L_include :
  348. case L_pragma :
  349. if (standard)
  350. break; /* Already read over the line */
  351. /* Else fall through */
  352. default : /* L_else, L_endif, L_undef, etc. */
  353. if (mcpp_mode == OLD_PREP) {
  354. /*
  355. * Ignore the rest of the #directive line so you can write
  356. * #if foo
  357. * #endif foo
  358. */
  359. ;
  360. } else if (skip_ws() != '\n') {
  361. #if COMPILER == GNUC
  362. if (standard && hash != L_endif)
  363. #else
  364. if (standard)
  365. #endif
  366. cerror( excess, infile->bptr-1, 0L, NULL);
  367. else if (warn_level & 1)
  368. cwarn( excess, infile->bptr-1, 0L, NULL);
  369. }
  370. skip_nl();
  371. }
  372. goto ret;
  373. in_file_nest_err:
  374. cerror( not_in_section, NULL, 0L, NULL);
  375. goto skip_line;
  376. nest_err:
  377. cerror( "Not in a #if (#ifdef) section", NULL, 0L, NULL); /* _E_ */
  378. goto skip_line;
  379. else_seen_err:
  380. cerror( "Already seen #else at line %.0s%ld" /* _E_ */
  381. , NULL, ifptr->elseline, NULL);
  382. skip_line:
  383. skip_nl(); /* Ignore rest of line */
  384. goto ret;
  385. if_nest_err:
  386. cfatal( many_nesting, NULL, (long) BLK_NEST, NULL);
  387. ret:
  388. in_directive = FALSE;
  389. keep_comments = option_flags.c && compiling && !no_output;
  390. keep_spaces = option_flags.k && compiling;
  391. /* keep_spaces is on for #define line even if no_output is TRUE */
  392. if (! wrong_line)
  393. newlines++;
  394. }
  395. static int do_if( int hash, const char * directive_name)
  396. /*
  397. * Process an #if (#elif), #ifdef or #ifndef. The latter two are straight-
  398. * forward, while #if needs a subroutine to evaluate the expression.
  399. * do_if() is called only if compiling is TRUE. If false, compilation is
  400. * always supressed, so we don't need to evaluate anything. This supresses
  401. * unnecessary warnings.
  402. */
  403. {
  404. int c;
  405. int found;
  406. DEFBUF * defp;
  407. if ((c = skip_ws()) == '\n') {
  408. unget_ch();
  409. cerror( no_arg, NULL, 0L, NULL);
  410. return FALSE;
  411. }
  412. if (mcpp_debug & MACRO_CALL) {
  413. sync_linenum();
  414. mcpp_fprintf( OUT, "/*%s %ld*/", directive_name, src_line);
  415. }
  416. if (hash == L_if) { /* #if or #elif */
  417. unget_ch();
  418. found = (eval_if() != 0L); /* Evaluate expression */
  419. if (mcpp_debug & MACRO_CALL)
  420. in_if = FALSE; /* 'in_if' is dynamically set in eval_lex() */
  421. hash = L_ifdef; /* #if is now like #ifdef */
  422. } else { /* #ifdef or #ifndef */
  423. if (scan_token( c, (workp = work_buf, &workp), work_end) != NAM) {
  424. cerror( not_ident, work_buf, 0L, NULL);
  425. return FALSE; /* Next token is not an identifier */
  426. }
  427. found = ((defp = look_id( identifier)) != NULL); /* Look in table*/
  428. if (mcpp_debug & MACRO_CALL) {
  429. if (found)
  430. mcpp_fprintf( OUT, "/*%s*/", defp->name);
  431. }
  432. }
  433. if (found == (hash == L_ifdef)) {
  434. compiling = TRUE;
  435. ifptr->stat |= TRUE_SEEN;
  436. } else {
  437. compiling = FALSE;
  438. }
  439. if (mcpp_debug & MACRO_CALL) {
  440. mcpp_fprintf( OUT, "/*i %c*/\n", compiling ? 'T' : 'F');
  441. /* Report wheather the directive is evaluated TRUE or FALSE */
  442. }
  443. return TRUE;
  444. }
  445. static void sync_linenum( void)
  446. /*
  447. * Put out newlines or #line line to synchronize line number with the
  448. * annotations about #if, #elif, #ifdef, #ifndef, #else or #endif on -K option.
  449. */
  450. {
  451. if (wrong_line || newlines > 10) {
  452. sharp( NULL, 0);
  453. } else {
  454. while (newlines-- > 0)
  455. mcpp_fputc('\n', OUT);
  456. }
  457. newlines = -1;
  458. }
  459. static long do_line( void)
  460. /*
  461. * Parse the line to update the line number and "filename" field for the next
  462. * input line.
  463. * Values returned are as follows:
  464. * -1: syntax error or out-of-range error (diagnosed by do_line(),
  465. * eval_num()).
  466. * [1,32767]: legal line number for C90, [1,2147483647] for C99.
  467. * Line number [32768,2147483647] in C90 mode is only warned (not an error).
  468. * do_line() always absorbs the line (except the <newline>).
  469. */
  470. {
  471. const char * const not_digits
  472. = "Line number \"%s\" isn't a decimal digits sequence"; /* _E_ _W1_ */
  473. const char * const out_of_range
  474. = "Line number \"%s\" is out of range of [1,%ld]"; /* _E_ _W1_ */
  475. int token_type;
  476. VAL_SIGN * valp;
  477. char * save;
  478. int c;
  479. if ((c = skip_ws()) == '\n') {
  480. cerror( no_arg, NULL, 0L, NULL);
  481. unget_ch(); /* Push back <newline> */
  482. return -1L; /* Line number is not changed */
  483. }
  484. if (standard) {
  485. token_type = get_unexpandable( c, FALSE);
  486. if (macro_line == MACRO_ERROR) /* Unterminated macro */
  487. return -1L; /* already diagnosed. */
  488. if (token_type == NO_TOKEN) /* Macro expanded to 0 token */
  489. goto no_num;
  490. if (token_type != NUM)
  491. goto illeg_num;
  492. } else if (scan_token( c, (workp = work_buf, &workp), work_end) != NUM) {
  493. goto illeg_num;
  494. }
  495. for (workp = work_buf; *workp != EOS; workp++) {
  496. if (! isdigit( *workp & UCHARMAX)) {
  497. if (standard) {
  498. cerror( not_digits, work_buf, 0L, NULL);
  499. return -1L;
  500. } else if (warn_level & 1) {
  501. cwarn( not_digits, work_buf, 0L, NULL);
  502. }
  503. }
  504. }
  505. valp = eval_num( work_buf); /* Evaluate number */
  506. if (valp->sign == VAL_ERROR) { /* Error diagnosed by eval_num()*/
  507. return -1;
  508. } else if (standard
  509. && (std_limits.line_num < valp->val || valp->val <= 0L)) {
  510. if (valp->val < LINE99LIMIT && valp->val > 0L) {
  511. if (warn_level & 1)
  512. cwarn( out_of_range, work_buf, std_limits.line_num, NULL);
  513. } else {
  514. cerror( out_of_range, work_buf, std_limits.line_num, NULL);
  515. return -1L;
  516. }
  517. }
  518. if (standard) {
  519. token_type = get_unexpandable( skip_ws(), FALSE);
  520. if (macro_line == MACRO_ERROR)
  521. return -1L;
  522. if (token_type != STR) {
  523. if (token_type == NO_TOKEN) { /* Filename is absent */
  524. return (long) valp->val;
  525. } else { /* Expanded macro should be a quoted string */
  526. goto not_fname;
  527. }
  528. }
  529. } else {
  530. if ((c = skip_ws()) == '\n') {
  531. unget_ch();
  532. return (long) valp->val;
  533. }
  534. if (scan_token( c, (workp = work_buf, &workp), work_end) != STR)
  535. goto not_fname;
  536. }
  537. #if COMPILER == GNUC
  538. if (memcmp( workp - 3, "//", 2) == 0) { /* "/cur-dir//" */
  539. save = infile->filename; /* Do not change the file name */
  540. } else
  541. #endif
  542. {
  543. *(workp - 1) = EOS; /* Ignore right '"' */
  544. save = save_string( &work_buf[ 1]); /* Ignore left '"' */
  545. }
  546. if (standard) {
  547. if (get_unexpandable( skip_ws(), FALSE) != NO_TOKEN) {
  548. cerror( excess, work_buf, 0L, NULL);
  549. free( save);
  550. return -1L;
  551. }
  552. } else if (mcpp_mode == OLD_PREP) {
  553. skip_nl();
  554. unget_ch();
  555. } else if ((c = skip_ws()) == '\n') {
  556. unget_ch();
  557. } else {
  558. if (warn_level & 1) {
  559. scan_token( c, (workp = work_buf, &workp), work_end);
  560. cwarn( excess, work_buf, 0, NULL);
  561. }
  562. skip_nl();
  563. unget_ch();
  564. }
  565. if (infile->filename)
  566. free( infile->filename);
  567. infile->filename = save; /* New file name */
  568. /* Note that this does not change infile->real_fname */
  569. return (long) valp->val; /* New line number */
  570. no_num:
  571. cerror( "No line number", NULL, 0L, NULL); /* _E_ */
  572. return -1L;
  573. illeg_num:
  574. cerror( "Not a line number \"%s\"", work_buf, 0L, NULL); /* _E_ */
  575. return -1L;
  576. not_fname:
  577. cerror( "Not a file name \"%s\"", work_buf, 0L, NULL); /* _E_ */
  578. return -1L;
  579. }
  580. /*
  581. * M a c r o D e f i n i t i o n s
  582. */
  583. /*
  584. * look_id() Looks for the name in the defined symbol table. Returns a
  585. * pointer to the definition if found, or NULL if not present.
  586. * install_macro() Installs the definition. Updates the symbol table.
  587. * undefine() Deletes the definition from the symbol table.
  588. */
  589. /*
  590. * Global work_buf[] are used to store #define parameter lists and
  591. * parms[].name point to them.
  592. * 'nargs' contains the actual number of parameters stored.
  593. */
  594. typedef struct {
  595. char * name; /* -> Start of each parameter */
  596. size_t len; /* Length of parameter name */
  597. } PARM;
  598. static PARM parms[ NMACPARS];
  599. static int nargs; /* Number of parameters */
  600. static char * token_p; /* Pointer to the token scanned */
  601. static char * repl_base; /* Base of buffer for repl-text */
  602. static char * repl_end; /* End of buffer for repl-text */
  603. static const char * const no_ident = "No identifier"; /* _E_ */
  604. #if COMPILER == GNUC
  605. static int gcc2_va_arg; /* GCC2-spec variadic macro */
  606. #endif
  607. DEFBUF * do_define(
  608. int ignore_redef, /* Do not redefine */
  609. int predefine /* Predefine compiler-specific name */
  610. /*
  611. * Note: The value of 'predefine' should be one of 0, DEF_NOARGS_PREDEF
  612. * or DEF_NOARGS_PREDEF_OLD, the other values cause errors.
  613. */
  614. )
  615. /*
  616. * Called from directive() when a #define is scanned or called from
  617. * do_options() when a -D option is scanned. This module parses formal
  618. * parameters by get_parm() and the replacement text by get_repl().
  619. *
  620. * There is some special case code to distinguish
  621. * #define foo bar -- object-like macro
  622. * from #define foo() bar -- function-like macro with no parameter
  623. *
  624. * Also, we make sure that
  625. * #define foo foo
  626. * expands to "foo" but doesn't put MCPP into an infinite loop.
  627. *
  628. * A warning is printed if you redefine a symbol with a non-identical
  629. * text. I.e,
  630. * #define foo 123
  631. * #define foo 123
  632. * is ok, but
  633. * #define foo 123
  634. * #define foo +123
  635. * is not.
  636. *
  637. * The following subroutines are called from do_define():
  638. * get_parm() parsing and remembering parameter names.
  639. * get_repl() parsing and remembering replacement text.
  640. *
  641. * The following subroutines are called from get_repl():
  642. * is_formal() is called when an identifier is scanned. It checks through
  643. * the array of formal parameters. If a match is found, the
  644. * identifier is replaced by a control byte which will be used
  645. * to locate the parameter when the macro is expanded.
  646. * def_stringization() is called when '#' operator is scanned. It surrounds
  647. * the token to stringize with magic-codes.
  648. *
  649. * modes other than STD ignore difference of parameter names in macro
  650. * redefinition.
  651. */
  652. {
  653. const char * const predef = "\"%s\" shouldn't be redefined"; /* _E_ */
  654. char repl_list[ NMACWORK + IDMAX]; /* Replacement text */
  655. char macroname[ IDMAX + 1]; /* Name of the macro defining */
  656. DEFBUF * defp; /* -> Old definition */
  657. DEFBUF ** prevp; /* -> Pointer to previous def in list */
  658. int c;
  659. int redefined; /* TRUE if redefined */
  660. int dnargs = 0; /* defp->nargs */
  661. int cmp; /* Result of name comparison */
  662. size_t def_start, def_end; /* Column of macro definition */
  663. repl_base = repl_list;
  664. repl_end = & repl_list[ NMACWORK];
  665. c = skip_ws();
  666. if ((mcpp_debug & MACRO_CALL) && src_line) /* Start of definition */
  667. def_start = infile->bptr - infile->buffer - 1;
  668. if (c == '\n') {
  669. cerror( no_ident, NULL, 0L, NULL);
  670. unget_ch();
  671. return NULL;
  672. } else if (scan_token( c, (workp = work_buf, &workp), work_end) != NAM) {
  673. cerror( not_ident, work_buf, 0L, NULL);
  674. return NULL;
  675. } else {
  676. prevp = look_prev( identifier, &cmp);
  677. /* Find place in the macro list to insert the definition */
  678. defp = *prevp;
  679. if (standard) {
  680. if (cmp || defp->push) { /* Not known or 'pushed' macro */
  681. if (str_eq( identifier, "defined")
  682. || ((stdc_val || cplus_val)
  683. && str_eq( identifier, "__VA_ARGS__"))) {
  684. cerror(
  685. "\"%s\" shouldn't be defined", identifier, 0L, NULL); /* _E_ */
  686. return NULL;
  687. }
  688. redefined = FALSE; /* Quite new definition */
  689. } else { /* It's known: */
  690. if (ignore_redef)
  691. return defp;
  692. dnargs = (defp->nargs == DEF_NOARGS_STANDARD
  693. || defp->nargs == DEF_NOARGS_PREDEF
  694. || defp->nargs == DEF_NOARGS_PREDEF_OLD)
  695. ? DEF_NOARGS : defp->nargs;
  696. if (dnargs <= DEF_NOARGS_DYNAMIC /* __FILE__ and such */
  697. || dnargs == DEF_PRAGMA /* _Pragma() pseudo-macro */
  698. ) {
  699. cerror( predef, identifier, 0L, NULL);
  700. return NULL;
  701. } else {
  702. redefined = TRUE; /* Remember this fact */
  703. }
  704. }
  705. } else {
  706. if (cmp) {
  707. redefined = FALSE; /* Quite new definition */
  708. } else { /* It's known: */
  709. if (ignore_redef)
  710. return defp;
  711. dnargs = (defp->nargs == DEF_NOARGS_STANDARD
  712. || defp->nargs == DEF_NOARGS_PREDEF
  713. || defp->nargs == DEF_NOARGS_PREDEF_OLD)
  714. ? DEF_NOARGS : defp->nargs;
  715. redefined = TRUE;
  716. }
  717. }
  718. }
  719. strcpy( macroname, identifier); /* Remember the name */
  720. in_define = TRUE; /* Recognize '#', '##' */
  721. if (get_parm() == FALSE) { /* Get parameter list */
  722. in_define = FALSE;
  723. return NULL; /* Syntax error */
  724. }
  725. if (get_repl( macroname) == FALSE) { /* Get replacement text */
  726. in_define = FALSE;
  727. return NULL; /* Syntax error */
  728. }
  729. if ((mcpp_debug & MACRO_CALL) && src_line) {
  730. /* Remember location on source */
  731. char * cp;
  732. cp = infile->bptr - 1; /* Before '\n' */
  733. while (char_type[ *cp & UCHARMAX] & HSP)
  734. cp--; /* Trailing space */
  735. cp++; /* Just after the last token */
  736. def_end = cp - infile->buffer; /* End of definition */
  737. }
  738. in_define = FALSE;
  739. if (redefined) {
  740. if (dnargs != nargs || ! str_eq( defp->repl, repl_list)
  741. || (mcpp_mode == STD && ! str_eq( defp->parmnames, work_buf))
  742. ) { /* Warn if differently redefined */
  743. if (warn_level & 1) {
  744. cwarn(
  745. "The macro is redefined", NULL, 0L, NULL); /* _W1_ */
  746. if (! option_flags.no_source_line)
  747. dump_a_def( " previously macro", defp, FALSE, TRUE
  748. , fp_err);
  749. }
  750. } else { /* Identical redefinition */
  751. return defp;
  752. }
  753. } /* Else new or re-definition*/
  754. defp = install_macro( macroname, nargs, work_buf, repl_list, prevp, cmp
  755. , predefine);
  756. if ((mcpp_debug & MACRO_CALL) && src_line) {
  757. /* Get location on source file */
  758. LINE_COL s_line_col, e_line_col;
  759. s_line_col.line = src_line;
  760. s_line_col.col = def_start;
  761. get_src_location( & s_line_col);
  762. /* Convert to pre-line-splicing data */
  763. e_line_col.line = src_line;
  764. e_line_col.col = def_end;
  765. get_src_location( & e_line_col);
  766. /* Putout the macro definition information embedded in comment */
  767. mcpp_fprintf( OUT, "/*m%s %ld:%d-%ld:%d*/\n", defp->name
  768. , s_line_col.line, s_line_col.col
  769. , e_line_col.line, e_line_col.col);
  770. wrong_line = TRUE; /* Need #line later */
  771. }
  772. if (mcpp_mode == STD && cplus_val && id_operator( macroname)
  773. && (warn_level & 1))
  774. /* These are operators, not identifiers, in C++98 */
  775. cwarn( "\"%s\" is defined as macro", macroname /* _W1_ */
  776. , 0L, NULL);
  777. return defp;
  778. }
  779. static int get_parm( void)
  780. /*
  781. * Get parameters i.e. numbers into nargs, name into work_buf[], name-length
  782. * into parms[].len. parms[].name point into work_buf.
  783. * Return TRUE if the parameters are legal, else return FALSE.
  784. * In STD mode preprocessor must remember the parameter names, only for
  785. * checking the validity of macro redefinitions. This is required by the
  786. * Standard (what an overhead !).
  787. */
  788. {
  789. const char * const many_parms
  790. = "More than %.0s%ld parameters"; /* _E_ _W4_ */
  791. const char * const illeg_parm
  792. = "Illegal parameter \"%s\""; /* _E_ */
  793. const char * const misplaced_ellip
  794. = "\"...\" isn't the last parameter"; /* _E_ */
  795. int token_type;
  796. int c;
  797. parms[ 0].name = workp = work_buf;
  798. work_buf[ 0] = EOS;
  799. #if COMPILER == GNUC
  800. gcc2_va_arg = FALSE;
  801. #endif
  802. /* POST_STD mode */
  803. insert_sep = NO_SEP; /* Clear the inserted token separator */
  804. c = get_ch();
  805. if (c == '(') { /* With arguments? */
  806. nargs = 0; /* Init parms counter */
  807. if (skip_ws() == ')')
  808. return TRUE; /* Macro with 0 parm */
  809. else
  810. unget_ch();
  811. do { /* Collect parameters */
  812. if (nargs >= NMACPARS) {
  813. cerror( many_parms, NULL, (long) NMACPARS, NULL);
  814. return FALSE;
  815. }
  816. parms[ nargs].name = workp; /* Save its start */
  817. if ((token_type = scan_token( c = skip_ws(), &workp, work_end))
  818. != NAM) {
  819. if (c == '\n') {
  820. break;
  821. } else if (c == ',' || c == ')') {
  822. cerror( "Empty parameter", NULL, 0L, NULL); /* _E_ */
  823. return FALSE;
  824. } else if (standard && (stdc_val || cplus_val)
  825. && token_type == OPE && openum == OP_ELL) {
  826. /*
  827. * Enable variable argument macro which is a feature of
  828. * C99. We enable this even on C90 or C++ for GCC
  829. * compatibility.
  830. */
  831. if (skip_ws() != ')') {
  832. cerror( misplaced_ellip, NULL, 0L, NULL);
  833. return FALSE;
  834. }
  835. parms[ nargs++].len = 3;
  836. nargs |= VA_ARGS;
  837. goto ret;
  838. } else {
  839. cerror( illeg_parm, parms[ nargs].name, 0L, NULL);
  840. return FALSE; /* Bad parameter syntax */
  841. }
  842. }
  843. if (standard && (stdc_val || cplus_val)
  844. && str_eq( identifier, "__VA_ARGS__")) {
  845. cerror( illeg_parm, parms[ nargs].name, 0L, NULL);
  846. return FALSE;
  847. /* __VA_ARGS__ should not be used as a parameter */
  848. }
  849. if (is_formal( parms[ nargs].name, FALSE)) {
  850. cerror( "Duplicate parameter name \"%s\"" /* _E_ */
  851. , parms[ nargs].name, 0L, NULL);
  852. return FALSE;
  853. }
  854. parms[ nargs].len = (size_t) (workp - parms[ nargs].name);
  855. /* Save length of param */
  856. *workp++ = ',';
  857. nargs++;
  858. } while ((c = skip_ws()) == ','); /* Get another parameter*/
  859. *--workp = EOS; /* Remove excessive ',' */
  860. if (c != ')') { /* Must end at ) */
  861. #if COMPILER == GNUC
  862. /* Handle GCC2 variadic params like par... */
  863. char * tp = workp;
  864. if (mcpp_mode == STD
  865. &&(token_type = scan_token( c, &workp, work_end)) == OPE
  866. && openum == OP_ELL) {
  867. if ((c = skip_ws()) != ')') {
  868. cerror( misplaced_ellip, NULL, 0L, NULL);
  869. return FALSE;
  870. }
  871. *tp = EOS; /* Remove "..." */
  872. nargs |= VA_ARGS;
  873. gcc2_va_arg = TRUE;
  874. goto ret;
  875. }
  876. #endif
  877. unget_ch(); /* Push back '\n' */
  878. cerror(
  879. "Missing \",\" or \")\" in parameter list \"(%s\"" /* _E_ */
  880. , work_buf, 0L, NULL);
  881. return FALSE;
  882. }
  883. } else {
  884. /*
  885. * DEF_NOARGS is needed to distinguish between
  886. * "#define foo" and "#define foo()".
  887. */
  888. nargs = DEF_NOARGS; /* Object-like macro */
  889. unget_ch();
  890. }
  891. ret:
  892. #if NMACPARS > NMACPARS90MIN
  893. if ((warn_level & 4) && (nargs & ~AVA_ARGS) > std_limits.n_mac_pars)
  894. cwarn( many_parms, NULL , (long) std_limits.n_mac_pars, NULL);
  895. #endif
  896. return TRUE;
  897. }
  898. static int get_repl(
  899. const char * macroname
  900. )
  901. /*
  902. * Get replacement text i.e. names of formal parameters are converted to
  903. * the magic numbers, and operators #, ## is converted to magic characters.
  904. * Return TRUE if replacement list is legal, else return FALSE.
  905. * Any token separator in the text is converted to a single space, no token
  906. * sepatator is inserted by MCPP. Those are required by the Standard for
  907. * stringizing of an argument by # operator.
  908. * In POST_STD mode, inserts a space between any tokens in source (except a
  909. * macro name and the next '(' in macro definition), hence presence or absence
  910. * of token separator makes no difference.
  911. */
  912. {
  913. const char * const mixed_ops
  914. = "Macro with mixing of ## and # operators isn't portable"; /* _W4_ */
  915. const char * const multiple_cats
  916. = "Macro with multiple ## operators isn't portable"; /* _W4_ */
  917. char * prev_token = NULL; /* Preceding token */
  918. char * prev_prev_token = NULL; /* Pre-preceding token */
  919. int multi_cats = FALSE; /* Multiple ## operators*/
  920. int c;
  921. int token_type; /* Type of token */
  922. char * temp;
  923. char * repl_cur = repl_base; /* Pointer into repl-text buffer*/
  924. *repl_cur = EOS;
  925. token_p = NULL;
  926. if (mcpp_mode == STD) {
  927. c = get_ch();
  928. unget_ch();
  929. if (((char_type[ c] & SPA) == 0) && (nargs < 0) && (warn_level & 1))
  930. cwarn( "No space between macro name \"%s\" and repl-text"/* _W1_ */
  931. , macroname, 0L, NULL);
  932. }
  933. c = skip_ws(); /* Get to the body */
  934. while (c != '\n') {
  935. if (standard) {
  936. prev_prev_token = prev_token;
  937. prev_token = token_p;
  938. }
  939. token_p = repl_cur; /* Remember the pointer */
  940. token_type = scan_token( c, &repl_cur, repl_end);
  941. switch (token_type) {
  942. case OPE: /* Operator or punctuator */
  943. if (! standard)
  944. break;
  945. switch (openum) {
  946. case OP_CAT: /* ## */
  947. if (prev_token == NULL) {
  948. cerror( "No token before ##" /* _E_ */
  949. , NULL, 0L, NULL);
  950. return FALSE;
  951. } else if (*prev_token == CAT) {
  952. cerror( "## after ##", NULL, 0L, NULL); /* _E_ */
  953. return FALSE;
  954. } else if (prev_prev_token && *prev_prev_token == CAT) {
  955. multi_cats = TRUE;
  956. } else if (prev_prev_token && *prev_prev_token == ST_QUOTE
  957. && (warn_level & 4)) { /* # parm ## */
  958. cwarn( mixed_ops, NULL, 0L, NULL);
  959. }
  960. repl_cur = token_p;
  961. *repl_cur++ = CAT; /* Convert to CAT */
  962. break;
  963. case OP_STR: /* # */
  964. if (nargs < 0) /* In object-like macro */
  965. break; /* '#' is an usual char */
  966. if (prev_token && *prev_token == CAT
  967. && (warn_level & 4)) /* ## # */
  968. cwarn( mixed_ops, NULL, 0L, NULL);
  969. repl_cur = token_p; /* Overwrite on # */
  970. if ((temp = def_stringization( repl_cur)) == NULL) {
  971. return FALSE; /* Error */
  972. } else {
  973. repl_cur = temp;
  974. }
  975. break;
  976. default: /* Any operator as it is */
  977. break;
  978. }
  979. break;
  980. case NAM:
  981. /*
  982. * Replace this name if it's a parm. Note that the macro name is a
  983. * possible replacement token. We stuff DEF_MAGIC in front of the
  984. * token which is treated as a LETTER by the token scanner and eaten
  985. * by the macro expanding routine. This prevents the macro expander
  986. * from looping if someone writes "#define foo foo".
  987. */
  988. temp = is_formal( identifier, TRUE);
  989. if (temp == NULL) { /* Not a parameter name */
  990. if (! standard)
  991. break;
  992. if ((stdc_val || cplus_val)
  993. && str_eq( identifier, "__VA_ARGS__")) {
  994. #if COMPILER == GNUC
  995. if (gcc2_va_arg) {
  996. cerror( "\"%s\" cannot be used in GCC2-spec variadic macro" /* _E_ */
  997. , identifier, 0L, NULL);
  998. return FALSE;
  999. }
  1000. #endif
  1001. cerror( "\"%s\" without corresponding \"...\"" /* _E_ */
  1002. , identifier, 0L, NULL);
  1003. return FALSE;
  1004. }
  1005. if ((temp = mgtoken_save( macroname)) != NULL)
  1006. repl_cur = temp; /* Macro name */
  1007. } else { /* Parameter name */
  1008. repl_cur = temp;
  1009. #if COMPILER == GNUC
  1010. if (mcpp_mode == STD && (nargs & VA_ARGS)
  1011. && *(repl_cur - 1) == (nargs & ~AVA_ARGS)) {
  1012. if (! str_eq( identifier, "__VA_ARGS__")
  1013. && (warn_level & 2))
  1014. cwarn(
  1015. "GCC2-spec variadic macro is defined" /* _W2_ */
  1016. , NULL, 0L, NULL);
  1017. if (prev_token && *prev_token == CAT
  1018. && prev_prev_token && *prev_prev_token == ',')
  1019. /* ", ## __VA_ARGS__" is sequence peculiar */
  1020. /* to GCC3-spec variadic macro. */
  1021. /* Or ", ## last_arg" is sequence peculiar */
  1022. /* to GCC2-spec variadic macro. */
  1023. nargs |= GVA_ARGS;
  1024. /* Mark as sequence peculiar to GCC */
  1025. /* This will be warned at expansion time */
  1026. }
  1027. #endif
  1028. }
  1029. break;
  1030. case STR: /* String in mac. body */
  1031. case CHR: /* Character constant */
  1032. if (mcpp_mode == OLD_PREP)
  1033. repl_cur = str_parm_scan( repl_cur);
  1034. break;
  1035. case SEP:
  1036. if (mcpp_mode == OLD_PREP && c == COM_SEP)
  1037. repl_cur--; /* Skip comment now */
  1038. break;
  1039. default: /* Any token as it is */
  1040. break;
  1041. }
  1042. if ((c = get_ch()) == ' ' || c == '\t') {
  1043. *repl_cur++ = ' '; /* Space */
  1044. while ((c = get_ch()) == ' ' || c == '\t')
  1045. ; /* Skip excessive spaces */
  1046. }
  1047. }
  1048. while (repl_base < repl_cur
  1049. && (*(repl_cur - 1) == ' ' || *(repl_cur - 1) == '\t'))
  1050. repl_cur--; /* Remove trailing spaces */
  1051. *repl_cur = EOS; /* Terminate work */
  1052. unget_ch(); /* For syntax check */
  1053. if (standard) {
  1054. if (token_p && *token_p == CAT) {
  1055. cerror( "No token after ##", NULL, 0L, NULL); /* _E_ */
  1056. return FALSE;
  1057. }
  1058. if (multi_cats && (warn_level & 4))
  1059. cwarn( multiple_cats, NULL, 0L, NULL);
  1060. if ((nargs & VA_ARGS) && stdc_ver < 199901L && (warn_level & 2))
  1061. /* Variable arg macro is the spec of C99, not C90 nor C++98 */
  1062. cwarn( "Variable argument macro is defined", /* _W2_ */
  1063. NULL, 0L, NULL);
  1064. }
  1065. return TRUE;
  1066. }
  1067. static char * is_formal(
  1068. const char * name,
  1069. int conv /* Convert to magic number? */
  1070. )
  1071. /*
  1072. * If the identifier is a formal parameter, save the MAC_PARM and formal
  1073. * offset, returning the advanced pointer into the replacement text.
  1074. * Else, return NULL.
  1075. */
  1076. {
  1077. char * repl_cur;
  1078. const char * va_arg = "__VA_ARGS__";
  1079. PARM parm;
  1080. size_t len;
  1081. int i;
  1082. len = strlen( name);
  1083. for (i = 0; i < (nargs & ~AVA_ARGS); i++) { /* For each parameter */
  1084. parm = parms[ i];
  1085. if ((len == parm.len
  1086. /* Note: parms[].name are comma separated */
  1087. && memcmp( name, parm.name, parm.len) == 0)
  1088. || (standard && (nargs & VA_ARGS)
  1089. && i == (nargs & ~AVA_ARGS) - 1 && conv
  1090. && str_eq( name, va_arg))) { /* __VA_ARGS__ */
  1091. /* If it's known */
  1092. #if COMPILER == GNUC
  1093. if (gcc2_va_arg && str_eq( name, va_arg))
  1094. return NULL; /* GCC2 variadic macro */
  1095. #endif
  1096. if (conv) {
  1097. repl_cur = token_p; /* Overwrite on the name*/
  1098. *repl_cur++ = MAC_PARM; /* Save the signal */
  1099. *repl_cur++ = i + 1; /* Save the parm number */
  1100. return repl_cur; /* Return "gotcha" */
  1101. } else {
  1102. return parm.name; /* Duplicate parm name */
  1103. }
  1104. }
  1105. }
  1106. return NULL; /* Not a formal param */
  1107. }
  1108. static char * def_stringization( char * repl_cur)
  1109. /*
  1110. * Define token stringization.
  1111. * We store a magic cookie (which becomes surrouding " on expansion) preceding
  1112. * the parameter as an operand of # operator.
  1113. * Return the current pointer into replacement text if the token following #
  1114. * is a parameter name, else return NULL.
  1115. */
  1116. {
  1117. int c;
  1118. char * temp;
  1119. *repl_cur++ = ST_QUOTE; /* Prefix */
  1120. if (char_type[ c = get_ch()] & HSP) { /* There is a space */
  1121. *repl_cur++ = ' ';
  1122. while (char_type[ c = get_ch()] & HSP) /* Skip excessive spaces*/
  1123. ;
  1124. }
  1125. token_p = repl_cur; /* Remember the pointer */
  1126. if (scan_token( c, &repl_cur, repl_end) == NAM) {
  1127. if ((temp = is_formal( identifier, TRUE)) != NULL) {
  1128. repl_cur = temp;
  1129. return repl_cur;
  1130. }
  1131. }
  1132. cerror( "Not a formal parameter \"%s\"", token_p, 0L, NULL); /* _E_ */
  1133. return NULL;
  1134. }
  1135. static char * mgtoken_save( const char * macroname)
  1136. /*
  1137. * A magic cookie is inserted if the token is identical to the macro name,
  1138. * so the expansion doesn't recurse.
  1139. * Return the advanced pointer into the replacement text or NULL.
  1140. */
  1141. {
  1142. char * repl_cur;
  1143. if (str_eq( macroname, identifier)) { /* Macro name in body */
  1144. repl_cur = token_p; /* Overwrite on token */
  1145. *repl_cur++ = DEF_MAGIC; /* Save magic marker */
  1146. repl_cur = stpcpy( repl_cur, identifier);
  1147. /* And save the token */
  1148. return repl_cur;
  1149. } else {
  1150. return NULL;
  1151. }
  1152. }
  1153. static char * str_parm_scan( char * string_end)
  1154. /*
  1155. * String parameter scan.
  1156. * This code -- if enabled -- recognizes a formal parameter in a string
  1157. * literal or in a character constant.
  1158. * #define foo(bar, v) printf("%bar\n", v)
  1159. * foo( d, i)
  1160. * expands to:
  1161. * printf("%d\n", i)
  1162. * str_parm_scan() return the advanced pointer into the replacement text.
  1163. * This has been superceded by # stringizing and string concatenation.
  1164. * This routine is called only in OLD_PREP mode.
  1165. */
  1166. {
  1167. int delim;
  1168. int c;
  1169. char * tp;
  1170. char * wp; /* Pointer into the quoted literal */
  1171. delim = *token_p;
  1172. unget_string( ++token_p, NULL);
  1173. /* Pseudo-token-parsing in a string literal */
  1174. wp = token_p;
  1175. while ((c = get_ch()) != delim) {
  1176. token_p = wp;
  1177. if (scan_token( c, &wp, string_end) != NAM)
  1178. continue;
  1179. if ((tp = is_formal( token_p, TRUE)) != NULL)
  1180. wp = tp;
  1181. }
  1182. *wp++ = delim;
  1183. return wp;
  1184. }
  1185. static void do_undef( void)
  1186. /*
  1187. * Remove the symbol from the defined list.
  1188. * Called from directive().
  1189. */
  1190. {
  1191. DEFBUF * defp;
  1192. int c;
  1193. if ((c = skip_ws()) == '\n') {
  1194. cerror( no_ident, NULL, 0L, NULL);
  1195. unget_ch();
  1196. return;
  1197. }
  1198. if (scan_token( c, (workp = work_buf, &workp), work_end) != NAM) {
  1199. cerror( not_ident, work_buf, 0L, NULL);
  1200. skip_nl();
  1201. unget_ch();
  1202. } else {
  1203. if ((defp = look_id( identifier)) == NULL) {
  1204. if (warn_level & 8)
  1205. cwarn( "\"%s\" wasn't defined" /* _W8_ */
  1206. , identifier, 0L, NULL);
  1207. } else if (standard && (defp->nargs <= DEF_NOARGS_STANDARD
  1208. /* Standard predef */
  1209. || defp->nargs == DEF_PRAGMA)) {
  1210. /* _Pragma() pseudo-macro */
  1211. cerror( "\"%s\" shouldn't be undefined" /* _E_ */
  1212. , identifier, 0L, NULL);
  1213. } else if (standard) {
  1214. c = skip_ws();
  1215. unget_ch();
  1216. if (c != '\n') /* Trailing junk */
  1217. return;
  1218. else
  1219. undefine( identifier);
  1220. } else {
  1221. undefine( identifier);
  1222. }
  1223. }
  1224. }
  1225. /*
  1226. * C P P S y m b o l T a b l e s
  1227. *
  1228. * SBSIZE defines the number of hash-table slots for the symbol table.
  1229. * It must be a power of 2.
  1230. */
  1231. /* Symbol table queue headers. */
  1232. static DEFBUF * symtab[ SBSIZE];
  1233. static long num_of_macro = 0;
  1234. #if MCPP_LIB
  1235. void init_directive( void)
  1236. /* Initialize static variables. */
  1237. {
  1238. num_of_macro = 0;
  1239. }
  1240. #endif
  1241. DEFBUF * look_id( const char * name)
  1242. /*
  1243. * Look for the identifier in the symbol table.
  1244. * If found, return the table pointer; Else return NULL.
  1245. */
  1246. {
  1247. DEFBUF ** prevp;
  1248. int cmp;
  1249. prevp = look_prev( name, &cmp);
  1250. if (standard)
  1251. return ((cmp == 0 && (*prevp)->push == 0) ? *prevp : NULL);
  1252. else
  1253. return ((cmp == 0) ? *prevp : NULL);
  1254. }
  1255. DEFBUF ** look_prev(
  1256. const char * name, /* Name of the macro */
  1257. int * cmp /* Result of comparison */
  1258. )
  1259. /*
  1260. * Look for the place to insert the macro definition.
  1261. * Return a pointer to the previous member in the linked list.
  1262. */
  1263. {
  1264. const char * np;
  1265. DEFBUF ** prevp;
  1266. DEFBUF * dp;
  1267. size_t s_name;
  1268. int hash;
  1269. for (hash = 0, np = name; *np != EOS; )
  1270. hash += *np++;
  1271. hash += s_name = (size_t)(np - name);
  1272. s_name++;
  1273. prevp = & symtab[ hash & SBMASK];
  1274. *cmp = -1; /* Initialize */
  1275. while ((dp = *prevp) != NULL) {
  1276. if ((*cmp = memcmp( dp->name, name, s_name)) >= 0)
  1277. break;
  1278. prevp = &dp->link;
  1279. }
  1280. return prevp;
  1281. }
  1282. DEFBUF * look_and_install(
  1283. const char * name, /* Name of the macro */
  1284. int numargs, /* The numbers of parms */
  1285. const char * parmnames, /* Names of parameters concatenated */
  1286. const char * repl /* Replacement text */
  1287. )
  1288. /*
  1289. * Look for the name and (re)define it.
  1290. * Returns a pointer to the definition block.
  1291. * Returns NULL if the symbol was Standard-predefined.
  1292. */
  1293. {
  1294. DEFBUF ** prevp; /* Place to insert definition */
  1295. DEFBUF * defp; /* New definition block */
  1296. int cmp; /* Result of comparison of new name and old */
  1297. prevp = look_prev( name, &cmp);
  1298. defp = install_macro( name, numargs, parmnames, repl, prevp, cmp, 0);
  1299. return defp;
  1300. }
  1301. DEFBUF * install_macro(
  1302. const char * name, /* Name of the macro */
  1303. int numargs, /* The numbers of parms */
  1304. const char * parmnames, /* Names of parameters concatenated */
  1305. const char * repl, /* Replacement text */
  1306. DEFBUF ** prevp, /* The place to insert definition */
  1307. int cmp, /* Result of comparison of new name and old */
  1308. int predefine /* Predefined macro without leading '_' */
  1309. )
  1310. /*
  1311. * Enter this name in the lookup table.
  1312. * Returns a pointer to the definition block.
  1313. * Returns NULL if the symbol was Standard-predefined.
  1314. * Note that predefinedness can be specified by either of 'numargs' or
  1315. * 'predefine'.
  1316. */
  1317. {
  1318. DEFBUF * dp;
  1319. DEFBUF * defp;
  1320. size_t s_name, s_parmnames, s_repl;
  1321. defp = *prevp; /* Old definition, if cmp == 0 */
  1322. if (cmp == 0 && defp->nargs < DEF_NOARGS - 1)
  1323. return NULL; /* Standard predefined */
  1324. if (parmnames == NULL || repl == NULL || (predefine && numargs > 0)
  1325. || (predefine && predefine != DEF_NOARGS_PREDEF
  1326. && predefine != DEF_NOARGS_PREDEF_OLD))
  1327. /* Shouldn't happen */
  1328. cfatal( "Bug: Illegal macro installation of \"%s\"" /* _F_ */
  1329. , name, 0L, NULL); /* Use "" instead of NULL */
  1330. s_name = strlen( name);
  1331. if (mcpp_mode == STD)
  1332. s_parmnames = strlen( parmnames) + 1;
  1333. else
  1334. s_parmnames = 0;
  1335. s_repl = strlen( repl) + 1;
  1336. dp = (DEFBUF *)
  1337. xmalloc( sizeof (DEFBUF) + s_name + s_parmnames + s_repl);
  1338. if (cmp || (standard && (*prevp)->push)) { /* New definition */
  1339. dp->link = defp; /* Insert to linked list */
  1340. *prevp = dp;
  1341. } else { /* Redefinition */
  1342. dp->link = defp->link; /* Replace old def with new */
  1343. *prevp = dp;
  1344. free( defp);
  1345. }
  1346. dp->nargs = predefine ? predefine : numargs;
  1347. if (standard) {
  1348. dp->push = 0;
  1349. dp->parmnames = (char *)dp + sizeof (DEFBUF) + s_name;
  1350. dp->repl = dp->parmnames + s_parmnames;
  1351. if (mcpp_mode == STD)
  1352. memcpy( dp->parmnames, parmnames, s_parmnames);
  1353. } else {
  1354. dp->repl = (char *)dp + sizeof (DEFBUF) + s_name;
  1355. }
  1356. memcpy( dp->name, name, s_name + 1);
  1357. memcpy( dp->repl, repl, s_repl);
  1358. /* Remember where the macro is defined */
  1359. dp->fname = cur_fullname; /* Full-path-list of current file */
  1360. dp->mline = src_line;
  1361. if (standard && cmp && ++num_of_macro == std_limits.n_macro + 1
  1362. && std_limits.n_macro && (warn_level & 4))
  1363. /* '&& std_limits.n_macro' to avoid warning before initialization */
  1364. cwarn( "More than %.0s%ld macros defined" /* _W4_ */
  1365. , NULL , std_limits.n_macro, NULL);
  1366. return dp;
  1367. }
  1368. int undefine(
  1369. const char * name /* Name of the macro */
  1370. )
  1371. /*
  1372. * Delete the macro definition from the symbol table.
  1373. * Returns TRUE, if deleted;
  1374. * Else returns FALSE (when the macro was not defined or was Standard
  1375. * predefined).
  1376. */
  1377. {
  1378. DEFBUF ** prevp; /* Preceding definition in list */
  1379. DEFBUF * dp; /* Definition to delete */
  1380. int cmp; /* 0 if defined, else not defined */
  1381. prevp = look_prev( name, &cmp);
  1382. dp = *prevp; /* Definition to delete */
  1383. if (cmp || dp->nargs <= DEF_NOARGS_STANDARD)
  1384. return FALSE; /* Not defined or Standard predefined */
  1385. if (standard && dp->push)
  1386. return FALSE; /* 'Pushed' macro */
  1387. *prevp = dp->link; /* Link the previous and the next */
  1388. if ((mcpp_debug & MACRO_CALL) && dp->mline) {
  1389. /* Notice this directive unless the macro is predefined */
  1390. mcpp_fprintf( OUT, "/*undef %ld*//*%s*/\n", src_line, dp->name);
  1391. wrong_line = TRUE;
  1392. }
  1393. free( dp); /* Delete the definition */
  1394. if (standard)
  1395. num_of_macro--;
  1396. return TRUE;
  1397. }
  1398. static void dump_repl(
  1399. const DEFBUF * dp,
  1400. FILE * fp,
  1401. int gcc2_va
  1402. )
  1403. /*
  1404. * Dump replacement text.
  1405. */
  1406. {
  1407. int numargs = dp->nargs;
  1408. char * cp1;
  1409. size_t i;
  1410. int c;
  1411. const char * cp;
  1412. for (cp = dp->repl; (c = *cp++ & UCHARMAX) != EOS; ) {
  1413. switch (c) {
  1414. case MAC_PARM: /* Parameter */
  1415. c = (*cp++ & UCHARMAX) - 1;
  1416. if (standard) {
  1417. PARM parm = parms[ c];
  1418. if ((numargs & VA_ARGS) && c == (numargs & ~AVA_ARGS) - 1) {
  1419. mcpp_fputs( gcc2_va ? parm.name : "__VA_ARGS__"
  1420. , FP2DEST( fp));
  1421. /* gcc2_va is possible only in STD mode */
  1422. } else {
  1423. if (mcpp_mode == STD) {
  1424. for (i = 0, cp1 = parm.name; i < parm.len; i++)
  1425. mcpp_fputc( *cp1++, FP2DEST( fp));
  1426. } else {
  1427. mcpp_fputc( 'a' + c % 26, FP2DEST( fp));
  1428. if (c > 26)
  1429. mcpp_fputc( '0' + c / 26, FP2DEST( fp));
  1430. }
  1431. }
  1432. } else {
  1433. mcpp_fputc( 'a' + c % 26, FP2DEST( fp));
  1434. if (c > 26)
  1435. mcpp_fputc( '0' + c / 26, FP2DEST( fp));
  1436. }
  1437. break;
  1438. case DEF_MAGIC:
  1439. if (! standard)
  1440. mcpp_fputc( c, FP2DEST( fp));
  1441. /* Else skip */
  1442. break;
  1443. case CAT:
  1444. if (standard)
  1445. mcpp_fputs( "##", FP2DEST( fp));
  1446. else
  1447. mcpp_fputc( c, FP2DEST( fp));
  1448. break;
  1449. case ST_QUOTE:
  1450. if (standard)
  1451. mcpp_fputs( "#", FP2DEST( fp));
  1452. else
  1453. mcpp_fputc( c, FP2DEST( fp));
  1454. break;
  1455. case COM_SEP:
  1456. /*
  1457. * Though TOK_SEP coincides to COM_SEP, this cannot appear in
  1458. * Standard mode.
  1459. */
  1460. if (mcpp_mode == OLD_PREP)
  1461. mcpp_fputs( "/**/", FP2DEST( fp));
  1462. break;
  1463. default:
  1464. mcpp_fputc( c, FP2DEST( fp));
  1465. break;
  1466. }
  1467. }
  1468. }
  1469. /*
  1470. * If the compiler is so-called "one-pass" compiler, compiler-predefined
  1471. * macros are commented out to avoid redefinition.
  1472. */
  1473. #if ONE_PASS
  1474. #define CAN_REDEF DEF_NOARGS
  1475. #else
  1476. #define CAN_REDEF DEF_NOARGS_PREDEF
  1477. #endif
  1478. void dump_a_def(
  1479. const char * why,
  1480. const DEFBUF * dp,
  1481. int newdef, /* TRUE if parmnames are currently in parms[] */
  1482. int comment, /* Show location of the definition in comment */
  1483. FILE * fp
  1484. )
  1485. /*
  1486. * Dump a macro definition.
  1487. */
  1488. {
  1489. char * cp, * cp1;
  1490. int numargs = dp->nargs & ~AVA_ARGS;
  1491. int commented; /* To be commented out */
  1492. int gcc2_va = FALSE; /* GCC2-spec variadic */
  1493. int i;
  1494. if (standard && numargs == DEF_PRAGMA) /* _Pragma pseudo-macro */
  1495. return;
  1496. if ((numargs < CAN_REDEF) || (standard && dp->push))
  1497. commented = TRUE;
  1498. else
  1499. commented = FALSE;
  1500. if (! comment && commented) /* For -dM option */
  1501. return;
  1502. if (why)
  1503. mcpp_fprintf( FP2DEST( fp), "%s \"%s\" defined as: ", why, dp->name);
  1504. mcpp_fprintf( FP2DEST( fp), "%s#define %s", commented ? "/* " : "",
  1505. dp->name); /* Macro name */
  1506. if (numargs >= 0) { /* Parameter list */
  1507. if (mcpp_mode == STD) {
  1508. const char * appendix = null;
  1509. if (! newdef) {
  1510. /* Make parms[] for dump_repl() */
  1511. for (i = 0, cp = dp->parmnames; i < numargs;
  1512. i++, cp = cp1 + 1) {
  1513. if ((cp1 = strchr( cp, ',')) == NULL) /* The last arg */
  1514. parms[ i].len = strlen( cp);
  1515. else
  1516. parms[ i].len = (size_t) (cp1 - cp);
  1517. parms[ i].name = cp;
  1518. }
  1519. }
  1520. #if COMPILER == GNUC
  1521. if ((dp->nargs & VA_ARGS)
  1522. && memcmp( parms[ numargs - 1].name, "...", 3) != 0) {
  1523. appendix = "..."; /* Append ... so as to become 'args...' */
  1524. gcc2_va = TRUE;
  1525. }
  1526. #endif
  1527. mcpp_fprintf( FP2DEST( fp), "(%s%s)", dp->parmnames, appendix);
  1528. } else {
  1529. if (newdef) {
  1530. mcpp_fprintf( FP2DEST( fp), "(%s)", parms[ 0].name);
  1531. } else if (numargs == 0) {
  1532. mcpp_fputs( "()", FP2DEST( fp));
  1533. } else {
  1534. /* Print parameter list automatically made as: */
  1535. /* a, b, c, ..., a0, b0, c0, ..., a1, b1, c1, ... */
  1536. mcpp_fputc( '(', FP2DEST( fp));
  1537. for (i = 0; i < numargs; i++) { /* Make parameter list */
  1538. mcpp_fputc( 'a' + i % 26, FP2DEST( fp));
  1539. if (i >= 26)
  1540. mcpp_fputc( '0' + i / 26, FP2DEST( fp));
  1541. if (i + 1 < numargs)
  1542. mcpp_fputc( ',', FP2DEST( fp));
  1543. }
  1544. mcpp_fputc( ')', FP2DEST( fp));
  1545. }
  1546. }
  1547. }
  1548. if (*dp->repl) {
  1549. mcpp_fputc( ' ', FP2DEST( fp));
  1550. dump_repl( dp, fp, gcc2_va); /* Replacement text */
  1551. }
  1552. if (commented)
  1553. /* Standard predefined or one-pass-compiler-predefined */
  1554. mcpp_fputs( " */", FP2DEST( fp));
  1555. if (comment) /* Not -dM option */
  1556. mcpp_fprintf( FP2DEST( fp), " \t/* %s:%ld\t*/", dp->fname, dp->mline);
  1557. mcpp_fputc( '\n', FP2DEST( fp));
  1558. }
  1559. void dump_def(
  1560. int comment, /* Location of definition in comment */
  1561. int K_opt /* -K option is specified */
  1562. )
  1563. /*
  1564. * Dump all the current macro definitions to output stream.
  1565. */
  1566. {
  1567. DEFBUF * dp;
  1568. DEFBUF ** symp;
  1569. sharp( NULL, 0); /* Report the current source file & line */
  1570. if (comment)
  1571. mcpp_fputs( "/* Currently defined macros. */\n", OUT);
  1572. for (symp = symtab; symp < &symtab[ SBSIZE]; symp++) {
  1573. if ((dp = *symp) != NULL) {
  1574. do {
  1575. if (K_opt)
  1576. mcpp_fprintf( OUT, "/*m%s*/\n", dp->name);
  1577. else
  1578. dump_a_def( NULL, dp, FALSE, comment, fp_out);
  1579. } while ((dp = dp->link) != NULL);
  1580. }
  1581. }
  1582. wrong_line = TRUE; /* Line number is out of sync */
  1583. }
  1584. #if MCPP_LIB
  1585. void clear_symtable( void)
  1586. /*
  1587. * Free all the macro definitions.
  1588. */
  1589. {
  1590. DEFBUF * next;
  1591. DEFBUF * dp;
  1592. DEFBUF ** symp;
  1593. for (symp = symtab; symp < &symtab[ SBSIZE]; symp++) {
  1594. for (next = *symp; next != NULL; ) {
  1595. dp = next;
  1596. next = dp->link;
  1597. free( dp); /* Free the symbol */
  1598. }
  1599. *symp = NULL;
  1600. }
  1601. }
  1602. #endif