cpp.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include <libgen.h>
  5. #include <linux/limits.h> // for PATH_MAX
  6. #include "cpp.h"
  7. #include "sti/string_int.h"
  8. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  9. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  10. #include "cpp_exp.c"
  11. void undef_macro(cpp_tu_t* tu, char* name);
  12. #define CPP_STATE_LIST \
  13. X(HASH) \
  14. X(HASH_DEF) \
  15. X(HASH_DEF_SP) \
  16. X(HASH_DEF_SP_ID) \
  17. X(HASH_DEF_SP_ID_LP) \
  18. X(MACRO_ARGS) \
  19. X(MACRO_ARGS_ELIPSIS) \
  20. X(MACRO_ARGS_RP) \
  21. X(MACRO_BODY) \
  22. X(FOUND_NAME) \
  23. X(INV_ARGS) \
  24. \
  25. X(HASH_IFDEF) \
  26. X(HASH_IFDEF_ID) \
  27. X(HASH_IFNDEF) \
  28. X(HASH_IFNDEF_ID) \
  29. X(HASH_IF) \
  30. X(HASH_ELSE) \
  31. X(HASH_ELSEIF) \
  32. X(HASH_ENDIF) \
  33. \
  34. X(HASH_ERROR) \
  35. X(HASH_WARNING) \
  36. \
  37. X(HASH_INC) \
  38. X(HASH_INC_SP) \
  39. X(HASH_INC_SP_LT) \
  40. \
  41. X(HASH_UNDEF) \
  42. X(HASH_UNDEF_SP) \
  43. \
  44. X(SKIP_REST)
  45. enum {
  46. _NONE = 0,
  47. #define X(a, ...) _##a,
  48. CPP_STATE_LIST
  49. #undef X
  50. };
  51. char* state_names[] = {
  52. [_NONE] = "NONE",
  53. #define X(a, ...) [_##a] = #a,
  54. CPP_STATE_LIST
  55. #undef X
  56. };
  57. static void inject_space(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* list) {
  58. lexer_token_t* t = calloc(1, sizeof(*t));
  59. t->type = LEXER_TOK_SPACE;
  60. t->text = strint_(tu->str_table, " ");
  61. VEC_PUSH(&list->tokens, t);
  62. }
  63. static void inject_comma(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* list) {
  64. lexer_token_t* t = calloc(1, sizeof(*t));
  65. t->type = LEXER_TOK_PUNCT;
  66. t->text = strint_(tu->str_table, ",");
  67. VEC_PUSH(&list->tokens, t);
  68. }
  69. static void inject_number(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* list, long num) {
  70. char buf[64];
  71. snprintf(buf, 64, "%ld", num);
  72. lexer_token_t* t = calloc(1, sizeof(*t));
  73. t->type = LEXER_TOK_PUNCT;
  74. t->text = strint_(tu->str_table, buf);
  75. VEC_PUSH(&list->tokens, t);
  76. }
  77. static void inject_stringified(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* list, cpp_token_list_t* input) {
  78. char* buf;
  79. size_t sz = 0;
  80. VEC_EACH(&input->tokens, ti, t) {
  81. if(t->type == LEXER_TOK_SPACE) {
  82. sz++;
  83. continue;
  84. }
  85. for(char* s = t->text; *s; s++) {
  86. if(*s == '\\' || *s == '"') sz++;
  87. sz++;
  88. }
  89. }
  90. buf = malloc(sz + 3); // two quotes and a null;
  91. buf[0] = '"';
  92. buf[sz + 1] = '"';
  93. buf[sz + 2] = 0;
  94. char* c = buf + 1;
  95. VEC_EACH(&input->tokens, ti, t) {
  96. if(t->type == LEXER_TOK_SPACE) {
  97. *c++ = ' ';
  98. continue;
  99. }
  100. for(char* s = t->text; *s; s++) {
  101. if(*s == '\\' || *s == '"') *c++ = '\\';
  102. *c++ = *s;
  103. }
  104. }
  105. lexer_token_t* t = calloc(1, sizeof(*t));
  106. t->type = LEXER_TOK_STRING;
  107. t->text = strint_(tu->str_table, buf);
  108. VEC_PUSH(&list->tokens, t);
  109. free(buf);
  110. }
  111. static void inject_pasted(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* list, lexer_token_t* l, lexer_token_t* r) {
  112. char* buf;
  113. buf = malloc(sizeof(*buf) * (1 + strlen(l->text) + strlen(r->text)));
  114. strcpy(buf, l->text);
  115. strcat(buf, r->text);
  116. lexer_source_t lx;
  117. lx.text = buf;
  118. lx.head = buf;
  119. lx.len = strlen(buf);
  120. lexer_token_t tok = {0};
  121. tok.alloc = 1 + strlen(buf);
  122. tok.text = malloc(tok.alloc);
  123. if(is_token(&lx, &tok) && tok.len == lx.len) {
  124. lexer_token_t* t = calloc(1, sizeof(*t));
  125. t->type = tok.type; // TODO: probe
  126. t->text = strint_(tu->str_table, buf);
  127. VEC_PUSH(&list->tokens, t);
  128. // printf(" > pasted token: '%s'\n", buf);
  129. }
  130. else {
  131. VEC_PUSH(&list->tokens, l);
  132. inject_space(tu, ctx, list);
  133. VEC_PUSH(&list->tokens, r);
  134. // printf(" > paste failed for '%s'\n", buf);
  135. }
  136. free(buf);
  137. free(tok.text);
  138. }
  139. cpp_token_list_t* lex_file(cpp_tu_t* tu, cpp_file_t* file) {
  140. lexer_source_t* src = calloc(1, sizeof(*src));
  141. src->text = readWholeFile(file->full_path, &src->len);
  142. src->head = src->text;
  143. if(!src->text) {
  144. fprintf(stderr, "Failed to read file '%s'\n", file->full_path);
  145. free(src);
  146. return NULL;
  147. }
  148. // lex the file
  149. cpp_token_list_t* tokens = calloc(1, sizeof(*tokens));
  150. lexer_token_t tok = {0};
  151. tok.start_line = 1;
  152. tok.alloc = 256;
  153. tok.text = malloc(tok.alloc * sizeof(*tok.text));
  154. for(int i = 0; i < 2000000000; i++) { // DEBUG: sanity limits
  155. is_token(src, &tok);
  156. if(tok.len == 0) break;
  157. lexer_token_t* n = malloc(sizeof(*n));
  158. VEC_PUSH(&tokens->tokens, n);
  159. *n = tok;
  160. n->alloc = n->len; // the interred string doesn't waste any space
  161. n->text = strnint_(tu->str_table, n->text, n->len);
  162. n->file = file;
  163. tok.start_line = tok.end_line;
  164. tok.start_col = tok.end_col + 1;
  165. }
  166. return tokens;
  167. }
  168. void cpp_tu_init(cpp_tu_t* tu) {
  169. HT_init(&tu->files, 64);
  170. HT_init(&tu->macros, 128);
  171. string_internment_table_init(&tu->str_table);
  172. #define X(a, b, ...) tu->a = strint_(tu->str_table, b);
  173. CPP_STRING_CACHE_LIST
  174. #undef X
  175. tu->fn_buf_len = 0;
  176. tu->fn_buf_alloc = 512;
  177. tu->filename_buffer = malloc(sizeof(*tu->filename_buffer) * tu->fn_buf_alloc);
  178. tu->initialized = 1;
  179. }
  180. static int is_regular_file(char* path) {
  181. struct stat st;
  182. if(!stat(path, &st)) {
  183. if((st.st_mode & S_IFMT) == S_IFREG) return 1;
  184. }
  185. return 0;
  186. }
  187. static cpp_file_t* init_file(cpp_tu_t* tu, char* full_path, char* include_name) {
  188. cpp_file_t* file;
  189. file = calloc(1, sizeof(*file));
  190. file->name = strdup(include_name);
  191. file->dir = dirname(strdup(full_path));
  192. file->full_path = strdup(full_path);
  193. HT_set(&tu->files, full_path, file);
  194. return file;
  195. }
  196. cpp_file_t* cpp_tu_get_file(cpp_tu_t* tu, char* cwd, char* path, char is_system) {
  197. cpp_file_t* file;
  198. char* full_path, *full_path_bad;
  199. if(!HT_get(&tu->files, path, &file)) {
  200. return file;
  201. }
  202. // construct a path and search the directories
  203. if(is_system) {
  204. // check system headers first
  205. VEC_EACH(&tu->system_inc_dirs, i, dir) {
  206. full_path = path_join(dir, path);
  207. // check the cache before hitting the filesystem
  208. if(!HT_get(&tu->files, full_path, &file)) {
  209. return file;
  210. }
  211. if(is_regular_file(full_path)) {
  212. // found the file
  213. // printf("Found system header '%s'\n", full_path);
  214. file = init_file(tu, full_path, path);
  215. file->is_system_header = 1;
  216. // free(full_path);
  217. return file;
  218. }
  219. free(full_path);
  220. }
  221. }
  222. // check the current directory
  223. full_path_bad = malloc(PATH_MAX + 100);
  224. strcpy(full_path_bad, cwd);
  225. strcat(full_path_bad, "/");
  226. strcat(full_path_bad, path);
  227. //path_join(cwd, path);
  228. full_path = malloc(PATH_MAX + 100);
  229. realpath(full_path_bad, full_path);
  230. free(full_path_bad);
  231. if(!HT_get(&tu->files, full_path, &file)) {
  232. return file;
  233. }
  234. if(is_regular_file(full_path)) {
  235. // found the file
  236. // printf("Found local include '%s'\n", full_path);
  237. file = init_file(tu, full_path, path);
  238. free(full_path);
  239. return file;
  240. }
  241. free(full_path);
  242. // check local include directories next
  243. VEC_EACH(&tu->local_inc_dirs, i, dir) {
  244. full_path_bad = path_join(dir, path);
  245. full_path = realpath(full_path_bad, NULL);
  246. free(full_path_bad);
  247. // check the cache before hitting the filesystem
  248. if(!HT_get(&tu->files, full_path, &file)) {
  249. return file;
  250. }
  251. if(is_regular_file(full_path)) {
  252. // found the file
  253. // printf("Found local include '%s'\n", full_path);
  254. file = init_file(tu, full_path, path);
  255. free(full_path);
  256. return file;
  257. }
  258. free(full_path);
  259. }
  260. return NULL;
  261. }
  262. void preprocess_file(cpp_tu_t* tu, cpp_context_t* parent, char* path, char is_system) {
  263. int is_first = 0;
  264. if(!tu->initialized) {
  265. cpp_tu_init(tu);
  266. is_first = 1;
  267. }
  268. char* dir;
  269. if(parent && parent->file) {
  270. dir = parent->file->dir;
  271. }
  272. else {
  273. dir = "./";
  274. }
  275. cpp_file_t* file = cpp_tu_get_file(tu, dir, path, is_system);
  276. if(!file) {
  277. fprintf(stderr, "Could not find file '%s'\n", path);
  278. return;
  279. }
  280. // printf("Including file: %s\n", file->full_path);
  281. // lex the file on first load
  282. if(!file->raw_tokens) {
  283. file->raw_tokens = lex_file(tu, file);
  284. if(!file->raw_tokens) return;
  285. }
  286. cpp_context_t* ctx = calloc(1, sizeof(*ctx));
  287. if(is_first) tu->root_ctx = ctx;
  288. ctx->file = file;
  289. ctx->out = calloc(1, sizeof(*ctx->out));
  290. ctx->parent = parent;
  291. if(parent) VEC_PUSH(&parent->children, ctx);
  292. ctx->tokens = file->raw_tokens;
  293. preprocess_token_list(tu, ctx, file->raw_tokens);
  294. // copy the expanded output into the including file
  295. if(parent) {
  296. VEC_CAT(&parent->out->tokens, &ctx->out->tokens);
  297. }
  298. return;
  299. }
  300. void preprocess_token_list(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* tokens) {
  301. // printf("proc token list\n");
  302. #define X(a, b, ...) char* a = tu->a;
  303. CPP_STRING_CACHE_LIST
  304. #undef X
  305. cpp_macro_name_t* mn;
  306. cpp_macro_def_t* m;
  307. cpp_macro_invocation_t* inv;
  308. cpp_token_list_t* in_arg;
  309. char* cached_arg = 0;
  310. int was_nl = 1; // the first line starts a line
  311. int was_ws = 1;
  312. int pdepth = 0; // parenthesis nesting depth
  313. // int cond_depth = 0;
  314. int out_enable = 1; // don't output anything for failed conditionals
  315. // int highest_enabled_level = 0;
  316. // int highest_disabled_level = 99999999;
  317. // VEC(cpp_macro_if_t*) cond_stack;
  318. // VEC_INIT(&cond_stack);
  319. cpp_macro_if_t* cond_head = NULL;
  320. // VEC_PUSH(&cond_stack, 1);
  321. int state = _NONE;
  322. int sanity = 0;
  323. VEC_EACH(&tokens->tokens, ni, n) {
  324. /*
  325. printf(" %s {%ld} wnl:%d p token list loop [%s] %s %s:%d:%d\n",
  326. out_enable ? "E" : "X",
  327. ni,
  328. was_nl,
  329. n->type == LEXER_TOK_SPACE ? " " : n->text,
  330. state_names[state],
  331. ctx->file->name, n->start_line, n->start_col
  332. ); //*/
  333. // if(sanity++ > 400) break;
  334. switch(state) {
  335. case _NONE:
  336. // printf("hash: %p\n", _hash);
  337. if(was_nl && n->text == _hash) {
  338. state = _HASH;
  339. break;
  340. }
  341. if(out_enable)
  342. expand_token(tu, ctx, ctx->out, tokens, &ni);
  343. break;
  344. case _HASH:
  345. if(n->text == _define) state = _HASH_DEF;
  346. else if(n->text == _undef) state = _HASH_UNDEF;
  347. else if(n->text == _error) state = _HASH_ERROR;
  348. else if(n->text == _warning) state = _HASH_WARNING;
  349. else if(n->text == _include) state = _HASH_INC;
  350. else if(n->text == _ifdef) {
  351. cpp_macro_if_t* mif = calloc(1, sizeof(*mif));
  352. mif->type = 'd';
  353. mif->decl_line = n->start_line;
  354. mif->raw = calloc(1, sizeof(*mif->raw));
  355. if(!cond_head) {
  356. VEC_PUSH(&tu->ifs, mif);
  357. }
  358. mif->parent = cond_head;
  359. cond_head = mif;
  360. if(mif->parent) {
  361. mif->disabled_higher = mif->parent->disabled_higher || !mif->parent->net_enabled;
  362. }
  363. state = _HASH_IFDEF;
  364. }
  365. else if(n->text == _ifndef) {
  366. cpp_macro_if_t* mif = calloc(1, sizeof(*mif));
  367. mif->type = 'n';
  368. mif->decl_line = n->start_line;
  369. mif->raw = calloc(1, sizeof(*mif->raw));
  370. if(!cond_head) {
  371. VEC_PUSH(&tu->ifs, mif);
  372. }
  373. mif->parent = cond_head;
  374. cond_head = mif;
  375. if(mif->parent) {
  376. mif->disabled_higher = mif->parent->disabled_higher || !mif->parent->net_enabled;
  377. }
  378. state = _HASH_IFNDEF;
  379. }
  380. else if(n->text == _else) {
  381. if(!cond_head) {
  382. fprintf(stderr, "#else without matching #if at %s:%d:%d\n", ctx->file->full_path, n->start_line, n->start_col);
  383. state = _SKIP_REST;
  384. break;
  385. }
  386. cpp_macro_if_t* mif = calloc(1, sizeof(*mif));
  387. mif->type = 'e';
  388. mif->decl_line = n->start_line;
  389. cond_head->next = mif;
  390. mif->prev = cond_head;
  391. mif->first = cond_head->first ? cond_head->first : cond_head;
  392. mif->parent = cond_head->parent;
  393. if(mif->parent) {
  394. mif->disabled_higher = mif->parent->disabled_higher || !mif->parent->net_enabled;
  395. }
  396. cond_head = mif;
  397. state = _HASH_ELSE;
  398. }
  399. else if(n->text == _elif) {
  400. if(!cond_head) {
  401. fprintf(stderr, "#elif without matching #if at %s:%d:%d\n", ctx->file->full_path, n->start_line, n->start_col);
  402. state = _SKIP_REST;
  403. break;
  404. }
  405. cpp_macro_if_t* mif = calloc(1, sizeof(*mif));
  406. mif->type = 'l';
  407. mif->decl_line = n->start_line;
  408. mif->raw = calloc(1, sizeof(*mif->raw));
  409. cond_head->next = mif;
  410. mif->prev = cond_head;
  411. mif->first = cond_head->first ? cond_head->first : cond_head;
  412. mif->parent = cond_head->parent;
  413. if(mif->parent) {
  414. mif->disabled_higher = mif->parent->disabled_higher || !mif->parent->net_enabled;
  415. }
  416. state = _HASH_ELSEIF;
  417. }
  418. else if(n->text == _if) {
  419. cpp_macro_if_t* mif = calloc(1, sizeof(*mif));
  420. mif->type = 'i';
  421. mif->decl_line = n->start_line;
  422. mif->raw = calloc(1, sizeof(*mif->raw));
  423. if(!cond_head) {
  424. VEC_PUSH(&tu->ifs, mif);
  425. }
  426. mif->parent = cond_head;
  427. cond_head = mif;
  428. if(mif->parent) {
  429. mif->disabled_higher = mif->parent->disabled_higher || !mif->parent->net_enabled;
  430. }
  431. state = _HASH_IF;
  432. }
  433. else if(n->text == _endif) {
  434. if(!cond_head) {
  435. fprintf(stderr, "#endif without matching #if at %d:%d\n", n->start_line, n->start_col);
  436. state = _SKIP_REST;
  437. break;
  438. }
  439. cond_head = cond_head->parent;
  440. if(cond_head) {
  441. out_enable = cond_head->net_enabled;
  442. }
  443. else {
  444. out_enable = 1; // default on
  445. }
  446. if(n->has_newline) state = _NONE;
  447. else state = _SKIP_REST;
  448. break;
  449. }
  450. else if(n->text == _line) state = _SKIP_REST;
  451. else if(n->text == _pragma) state = _SKIP_REST;
  452. else if(n->text == _ident) state = _SKIP_REST;
  453. else if(n->type != LEXER_TOK_SPACE && n->type != LEXER_TOK_COMMENT) { state = _SKIP_REST;
  454. printf("_endif: %p '%s', token: %p, '%s'\n", _endif, _endif, n->text, n->text);
  455. fprintf(stderr, "Unknown PP directive: %s at %s:%d:%d\n", n->text, ctx->file->full_path, n->start_line, n->start_col);
  456. exit(1);
  457. }
  458. break;
  459. case _HASH_DEF:
  460. if(n->type == LEXER_TOK_SPACE) {
  461. // start macro here
  462. if(out_enable)
  463. m = calloc(1, sizeof(*m));
  464. state = _HASH_DEF_SP;
  465. }
  466. else {
  467. fprintf(stderr, "Whitespace is required after #define at %s:%d:%d\n", ctx->file->full_path, n->start_line, n->start_col);
  468. state = _NONE;
  469. }
  470. break;
  471. case _HASH_DEF_SP:
  472. if(n->type != LEXER_TOK_IDENT) {
  473. fprintf(stderr, "An Identifier is required after #define at %s:%d:%d\n", ctx->file->full_path, n->start_line, n->start_col);
  474. state = _NONE;
  475. }
  476. // macro name
  477. if(out_enable) {
  478. m->name = n->text;
  479. if(HT_get(&tu->macros, n->text, &mn)) {
  480. mn = calloc(1, sizeof(*mn));
  481. HT_set(&tu->macros, n->text, mn);
  482. }
  483. VEC_PUSH(&mn->defs, m);
  484. VEC_PUSH(&tu->all_defs, m);
  485. }
  486. state = _HASH_DEF_SP_ID;
  487. break;
  488. case _HASH_DEF_SP_ID:
  489. if(n->has_newline) {
  490. state = _NONE;
  491. break;
  492. }
  493. // spaces are not allowed between the macro name and the fn parens
  494. if(n->type == LEXER_TOK_SPACE) {
  495. if(out_enable)
  496. m->obj_like = 1;
  497. state = _MACRO_BODY;
  498. }
  499. if(n->text == _lparen) {
  500. // function-like macro
  501. // printf("fn-like macro\n");
  502. if(out_enable)
  503. m->fn_like = 1;
  504. state = _MACRO_ARGS;//_HASH_DEF_SP_ID_LP;
  505. }
  506. else {
  507. if(out_enable)
  508. m->obj_like = 1;
  509. state = _MACRO_BODY;
  510. ni--;
  511. }
  512. break;
  513. case _HASH_DEF_SP_ID_LP:
  514. // printf("SP_ID_LP\n");
  515. state = _MACRO_ARGS;
  516. break;
  517. case _MACRO_ARGS:
  518. if(n->has_newline) {
  519. fprintf(stderr, "Unexpected linebreak at %s:%d:%d\n", ctx->file->full_path, n->start_line, n->start_col);
  520. state = _NONE;
  521. break;
  522. }
  523. if(n->text == _rparen && pdepth == 0) {
  524. if(cached_arg) {
  525. if(out_enable)
  526. VEC_PUSH(&m->args, cached_arg);
  527. cached_arg = 0;
  528. }
  529. state = _MACRO_ARGS_RP;
  530. }
  531. else if(n->type == LEXER_TOK_IDENT) {
  532. cached_arg = n->text;
  533. }
  534. else if(n->text == _comma) {
  535. if(cached_arg) {
  536. if(out_enable)
  537. VEC_PUSH(&m->args, cached_arg);
  538. cached_arg = 0;
  539. }
  540. else {
  541. fprintf(stderr, "An argument name identifier is required at %s:%d:%d\n", ctx->file->full_path, n->start_line, n->start_col);
  542. exit(1);
  543. }
  544. }
  545. else if(n->text == _elipsis) {
  546. // variadic macro
  547. if(pdepth != 0) {
  548. fprintf(stderr, "Varargs elipsis encountered inside nested parenthesis.\n");
  549. }
  550. if(out_enable)
  551. m->variadic = 1;
  552. state = _MACRO_ARGS_ELIPSIS;
  553. }
  554. else if(n->type != LEXER_TOK_SPACE) {
  555. fprintf(stderr, "Unexpected token '%s' at %s:%d:%d (%d)\n", n->text, ctx->file->full_path, n->start_line, n->start_col, __LINE__);
  556. }
  557. break;
  558. case _MACRO_ARGS_ELIPSIS:
  559. if(n->text == _rparen) {
  560. state = _MACRO_ARGS_RP;
  561. }
  562. else if(n->type != LEXER_TOK_SPACE) {
  563. fprintf(stderr, "Unexpected token '%s' at %s:%d:%d (%d)\n", n->text, ctx->file->full_path, n->start_line, n->start_col, __LINE__);
  564. }
  565. break;
  566. case _MACRO_ARGS_RP:
  567. if(n->type != LEXER_TOK_SPACE) {
  568. fprintf(stderr, "Whitespace required after macro parameter list at %d:%d\n", n->start_line, n->start_col);
  569. }
  570. state = _MACRO_BODY;
  571. break;
  572. case _MACRO_BODY:
  573. if(n->has_newline) {
  574. state = _NONE;
  575. }
  576. else {
  577. if(!out_enable) break;
  578. if(n->type == LEXER_TOK_SPACE) {
  579. if(VEC_LEN(&m->body.tokens) == 0) break;
  580. }
  581. // printf(" [%s] pushing body token: '%s'\n", m->name, n->type == LEXER_TOK_SPACE ? " " : n->text);
  582. if(n->type != LEXER_TOK_SPACE) {
  583. if(was_ws) {
  584. inject_space(tu, ctx, &m->body);
  585. }
  586. VEC_PUSH(&m->body.tokens, n);
  587. was_ws = 0;
  588. }
  589. else {
  590. was_ws = 1;
  591. }
  592. }
  593. break;
  594. case _HASH_UNDEF:
  595. if(!out_enable) {
  596. state = _SKIP_REST;
  597. break;
  598. }
  599. if(n->type == LEXER_TOK_SPACE) {
  600. state = _HASH_UNDEF_SP;
  601. }
  602. else {
  603. fprintf(stderr, "Whitespace is required after #undef at %d:%d\n", n->start_line, n->start_col);
  604. state = _NONE;
  605. }
  606. break;
  607. case _HASH_UNDEF_SP:
  608. if(n->type != LEXER_TOK_IDENT) {
  609. fprintf(stderr, "An Identifier is required after #undef at %d:%d\n", n->start_line, n->start_col);
  610. state = _NONE;
  611. }
  612. undef_macro(tu, n->text);
  613. state = _SKIP_REST;
  614. break;
  615. /*-----------------------
  616. Diagnostics
  617. -------------------------*/
  618. case _HASH_ERROR:
  619. if(!out_enable) {
  620. state = _SKIP_REST;
  621. break;
  622. }
  623. if(n->type == LEXER_TOK_SPACE || n->type == LEXER_TOK_COMMENT) {
  624. printf(" ");
  625. }
  626. else {
  627. printf(n->text);
  628. }
  629. if(n->has_newline) {
  630. printf("\n");
  631. state = _NONE;
  632. }
  633. break;
  634. case _HASH_WARNING:
  635. if(!out_enable) {
  636. state = _SKIP_REST;
  637. break;
  638. }
  639. if(n->type == LEXER_TOK_SPACE || n->type == LEXER_TOK_COMMENT) {
  640. printf(" ");
  641. }
  642. else {
  643. printf(n->text);
  644. }
  645. if(n->has_newline) {
  646. printf("\n");
  647. state = _NONE;
  648. }
  649. break;
  650. /*-------------------------------
  651. Simple Conditionals
  652. ---------------------------------*/
  653. case _HASH_IFDEF:
  654. if(n->type == LEXER_TOK_IDENT) {
  655. // todo: save tokens
  656. // cond_head->expanded = expand_token_list(tu, ctx, &cond_head->raw.tokens);
  657. cond_head->result = is_defined(tu, n);
  658. cond_head->net_enabled = (cond_head->result != 0) && !cond_head->disabled_higher;
  659. out_enable = cond_head->net_enabled;
  660. if(n->has_newline) state = _NONE;
  661. else state = _SKIP_REST;
  662. }
  663. break;
  664. case _HASH_IFNDEF:
  665. if(n->type == LEXER_TOK_IDENT) {
  666. cond_head->result = !is_defined(tu, n);
  667. cond_head->net_enabled = (cond_head->result != 0) && !cond_head->disabled_higher;
  668. out_enable = cond_head->net_enabled;
  669. if(n->has_newline) state = _NONE;
  670. else state = _SKIP_REST;
  671. }
  672. break;
  673. case _HASH_ELSE:
  674. cond_head->net_enabled = (!cond_head->prev->net_enabled) && !cond_head->disabled_higher;
  675. out_enable = cond_head->net_enabled;
  676. if(n->has_newline) state = _NONE;
  677. else state = _SKIP_REST;
  678. break;
  679. case _HASH_ENDIF:
  680. if(!cond_head) {
  681. fprintf(stderr, "#endif without matching #if at %d:%d\n", n->start_line, n->start_col);
  682. state = _SKIP_REST;
  683. break;
  684. }
  685. cond_head = cond_head->parent;
  686. if(cond_head) {
  687. out_enable = cond_head->net_enabled;
  688. }
  689. else {
  690. out_enable = 1; // default on
  691. }
  692. if(n->has_newline) state = _NONE;
  693. else state = _SKIP_REST;
  694. break;
  695. /*------------------------------------
  696. Complicated Conditionals
  697. --------------------------------------*/
  698. case _HASH_IF:
  699. if(n->type != LEXER_TOK_SPACE && n->type != LEXER_TOK_COMMENT) {
  700. VEC_PUSH(&cond_head->raw->tokens, n);
  701. }
  702. // expand macros and do the evaluation at the end of the line
  703. if(n->has_newline) {
  704. cond_head->expanded = expand_token_list(tu, ctx, cond_head->raw);
  705. cond_head->result = eval_exp(tu, ctx, cond_head->expanded);
  706. cond_head->net_enabled = (cond_head->result != 0) && !cond_head->disabled_higher;
  707. out_enable = cond_head->net_enabled;
  708. state = _NONE;
  709. }
  710. break;
  711. case _HASH_ELSEIF:
  712. // buffer up all the expression tokens first
  713. if(n->type != LEXER_TOK_SPACE && n->type != LEXER_TOK_COMMENT) {
  714. VEC_PUSH(&cond_head->raw->tokens, n);;
  715. }
  716. // expand macros and do the evaluation at the end of the line
  717. if(n->has_newline) {
  718. cond_head->expanded = expand_token_list(tu, ctx, cond_head->raw);
  719. cond_head->result = eval_exp(tu, ctx, cond_head->expanded);
  720. cond_head->net_enabled = (cond_head->result != 0) && !cond_head->disabled_higher;
  721. out_enable = cond_head->net_enabled;
  722. state = _NONE;
  723. }
  724. break;
  725. /*---------------------
  726. Includes
  727. -----------------------*/
  728. // Include syntax is somewhat complicated. It uses alternate semantics for the file names
  729. // than normal C code, and the < > version can potentially have otherwise invalid lexer
  730. // tokens inside it. The lexer for this preprocessor divides and classifies tokens, it
  731. // does not actually remove any of them, including comments and invalid characters.
  732. // The #include handing code takes advantage of this in parsing filenames literally.
  733. case _HASH_INC:
  734. if(!out_enable) {
  735. state = _SKIP_REST;
  736. break;
  737. }
  738. tu->fn_buf_len = 0;
  739. if(n->type == LEXER_TOK_SPACE) {
  740. state = _HASH_INC_SP;
  741. }
  742. else {
  743. fprintf(stderr, "Invalid token after #include: '%s'\n", n->text);
  744. state = _SKIP_REST;
  745. }
  746. break;
  747. case _HASH_INC_SP:
  748. if(n->type == LEXER_TOK_SPACE || n->type == LEXER_TOK_COMMENT) {
  749. break;
  750. }
  751. else if(n->type == LEXER_TOK_STRING) { // normal file name
  752. // strip the double quotes out
  753. if(tu->fn_buf_alloc < n->len) {
  754. tu->fn_buf_alloc = tu->fn_buf_alloc * 2 + n->len;
  755. tu->filename_buffer = realloc(tu->filename_buffer, sizeof(*tu->filename_buffer) * tu->fn_buf_alloc);
  756. }
  757. strncpy(tu->filename_buffer, n->text + 1, n->len - 2);
  758. tu->fn_buf_len = n->len - 2;
  759. tu->filename_buffer[tu->fn_buf_len] = 0;
  760. preprocess_file(tu, ctx, tu->filename_buffer, 0);
  761. state = _SKIP_REST;
  762. }
  763. else if(n->text == _lt) { // system header
  764. state = _HASH_INC_SP_LT;
  765. }
  766. else if(n->type == LEXER_TOK_IDENT) { // expand macro
  767. fprintf(stderr, "Indirect includes NYI\n");
  768. state = _SKIP_REST;
  769. }
  770. else {
  771. fprintf(stderr, "Invalid token after #include (2): '%s' at %s:%d:%d\n", n->text, ctx->file->full_path, n->start_line, n->start_col);
  772. state = _SKIP_REST;
  773. }
  774. break;
  775. case _HASH_INC_SP_LT:
  776. // concat all the token text before reaching another doublequote
  777. // escapes are ignored
  778. if(n->text == _gt) {
  779. // do the include
  780. preprocess_file(tu, ctx, tu->filename_buffer, 1);
  781. state = _SKIP_REST;
  782. }
  783. else if(n->has_newline) {
  784. // issue warning,
  785. // do the include
  786. fprintf(stderr, "Unexpected EOL in #include at %d:%d\n", n->start_line, n->start_col);
  787. state = _NONE;
  788. }
  789. else {
  790. // concat tokens
  791. if(tu->fn_buf_alloc < tu->fn_buf_len + n->len + 1) {
  792. tu->fn_buf_alloc = tu->fn_buf_alloc * 2 + n->len;
  793. tu->filename_buffer = realloc(tu->filename_buffer, sizeof(*tu->filename_buffer) * tu->fn_buf_alloc);
  794. }
  795. strncpy(tu->filename_buffer + tu->fn_buf_len, n->text, n->len);
  796. tu->fn_buf_len += n->len;
  797. tu->filename_buffer[tu->fn_buf_len] = 0;
  798. }
  799. break;
  800. case _SKIP_REST:
  801. // skip everything to the end of the line
  802. if(n->has_newline) {
  803. state = _NONE;
  804. }
  805. break;
  806. }
  807. was_nl = n->has_newline;
  808. }// for
  809. }
  810. // returns a raw invocation struct with no replacements
  811. cpp_macro_invocation_t* collect_invocation_args(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* input, cpp_macro_def_t* m, size_t* cursor) {
  812. char* _lparen = tu->_lparen;
  813. char* _rparen = tu->_rparen;
  814. char* _comma = tu->_comma;
  815. char* _space = tu->_space;
  816. cpp_macro_name_t* mn;
  817. // cpp_macro_def_t* m;
  818. cpp_macro_invocation_t* inv;
  819. // if(!m->fn_like) return NULL;
  820. cpp_token_list_t* in_arg;
  821. char* cached_arg = 0;
  822. int pdepth = 0; // parenthesis nesting depth
  823. int state = _FOUND_NAME;
  824. int i;
  825. int argn = 0;
  826. int was_ws = 0;
  827. // BUG: should read from a cursor of some kind
  828. for(i = *cursor; i < VEC_LEN(&input->tokens); i++) {
  829. lexer_token_t* n = VEC_ITEM(&input->tokens, i);
  830. // printf(" collecting '%s' (arg# %d)\n", n->text, argn);
  831. switch(state) {
  832. case _FOUND_NAME:
  833. if(n->text == _lparen) {
  834. inv = calloc(1, sizeof(*inv));
  835. inv->def = m;
  836. in_arg = calloc(1, sizeof(*in_arg));
  837. state = _INV_ARGS;
  838. }
  839. else if(n->type != LEXER_TOK_SPACE) {
  840. // missing parens
  841. // printf("No parens found for macro invocation '%s', not expanding (found '%s')\n", m->name, n->text);
  842. return NULL;
  843. }
  844. else {
  845. // printf(" skipping space\n");
  846. }
  847. // skip witespace between the name and the opening paren
  848. break;
  849. case _INV_ARGS:
  850. // collect up all the arguments being passed in
  851. if(n->text == _lparen) {
  852. VEC_PUSH(&in_arg->tokens, n);
  853. pdepth++;
  854. }
  855. else if(n->text == _rparen) {
  856. if(pdepth == 0) {
  857. // BUG? should push a space if there was whitespace?
  858. VEC_PUSH(&inv->in_args, in_arg);
  859. /*
  860. printf("found %ld arguments:\n", VEC_LEN(&inv->in_args));
  861. VEC_EACH(&inv->in_args, ai, a) {
  862. printf(" %ld) ", ai);
  863. VEC_EACH(&a->tokens, aai, aa) printf("%s ", aa->text);
  864. printf("\n");
  865. }
  866. printf("\n");
  867. // args done
  868. */
  869. goto DONE;
  870. }
  871. else VEC_PUSH(&in_arg->tokens, n);
  872. pdepth = MAX(0, pdepth - 1);
  873. }
  874. else if(n->text == _comma && pdepth == 0) {
  875. // push the arg
  876. VEC_PUSH(&inv->in_args, in_arg);
  877. in_arg = calloc(1, sizeof(*in_arg));
  878. argn++;
  879. was_ws = 0;
  880. }
  881. else {
  882. if(n->type == LEXER_TOK_SPACE) {
  883. if(VEC_LEN(&in_arg->tokens) == 0) break;
  884. }
  885. // printf(" [%s] pushing body token: '%s'\n", m->name, n->type == LEXER_TOK_SPACE ? " " : n->text);
  886. if(n->type != LEXER_TOK_SPACE) {
  887. if(was_ws) {
  888. // printf(" --space injected\n");
  889. inject_space(tu, ctx, &m->body);
  890. }
  891. // printf(" --arg pushed '%s'\n", n->text);
  892. VEC_PUSH(&in_arg->tokens, n);
  893. was_ws = 0;
  894. }
  895. else {
  896. was_ws = 1;
  897. }
  898. // printf(" arg: %s\n", n->text);
  899. }
  900. break;
  901. }
  902. }
  903. // no parens because the list ended
  904. return NULL;
  905. DONE:
  906. *cursor = i;
  907. return inv;
  908. }
  909. // gets the next token without consideration of macro expansion
  910. // used for seeing if there's an opening paren after the current token
  911. lexer_token_t* peek_token_raw(cpp_context_t* ctx) {
  912. // if(!ctx) return ctx->EOF;
  913. if(!ctx) return NULL;
  914. int i = ctx->cur_index + 1;
  915. if(VEC_LEN(&ctx->tokens->tokens) < i) {
  916. peek_token_raw(ctx->parent);
  917. }
  918. return VEC_ITEM(&ctx->tokens->tokens, i);
  919. }
  920. cpp_macro_def_t* get_macro_def(cpp_tu_t* tu, lexer_token_t* query) {
  921. // if(!tu) return NULL;
  922. cpp_macro_name_t* name = NULL;
  923. if(HT_get(&tu->macros, query->text, &name) || !name) {
  924. return NULL; //return get_macro_def(tu->parent, query);
  925. }
  926. return VEC_TAIL(&name->defs);
  927. }
  928. cpp_token_list_t* expand_token_list(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* in) {
  929. cpp_token_list_t* out = calloc(1, sizeof(*out));
  930. VEC_EACH(&in->tokens, ti, t) {
  931. expand_token(tu, ctx, out, in, &ti);
  932. }
  933. return out;
  934. }
  935. void expand_token(cpp_tu_t* tu, cpp_context_t* ctx, cpp_token_list_t* out, cpp_token_list_t* in, size_t* cursor) {
  936. lexer_token_t* t = VEC_ITEM(&in->tokens, *cursor);
  937. cpp_macro_def_t* m = get_macro_def(tu, t);
  938. if(!m) {
  939. // just a regular token. push it to the output
  940. // printf(" regular token, pushing '%s' to output\n", t->type == LEXER_TOK_SPACE ? " " : t->text);
  941. VEC_PUSH(&out->tokens, t);
  942. }
  943. else if(m->fn_like) {
  944. // printf(" fnlike, checking '%s' for parens\n", t->text);
  945. size_t c2 = *cursor + 1;
  946. cpp_macro_invocation_t* inv = collect_invocation_args(tu, ctx, in, m, &c2);
  947. if(!inv) {
  948. // it's fn like but not being invoked due to lack of subsequent parens
  949. // printf(" non-invoked fnlike, pushing '%s' to output\n", t->text);
  950. VEC_PUSH(&out->tokens, t);
  951. return;
  952. }
  953. // printf(" fnlike, expanding '%s'\n", t->text);
  954. expand_fnlike_macro(tu, ctx, inv);
  955. VEC_CAT(&out->tokens, &inv->output->tokens);
  956. *cursor = c2;
  957. // TODO: process fn-like macro expansion
  958. }
  959. else if(m->obj_like) {
  960. /*
  961. printf(" objlike, expanding '%s' to ->", t->text);
  962. VEC_EACH(&m->body.tokens, bi, b) {
  963. printf("%s ", b->text);
  964. } printf("<-\n");
  965. */
  966. cpp_token_list_t* expanded = expand_token_list(tu, ctx, &m->body);
  967. VEC_CAT(&out->tokens, &expanded->tokens);
  968. VEC_FREE(&expanded->tokens);
  969. free(expanded);
  970. }
  971. else if(m->special) {
  972. // __FILE__, etc
  973. }
  974. }
  975. ssize_t arg_index(cpp_macro_def_t* m, lexer_token_t* name) {
  976. VEC_EACH(&m->args, ani, aname) {
  977. if(aname == name->text) {
  978. return ani;
  979. }
  980. }
  981. return -1;
  982. }
  983. lexer_token_t* next_real_token(cpp_token_list_t* list, size_t* cursor) {
  984. size_t i = *cursor + 1;
  985. // printf("li: %ld, len: %ld\n", i, VEC_LEN(&list->tokens));
  986. for(; i < VEC_LEN(&list->tokens); i++) {
  987. lexer_token_t* t = VEC_ITEM(&list->tokens, i);
  988. if(t->type != LEXER_TOK_SPACE && t->type != LEXER_TOK_COMMENT) {
  989. *cursor = i;
  990. return t;
  991. }
  992. // printf("i: %ld \n", i);
  993. }
  994. return NULL;
  995. }
  996. void expand_fnlike_macro(cpp_tu_t* tu, cpp_context_t* ctx, cpp_macro_invocation_t* inv) {
  997. char* _va_args = tu->_va_args;
  998. char* _va_opt = tu->_va_opt;
  999. char* _va_narg = tu->_va_narg;
  1000. char* _lparen = tu->_lparen;
  1001. char* _rparen = tu->_rparen;
  1002. char* _hash = tu->_hash;
  1003. char* _concat = tu->_concat;
  1004. cpp_macro_def_t* m = inv->def;
  1005. // argument prescan
  1006. // printf(" -- argument prescan --\n");
  1007. VEC_EACH(&inv->in_args, i, arg) {
  1008. VEC_PUSH(&inv->in_args_expanded, expand_token_list(tu, ctx, arg));
  1009. }
  1010. int vararg_count = VEC_LEN(&inv->in_args) - VEC_LEN(&m->args);
  1011. // printf(" -- argument replacement --\n");
  1012. // fill replacement list
  1013. inv->replaced = calloc(1, sizeof(*inv->replaced));
  1014. VEC_EACH(&m->body.tokens, bti, bt) {
  1015. // special lookahead for ##
  1016. if(bti < VEC_LEN(&m->body.tokens) - 1) {
  1017. size_t next_ind = bti;
  1018. lexer_token_t* bt_next = next_real_token(&m->body, &next_ind);
  1019. if(bt_next && bt_next->text == _concat) {
  1020. // printf(" Token pasting operator encountered\n");
  1021. if(bti >= VEC_LEN(&m->body.tokens) - 2) {
  1022. fprintf(stderr, "Token pasting operator at end of macro body\n");
  1023. }
  1024. else {
  1025. // the token after the ##
  1026. size_t c_ind = next_ind;
  1027. lexer_token_t* ct = next_real_token(&m->body, &c_ind);
  1028. // printf(" body tokens being pasted: '%s' ## '%s'\n", bt->text, ct->text);
  1029. lexer_token_t* paste_l, *paste_r;
  1030. ssize_t bai = arg_index(m, bt);
  1031. if(bai > -1) {
  1032. cpp_token_list_t* l_arg_tokens = VEC_ITEM(&inv->in_args, bai);
  1033. // append all but the last of the left tokens (bt)
  1034. for(int i = 0; i < VEC_LEN(&l_arg_tokens->tokens) - 1; i++) {
  1035. VEC_PUSH(&inv->replaced->tokens, VEC_ITEM(&l_arg_tokens->tokens, i));
  1036. }
  1037. paste_l = VEC_ITEM(&l_arg_tokens->tokens, VEC_LEN(&l_arg_tokens->tokens) - 1);
  1038. }
  1039. else {
  1040. // literal token
  1041. paste_l = bt;
  1042. }
  1043. cpp_token_list_t* r_arg_tokens;
  1044. ssize_t cai = arg_index(m, ct);
  1045. if(cai > -1) {
  1046. // handle the argument replacement
  1047. r_arg_tokens = VEC_ITEM(&inv->in_args, cai);
  1048. paste_r = VEC_ITEM(&r_arg_tokens->tokens, 0);
  1049. }
  1050. else {
  1051. // literal token
  1052. paste_r = ct;
  1053. }
  1054. // printf(" literal tokens being pasted: '%s' ## '%s'\n", paste_l->text, paste_r->text);
  1055. // paste the last of bt with the first of ct
  1056. // BUG: right now the CPP will not validate if it's a valid token. It will just paste it.
  1057. inject_pasted(tu, ctx, inv->replaced, paste_l, paste_r);
  1058. // append the rest of ct tokens
  1059. if(cai > -1) {
  1060. for(int i = 1; i < VEC_LEN(&r_arg_tokens->tokens); i++) {
  1061. VEC_PUSH(&inv->replaced->tokens, VEC_ITEM(&r_arg_tokens->tokens, i));
  1062. }
  1063. }
  1064. bti = c_ind + 1;
  1065. continue;
  1066. }
  1067. }
  1068. }
  1069. if(bt->text == _va_args) {
  1070. // special __VA_ARGS__ handling
  1071. size_t start_arg = VEC_LEN(&m->args);
  1072. for(int i = start_arg; i < VEC_LEN(&inv->in_args); i++) {
  1073. if(i > start_arg) inject_comma(tu, ctx, inv->replaced);
  1074. VEC_CAT(&inv->replaced->tokens, &VEC_ITEM(&inv->in_args_expanded, i)->tokens);
  1075. }
  1076. goto ARG_REPLACED;
  1077. }
  1078. else if(bt->text == _va_opt) {
  1079. int pdepth = 0;
  1080. int got_lparen = 0;
  1081. for(bti++; bti < VEC_LEN(&m->body.tokens); bti++) {
  1082. bt = VEC_ITEM(&m->body.tokens, bti);
  1083. if(!got_lparen) {
  1084. if(bt->type == LEXER_TOK_SPACE) continue;
  1085. if(bt->text == _lparen) {
  1086. got_lparen = 1;
  1087. continue;
  1088. }
  1089. fprintf(stderr, "Missing lparen after __VA_OPT__\n");
  1090. break;
  1091. }
  1092. if(bt->text == _lparen) {
  1093. pdepth++;
  1094. }
  1095. else if(bt->text == _rparen) {
  1096. if(pdepth == 0) goto ARG_REPLACED;
  1097. pdepth--;
  1098. }
  1099. if(vararg_count > 0) { // __VA_OPT__ only works if there are args left
  1100. if(bt->text == _va_args) {
  1101. size_t start_arg = VEC_LEN(&m->args);
  1102. for(int i = start_arg; i < VEC_LEN(&inv->in_args); i++) {
  1103. if(i > start_arg) inject_comma(tu, ctx, inv->replaced);
  1104. VEC_CAT(&inv->replaced->tokens, &VEC_ITEM(&inv->in_args_expanded, i)->tokens);
  1105. }
  1106. }
  1107. else if(bt->text == _va_narg) {
  1108. inject_number(tu, ctx, inv->replaced, vararg_count);
  1109. }
  1110. else {
  1111. VEC_PUSH(&inv->replaced->tokens, bt);
  1112. }
  1113. }
  1114. }
  1115. }
  1116. else if(bt->text == _va_narg) {
  1117. inject_number(tu, ctx, inv->replaced, vararg_count);
  1118. goto ARG_REPLACED;
  1119. }
  1120. else if(bt->text == _hash) {
  1121. bti++;
  1122. if(bti >= VEC_LEN(&m->body.tokens)) {
  1123. fprintf(stderr, "Stringifier operator at end of macro body.\n");
  1124. break;
  1125. }
  1126. bt = VEC_ITEM(&m->body.tokens, bti);
  1127. // TODO: #__VA_ARGS__, et al
  1128. VEC_EACH(&m->args, ani, aname) {
  1129. if(aname == bt->text) {
  1130. inject_stringified(tu, ctx, inv->replaced, VEC_ITEM(&inv->in_args, ani));
  1131. goto ARG_REPLACED;
  1132. }
  1133. }
  1134. goto ARG_REPLACED;
  1135. }
  1136. // TODO: implement lookahead
  1137. else if(bt->text == _concat) {
  1138. bti++;
  1139. if(bti >= VEC_LEN(&m->body.tokens)) {
  1140. fprintf(stderr, "Token pasting operator at end of macro body.\n");
  1141. break;
  1142. }
  1143. }
  1144. else {
  1145. // normal tokens
  1146. VEC_EACH(&m->args, ani, aname) {
  1147. if(aname == bt->text) {
  1148. VEC_CAT(&inv->replaced->tokens, &VEC_ITEM(&inv->in_args_expanded, ani)->tokens);
  1149. goto ARG_REPLACED;
  1150. }
  1151. }
  1152. }
  1153. // no arg replacement done.
  1154. VEC_PUSH(&inv->replaced->tokens, bt);
  1155. ARG_REPLACED:
  1156. }
  1157. // re-scan the final list
  1158. inv->output = calloc(1, sizeof(*inv->output));
  1159. // printf(" -- final rescan --\n");
  1160. inv->output = expand_token_list(tu, ctx, inv->replaced);
  1161. // TODO: mark macro disabled
  1162. return;
  1163. }
  1164. void undef_macro(cpp_tu_t* tu, char* name) {
  1165. cpp_macro_name_t* mn;
  1166. if(!HT_get(&tu->macros, name, &mn)) {
  1167. // printf(" undefining %s\n", name);
  1168. VEC_FREE(&mn->defs);
  1169. free(mn);
  1170. HT_delete(&tu->macros, name);
  1171. }
  1172. // if(ctx->parent) undef_macro(ctx->parent, name);
  1173. }