/
pre.l
3211 lines (3081 loc) · 87.8 KB
/
pre.l
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
/******************************************************************************
*
*
*
* Copyright (C) 1997-2014 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/
%{
/*
* includes
*/
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <qarray.h>
#include <qstack.h>
#include <qfile.h>
#include <qstrlist.h>
#include <qdict.h>
#include <qregexp.h>
#include <qfileinfo.h>
#include <qdir.h>
#include "pre.h"
#include "constexp.h"
#include "define.h"
#include "doxygen.h"
#include "message.h"
#include "util.h"
#include "defargs.h"
#include "debug.h"
#include "bufstr.h"
#include "portable.h"
#include "bufstr.h"
#include "arguments.h"
#include "entry.h"
#include "condparser.h"
#include "config.h"
#include "filedef.h"
#include "memberdef.h"
#include "membername.h"
// Toggle for some debugging info
//#define DBG_CTX(x) fprintf x
#define DBG_CTX(x) do { } while(0)
#define YY_NEVER_INTERACTIVE 1
struct CondCtx
{
CondCtx(int line,QCString id,bool b)
: lineNr(line),sectionId(id), skip(b) {}
int lineNr;
QCString sectionId;
bool skip;
};
struct FileState
{
FileState(int size) : fileBuf(size),
oldFileBuf(0), oldFileBufPos(0) {}
int lineNr;
BufStr fileBuf;
BufStr *oldFileBuf;
int oldFileBufPos;
YY_BUFFER_STATE bufState;
QCString fileName;
};
/** @brief Singleton that manages the defines available while
* proprocessing files.
*/
class DefineManager
{
/** Local class used to hold the defines for a single file */
class DefinesPerFile
{
public:
/** Creates an empty container for defines */
DefinesPerFile() : m_defines(257), m_includedFiles(17)
{
m_defines.setAutoDelete(TRUE);
}
/** Destroys the object */
virtual ~DefinesPerFile()
{
}
/** Adds a define in the context of a file. Will replace
* an existing define with the same name (redefinition)
* @param def The Define object to add.
*/
void addDefine(Define *def)
{
Define *d = m_defines.find(def->name);
if (d!=0) // redefine
{
m_defines.remove(d->name);
}
m_defines.insert(def->name,def);
}
/** Adds an include file for this file
* @param fileName The name of the include file
*/
void addInclude(const char *fileName)
{
m_includedFiles.insert(fileName,(void*)0x8);
}
void collectDefines(DefineDict *dict,QDict<void> &includeStack);
private:
DefineDict m_defines;
QDict<void> m_includedFiles;
};
public:
friend class DefinesPerFile;
/** Returns a reference to the singleton */
static DefineManager &instance()
{
if (theInstance==0) theInstance = new DefineManager;
return *theInstance;
}
/** Deletes the singleton */
static void deleteInstance()
{
delete theInstance;
theInstance = 0;
}
/** Starts a context in which defines are collected.
* Called at the start of a new file that is preprocessed.
* @param fileName the name of the file to process.
*/
void startContext(const char *fileName)
{
//printf("DefineManager::startContext()\n");
m_contextDefines.clear();
if (fileName==0) return;
DefinesPerFile *dpf = m_fileMap.find(fileName);
if (dpf==0)
{
//printf("New file!\n");
dpf = new DefinesPerFile;
m_fileMap.insert(fileName,dpf);
}
}
/** Ends the context started with startContext() freeing any
* defines collected within in this context.
*/
void endContext()
{
//printf("DefineManager::endContext()\n");
m_contextDefines.clear();
}
/** Add an included file to the current context.
* If the file has been pre-processed already, all defines are added
* to the context.
* @param fileName The name of the include file to add to the context.
*/
void addFileToContext(const char *fileName)
{
if (fileName==0) return;
//printf("DefineManager::addFileToContext(%s)\n",fileName);
DefinesPerFile *dpf = m_fileMap.find(fileName);
if (dpf==0)
{
//printf("New file!\n");
dpf = new DefinesPerFile;
m_fileMap.insert(fileName,dpf);
}
else
{
//printf("existing file!\n");
QDict<void> includeStack(17);
dpf->collectDefines(&m_contextDefines,includeStack);
}
}
/** Add a define to the manager object.
* @param fileName The file in which the define was found
* @param def The Define object to add.
*/
void addDefine(const char *fileName,Define *def)
{
if (fileName==0) return;
//printf("DefineManager::addDefine(%s,%s)\n",fileName,def->name.data());
Define *d = m_contextDefines.find(def->name);
if (d!=0) // redefine
{
m_contextDefines.remove(d->name);
}
m_contextDefines.insert(def->name,def);
DefinesPerFile *dpf = m_fileMap.find(fileName);
if (dpf==0)
{
dpf = new DefinesPerFile;
m_fileMap.insert(fileName,dpf);
}
dpf->addDefine(def);
}
/** Add an include relation to the manager object.
* @param fromFileName file name in which the include was found.
* @param toFileName file name that is included.
*/
void addInclude(const char *fromFileName,const char *toFileName)
{
//printf("DefineManager::addInclude(%s,%s)\n",fromFileName,toFileName);
if (fromFileName==0 || toFileName==0) return;
DefinesPerFile *dpf = m_fileMap.find(fromFileName);
if (dpf==0)
{
dpf = new DefinesPerFile;
m_fileMap.insert(fromFileName,dpf);
}
dpf->addInclude(toFileName);
}
/** Returns a Define object given its name or 0 if the Define does
* not exist.
*/
Define *isDefined(const char *name) const
{
Define *d = m_contextDefines.find(name);
if (d && d->undef) d=0;
//printf("isDefined(%s)=%p\n",name,d);
return d;
}
/** Returns a reference to the defines found in the current context. */
const DefineDict &defineContext() const
{
return m_contextDefines;
}
private:
static DefineManager *theInstance;
/** Helper function to collect all define for a given file */
void collectDefinesForFile(const char *fileName,DefineDict *dict)
{
if (fileName==0) return;
DefinesPerFile *dpf = m_fileMap.find(fileName);
if (dpf)
{
QDict<void> includeStack(17);
dpf->collectDefines(dict,includeStack);
}
}
/** Helper function to return the DefinesPerFile object for a given file name. */
DefinesPerFile *find(const char *fileName) const
{
if (fileName==0) return 0;
return m_fileMap.find(fileName);
}
/** Creates a new DefineManager object */
DefineManager() : m_fileMap(1009), m_contextDefines(1009)
{
m_fileMap.setAutoDelete(TRUE);
}
/** Destroys the object */
virtual ~DefineManager()
{
}
QDict<DefinesPerFile> m_fileMap;
DefineDict m_contextDefines;
};
/** Singleton instance */
DefineManager *DefineManager::theInstance = 0;
/** Collects all defines for a file and all files that the file includes.
* This function will recursively call itself for each file.
* @param dict The dictionary to fill with the defines. A redefine will
* replace a previous definition.
* @param includeStack The stack of includes, used to stop recursion in
* case there is a cyclic include dependency.
*/
void DefineManager::DefinesPerFile::collectDefines(
DefineDict *dict,QDict<void> &includeStack)
{
//printf("DefinesPerFile::collectDefines #defines=%d\n",m_defines.count());
{
QDictIterator<void> di(m_includedFiles);
for (di.toFirst();(di.current());++di)
{
QCString incFile = di.currentKey();
DefinesPerFile *dpf = DefineManager::instance().find(incFile);
if (dpf && includeStack.find(incFile)==0)
{
//printf(" processing include %s\n",incFile.data());
includeStack.insert(incFile,(void*)0x8);
dpf->collectDefines(dict,includeStack);
}
}
}
{
QDictIterator<Define> di(m_defines);
Define *def;
for (di.toFirst();(def=di.current());++di)
{
Define *d = dict->find(def->name);
if (d!=0) // redefine
{
dict->remove(d->name);
}
dict->insert(def->name,def);
//printf(" adding define %s\n",def->name.data());
}
}
}
/* -----------------------------------------------------------------
*
* scanner's state
*/
static int g_yyLineNr = 1;
static int g_yyMLines = 1;
static int g_yyColNr = 1;
static QCString g_yyFileName;
static FileDef *g_yyFileDef;
static FileDef *g_inputFileDef;
static int g_ifcount = 0;
static QStrList *g_pathList = 0;
static QStack<FileState> g_includeStack;
static QDict<int> *g_argDict;
static int g_defArgs = -1;
static QCString g_defName;
static QCString g_defText;
static QCString g_defLitText;
static QCString g_defArgsStr;
static QCString g_defExtraSpacing;
static bool g_defVarArgs;
static int g_level;
static int g_lastCContext;
static int g_lastCPPContext;
static QArray<int> g_levelGuard;
static BufStr *g_inputBuf;
static int g_inputBufPos;
static BufStr *g_outputBuf;
static int g_roundCount;
static bool g_quoteArg;
static DefineDict *g_expandedDict;
static int g_findDefArgContext;
static bool g_expectGuard;
static QCString g_guardName;
static QCString g_lastGuardName;
static QCString g_incName;
static QCString g_guardExpr;
static int g_curlyCount;
static bool g_nospaces; // add extra spaces during macro expansion
static bool g_macroExpansion; // from the configuration
static bool g_expandOnlyPredef; // from the configuration
static int g_commentCount;
static bool g_insideComment;
static bool g_isImported;
static QCString g_blockName;
static int g_condCtx;
static bool g_skip;
static QStack<CondCtx> g_condStack;
static bool g_insideCS; // C# has simpler preprocessor
static bool g_isSource;
static bool g_lexInit = FALSE;
static int g_fenceSize = 0;
static bool g_ccomment;
//DefineDict* getGlobalDefineDict()
//{
// return g_globalDefineDict;
//}
static void setFileName(const char *name)
{
bool ambig;
QFileInfo fi(name);
g_yyFileName=fi.absFilePath().utf8();
g_yyFileDef=findFileDef(Doxygen::inputNameDict,g_yyFileName,ambig);
if (g_yyFileDef==0) // if this is not an input file check if it is an
// include file
{
g_yyFileDef=findFileDef(Doxygen::includeNameDict,g_yyFileName,ambig);
}
//printf("setFileName(%s) g_yyFileName=%s g_yyFileDef=%p\n",
// name,g_yyFileName.data(),g_yyFileDef);
if (g_yyFileDef && g_yyFileDef->isReference()) g_yyFileDef=0;
g_insideCS = getLanguageFromFileName(g_yyFileName)==SrcLangExt_CSharp;
g_isSource = guessSection(g_yyFileName);
}
static void incrLevel()
{
g_level++;
g_levelGuard.resize(g_level);
g_levelGuard[g_level-1]=FALSE;
//printf("%s line %d: incrLevel %d\n",g_yyFileName.data(),g_yyLineNr,g_level);
}
static void decrLevel()
{
//printf("%s line %d: decrLevel %d\n",g_yyFileName.data(),g_yyLineNr,g_level);
if (g_level > 0)
{
g_level--;
g_levelGuard.resize(g_level);
}
else
{
warn(g_yyFileName,g_yyLineNr,"More #endif's than #if's found.\n");
}
}
static bool otherCaseDone()
{
if (g_level==0)
{
warn(g_yyFileName,g_yyLineNr,"Found an #else without a preceding #if.\n");
return TRUE;
}
else
{
return g_levelGuard[g_level-1];
}
}
static void setCaseDone(bool value)
{
g_levelGuard[g_level-1]=value;
}
static QDict<void> g_allIncludes(10009);
static FileState *checkAndOpenFile(const QCString &fileName,bool &alreadyIncluded)
{
alreadyIncluded = FALSE;
FileState *fs = 0;
//printf("checkAndOpenFile(%s)\n",fileName.data());
QFileInfo fi(fileName);
if (fi.exists() && fi.isFile())
{
static QStrList &exclPatterns = Config_getList("EXCLUDE_PATTERNS");
if (patternMatch(fi,&exclPatterns)) return 0;
QCString absName = fi.absFilePath().utf8();
// global guard
if (g_curlyCount==0) // not #include inside { ... }
{
if (g_allIncludes.find(absName)!=0)
{
alreadyIncluded = TRUE;
//printf(" already included 1\n");
return 0; // already done
}
g_allIncludes.insert(absName,(void *)0x8);
}
// check include stack for absName
QStack<FileState> tmpStack;
g_includeStack.setAutoDelete(FALSE);
while ((fs=g_includeStack.pop()))
{
if (fs->fileName==absName) alreadyIncluded=TRUE;
tmpStack.push(fs);
}
while ((fs=tmpStack.pop()))
{
g_includeStack.push(fs);
}
g_includeStack.setAutoDelete(TRUE);
if (alreadyIncluded)
{
//printf(" already included 2\n");
return 0;
}
//printf("#include %s\n",absName.data());
fs = new FileState(fi.size()+4096);
alreadyIncluded = FALSE;
if (!readInputFile(absName,fs->fileBuf))
{ // error
//printf(" error reading\n");
delete fs;
fs=0;
}
else
{
fs->oldFileBuf = g_inputBuf;
fs->oldFileBufPos = g_inputBufPos;
}
}
return fs;
}
static FileState *findFile(const char *fileName,bool localInclude,bool &alreadyIncluded)
{
//printf("** findFile(%s,%d) g_yyFileName=%s\n",fileName,localInclude,g_yyFileName.data());
if (portable_isAbsolutePath(fileName))
{
FileState *fs = checkAndOpenFile(fileName,alreadyIncluded);
if (fs)
{
setFileName(fileName);
g_yyLineNr=1;
return fs;
}
else if (alreadyIncluded)
{
return 0;
}
}
if (localInclude && !g_yyFileName.isEmpty())
{
QFileInfo fi(g_yyFileName);
if (fi.exists())
{
QCString absName = QCString(fi.dirPath(TRUE).data())+"/"+fileName;
FileState *fs = checkAndOpenFile(absName,alreadyIncluded);
if (fs)
{
setFileName(absName);
g_yyLineNr=1;
return fs;
}
else if (alreadyIncluded)
{
return 0;
}
}
}
if (g_pathList==0)
{
return 0;
}
char *s=g_pathList->first();
while (s)
{
QCString absName = (QCString)s+"/"+fileName;
//printf(" Looking for %s in %s\n",fileName,s);
FileState *fs = checkAndOpenFile(absName,alreadyIncluded);
if (fs)
{
setFileName(absName);
g_yyLineNr=1;
//printf(" -> found it\n");
return fs;
}
else if (alreadyIncluded)
{
return 0;
}
s=g_pathList->next();
}
return 0;
}
static QCString extractTrailingComment(const char *s)
{
if (s==0) return "";
int i=strlen(s)-1;
while (i>=0)
{
char c=s[i];
switch (c)
{
case '/':
{
i--;
if (i>=0 && s[i]=='*') // end of a comment block
{
i--;
while (i>0 && !(s[i-1]=='/' && s[i]=='*')) i--;
if (i==0)
{
i++;
}
// only /*!< or /**< are treated as a comment for the macro name,
// otherwise the comment is treated as part of the macro definition
return ((s[i+1]=='*' || s[i+1]=='!') && s[i+2]=='<') ? &s[i-1] : "";
}
else
{
return "";
}
}
break;
// whitespace or line-continuation
case ' ':
case '\t':
case '\r':
case '\n':
case '\\':
break;
default:
return "";
}
i--;
}
return "";
}
static int getNextChar(const QCString &expr,QCString *rest,uint &pos);
static int getCurrentChar(const QCString &expr,QCString *rest,uint pos);
static void unputChar(const QCString &expr,QCString *rest,uint &pos,char c);
static void expandExpression(QCString &expr,QCString *rest,int pos);
static QCString stringize(const QCString &s)
{
QCString result;
uint i=0;
bool inString=FALSE;
bool inChar=FALSE;
char c,pc;
while (i<s.length())
{
if (!inString && !inChar)
{
while (i<s.length() && !inString && !inChar)
{
c=s.at(i++);
if (c=='"')
{
result+="\\\"";
inString=TRUE;
}
else if (c=='\'')
{
result+=c;
inChar=TRUE;
}
else
{
result+=c;
}
}
}
else if (inChar)
{
while (i<s.length() && inChar)
{
c=s.at(i++);
if (c=='\'')
{
result+='\'';
inChar=FALSE;
}
else if (c=='\\')
{
result+="\\\\";
}
else
{
result+=c;
}
}
}
else
{
pc=0;
while (i<s.length() && inString)
{
char c=s.at(i++);
if (c=='"')
{
result+="\\\"";
inString= pc=='\\';
}
else if (c=='\\')
result+="\\\\";
else
result+=c;
pc=c;
}
}
}
//printf("stringize `%s'->`%s'\n",s.data(),result.data());
return result;
}
/*! Execute all ## operators in expr.
* If the macro name before or after the operator contains a no-rescan
* marker (@-) then this is removed (before the concatenated macro name
* may be expanded again.
*/
static void processConcatOperators(QCString &expr)
{
//printf("processConcatOperators: in=`%s'\n",expr.data());
QRegExp r("[ \\t\\n]*##[ \\t\\n]*");
int l,n,i=0;
if (expr.isEmpty()) return;
while ((n=r.match(expr,i,&l))!=-1)
{
//printf("Match: `%s'\n",expr.data()+i);
if (n+l+1<(int)expr.length() && expr.at(n+l)=='@' && expr.at(n+l+1)=='-')
{
// remove no-rescan marker after ID
l+=2;
}
//printf("found `%s'\n",expr.mid(n,l).data());
// remove the ## operator and the surrounding whitespace
expr=expr.left(n)+expr.right(expr.length()-n-l);
int k=n-1;
while (k>=0 && isId(expr.at(k))) k--;
if (k>0 && expr.at(k)=='-' && expr.at(k-1)=='@')
{
// remove no-rescan marker before ID
expr=expr.left(k-1)+expr.right(expr.length()-k-1);
n-=2;
}
i=n;
}
//printf("processConcatOperators: out=`%s'\n",expr.data());
}
static void yyunput (int c,char *buf_ptr );
static void returnCharToStream(char c)
{
unput(c);
}
static inline void addTillEndOfString(const QCString &expr,QCString *rest,
uint &pos,char term,QCString &arg)
{
int cc;
while ((cc=getNextChar(expr,rest,pos))!=EOF && cc!=0)
{
if (cc=='\\') arg+=(char)cc,cc=getNextChar(expr,rest,pos);
else if (cc==term) return;
arg+=(char)cc;
}
}
/*! replaces the function macro \a def whose argument list starts at
* \a pos in expression \a expr.
* Notice that this routine may scan beyond the \a expr string if needed.
* In that case the characters will be read from the input file.
* The replacement string will be returned in \a result and the
* length of the (unexpanded) argument list is stored in \a len.
*/
static bool replaceFunctionMacro(const QCString &expr,QCString *rest,int pos,int &len,const Define *def,QCString &result)
{
//printf("replaceFunctionMacro(expr=%s,rest=%s,pos=%d,def=%s) level=%d\n",expr.data(),rest ? rest->data() : 0,pos,def->name.data(),g_level);
uint j=pos;
len=0;
result.resize(0);
int cc;
while ((cc=getCurrentChar(expr,rest,j))!=EOF && isspace(cc))
{
len++;
getNextChar(expr,rest,j);
}
if (cc!='(')
{
unputChar(expr,rest,j,' ');
return FALSE;
}
getNextChar(expr,rest,j); // eat the `(' character
QDict<QCString> argTable; // list of arguments
argTable.setAutoDelete(TRUE);
QCString arg;
int argCount=0;
bool done=FALSE;
// PHASE 1: read the macro arguments
if (def->nargs==0)
{
while ((cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
{
char c = (char)cc;
if (c==')') break;
}
}
else
{
while (!done && (argCount<def->nargs || def->varArgs) &&
((cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
)
{
char c=(char)cc;
if (c=='(') // argument is a function => search for matching )
{
int level=1;
arg+=c;
//char term='\0';
while ((cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
{
char c=(char)cc;
//printf("processing %c: term=%c (%d)\n",c,term,term);
if (c=='\'' || c=='\"') // skip ('s and )'s inside strings
{
arg+=c;
addTillEndOfString(expr,rest,j,c,arg);
}
if (c==')')
{
level--;
arg+=c;
if (level==0) break;
}
else if (c=='(')
{
level++;
arg+=c;
}
else
arg+=c;
}
}
else if (c==')' || c==',') // last or next argument found
{
if (c==',' && argCount==def->nargs-1 && def->varArgs)
{
arg=arg.stripWhiteSpace();
arg+=',';
}
else
{
QCString argKey;
argKey.sprintf("@%d",argCount++); // key name
arg=arg.stripWhiteSpace();
// add argument to the lookup table
argTable.insert(argKey, new QCString(arg));
arg.resize(0);
if (c==')') // end of the argument list
{
done=TRUE;
}
}
}
else if (c=='\"') // append literal strings
{
arg+=c;
bool found=FALSE;
while (!found && (cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
{
found = cc=='"';
if (cc=='\\')
{
c=(char)cc;
arg+=c;
if ((cc=getNextChar(expr,rest,j))==EOF || cc==0) break;
}
c=(char)cc;
arg+=c;
}
}
else if (c=='\'') // append literal characters
{
arg+=c;
bool found=FALSE;
while (!found && (cc=getNextChar(expr,rest,j))!=EOF && cc!=0)
{
found = cc=='\'';
if (cc=='\\')
{
c=(char)cc;
arg+=c;
if ((cc=getNextChar(expr,rest,j))==EOF || cc==0) break;
}
c=(char)cc;
arg+=c;
}
}
else // append other characters
{
arg+=c;
}
}
}
// PHASE 2: apply the macro function
if (argCount==def->nargs || // same number of arguments
(argCount>=def->nargs-1 && def->varArgs)) // variadic macro with at least as many
// params as the non-variadic part (see bug731985)
{
uint k=0;
// substitution of all formal arguments
QCString resExpr;
const QCString d=def->definition.stripWhiteSpace();
//printf("Macro definition: %s\n",d.data());
bool inString=FALSE;
while (k<d.length())
{
if (d.at(k)=='@') // maybe a marker, otherwise an escaped @
{
if (d.at(k+1)=='@') // escaped @ => copy it (is unescaped later)
{
k+=2;
resExpr+="@@"; // we unescape these later
}
else if (d.at(k+1)=='-') // no-rescan marker
{
k+=2;
resExpr+="@-";
}
else // argument marker => read the argument number
{
QCString key="@";
QCString *subst=0;
bool hash=FALSE;
int l=k-1;
// search for ## backward
if (l>=0 && d.at(l)=='"') l--;
while (l>=0 && d.at(l)==' ') l--;
if (l>0 && d.at(l)=='#' && d.at(l-1)=='#') hash=TRUE;
k++;
// scan the number
while (k<d.length() && d.at(k)>='0' && d.at(k)<='9') key+=d.at(k++);
if (!hash)
{
// search for ## forward
l=k;
if (l<(int)d.length() && d.at(l)=='"') l++;
while (l<(int)d.length() && d.at(l)==' ') l++;
if (l<(int)d.length()-1 && d.at(l)=='#' && d.at(l+1)=='#') hash=TRUE;
}
//printf("request key %s result %s\n",key.data(),argTable[key]->data());
if (key.length()>1 && (subst=argTable[key]))
{
QCString substArg=*subst;
//printf("substArg=`%s'\n",substArg.data());
// only if no ## operator is before or after the argument
// marker we do macro expansion.
if (!hash) expandExpression(substArg,0,0);
if (inString)
{
//printf("`%s'=stringize(`%s')\n",stringize(*subst).data(),subst->data());
// if the marker is inside a string (because a # was put
// before the macro name) we must escape " and \ characters
resExpr+=stringize(substArg);
}
else
{
if (hash && substArg.isEmpty())
{
resExpr+="@E"; // empty argument will be remove later on
}
else if (g_nospaces)
{
resExpr+=substArg;
}
else
{
resExpr+=" "+substArg+" ";
}
}
}
}
}
else // no marker, just copy
{
if (!inString && d.at(k)=='\"')
{
inString=TRUE; // entering a literal string
}
else if (inString && d.at(k)=='\"' && (d.at(k-1)!='\\' || d.at(k-2)=='\\'))
{
inString=FALSE; // leaving a literal string
}
resExpr+=d.at(k++);
}
}
len=j-pos;
result=resExpr;
//printf("result after substitution `%s' expr=`%s'\n",
// result.data(),expr.mid(pos,len).data());
return TRUE;
}
return FALSE;
}
/*! returns the next identifier in string \a expr by starting at position \a p.
* The position of the identifier is returned (or -1 if nothing is found)
* and \a l is its length. Any quoted strings are skipping during the search.
*/
static int getNextId(const QCString &expr,int p,int *l)
{
int n;
while (p<(int)expr.length())