Repository navigation
Expand file tree
/
Copy pathccpp_body.h
More file actions
5930 lines (5625 loc) · 230 KB
/
Copy pathccpp_body.h
File metadata and controls
5930 lines (5625 loc) · 230 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef CCPP_BARE
typedef unsigned long size_t;
typedef long time_t;
typedef struct __neo_FILE FILE;
extern int* __errno_location(void);
#define errno (*__errno_location())
extern FILE* stdin;
extern FILE* stdout;
extern FILE* stderr;
struct utsname {
char sysname[65];
char nodename[65];
char release[65];
char version[65];
char machine[65];
char domainname[65];
};
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
long tm_gmtoff;
const char* tm_zone;
};
#ifndef NULL
#define NULL ((void*)0)
#endif
#ifndef EOF
#define EOF (-1)
#endif
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif
#ifndef UINT_MAX
#define UINT_MAX 4294967295U
#endif
#ifndef LONG_MAX
#define LONG_MAX 9223372036854775807L
#endif
#ifndef ULONG_MAX
#define ULONG_MAX 18446744073709551615UL
#endif
#ifndef LLONG_MAX
#define LLONG_MAX 9223372036854775807LL
#endif
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif
#ifndef ERANGE
#define ERANGE 34
#endif
#define true 1
#define false 0
typedef int bool;
void exit(int status);
void perror(const char* s);
void* malloc(size_t size);
void* calloc(size_t nmemb, size_t size);
void* realloc(void* ptr, size_t size);
void free(void* ptr);
int strcmp(const char* s1, const char* s2);
int strncmp(const char* s1, const char* s2, size_t n);
char* strcpy(char* dst, const char* src);
char* strncpy(char* dst, const char* src, size_t n);
char* strcat(char* dst, const char* src);
size_t strlen(const char* s);
void* memcpy(void* dst, const void* src, size_t n);
void* memmove(void* dst, const void* src, size_t n);
void* memset(void* dst, int c, size_t n);
int memcmp(const void* s1, const void* s2, size_t n);
void* memchr(const void* s, int c, size_t n);
char* strchr(const char* s, int c);
char* strrchr(const char* s, int c);
char* strstr(const char* haystack, const char* needle);
long strtol(const char* nptr, char** endptr, int base);
unsigned long strtoul(const char* nptr, char** endptr, int base);
unsigned long long strtoull(const char* nptr, char** endptr, int base);
int snprintf(char* out, size_t out_size, const char* fmt, ...);
int fprintf(FILE* f, const char* fmt, ...);
int fputs(const char* s, FILE* f);
int fputc(int c, FILE* f);
int fgetc(FILE* f);
int ungetc(int c, FILE* f);
int fclose(FILE* f);
FILE* fopen(const char* path, const char* mode);
char* fgets(char* s, int size, FILE* f);
int fflush(FILE* f);
char* getenv(const char* name);
time_t time(time_t* t);
struct tm* localtime(const time_t* t);
int uname(struct utsname* buf);
static int isspace(int c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f'; }
static int isalpha(int c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); }
static int isdigit(int c) { return c >= '0' && c <= '9'; }
static int isalnum(int c) { return isalpha(c) || isdigit(c); }
static int isxdigit(int c) { return isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); }
static int toupper(int c) { return (c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c; }
static int tolower(int c) { return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; }
#else
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <time.h>
#endif
#define PP_RESCAN_MAX 1024
typedef struct {
char *name;
char *value; // replacement text for both object/function macros
bool is_func;
char **params; // parameter names if is_func
size_t nparams;
char *var_param; // name for GNU-style named variadic macros (e.g., args...)
bool is_variadic;
bool self_ref;
} Macro;
typedef struct MacroHashNode {
size_t macro_index;
struct MacroHashNode *next;
} MacroHashNode;
typedef struct {
Macro *items;
size_t len;
size_t cap;
MacroHashNode **buckets;
size_t nbuckets;
} MacroTable;
typedef struct {
char *name;
char *sig;
} MacroSnapItem;
typedef struct MacroSnapshot {
MacroSnapItem *items;
size_t len;
size_t cap;
} MacroSnapshot;
static bool macro_is_vararg_token(const Macro *m, const char *tok) {
if (!m || !m->is_variadic || !tok) return false;
if (strcmp(tok, "__VA_ARGS__") == 0) return true;
return m->var_param && strcmp(tok, m->var_param) == 0;
}
static void die(const char *msg) {
perror(msg);
exit(1);
}
static void *xrealloc(void *p, size_t n) {
void *q = realloc(p, n);
if (!q && n != 0) die("realloc");
return q;
}
static char *xstrdup(const char *s) {
if (!s) return NULL;
size_t n = strlen(s) + 1;
char *p = (char *)malloc(n);
if (!p) die("malloc");
memcpy(p, s, n);
return p;
}
static bool file_exists(const char *path) {
if (!path || !*path) return false;
FILE *f = fopen(path, "r");
if (f) { fclose(f); return true; }
return false;
}
static void mtable_init(MacroTable *t) {
t->items = NULL;
t->len = 0;
t->cap = 0;
t->buckets = NULL;
t->nbuckets = 0;
}
static size_t macro_hash_name(const char *name) {
size_t hash = 1469598103934665603ull;
const unsigned char *p = (const unsigned char *)name;
while (*p) {
hash ^= (size_t)*p++;
hash *= 1099511628211ull;
}
return hash;
}
static size_t macro_hash_name_n(const char *name, size_t len) {
size_t hash = 1469598103934665603ull;
const unsigned char *p = (const unsigned char *)name;
for (size_t i = 0; i < len; ++i) {
hash ^= (size_t)p[i];
hash *= 1099511628211ull;
}
return hash;
}
static size_t mtable_bucket_index(const MacroTable *t, const char *name) {
return t->nbuckets ? (macro_hash_name(name) % t->nbuckets) : 0;
}
static size_t mtable_bucket_index_n(const MacroTable *t, const char *name, size_t len) {
return t->nbuckets ? (macro_hash_name_n(name, len) % t->nbuckets) : 0;
}
static void mtable_rehash(MacroTable *t, size_t nbuckets) {
MacroHashNode **new_buckets = (MacroHashNode **)calloc(nbuckets, sizeof(MacroHashNode *));
if (!new_buckets) die("calloc");
if (t->buckets) {
for (size_t i = 0; i < t->nbuckets; ++i) {
MacroHashNode *node = t->buckets[i];
while (node) {
MacroHashNode *next = node->next;
size_t bucket = macro_hash_name(t->items[node->macro_index].name) % nbuckets;
node->next = new_buckets[bucket];
new_buckets[bucket] = node;
node = next;
}
}
free(t->buckets);
}
t->buckets = new_buckets;
t->nbuckets = nbuckets;
}
static void mtable_maybe_grow_buckets(MacroTable *t, size_t next_len) {
size_t target = t->nbuckets;
if (target == 0) {
target = 64;
}
while (next_len * 4 >= target * 3) {
target *= 2;
}
if (target != t->nbuckets) {
mtable_rehash(t, target);
}
}
static MacroHashNode **mtable_find_slot(MacroTable *t, const char *name) {
if (!t->buckets || t->nbuckets == 0) {
return NULL;
}
MacroHashNode **slot = &t->buckets[mtable_bucket_index(t, name)];
while (*slot) {
if (strcmp(t->items[(*slot)->macro_index].name, name) == 0) {
return slot;
}
slot = &(*slot)->next;
}
return NULL;
}
static void mtable_index_insert(MacroTable *t, size_t macro_index) {
mtable_maybe_grow_buckets(t, t->len);
size_t bucket = mtable_bucket_index(t, t->items[macro_index].name);
MacroHashNode *node = (MacroHashNode *)malloc(sizeof(MacroHashNode));
if (!node) die("malloc");
node->macro_index = macro_index;
node->next = t->buckets[bucket];
t->buckets[bucket] = node;
}
static void mtable_index_remove(MacroTable *t, const char *name) {
MacroHashNode **slot = mtable_find_slot(t, name);
if (!slot) {
return;
}
MacroHashNode *node = *slot;
*slot = node->next;
free(node);
}
static void mtable_index_update(MacroTable *t, const char *name, size_t macro_index) {
MacroHashNode **slot = mtable_find_slot(t, name);
if (slot) {
(*slot)->macro_index = macro_index;
}
}
static size_t mtable_find_index(const MacroTable *t, const char *name) {
if (!t->buckets || t->nbuckets == 0) {
return (size_t)-1;
}
MacroHashNode *node = t->buckets[mtable_bucket_index(t, name)];
while (node) {
if (strcmp(t->items[node->macro_index].name, name) == 0) {
return node->macro_index;
}
node = node->next;
}
return (size_t)-1;
}
static size_t mtable_find_index_n(const MacroTable *t, const char *name, size_t len) {
if (!t->buckets || t->nbuckets == 0) {
return (size_t)-1;
}
MacroHashNode *node = t->buckets[mtable_bucket_index_n(t, name, len)];
while (node) {
const char *entry_name = t->items[node->macro_index].name;
if (strncmp(entry_name, name, len) == 0 && entry_name[len] == '\0') {
return node->macro_index;
}
node = node->next;
}
return (size_t)-1;
}
static Macro *mtable_append(MacroTable *t, const char *name) {
if (t->len == t->cap) {
size_t ncap = t->cap ? t->cap * 2 : 8;
Macro *nitems = (Macro *)xrealloc(t->items, ncap * sizeof(Macro));
t->items = nitems;
t->cap = ncap;
}
size_t index = t->len++;
Macro *m = &t->items[index];
m->name = xstrdup(name);
m->value = NULL;
m->is_func = false;
m->params = NULL;
m->nparams = 0;
m->var_param = NULL;
m->is_variadic = false;
m->self_ref = false;
mtable_index_insert(t, index);
return m;
}
static void mtable_free(MacroTable *t) {
if (!t) return;
for (size_t i = 0; i < t->len; ++i) {
free(t->items[i].name);
free(t->items[i].value);
if (t->items[i].params) {
for (size_t j=0;j<t->items[i].nparams;++j) free(t->items[i].params[j]);
free(t->items[i].params);
}
free(t->items[i].var_param);
}
free(t->items);
if (t->buckets) {
for (size_t i = 0; i < t->nbuckets; ++i) {
MacroHashNode *node = t->buckets[i];
while (node) {
MacroHashNode *next = node->next;
free(node);
node = next;
}
}
}
free(t->buckets);
}
static void mtable_unset(MacroTable *t, const char *name) {
size_t index = mtable_find_index(t, name);
if (index == (size_t)-1) {
return;
}
mtable_index_remove(t, name);
free(t->items[index].name);
free(t->items[index].value);
if (t->items[index].params) {
for (size_t j = 0; j < t->items[index].nparams; ++j) free(t->items[index].params[j]);
free(t->items[index].params);
}
free(t->items[index].var_param);
size_t last = t->len - 1;
if (index != last) {
t->items[index] = t->items[last];
mtable_index_update(t, t->items[index].name, index);
}
t->len--;
}
static Macro *mtable_find(MacroTable *t, const char *name) {
size_t index = mtable_find_index(t, name);
return index == (size_t)-1 ? NULL : &t->items[index];
}
static Macro *mtable_find_n(MacroTable *t, const char *name, size_t len) {
size_t index = mtable_find_index_n(t, name, len);
return index == (size_t)-1 ? NULL : &t->items[index];
}
static bool macro_value_contains_ident(const char *val, const char *name) {
if (!val || !name || !*name) return false;
enum { S_CODE, S_DQ, S_SQ, S_SL, S_BL } st = S_CODE;
int bl_depth = 0;
for (size_t i = 0; val[i]; ++i) {
char c = val[i];
if (st == S_CODE) {
if (c == '"') { st = S_DQ; continue; }
if (c == '\'') { st = S_SQ; continue; }
if (c == '/' && val[i+1] == '/') { st = S_SL; i++; continue; }
if (c == '/' && val[i+1] == '*') { st = S_BL; bl_depth = 1; i++; continue; }
if (isalpha((unsigned char)c) || c == '_') {
char id[256]; size_t j = 0; size_t k = i;
while (val[k] && (isalnum((unsigned char)val[k]) || val[k] == '_')) {
if (j + 1 < sizeof id) id[j++] = val[k];
k++;
}
id[j] = '\0';
if (strcmp(id, name) == 0) return true;
i = k - 1;
}
} else if (st == S_DQ) {
if (c == '"' && val[i-1] != '\\') st = S_CODE;
} else if (st == S_SQ) {
if (c == '\'' && val[i-1] != '\\') st = S_CODE;
} else if (st == S_SL) {
if (c == '\n' || c == '\r') st = S_CODE;
} else if (st == S_BL) {
if (c == '/' && val[i+1] == '*') { bl_depth++; i++; continue; }
if (c == '*' && val[i+1] == '/') {
bl_depth--; i++;
if (bl_depth <= 0) { bl_depth = 0; st = S_CODE; }
continue;
}
}
}
return false;
}
static void mtable_set_obj(MacroTable *t, const char *name, const char *value) {
// If exists, replace
Macro *m = mtable_find(t, name);
if (m) {
free(m->value);
if (m->params) { for (size_t j=0;j<m->nparams;++j) free(m->params[j]); free(m->params); m->params=NULL; m->nparams=0; }
if (m->var_param) { free(m->var_param); m->var_param = NULL; }
m->is_func = false;
m->value = xstrdup(value ? value : "");
m->self_ref = macro_value_contains_ident(m->value, name);
return;
}
m = mtable_append(t, name);
m->value = xstrdup(value ? value : "");
m->self_ref = macro_value_contains_ident(m->value, name);
}
static const char *mtable_get(const MacroTable *t, const char *name) {
size_t index = mtable_find_index(t, name);
if (index == (size_t)-1 || t->items[index].is_func) {
return NULL;
}
return t->items[index].value;
}
typedef struct {
bool initialized;
bool ok;
struct utsname uts;
} HostInfoCache;
static HostInfoCache g_host_info = {0};
static void init_host_info(void) {
if (g_host_info.initialized) return;
g_host_info.initialized = true;
g_host_info.ok = uname(&g_host_info.uts) == 0;
}
static int uname_has(const char *token) {
if (!token || !*token) return 0;
init_host_info();
if (!g_host_info.ok) return 0;
return strstr(g_host_info.uts.sysname, token) != NULL ||
strstr(g_host_info.uts.nodename, token) != NULL ||
strstr(g_host_info.uts.release, token) != NULL ||
strstr(g_host_info.uts.version, token) != NULL ||
strstr(g_host_info.uts.machine, token) != NULL;
}
static void set_host_macros(MacroTable *t) {
int is64 = (sizeof(void*) == 8);
union { unsigned int u; unsigned char b[4]; } endu;
endu.u = 0x01020304u;
int is_little = (endu.b[0] == 0x04);
// Common byte/bit and size hints
mtable_set_obj(t, "__CHAR_BIT__", "8");
mtable_set_obj(t, "__ORDER_LITTLE_ENDIAN__", "1234");
mtable_set_obj(t, "__ORDER_BIG_ENDIAN__", "4321");
mtable_set_obj(t, "__ORDER_PDP_ENDIAN__", "3412");
mtable_set_obj(t, "__BYTE_ORDER__", is_little ? "1234" : "4321");
if (is_little) mtable_set_obj(t, "__LITTLE_ENDIAN__", "1"); else mtable_set_obj(t, "__BIG_ENDIAN__", "1");
const char *psz = is64 ? "8" : "4";
mtable_set_obj(t, "__SIZEOF_POINTER__", psz);
mtable_set_obj(t, "__SIZEOF_SIZE_T__", psz);
mtable_set_obj(t, "__SIZEOF_SHORT__", "2");
mtable_set_obj(t, "__SIZEOF_INT__", "4");
mtable_set_obj(t, "__SIZEOF_LONG_LONG__", "8");
mtable_set_obj(t, "__SIZEOF_LONG__", is64 ? "8" : "4");
const char *i8 = "signed char";
const char *u8 = "unsigned char";
const char *i16 = "short int";
const char *u16 = "unsigned short int";
const char *i32 = "int";
const char *u32 = "unsigned int";
const char *i64 = is64 ? "long int" : "long long int";
const char *u64 = is64 ? "unsigned long int" : "unsigned long long int";
const char *iptr = is64 ? "long int" : "int";
const char *uptr = is64 ? "unsigned long int" : "unsigned int";
if (!mtable_get(t, "__CHAR16_TYPE__")) mtable_set_obj(t, "__CHAR16_TYPE__", u16);
if (!mtable_get(t, "__CHAR32_TYPE__")) mtable_set_obj(t, "__CHAR32_TYPE__", u32);
if (!mtable_get(t, "__INT8_TYPE__")) mtable_set_obj(t, "__INT8_TYPE__", i8);
if (!mtable_get(t, "__UINT8_TYPE__")) mtable_set_obj(t, "__UINT8_TYPE__", u8);
if (!mtable_get(t, "__INT16_TYPE__")) mtable_set_obj(t, "__INT16_TYPE__", i16);
if (!mtable_get(t, "__UINT16_TYPE__")) mtable_set_obj(t, "__UINT16_TYPE__", u16);
if (!mtable_get(t, "__INT32_TYPE__")) mtable_set_obj(t, "__INT32_TYPE__", i32);
if (!mtable_get(t, "__UINT32_TYPE__")) mtable_set_obj(t, "__UINT32_TYPE__", u32);
if (!mtable_get(t, "__INT64_TYPE__")) mtable_set_obj(t, "__INT64_TYPE__", i64);
if (!mtable_get(t, "__UINT64_TYPE__")) mtable_set_obj(t, "__UINT64_TYPE__", u64);
if (!mtable_get(t, "__INTPTR_TYPE__")) mtable_set_obj(t, "__INTPTR_TYPE__", iptr);
if (!mtable_get(t, "__UINTPTR_TYPE__")) mtable_set_obj(t, "__UINTPTR_TYPE__", uptr);
if (!mtable_get(t, "__INTMAX_TYPE__")) mtable_set_obj(t, "__INTMAX_TYPE__", i64);
if (!mtable_get(t, "__UINTMAX_TYPE__")) mtable_set_obj(t, "__UINTMAX_TYPE__", u64);
if (!mtable_get(t, "__INT_LEAST8_TYPE__")) mtable_set_obj(t, "__INT_LEAST8_TYPE__", i8);
if (!mtable_get(t, "__UINT_LEAST8_TYPE__")) mtable_set_obj(t, "__UINT_LEAST8_TYPE__", u8);
if (!mtable_get(t, "__INT_LEAST16_TYPE__")) mtable_set_obj(t, "__INT_LEAST16_TYPE__", i16);
if (!mtable_get(t, "__UINT_LEAST16_TYPE__")) mtable_set_obj(t, "__UINT_LEAST16_TYPE__", u16);
if (!mtable_get(t, "__INT_LEAST32_TYPE__")) mtable_set_obj(t, "__INT_LEAST32_TYPE__", i32);
if (!mtable_get(t, "__UINT_LEAST32_TYPE__")) mtable_set_obj(t, "__UINT_LEAST32_TYPE__", u32);
if (!mtable_get(t, "__INT_LEAST64_TYPE__")) mtable_set_obj(t, "__INT_LEAST64_TYPE__", i64);
if (!mtable_get(t, "__UINT_LEAST64_TYPE__")) mtable_set_obj(t, "__UINT_LEAST64_TYPE__", u64);
if (!mtable_get(t, "__INT_FAST8_TYPE__")) mtable_set_obj(t, "__INT_FAST8_TYPE__", i32);
if (!mtable_get(t, "__UINT_FAST8_TYPE__")) mtable_set_obj(t, "__UINT_FAST8_TYPE__", u32);
if (!mtable_get(t, "__INT_FAST16_TYPE__")) mtable_set_obj(t, "__INT_FAST16_TYPE__", i32);
if (!mtable_get(t, "__UINT_FAST16_TYPE__")) mtable_set_obj(t, "__UINT_FAST16_TYPE__", u32);
if (!mtable_get(t, "__INT_FAST32_TYPE__")) mtable_set_obj(t, "__INT_FAST32_TYPE__", i32);
if (!mtable_get(t, "__UINT_FAST32_TYPE__")) mtable_set_obj(t, "__UINT_FAST32_TYPE__", u32);
if (!mtable_get(t, "__INT_FAST64_TYPE__")) mtable_set_obj(t, "__INT_FAST64_TYPE__", i64);
if (!mtable_get(t, "__UINT_FAST64_TYPE__")) mtable_set_obj(t, "__UINT_FAST64_TYPE__", u64);
if (!mtable_get(t, "__INT_MAX__")) {
char buf[64];
snprintf(buf, sizeof buf, "%d", INT_MAX);
mtable_set_obj(t, "__INT_MAX__", buf);
}
if (!mtable_get(t, "__FLT_MIN__")) mtable_set_obj(t, "__FLT_MIN__", "1.17549435e-38F");
if (!mtable_get(t, "__DBL_MIN__")) mtable_set_obj(t, "__DBL_MIN__", "2.2250738585072014e-308");
if (!mtable_get(t, "__LDBL_MIN__")) mtable_set_obj(t, "__LDBL_MIN__", "3.36210314311209350626e-4932L");
int is_android = uname_has("Android") || getenv("ANDROID_ROOT") != NULL;
int is_linux = uname_has("Linux");
int is_mac = uname_has("Darwin");
int is_bsd = uname_has("FreeBSD") || uname_has("OpenBSD") || uname_has("NetBSD") || uname_has("DragonFly");
init_host_info();
int is_unix = is_linux || is_android || is_mac || is_bsd || g_host_info.ok;
if (is_linux || is_android) {
mtable_set_obj(t, "__linux__", "1");
mtable_set_obj(t, "linux", "1");
mtable_set_obj(t, "__gnu_linux__", "1");
mtable_set_obj(t, "__LINUX__", "1");
mtable_set_obj(t, "__unix__", "1");
mtable_set_obj(t, "__unix", "1");
mtable_set_obj(t, "__ELF__", "1");
mtable_set_obj(t, "__WCHAR_TYPE__", "int");
mtable_set_obj(t, "__gwchar_t", "wchar_t");
mtable_set_obj(t, "__WCHAR_TYPE__", "int");
mtable_set_obj(t, "__WINT_TYPE__", "unsigned int");
mtable_set_obj(t, "__SIZE_TYPE__", "unsigned long");
mtable_set_obj(t, "__SIZE_TYPE__", "unsigned long");
mtable_set_obj(t, "__PTRDIFF_TYPE__", "long");
mtable_set_obj(t, "__WCHAR_MAX__", "0x7fffffff");
mtable_set_obj(t, "__WCHAR_MINU__", "-0x7fffffff-1");
if (is64) mtable_set_obj(t, "__LP64__", "1");
mtable_set_obj(t, "_GNU_SOURCE", "1");
mtable_set_obj(t, "_DEFAULT_SOURCE", "1");
if (is_android) {
mtable_set_obj(t, "__ANDROID__", "1");
}
}
else if (is_mac) {
mtable_set_obj(t, "__APPLE__", "1");
mtable_set_obj(t, "__MACH__", "1");
mtable_set_obj(t, "__MAC__", "1");
if (is64) mtable_set_obj(t, "__LP64__", "1");
mtable_set_obj(t, "_DARWIN_C_SOURCE", "1");
mtable_set_obj(t, "_POSIX_C_SOURCE", "200809L");
mtable_set_obj(t, "__DARWIN_C_LEVEL", "200809L");
mtable_set_obj(t, "__DARWIN_NBBY", "8");
mtable_set_obj(t, "__DARWIN_NFDBITS", "32");
mtable_set_obj(t, "__DARWIN_NULL", "0");
mtable_set_obj(t, "_CACHED_RUNES", "256");
mtable_set_obj(t, "__WCHAR_TYPE__", "int");
mtable_set_obj(t, "__WINT_TYPE__", "unsigned int");
mtable_set_obj(t, "__SIZE_TYPE__", "unsigned long");
mtable_set_obj(t, "__SIZE_TYPE__", "unsigned long");
mtable_set_obj(t, "__PTRDIFF_TYPE__", "long");
mtable_set_obj(t, "__WCHAR_MAX__", "0x7fffffff");
mtable_set_obj(t, "__WCHAR_MINU__", "-0x7fffffff-1");
}
else if (is_bsd) {
mtable_set_obj(t, "__unix__", "1");
mtable_set_obj(t, "__unix", "1");
if (is64) mtable_set_obj(t, "__LP64__", "1");
}
else if (is_unix) {
mtable_set_obj(t, "__unix__", "1");
mtable_set_obj(t, "__unix", "1");
if (is64) mtable_set_obj(t, "__LP64__", "1");
}
int x86_64 = uname_has("x86_64");
int aarch64 = uname_has("aarch64") || uname_has("arm64");
int i386 = uname_has("i386");
int riscv = uname_has("riscv64");
if(x86_64) {
mtable_set_obj(t, "__x86_64__", "1");
mtable_set_obj(t, "__x86_64__", "1");
mtable_set_obj(t, "__amd64__", "1");
mtable_set_obj(t, "__LP64__", "1");
mtable_set_obj(t, "__WORDSIZE", "64");
}
else if(aarch64) {
mtable_set_obj(t, "__aarch64__", "1");
mtable_set_obj(t, "__arm64__", "1");
mtable_set_obj(t, "__LP64__", "1");
mtable_set_obj(t, "__WORDSIZE", "64");
}
else if(i386) {
mtable_set_obj(t, "__i386__", "1");
mtable_set_obj(t, "__WORDSIZE", "32");
}
else if(riscv) {
mtable_set_obj(t, "__riscv", "1");
mtable_set_obj(t, "__riscv_xlen", is64 ? "64" : "32");
if (is64) {
mtable_set_obj(t, "__LP64__", "1");
mtable_set_obj(t, "__WORDSIZE", "64");
}
else {
mtable_set_obj(t, "__WORDSIZE", "32");
}
}
}
static void set_datetime_macros(MacroTable *t) {
char datebuf[32];
char timebuf[16];
time_t now = time(NULL);
struct tm *tm_now = localtime(&now);
static const char *months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
if (tm_now && tm_now->tm_mon >= 0 && tm_now->tm_mon < 12) {
snprintf(datebuf, sizeof(datebuf), "\"%s %2d %04d\"",
months[tm_now->tm_mon], tm_now->tm_mday, tm_now->tm_year + 1900);
snprintf(timebuf, sizeof(timebuf), "\"%02d:%02d:%02d\"",
tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
}
else {
snprintf(datebuf, sizeof(datebuf), "\"??? ?? ????\"");
snprintf(timebuf, sizeof(timebuf), "\"??:??:??\"");
}
mtable_set_obj(t, "__DATE__", datebuf);
mtable_set_obj(t, "__TIME__", timebuf);
}
struct PPOpts {
const char **incdirs;
size_t nincdirs;
const char **sysincdirs;
size_t nsysincdirs;
const char **once_paths;
size_t nonce_paths;
const char **cmd_defs; size_t ncmd_defs;
const char **cmd_undefs; size_t ncmd_undefs;
const char **cmd_includes; size_t ncmd_includes;
// Track currently-open include chain to avoid cycles (A->B->A...)
const char **open_stack;
size_t nopen_stack;
// Whether to keep comments in the output (like gcc -C). Currently, comments
// are preserved by default; this flag exists to accept the -C option
// without error and for potential future use.
bool keep_comments;
};
typedef struct PPOpts PPOpts;
static bool process_define(MacroTable *t, char *line);
static bool parse_ident(const char *p, size_t *adv, char *buf, size_t bufsz);
static void mtable_set_func(MacroTable *t, const char *name, char **params, size_t nparams,
bool variadic, char *var_param, const char *value);
static void apply_predefined_macros(MacroTable *t, const PPOpts *opts) {
// Baseline C macros
mtable_set_obj(t, "__STDC__", "1");
mtable_set_obj(t, "__STDC_VERSION__", "201112L");
mtable_set_obj(t, "__STDC_NO_VLA__", "1");
mtable_set_obj(t, "__STDC_HOSTED__", "1");
set_datetime_macros(t);
// if (!mtable_get(t, "true")) mtable_set_obj(t, "true", "1");
// if (!mtable_get(t, "false")) mtable_set_obj(t, "false", "0");
if (!mtable_get(t, "__bool_true_false_are_defined")) mtable_set_obj(t, "__bool_true_false_are_defined", "1");
if (!mtable_get(t, "__ATOMIC_RELAXED")) mtable_set_obj(t, "__ATOMIC_RELAXED", "0");
if (!mtable_get(t, "__ATOMIC_CONSUME")) mtable_set_obj(t, "__ATOMIC_CONSUME", "1");
if (!mtable_get(t, "__ATOMIC_ACQUIRE")) mtable_set_obj(t, "__ATOMIC_ACQUIRE", "2");
if (!mtable_get(t, "__ATOMIC_RELEASE")) mtable_set_obj(t, "__ATOMIC_RELEASE", "3");
if (!mtable_get(t, "__ATOMIC_ACQ_REL")) mtable_set_obj(t, "__ATOMIC_ACQ_REL", "4");
if (!mtable_get(t, "__ATOMIC_SEQ_CST")) mtable_set_obj(t, "__ATOMIC_SEQ_CST", "5");
// Provide common compiler-identification macros so system headers don't complain
mtable_set_obj(t, "__GNUC__", "3");
mtable_set_obj(t, "__GNUC_MINOR__", "2");
mtable_set_obj(t, "__clang__", "1");
mtable_set_obj(t, "__NEO_C__", "1");
mtable_set_obj(t, "__BEGIN_DECLS", "");
mtable_set_obj(t, "__END_DECLS", "");
// Common feature-test helpers frequently used in system headers
process_define(t, "#define __GNUC_PREREQ(ma,mi) (((__GNUC__)*100 + (__GNUC_MINOR__)) >= ((ma)*100 + (mi)))");
process_define(t, "#define __has_include(x) 0");
process_define(t, "#define __has_include_next(x) 0");
// Detect host OS/arch/endianness using runtime hints when possible
set_host_macros(t);
// Provide conservative fallbacks for feature/attribute checks
process_define(t, "#define __has_feature(x) 0");
process_define(t, "#define __has_extension(x) 0");
process_define(t, "#define __has_attribute(x) 0");
process_define(t, "#define __has_builtin(x) 0");
process_define(t, "#define __has_c_attribute(x) 0");
// Common POSIX access() mode constants as fallbacks
process_define(t, "#define F_OK 0");
process_define(t, "#define X_OK 1");
process_define(t, "#define W_OK 2");
process_define(t, "#define R_OK 4");
// Suppress SIMD decl attribute markers from glibc headers for compatibility.
process_define(t, "#define __SIMD_DECL(function)");
// OpenSSL headers rely on these attribute/visibility helpers. Provide
// no-op fallbacks so unknown tokens don't leak into neo-c parsing.
if (!mtable_get(t, "ossl_inline")) mtable_set_obj(t, "ossl_inline", "");
if (!mtable_get(t, "ossl_unused")) mtable_set_obj(t, "ossl_unused", "");
if (!mtable_get(t, "ossl_noreturn")) mtable_set_obj(t, "ossl_noreturn", "");
if (!mtable_get(t, "OPENSSL_EXPORT")) mtable_set_obj(t, "OPENSSL_EXPORT", "");
if (!mtable_get(t, "OPENSSL_EXTERN")) mtable_set_obj(t, "OPENSSL_EXTERN", "extern");
if (!mtable_get(t, "__owur")) mtable_set_obj(t, "__owur", "");
if (!mtable_get(t, "ossl_ssize_t")) mtable_set_obj(t, "ossl_ssize_t", "ssize_t");
if (opts) {
for (size_t di=0; di<opts->ncmd_defs; ++di) {
const char *d = opts->cmd_defs[di];
const char *eq = strchr(d, '=');
if (eq) {
char name[256]; size_t nlen = (size_t)(eq - d); if (nlen >= sizeof name) nlen = sizeof name - 1; memcpy(name, d, nlen); name[nlen]='\0';
const char *val = eq+1;
mtable_set_obj(t, name, val);
} else {
mtable_set_obj(t, d, "1");
}
}
for (size_t ui=0; ui<opts->ncmd_undefs; ++ui) {
mtable_unset(t, opts->cmd_undefs[ui]);
}
}
}
static bool starts_with(const char *s, const char *prefix) {
size_t a = strlen(s), b = strlen(prefix);
return a >= b && memcmp(s, prefix, b) == 0;
}
static bool is_system_header_path(const char *path) {
if (!path || !*path) return false;
return strstr(path, "/usr/include") != NULL ||
strstr(path, "/usr/local/include") != NULL ||
strstr(path, "/opt/homebrew/include") != NULL ||
strstr(path, "/usr/lib/clang") != NULL ||
strstr(path, "MacOSX.sdk/usr/include") != NULL;
}
static bool parse_define_name(const char *line, char *namebuf, size_t namebuf_sz) {
if (!line || !namebuf || namebuf_sz == 0) return false;
const char *p = line;
while (*p && isspace((unsigned char)*p)) p++;
if (*p == '#') {
p++;
while (*p && isspace((unsigned char)*p)) p++;
}
if (starts_with(p, "define")) p += 6;
while (*p && isspace((unsigned char)*p)) p++;
size_t adv = 0;
return parse_ident(p, &adv, namebuf, namebuf_sz);
}
static char *path_join(const char *a, const char *b);
static bool add_sysinc_if_header(PPOpts *opts, char *incdir, const char *header) {
if (!opts || !incdir || !header) { free(incdir); return false; }
char *probe = path_join(incdir, header);
bool ok = file_exists(probe);
free(probe);
if (ok) {
opts->sysincdirs = (const char**)xrealloc((void*)opts->sysincdirs, sizeof(char*)*(opts->nsysincdirs+1));
opts->sysincdirs[opts->nsysincdirs++] = incdir;
return true;
}
free(incdir);
return false;
}
static bool add_best_clang_resource_include(PPOpts *opts, const char *base) {
if (!opts || !base) return false;
// Probe common clang resource version directories without directory enumeration.
for (int major = 30; major >= 3; --major) {
char vbuf[32];
snprintf(vbuf, sizeof vbuf, "%d", major);
{
char *verdir = path_join(base, vbuf);
char *incdir = path_join(verdir, "include");
free(verdir);
if (add_sysinc_if_header(opts, incdir, "stdatomic.h")) return true;
}
for (int minor = 0; minor <= 2; ++minor) {
for (int patch = 0; patch <= 9; ++patch) {
snprintf(vbuf, sizeof vbuf, "%d.%d.%d", major, minor, patch);
char *verdir = path_join(base, vbuf);
char *incdir = path_join(verdir, "include");
free(verdir);
if (add_sysinc_if_header(opts, incdir, "stdatomic.h")) return true;
}
}
}
return false;
}
static bool add_gcc_internal_includes(PPOpts *opts, const char *gcc_base) {
if (!opts || !gcc_base) return false;
const char *triples[] = {
"x86_64-linux-gnu",
"x86_64-redhat-linux",
"x86_64-pc-linux-gnu",
"aarch64-linux-gnu",
"i386-linux-gnu",
"arm-linux-gnueabihf",
"arm-linux-gnueabi",
"powerpc64le-linux-gnu",
"riscv64-linux-gnu",
"s390x-linux-gnu"
};
for (size_t ti = 0; ti < sizeof(triples)/sizeof(triples[0]); ++ti) {
char *tbase = path_join(gcc_base, triples[ti]);
bool found = false;
for (int major = 20; major >= 4; --major) {
char vbuf[16];
snprintf(vbuf, sizeof vbuf, "%d", major);
char *vdir = path_join(tbase, vbuf);
char *incdir = path_join(vdir, "include");
if (add_sysinc_if_header(opts, incdir, "stdatomic.h")) {
char *incfixed = path_join(vdir, "include-fixed");
add_sysinc_if_header(opts, incfixed, "limits.h");
found = true;
free(vdir);
break;
}
free(vdir);
}
free(tbase);
if (found) return true;
}
return false;
}
static bool is_escaped(const char *s, size_t i) {
size_t n = 0;
while (i > 0 && s[i-1] == '\\') { n++; i--; }
return (n % 2) != 0;
}
static void scan_paren_state(const char *s, int *paren_depth, int *bl_depth) {
enum { PS_CODE, PS_DQ, PS_SQ, PS_BL } st = (*bl_depth > 0) ? PS_BL : PS_CODE;
for (size_t i = 0; s && s[i]; ++i) {
char c = s[i];
if (st == PS_CODE) {
if (c == '"') { st = PS_DQ; continue; }
if (c == '\'') { st = PS_SQ; continue; }
if (c == '/' && s[i+1] == '/') break;
if (c == '/' && s[i+1] == '*') { st = PS_BL; (*bl_depth)++; i++; continue; }
if (c == '(') { (*paren_depth)++; continue; }
if (c == ')' && *paren_depth > 0) { (*paren_depth)--; continue; }
} else if (st == PS_DQ) {
if (c == '"' && !is_escaped(s, i)) st = PS_CODE;
} else if (st == PS_SQ) {
if (c == '\'' && !is_escaped(s, i)) st = PS_CODE;
} else if (st == PS_BL) {
if (c == '/' && s[i+1] == '*') { (*bl_depth)++; i++; continue; }
if (c == '*' && s[i+1] == '/') { (*bl_depth)--; i++; if (*bl_depth <= 0) { *bl_depth = 0; st = PS_CODE; } continue; }
}
}
}
static void scan_comment_depth(const char *s, int *depth) {
enum { SD_CODE, SD_DQ, SD_SQ, SD_BL, SD_SL } st = (*depth > 0) ? SD_BL : SD_CODE;
for (size_t i = 0; s && s[i]; ++i) {
char c = s[i];
if (st == SD_CODE) {
if (c == '"') { st = SD_DQ; continue; }
if (c == '\'') { st = SD_SQ; continue; }
if (c == '/' && s[i+1] == '/') { st = SD_SL; break; }
if (c == '/' && s[i+1] == '*') { (*depth)++; st = SD_BL; i++; continue; }
} else if (st == SD_DQ) {
if (c == '"' && !is_escaped(s, i)) st = SD_CODE;
} else if (st == SD_SQ) {
if (c == '\'' && !is_escaped(s, i)) st = SD_CODE;
} else if (st == SD_BL) {
if (c == '/' && s[i+1] == '*') { (*depth)++; i++; continue; }
if (c == '*' && s[i+1] == '/') { (*depth)--; i++; if (*depth <= 0) { *depth = 0; st = SD_CODE; } continue; }
}
}
}
static bool starts_c_include_block(const char *s) {
const char *p = s ? s : "";
while (*p && isspace((unsigned char)*p)) p++;
if (strncmp(p, "c_include", 9) != 0) return false;
if (isalnum((unsigned char)p[9]) || p[9] == '_') return false;
p += 9;
while (*p && isspace((unsigned char)*p)) p++;
return *p == '{';
}
static void scan_c_include_brace_depth(const char *s, int *depth) {
enum { CB_CODE, CB_DQ, CB_SQ, CB_BL, CB_SL } st = CB_CODE;
for (size_t i = 0; s && s[i]; ++i) {
char c = s[i];
if (st == CB_CODE) {
if (c == '"') { st = CB_DQ; continue; }
if (c == '\'') { st = CB_SQ; continue; }
if (c == '/' && s[i+1] == '/') { st = CB_SL; break; }
if (c == '/' && s[i+1] == '*') { st = CB_BL; i++; continue; }
if (c == '{') { (*depth)++; continue; }
if (c == '}' && *depth > 0) { (*depth)--; continue; }
} else if (st == CB_DQ) {
if (c == '"' && !is_escaped(s, i)) st = CB_CODE;
} else if (st == CB_SQ) {
if (c == '\'' && !is_escaped(s, i)) st = CB_CODE;
} else if (st == CB_BL) {
if (c == '*' && s[i+1] == '/') { st = CB_CODE; i++; continue; }
}
}
}
static bool only_trailing_space_or_comments(const char *p) {
const char *q = p ? p : "";
while (1) {
while (*q && isspace((unsigned char)*q)) q++;
if (*q == '\0') return true;
if (q[0] == '/' && q[1] == '/') return true;
if (q[0] == '/' && q[1] == '*') {
q += 2;
while (*q && !(q[0] == '*' && q[1] == '/')) q++;
if (*q == '\0') return true;
q += 2;
continue;
}
return false;
}
}
static void pp_directive_error(const char *path, long line, const char *msg);
static void pp_directive_warning(const char *path, long line, const char *msg);
static void parse_line_control_payload(const char *payload, bool allow_linemarker_flags,
const char *path, long err_line,
long *logical_line_out, bool *has_fname_out, char **fname_out) {
const char *lp = payload ? payload : "";
while (*lp && isspace((unsigned char)*lp)) lp++;