-
Notifications
You must be signed in to change notification settings - Fork 5
/
makeheaders.c
3386 lines (3174 loc) · 97.1 KB
/
makeheaders.c
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
static const char ident[] = "@(#) $Header: /cvstrac/cvstrac/makeheaders.c,v 1.4 2005/03/16 22:17:51 drh Exp $";
/*
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
**
** Copyright 1993 D. Richard Hipp. All rights reserved.
**
** Redistribution and use in source and binary forms, with or
** without modification, are permitted provided that the following
** conditions are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
**
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
**
** This software is provided "as is" and any express or implied warranties,
** including, but not limited to, the implied warranties of merchantability
** and fitness for a particular purpose are disclaimed. In no event shall
** the author or contributors be liable for any direct, indirect, incidental,
** special, exemplary, or consequential damages (including, but not limited
** to, procurement of substitute goods or services; loss of use, data or
** profits; or business interruption) however caused and on any theory of
** liability, whether in contract, strict liability, or tort (including
** negligence or otherwise) arising in any way out of the use of this
** software, even if advised of the possibility of such damage.
**
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
** appropriate header files.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <memory.h>
#include <sys/stat.h>
#include <assert.h>
#if defined( __MINGW32__) || defined(__DMC__) || defined(_MSC_VER) || defined(__POCC__)
# ifndef WIN32
# define WIN32
# endif
# include <string.h>
#else
# include <unistd.h>
#endif
/*
** Macros for debugging.
*/
#ifdef DEBUG
static int debugMask = 0;
# define debug0(F,M) if( (F)&debugMask ){ fprintf(stderr,M); }
# define debug1(F,M,A) if( (F)&debugMask ){ fprintf(stderr,M,A); }
# define debug2(F,M,A,B) if( (F)&debugMask ){ fprintf(stderr,M,A,B); }
# define debug3(F,M,A,B,C) if( (F)&debugMask ){ fprintf(stderr,M,A,B,C); }
# define PARSER 0x00000001
# define DECL_DUMP 0x00000002
# define TOKENIZER 0x00000004
#else
# define debug0(Flags, Format)
# define debug1(Flags, Format, A)
# define debug2(Flags, Format, A, B)
# define debug3(Flags, Format, A, B, C)
#endif
/*
** The following macros are purely for the purpose of testing this
** program on itself. They don't really contribute to the code.
*/
#define INTERFACE 1
#define EXPORT_INTERFACE 1
#define EXPORT
/*
** Each token in a source file is represented by an instance of
** the following structure. Tokens are collected onto a list.
*/
typedef struct Token Token;
struct Token {
const char *zText; /* The text of the token */
int nText; /* Number of characters in the token's text */
int eType; /* The type of this token */
int nLine; /* The line number on which the token starts */
Token *pComment; /* Most recent block comment before this token */
Token *pNext; /* Next token on the list */
Token *pPrev; /* Previous token on the list */
};
/*
** During tokenization, information about the state of the input
** stream is held in an instance of the following structure
*/
typedef struct InStream InStream;
struct InStream {
const char *z; /* Complete text of the input */
int i; /* Next character to read from the input */
int nLine; /* The line number for character z[i] */
};
/*
** Each declaration in the C or C++ source files is parsed out and stored as
** an instance of the following structure.
**
** A "forward declaration" is a declaration that an object exists that
** doesn't tell about the objects structure. A typical forward declaration
** is:
**
** struct Xyzzy;
**
** Not every object has a forward declaration. If it does, thought, the
** forward declaration will be contained in the zFwd field for C and
** the zFwdCpp for C++. The zDecl field contains the complete
** declaration text.
*/
typedef struct Decl Decl;
struct Decl {
char *zName; /* Name of the object being declared. The appearance
** of this name is a source file triggers the declaration
** to be added to the header for that file. */
char *zFile; /* File from which extracted. */
char *zIf; /* Surround the declaration with this #if */
char *zFwd; /* A forward declaration. NULL if there is none. */
char *zFwdCpp; /* Use this forward declaration for C++. */
char *zDecl; /* A full declaration of this object */
char *zExtra; /* Extra declaration text inserted into class objects */
int extraType; /* Last public:, protected: or private: in zExtraDecl */
struct Include *pInclude; /* #includes that come before this declaration */
int flags; /* See the "Properties" below */
Token *pComment; /* A block comment associated with this declaration */
Token tokenCode; /* Implementation of functions and procedures */
Decl *pSameName; /* Next declaration with the same "zName" */
Decl *pSameHash; /* Next declaration with same hash but different zName */
Decl *pNext; /* Next declaration with a different name */
};
/*
** Properties associated with declarations.
**
** DP_Forward and DP_Declared are used during the generation of a single
** header file in order to prevent duplicate declarations and definitions.
** DP_Forward is set after the object has been given a forward declaration
** and DP_Declared is set after the object gets a full declarations.
** (Example: A forward declaration is "typedef struct Abc Abc;" and the
** full declaration is "struct Abc { int a; float b; };".)
**
** The DP_Export and DP_Local flags are more permanent. They mark objects
** that have EXPORT scope and LOCAL scope respectively. If both of these
** marks are missing, then the object has library scope. The meanings of
** the scopes are as follows:
**
** LOCAL scope The object is only usable within the file in
** which it is declared.
**
** library scope The object is visible and usable within other
** files in the same project. By if the project is
** a library, then the object is not visible to users
** of the library. (i.e. the object does not appear
** in the output when using the -H option.)
**
** EXPORT scope The object is visible and usable everywhere.
**
** The DP_Flag is a temporary use flag that is used during processing to
** prevent an infinite loop. It's use is localized.
**
** The DP_Cplusplus, DP_ExternCReqd and DP_ExternReqd flags are permanent
** and are used to specify what type of declaration the object requires.
*/
#define DP_Forward 0x001 /* Has a forward declaration in this file */
#define DP_Declared 0x002 /* Has a full declaration in this file */
#define DP_Export 0x004 /* Export this declaration */
#define DP_Local 0x008 /* Declare in its home file only */
#define DP_Flag 0x010 /* Use to mark a subset of a Decl list
** for special processing */
#define DP_Cplusplus 0x020 /* Has C++ linkage and cannot appear in a
** C header file */
#define DP_ExternCReqd 0x040 /* Prepend 'extern "C"' in a C++ header.
** Prepend nothing in a C header */
#define DP_ExternReqd 0x080 /* Prepend 'extern "C"' in a C++ header if
** DP_Cplusplus is not also set. If DP_Cplusplus
** is set or this is a C header then
** prepend 'extern' */
/*
** Convenience macros for dealing with declaration properties
*/
#define DeclHasProperty(D,P) (((D)->flags&(P))==(P))
#define DeclHasAnyProperty(D,P) (((D)->flags&(P))!=0)
#define DeclSetProperty(D,P) (D)->flags |= (P)
#define DeclClearProperty(D,P) (D)->flags &= ~(P)
/*
** These are state properties of the parser. Each of the values is
** distinct from the DP_ values above so that both can be used in
** the same "flags" field.
**
** Be careful not to confuse PS_Export with DP_Export or
** PS_Local with DP_Local. Their names are similar, but the meanings
** of these flags are very different.
*/
#define PS_Extern 0x000800 /* "extern" has been seen */
#define PS_Export 0x001000 /* If between "#if EXPORT_INTERFACE"
** and "#endif" */
#define PS_Export2 0x002000 /* If "EXPORT" seen */
#define PS_Typedef 0x004000 /* If "typedef" has been seen */
#define PS_Static 0x008000 /* If "static" has been seen */
#define PS_Interface 0x010000 /* If within #if INTERFACE..#endif */
#define PS_Method 0x020000 /* If "::" token has been seen */
#define PS_Local 0x040000 /* If within #if LOCAL_INTERFACE..#endif */
#define PS_Local2 0x080000 /* If "LOCAL" seen. */
#define PS_Public 0x100000 /* If "PUBLIC" seen. */
#define PS_Protected 0x200000 /* If "PROTECTED" seen. */
#define PS_Private 0x400000 /* If "PRIVATE" seen. */
#define PS_PPP 0x700000 /* If any of PUBLIC, PRIVATE, PROTECTED */
/*
** The following set of flags are ORed into the "flags" field of
** a Decl in order to identify what type of object is being
** declared.
*/
#define TY_Class 0x00100000
#define TY_Subroutine 0x00200000
#define TY_Macro 0x00400000
#define TY_Typedef 0x00800000
#define TY_Variable 0x01000000
#define TY_Structure 0x02000000
#define TY_Union 0x04000000
#define TY_Enumeration 0x08000000
#define TY_Defunct 0x10000000 /* Used to erase a declaration */
/*
** Each nested #if (or #ifdef or #ifndef) is stored in a stack of
** instances of the following structure.
*/
typedef struct Ifmacro Ifmacro;
struct Ifmacro {
int nLine; /* Line number where this macro occurs */
char *zCondition; /* Text of the condition for this macro */
Ifmacro *pNext; /* Next down in the stack */
int flags; /* Can hold PS_Export, PS_Interface or PS_Local flags */
};
/*
** When parsing a file, we need to keep track of what other files have
** be #include-ed. For each #include found, we create an instance of
** the following structure.
*/
typedef struct Include Include;
struct Include {
char *zFile; /* The name of file include. Includes "" or <> */
char *zIf; /* If not NULL, #include should be enclosed in #if */
char *zLabel; /* A unique label used to test if this #include has
* appeared already in a file or not */
Include *pNext; /* Previous include file, or NULL if this is the first */
};
/*
** Identifiers found in a source file that might be used later to provoke
** the copying of a declaration into the corresponding header file are
** stored in a hash table as instances of the following structure.
*/
typedef struct Ident Ident;
struct Ident {
char *zName; /* The text of this identifier */
Ident *pCollide; /* Next identifier with the same hash */
Ident *pNext; /* Next identifier in a list of them all */
};
/*
** A complete table of identifiers is stored in an instance of
** the next structure.
*/
#define IDENT_HASH_SIZE 2237
typedef struct IdentTable IdentTable;
struct IdentTable {
Ident *pList; /* List of all identifiers in this table */
Ident *apTable[IDENT_HASH_SIZE]; /* The hash table */
};
/*
** The following structure holds all information for a single
** source file named on the command line of this program.
*/
typedef struct InFile InFile;
struct InFile {
char *zSrc; /* Name of input file */
char *zHdr; /* Name of the generated .h file for this input.
** Will be NULL if input is to be scanned only */
int flags; /* One or more DP_, PS_ and/or TY_ flags */
InFile *pNext; /* Next input file in the list of them all */
IdentTable idTable; /* All identifiers in this input file */
};
/*
** An unbounded string is able to grow without limit. We use these
** to construct large in-memory strings from lots of smaller components.
*/
typedef struct String String;
struct String {
int nAlloc; /* Number of bytes allocated */
int nUsed; /* Number of bytes used (not counting null terminator) */
char *zText; /* Text of the string */
};
/*
** The following structure contains a lot of state information used
** while generating a .h file. We put the information in this structure
** and pass around a pointer to this structure, rather than pass around
** all of the information separately. This helps reduce the number of
** arguments to generator functions.
*/
typedef struct GenState GenState;
struct GenState {
String *pStr; /* Write output to this string */
IdentTable *pTable; /* A table holding the zLabel of every #include that
* has already been generated. Used to avoid
* generating duplicate #includes. */
const char *zIf; /* If not NULL, then we are within a #if with
* this argument. */
int nErr; /* Number of errors */
const char *zFilename; /* Name of the source file being scanned */
int flags; /* Various flags (DP_ and PS_ flags above) */
};
/*
** The following text line appears at the top of every file generated
** by this program. By recognizing this line, the program can be sure
** never to read a file that it generated itself.
*/
const char zTopLine[] =
"/* \aThis file was automatically generated. Do not edit! */\n";
#define nTopLine (sizeof(zTopLine)-1)
/*
** The name of the file currently being parsed.
*/
static char *zFilename;
/*
** The stack of #if macros for the file currently being parsed.
*/
static Ifmacro *ifStack = 0;
/*
** A list of all files that have been #included so far in a file being
** parsed.
*/
static Include *includeList = 0;
/*
** The last block comment seen.
*/
static Token *blockComment = 0;
/*
** The following flag is set if the -doc flag appears on the
** command line.
*/
static int doc_flag = 0;
/*
** If the following flag is set, then makeheaders will attempt to
** generate prototypes for static functions and procedures.
*/
static int proto_static = 0;
/*
** A list of all declarations. The list is held together using the
** pNext field of the Decl structure.
*/
static Decl *pDeclFirst; /* First on the list */
static Decl *pDeclLast; /* Last on the list */
/*
** A hash table of all declarations
*/
#define DECL_HASH_SIZE 3371
static Decl *apTable[DECL_HASH_SIZE];
/*
** The TEST macro must be defined to something. Make sure this is the
** case.
*/
#ifndef TEST
# define TEST 0
#endif
#ifdef NOT_USED
/*
** We do our own assertion macro so that we can have more control
** over debugging.
*/
#define Assert(X) if(!(X)){ CantHappen(__LINE__); }
#define CANT_HAPPEN CantHappen(__LINE__)
static void CantHappen(int iLine){
fprintf(stderr,"Assertion failed on line %d\n",iLine);
*(char*)1 = 0; /* Force a core-dump */
}
#endif
/*
** Memory allocation functions that are guaranteed never to return NULL.
*/
static void *SafeMalloc(int nByte){
void *p = malloc( nByte );
if( p==0 ){
fprintf(stderr,"Out of memory. Can't allocate %d bytes.\n",nByte);
exit(1);
}
return p;
}
static void SafeFree(void *pOld){
if( pOld ){
free(pOld);
}
}
static void *SafeRealloc(void *pOld, int nByte){
void *p;
if( pOld==0 ){
p = SafeMalloc(nByte);
}else{
p = realloc(pOld, nByte);
if( p==0 ){
fprintf(stderr,
"Out of memory. Can't enlarge an allocation to %d bytes\n",nByte);
exit(1);
}
}
return p;
}
static char *StrDup(const char *zSrc, int nByte){
char *zDest;
if( nByte<=0 ){
nByte = strlen(zSrc);
}
zDest = SafeMalloc( nByte + 1 );
strncpy(zDest,zSrc,nByte);
zDest[nByte] = 0;
return zDest;
}
/*
** Return TRUE if the character X can be part of an identifier
*/
#define ISALNUM(X) ((X)=='_' || isalnum(X))
/*
** Routines for dealing with unbounded strings.
*/
static void StringInit(String *pStr){
pStr->nAlloc = 0;
pStr->nUsed = 0;
pStr->zText = 0;
}
static void StringReset(String *pStr){
SafeFree(pStr->zText);
StringInit(pStr);
}
static void StringAppend(String *pStr, const char *zText, int nByte){
if( nByte<=0 ){
nByte = strlen(zText);
}
if( pStr->nUsed + nByte >= pStr->nAlloc ){
if( pStr->nAlloc==0 ){
pStr->nAlloc = nByte + 100;
pStr->zText = SafeMalloc( pStr->nAlloc );
}else{
pStr->nAlloc = pStr->nAlloc*2 + nByte;
pStr->zText = SafeRealloc(pStr->zText, pStr->nAlloc);
}
}
strncpy(&pStr->zText[pStr->nUsed],zText,nByte);
pStr->nUsed += nByte;
pStr->zText[pStr->nUsed] = 0;
}
#define StringGet(S) ((S)->zText?(S)->zText:"")
/*
** Compute a hash on a string. The number returned is a non-negative
** value between 0 and 2**31 - 1
*/
static int Hash(const char *z, int n){
int h = 0;
if( n<=0 ){
n = strlen(z);
}
while( n-- ){
h = h ^ (h<<5) ^ *z++;
}
return h & 0x7fffffff;
}
/*
** Given an identifier name, try to find a declaration for that
** identifier in the hash table. If found, return a pointer to
** the Decl structure. If not found, return 0.
*/
static Decl *FindDecl(const char *zName, int len){
int h;
Decl *p;
if( len<=0 ){
len = strlen(zName);
}
h = Hash(zName,len) % DECL_HASH_SIZE;
p = apTable[h];
while( p && (strncmp(p->zName,zName,len)!=0 || p->zName[len]!=0) ){
p = p->pSameHash;
}
return p;
}
/*
** Install the given declaration both in the hash table and on
** the list of all declarations.
*/
static void InstallDecl(Decl *pDecl){
int h;
Decl *pOther;
h = Hash(pDecl->zName,0) % DECL_HASH_SIZE;
pOther = apTable[h];
while( pOther && strcmp(pDecl->zName,pOther->zName)!=0 ){
pOther = pOther->pSameHash;
}
if( pOther ){
pDecl->pSameName = pOther->pSameName;
pOther->pSameName = pDecl;
}else{
pDecl->pSameName = 0;
pDecl->pSameHash = apTable[h];
apTable[h] = pDecl;
}
pDecl->pNext = 0;
if( pDeclFirst==0 ){
pDeclFirst = pDeclLast = pDecl;
}else{
pDeclLast->pNext = pDecl;
pDeclLast = pDecl;
}
}
/*
** Look at the current ifStack. If anything declared at the current
** position must be surrounded with
**
** #if STUFF
** #endif
**
** Then this routine computes STUFF and returns a pointer to it. Memory
** to hold the value returned is obtained from malloc().
*/
static char *GetIfString(void){
Ifmacro *pIf;
char *zResult = 0;
int hasIf = 0;
String str;
for(pIf = ifStack; pIf; pIf=pIf->pNext){
if( pIf->zCondition==0 || *pIf->zCondition==0 ) continue;
if( !hasIf ){
hasIf = 1;
StringInit(&str);
}else{
StringAppend(&str," && ",4);
}
StringAppend(&str,pIf->zCondition,0);
}
if( hasIf ){
zResult = StrDup(StringGet(&str),0);
StringReset(&str);
}else{
zResult = 0;
}
return zResult;
}
/*
** Create a new declaration and put it in the hash table. Also
** return a pointer to it so that we can fill in the zFwd and zDecl
** fields, and so forth.
*/
static Decl *CreateDecl(
const char *zName, /* Name of the object being declared. */
int nName /* Length of the name */
){
Decl *pDecl;
pDecl = SafeMalloc( sizeof(Decl) + nName + 1);
memset(pDecl,0,sizeof(Decl));
pDecl->zName = (char*)&pDecl[1];
sprintf(pDecl->zName,"%.*s",nName,zName);
pDecl->zFile = zFilename;
pDecl->pInclude = includeList;
pDecl->zIf = GetIfString();
InstallDecl(pDecl);
return pDecl;
}
/*
** Insert a new identifier into an table of identifiers. Return TRUE if
** a new identifier was inserted and return FALSE if the identifier was
** already in the table.
*/
static int IdentTableInsert(
IdentTable *pTable, /* The table into which we will insert */
const char *zId, /* Name of the identifiers */
int nId /* Length of the identifier name */
){
int h;
Ident *pId;
if( nId<=0 ){
nId = strlen(zId);
}
h = Hash(zId,nId) % IDENT_HASH_SIZE;
for(pId = pTable->apTable[h]; pId; pId=pId->pCollide){
if( strncmp(zId,pId->zName,nId)==0 && pId->zName[nId]==0 ){
/* printf("Already in table: %.*s\n",nId,zId); */
return 0;
}
}
pId = SafeMalloc( sizeof(Ident) + nId + 1 );
pId->zName = (char*)&pId[1];
sprintf(pId->zName,"%.*s",nId,zId);
pId->pNext = pTable->pList;
pTable->pList = pId;
pId->pCollide = pTable->apTable[h];
pTable->apTable[h] = pId;
/* printf("Add to table: %.*s\n",nId,zId); */
return 1;
}
/*
** Check to see if the given value is in the given IdentTable. Return
** true if it is and false if it is not.
*/
static int IdentTableTest(
IdentTable *pTable, /* The table in which to search */
const char *zId, /* Name of the identifiers */
int nId /* Length of the identifier name */
){
int h;
Ident *pId;
if( nId<=0 ){
nId = strlen(zId);
}
h = Hash(zId,nId) % IDENT_HASH_SIZE;
for(pId = pTable->apTable[h]; pId; pId=pId->pCollide){
if( strncmp(zId,pId->zName,nId)==0 && pId->zName[nId]==0 ){
return 1;
}
}
return 0;
}
/*
** Remove every identifier from the given table. Reset the table to
** its initial state.
*/
static void IdentTableReset(IdentTable *pTable){
Ident *pId, *pNext;
for(pId = pTable->pList; pId; pId = pNext){
pNext = pId->pNext;
SafeFree(pId);
}
memset(pTable,0,sizeof(IdentTable));
}
#ifdef DEBUG
/*
** Print the name of every identifier in the given table, one per line
*/
static void IdentTablePrint(IdentTable *pTable, FILE *pOut){
Ident *pId;
for(pId = pTable->pList; pId; pId = pId->pNext){
fprintf(pOut,"%s\n",pId->zName);
}
}
#endif
/*
** Read an entire file into memory. Return a pointer to the memory.
**
** The memory is obtained from SafeMalloc and must be freed by the
** calling function.
**
** If the read fails for any reason, 0 is returned.
*/
static char *ReadFile(const char *zFilename){
struct stat sStat;
FILE *pIn;
char *zBuf;
int n;
if( stat(zFilename,&sStat)!=0
#ifndef WIN32
|| !S_ISREG(sStat.st_mode)
#endif
){
return 0;
}
pIn = fopen(zFilename,"r");
if( pIn==0 ){
return 0;
}
zBuf = SafeMalloc( sStat.st_size + 1 );
n = fread(zBuf,1,sStat.st_size,pIn);
zBuf[n] = 0;
fclose(pIn);
return zBuf;
}
/*
** Write the contents of a string into a file. Return the number of
** errors
*/
static int WriteFile(const char *zFilename, const char *zOutput){
FILE *pOut;
pOut = fopen(zFilename,"w");
if( pOut==0 ){
return 1;
}
fwrite(zOutput,1,strlen(zOutput),pOut);
fclose(pOut);
return 0;
}
/*
** Major token types
*/
#define TT_Space 1 /* Contiguous white space */
#define TT_Id 2 /* An identifier */
#define TT_Preprocessor 3 /* Any C preprocessor directive */
#define TT_Comment 4 /* Either C or C++ style comment */
#define TT_Number 5 /* Any numeric constant */
#define TT_String 6 /* String or character constants. ".." or '.' */
#define TT_Braces 7 /* All text between { and a matching } */
#define TT_EOF 8 /* End of file */
#define TT_Error 9 /* An error condition */
#define TT_BlockComment 10 /* A C-Style comment at the left margin that
* spans multple lines */
#define TT_Other 0 /* None of the above */
/*
** Get a single low-level token from the input file. Update the
** file pointer so that it points to the first character beyond the
** token.
**
** A "low-level token" is any token except TT_Braces. A TT_Braces token
** consists of many smaller tokens and is assembled by a routine that
** calls this one.
**
** The function returns the number of errors. An error is an
** unterminated string or character literal or an unterminated
** comment.
**
** Profiling shows that this routine consumes about half the
** CPU time on a typical run of makeheaders.
*/
static int GetToken(InStream *pIn, Token *pToken){
int i;
const char *z;
int cStart;
int c;
int startLine; /* Line on which a structure begins */
int nlisc = 0; /* True if there is a new-line in a ".." or '..' */
int nErr = 0; /* Number of errors seen */
z = pIn->z;
i = pIn->i;
pToken->nLine = pIn->nLine;
pToken->zText = &z[i];
switch( z[i] ){
case 0:
pToken->eType = TT_EOF;
pToken->nText = 0;
break;
case '#':
if( i==0 || z[i-1]=='\n' || (i>1 && z[i-1]=='\r' && z[i-2]=='\n')){
/* We found a preprocessor statement */
pToken->eType = TT_Preprocessor;
i++;
while( z[i]!=0 && z[i]!='\n' ){
if( z[i]=='\\' ){
i++;
if( z[i]=='\n' ) pIn->nLine++;
}
i++;
}
pToken->nText = i - pIn->i;
}else{
/* Just an operator */
pToken->eType = TT_Other;
pToken->nText = 1;
}
break;
case ' ':
case '\t':
case '\r':
case '\f':
case '\n':
while( isspace(z[i]) ){
if( z[i]=='\n' ) pIn->nLine++;
i++;
}
pToken->eType = TT_Space;
pToken->nText = i - pIn->i;
break;
case '\\':
pToken->nText = 2;
pToken->eType = TT_Other;
if( z[i+1]=='\n' ){
pIn->nLine++;
pToken->eType = TT_Space;
}else if( z[i+1]==0 ){
pToken->nText = 1;
}
break;
case '\'':
case '\"':
cStart = z[i];
startLine = pIn->nLine;
do{
i++;
c = z[i];
if( c=='\n' ){
if( !nlisc ){
fprintf(stderr,
"%s:%d: (warning) Newline in string or character literal.\n",
zFilename, pIn->nLine);
nlisc = 1;
}
pIn->nLine++;
}
if( c=='\\' ){
i++;
c = z[i];
if( c=='\n' ){
pIn->nLine++;
}
}else if( c==cStart ){
i++;
c = 0;
}else if( c==0 ){
fprintf(stderr, "%s:%d: Unterminated string or character literal.\n",
zFilename, startLine);
nErr++;
}
}while( c );
pToken->eType = TT_String;
pToken->nText = i - pIn->i;
break;
case '/':
if( z[i+1]=='/' ){
/* C++ style comment */
while( z[i] && z[i]!='\n' ){ i++; }
pToken->eType = TT_Comment;
pToken->nText = i - pIn->i;
}else if( z[i+1]=='*' ){
/* C style comment */
int isBlockComment = i==0 || z[i-1]=='\n';
i += 2;
startLine = pIn->nLine;
while( z[i] && (z[i]!='*' || z[i+1]!='/') ){
if( z[i]=='\n' ){
pIn->nLine++;
if( isBlockComment ){
if( z[i+1]=='*' || z[i+2]=='*' ){
isBlockComment = 2;
}else{
isBlockComment = 0;
}
}
}
i++;
}
if( z[i] ){
i += 2;
}else{
isBlockComment = 0;
fprintf(stderr,"%s:%d: Unterminated comment\n",
zFilename, startLine);
nErr++;
}
pToken->eType = isBlockComment==2 ? TT_BlockComment : TT_Comment;
pToken->nText = i - pIn->i;
}else{
/* A divide operator */
pToken->eType = TT_Other;
pToken->nText = 1 + (z[i+1]=='+');
}
break;
case '0':
if( z[i+1]=='x' || z[i+1]=='X' ){
/* A hex constant */
i += 2;
while( isxdigit(z[i]) ){ i++; }
}else{
/* An octal constant */
while( isdigit(z[i]) ){ i++; }
}
pToken->eType = TT_Number;
pToken->nText = i - pIn->i;
break;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
while( isdigit(z[i]) ){ i++; }
if( (c=z[i])=='.' ){
i++;
while( isdigit(z[i]) ){ i++; }
c = z[i];
if( c=='e' || c=='E' ){
i++;
if( ((c=z[i])=='+' || c=='-') && isdigit(z[i+1]) ){ i++; }
while( isdigit(z[i]) ){ i++; }
c = z[i];
}
if( c=='f' || c=='F' || c=='l' || c=='L' ){ i++; }
}else if( c=='e' || c=='E' ){
i++;
if( ((c=z[i])=='+' || c=='-') && isdigit(z[i+1]) ){ i++; }
while( isdigit(z[i]) ){ i++; }
}else if( c=='L' || c=='l' ){
i++;
c = z[i];
if( c=='u' || c=='U' ){ i++; }
}else if( c=='u' || c=='U' ){
i++;
c = z[i];
if( c=='l' || c=='L' ){ i++; }
}
pToken->eType = TT_Number;
pToken->nText = i - pIn->i;
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I':
case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P':
case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W':
case 'X': case 'Y': case 'Z': case '_':
while( isalnum(z[i]) || z[i]=='_' ){ i++; };
pToken->eType = TT_Id;
pToken->nText = i - pIn->i;
break;
case ':':
pToken->eType = TT_Other;
pToken->nText = 1 + (z[i+1]==':');
break;
case '=':
case '<':
case '>':
case '+':
case '-':
case '*':
case '%':
case '^':
case '&':
case '|':
pToken->eType = TT_Other;
pToken->nText = 1 + (z[i+1]=='=');
break;
default:
pToken->eType = TT_Other;
pToken->nText = 1;
break;
}
pIn->i += pToken->nText;
return nErr;
}
/*
** This routine recovers the next token from the input file which is
** not a space or a comment or any text between an "#if 0" and "#endif".
**
** This routine returns the number of errors encountered. An error
** is an unterminated token or unmatched "#if 0".
**
** Profiling shows that this routine uses about a quarter of the
** CPU time in a typical run.