/
util.cpp
7391 lines (6941 loc) · 216 KB
/
util.cpp
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-2015 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.
*
*/
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include <mutex>
#include <unordered_set>
#include <codecvt>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cinttypes>
#include <sstream>
#include "md5.h"
#include "regex.h"
#include "util.h"
#include "message.h"
#include "classdef.h"
#include "filedef.h"
#include "doxygen.h"
#include "outputlist.h"
#include "defargs.h"
#include "language.h"
#include "config.h"
#include "htmlhelp.h"
#include "example.h"
#include "version.h"
#include "groupdef.h"
#include "reflist.h"
#include "pagedef.h"
#include "debug.h"
#include "searchindex.h"
#include "doxygen.h"
#include "textdocvisitor.h"
#include "latexdocvisitor.h"
#include "portable.h"
#include "parserintf.h"
#include "bufstr.h"
#include "image.h"
#include "growbuf.h"
#include "entry.h"
#include "arguments.h"
#include "memberlist.h"
#include "classlist.h"
#include "namespacedef.h"
#include "membername.h"
#include "filename.h"
#include "membergroup.h"
#include "dirdef.h"
#include "htmlentity.h"
#include "symbolresolver.h"
#include "fileinfo.h"
#include "dir.h"
#include "utf8.h"
#include "textstream.h"
#define ENABLE_TRACINGSUPPORT 0
#if defined(_OS_MAC_) && ENABLE_TRACINGSUPPORT
#define TRACINGSUPPORT
#endif
#ifdef TRACINGSUPPORT
#include <execinfo.h>
#include <unistd.h>
#endif
//------------------------------------------------------------------------
#define REL_PATH_TO_ROOT "../../"
static const char *hex = "0123456789ABCDEF";
//------------------------------------------------------------------------
// TextGeneratorOLImpl implementation
//------------------------------------------------------------------------
TextGeneratorOLImpl::TextGeneratorOLImpl(OutputDocInterface &od) : m_od(od)
{
}
void TextGeneratorOLImpl::writeString(const QCString &s,bool keepSpaces) const
{
if (s.isEmpty()) return;
//printf("TextGeneratorOlImpl::writeString('%s',%d)\n",s,keepSpaces);
if (keepSpaces)
{
const char *p=s.data();
if (p)
{
char cs[2];
char c;
cs[1]='\0';
while ((c=*p++))
{
if (c==' ') m_od.writeNonBreakableSpace(1);
else cs[0]=c,m_od.docify(cs);
}
}
}
else
{
m_od.docify(s);
}
}
void TextGeneratorOLImpl::writeBreak(int indent) const
{
m_od.lineBreak("typebreak");
int i;
for (i=0;i<indent;i++)
{
m_od.writeNonBreakableSpace(3);
}
}
void TextGeneratorOLImpl::writeLink(const QCString &extRef,const QCString &file,
const QCString &anchor,const QCString &text
) const
{
//printf("TextGeneratorOlImpl::writeLink('%s')\n",text);
m_od.writeObjectLink(extRef,file,anchor,text);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// an inheritance tree of depth of 100000 should be enough for everyone :-)
const int maxInheritanceDepth = 100000;
/*!
Removes all anonymous scopes from string s
Possible examples:
\verbatim
"bla::@10::blep" => "bla::blep"
"bla::@10::@11::blep" => "bla::blep"
"@10::blep" => "blep"
" @10::blep" => "blep"
"@9::@10::blep" => "blep"
"bla::@1" => "bla"
"bla::@1::@2" => "bla"
"bla @1" => "bla"
\endverbatim
*/
QCString removeAnonymousScopes(const QCString &str)
{
std::string result;
if (str.isEmpty()) return QCString(result);
// helper to check if the found delimiter starts with a colon
auto startsWithColon = [](const std::string &del)
{
for (size_t i=0;i<del.size();i++)
{
if (del[i]=='@') return false;
else if (del[i]==':') return true;
}
return false;
};
// helper to check if the found delimiter ends with a colon
auto endsWithColon = [](const std::string &del)
{
for (int i=(int)del.size()-1;i>=0;i--)
{
if (del[i]=='@') return false;
else if (del[i]==':') return true;
}
return false;
};
static const reg::Ex re(R"([\s:]*@\d+[\s:]*)");
std::string s = str.str();
reg::Iterator iter(s,re);
reg::Iterator end;
size_t p=0;
size_t sl=s.length();
bool needsSeparator=false;
for ( ; iter!=end ; ++iter)
{
const auto &match = *iter;
size_t i = match.position();
if (i>p) // add non-matching prefix
{
if (needsSeparator) result+="::";
needsSeparator=false;
result+=s.substr(p,i-p);
}
std::string delim = match.str();
needsSeparator = needsSeparator || (startsWithColon(delim) && endsWithColon(delim));
p = match.position()+match.length();
}
if (p<sl) // add trailing remainder
{
if (needsSeparator) result+="::";
result+=s.substr(p);
}
return result;
}
// replace anonymous scopes with __anonymous__ or replacement if provided
QCString replaceAnonymousScopes(const QCString &s,const QCString &replacement)
{
if (s.isEmpty()) return s;
static const reg::Ex marker(R"(@\d+)");
std::string result = reg::replace(s.str(),marker,
replacement.isEmpty() ? replacement.data() : "__anonymous__");
//printf("replaceAnonymousScopes('%s')='%s'\n",qPrint(s),qPrint(result));
return QCString(result);
}
// strip anonymous left hand side part of the scope
QCString stripAnonymousNamespaceScope(const QCString &s)
{
int i,p=0,l;
QCString newScope;
int sl = s.length();
while ((i=getScopeFragment(s,p,&l))!=-1)
{
//printf("Scope fragment %s\n",qPrint(s.mid(i,l)));
if (Doxygen::namespaceLinkedMap->find(s.left(i+l))!=0)
{
if (s.at(i)!='@')
{
if (!newScope.isEmpty()) newScope+="::";
newScope+=s.mid(i,l);
}
}
else if (i<sl)
{
if (!newScope.isEmpty()) newScope+="::";
newScope+=s.right(sl-i);
goto done;
}
p=i+l;
}
done:
//printf("stripAnonymousNamespaceScope('%s')='%s'\n",qPrint(s),qPrint(newScope));
return newScope;
}
void writePageRef(OutputDocInterface &od,const QCString &cn,const QCString &mn)
{
od.pushGeneratorState();
od.disable(OutputGenerator::Html);
od.disable(OutputGenerator::Man);
od.disable(OutputGenerator::Docbook);
if (Config_getBool(PDF_HYPERLINKS)) od.disable(OutputGenerator::Latex);
if (Config_getBool(RTF_HYPERLINKS)) od.disable(OutputGenerator::RTF);
od.startPageRef();
od.docify(theTranslator->trPageAbbreviation());
od.endPageRef(cn,mn);
od.popGeneratorState();
}
/*! Generate a place holder for a position in a list. Used for
* translators to be able to specify different elements orders
* depending on whether text flows from left to right or visa versa.
*/
QCString generateMarker(int id)
{
const int maxMarkerStrLen = 20;
char result[maxMarkerStrLen];
qsnprintf(result,maxMarkerStrLen,"@%d",id);
return result;
}
static QCString stripFromPath(const QCString &path,const StringVector &l)
{
// look at all the strings in the list and strip the longest match
QCString potential;
unsigned int length = 0;
for (const auto &s : l)
{
QCString prefix = s.c_str();
if (prefix.length() > length &&
qstricmp(path.left(prefix.length()),prefix)==0) // case insensitive compare
{
length = prefix.length();
potential = path.right(path.length()-prefix.length());
}
}
if (length) return potential;
return path;
}
/*! strip part of \a path if it matches
* one of the paths in the Config_getList(STRIP_FROM_PATH) list
*/
QCString stripFromPath(const QCString &path)
{
return stripFromPath(path,Config_getList(STRIP_FROM_PATH));
}
/*! strip part of \a path if it matches
* one of the paths in the Config_getList(INCLUDE_PATH) list
*/
QCString stripFromIncludePath(const QCString &path)
{
return stripFromPath(path,Config_getList(STRIP_FROM_INC_PATH));
}
/*! try to determine if \a name is a source or a header file name by looking
* at the extension. A number of variations is allowed in both upper and
* lower case) If anyone knows or uses another extension please let me know :-)
*/
int guessSection(const QCString &name)
{
QCString n=name.lower();
if (n.right(2)==".c" || // source
n.right(3)==".cc" ||
n.right(4)==".cxx" ||
n.right(4)==".cpp" ||
n.right(4)==".c++" ||
n.right(5)==".java" ||
n.right(2)==".m" ||
n.right(3)==".mm" ||
n.right(3)==".ii" || // inline
n.right(4)==".ixx" ||
n.right(4)==".ipp" ||
n.right(4)==".i++" ||
n.right(4)==".inl" ||
n.right(4)==".xml" ||
n.right(4)==".lex" ||
n.right(4)==".sql"
) return Entry::SOURCE_SEC;
if (n.right(2)==".h" || // header
n.right(3)==".hh" ||
n.right(4)==".hxx" ||
n.right(4)==".hpp" ||
n.right(4)==".h++" ||
n.right(4)==".idl" ||
n.right(4)==".ddl" ||
n.right(5)==".pidl" ||
n.right(4)==".ice"
) return Entry::HEADER_SEC;
return 0;
}
QCString resolveTypeDef(const Definition *context,const QCString &qualifiedName,
const Definition **typedefContext)
{
//printf("<<resolveTypeDef(%s,%s)\n",
// context ? qPrint(context->name()) : "<none>",qPrint(qualifiedName));
QCString result;
if (qualifiedName.isEmpty())
{
//printf(" qualified name empty!\n");
return result;
}
const Definition *mContext=context;
if (typedefContext) *typedefContext=context;
// see if the qualified name has a scope part
int scopeIndex = qualifiedName.findRev("::");
QCString resName=qualifiedName;
if (scopeIndex!=-1) // strip scope part for the name
{
resName=qualifiedName.right(qualifiedName.length()-scopeIndex-2);
if (resName.isEmpty())
{
// qualifiedName was of form A:: !
//printf(" qualified name of form A::!\n");
return result;
}
}
const MemberDef *md=0;
while (mContext && md==0)
{
// step 1: get the right scope
const Definition *resScope=mContext;
if (scopeIndex!=-1)
{
// split-off scope part
QCString resScopeName = qualifiedName.left(scopeIndex);
//printf("resScopeName='%s'\n",qPrint(resScopeName));
// look-up scope in context
int is,ps=0;
int l;
while ((is=getScopeFragment(resScopeName,ps,&l))!=-1)
{
QCString qualScopePart = resScopeName.mid(is,l);
QCString tmp = resolveTypeDef(mContext,qualScopePart);
if (!tmp.isEmpty()) qualScopePart=tmp;
resScope = resScope->findInnerCompound(qualScopePart);
//printf("qualScopePart='%s' resScope=%p\n",qPrint(qualScopePart),resScope);
if (resScope==0) break;
ps=is+l;
}
}
//printf("resScope=%s\n",resScope? qPrint(resScope->name()) : "<none>");
// step 2: get the member
if (resScope) // no scope or scope found in the current context
{
//printf("scope found: %s, look for typedef %s\n",
// qPrint(resScope->qualifiedName()),qPrint(resName));
MemberNameLinkedMap *mnd=0;
if (resScope->definitionType()==Definition::TypeClass)
{
mnd=Doxygen::memberNameLinkedMap;
}
else
{
mnd=Doxygen::functionNameLinkedMap;
}
MemberName *mn=mnd->find(resName);
if (mn)
{
int minDist=-1;
for (const auto &tmd_p : *mn)
{
const MemberDef *tmd = tmd_p.get();
//printf("Found member %s resScope=%s outerScope=%s mContext=%p\n",
// qPrint(tmd->name()),qPrint( resScope->name()),
// qPrint(tmd->getOuterScope()->name()), mContext);
if (tmd->isTypedef() /*&& tmd->getOuterScope()==resScope*/)
{
SymbolResolver resolver;
int dist=resolver.isAccessibleFrom(resScope,tmd);
if (dist!=-1 && (md==0 || dist<minDist))
{
md = tmd;
minDist = dist;
}
}
}
}
}
mContext=mContext->getOuterScope();
}
// step 3: get the member's type
if (md)
{
//printf(">>resolveTypeDef: Found typedef name '%s' in scope '%s' value='%s' args='%s'\n",
// qPrint(qualifiedName),qPrint(context->name()),md->typeString(),md->argsString()
// );
result=md->typeString();
QCString args = md->argsString();
if (args.find(")(")!=-1) // typedef of a function/member pointer
{
result+=args;
}
else if (args.find('[')!=-1) // typedef of an array
{
result+=args;
}
if (typedefContext) *typedefContext=md->getOuterScope();
}
else
{
//printf(">>resolveTypeDef: Typedef '%s' not found in scope '%s'!\n",
// qPrint(qualifiedName),context ? qPrint(context->name()) : "<global>");
}
return result;
}
int computeQualifiedIndex(const QCString &name)
{
int i = name.find('<');
return name.findRev("::",i==-1 ? name.length() : i);
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
static const char constScope[] = { 'c', 'o', 'n', 's', 't', ':' };
static const char volatileScope[] = { 'v', 'o', 'l', 'a', 't', 'i', 'l', 'e', ':' };
static const char virtualScope[] = { 'v', 'i', 'r', 't', 'u', 'a', 'l', ':' };
static const char operatorScope[] = { 'o', 'p', 'e', 'r', 'a', 't', 'o', 'r', '?', '?', '?' };
struct CharAroundSpace
{
CharAroundSpace()
{
charMap[static_cast<int>('(')].before=FALSE;
charMap[static_cast<int>('=')].before=FALSE;
charMap[static_cast<int>('&')].before=FALSE;
charMap[static_cast<int>('*')].before=FALSE;
charMap[static_cast<int>('[')].before=FALSE;
charMap[static_cast<int>('|')].before=FALSE;
charMap[static_cast<int>('+')].before=FALSE;
charMap[static_cast<int>(';')].before=FALSE;
charMap[static_cast<int>(':')].before=FALSE;
charMap[static_cast<int>('/')].before=FALSE;
charMap[static_cast<int>('=')].after=FALSE;
charMap[static_cast<int>(' ')].after=FALSE;
charMap[static_cast<int>('[')].after=FALSE;
charMap[static_cast<int>(']')].after=FALSE;
charMap[static_cast<int>('\t')].after=FALSE;
charMap[static_cast<int>('\n')].after=FALSE;
charMap[static_cast<int>(')')].after=FALSE;
charMap[static_cast<int>(',')].after=FALSE;
charMap[static_cast<int>('<')].after=FALSE;
charMap[static_cast<int>('|')].after=FALSE;
charMap[static_cast<int>('+')].after=FALSE;
charMap[static_cast<int>('(')].after=FALSE;
charMap[static_cast<int>('/')].after=FALSE;
}
struct CharElem
{
CharElem() : before(TRUE), after(TRUE) {}
bool before;
bool after;
};
CharElem charMap[256];
};
static CharAroundSpace g_charAroundSpace;
// Note: this function is not reentrant due to the use of static buffer!
QCString removeRedundantWhiteSpace(const QCString &s)
{
bool cliSupport = Config_getBool(CPP_CLI_SUPPORT);
bool vhdl = Config_getBool(OPTIMIZE_OUTPUT_VHDL);
if (s.isEmpty() || vhdl) return s;
// We use a static character array to
// improve the performance of this function
// and thread_local is needed to make it multi-thread safe
static THREAD_LOCAL char *growBuf = 0;
static THREAD_LOCAL int growBufLen = 0;
if ((int)s.length()*3>growBufLen) // For input character we produce at most 3 output characters,
{
growBufLen = s.length()*3;
growBuf = (char *)realloc(growBuf,growBufLen+1); // add 1 for 0-terminator
}
if (growBuf==0) return s; // should not happen, only we run out of memory
const char *src=s.data();
char *dst=growBuf;
uint i=0;
uint l=s.length();
uint csp=0;
uint vosp=0;
uint vsp=0;
uint osp=0;
char c;
char pc=0;
// skip leading whitespace
while (i<l && isspace((uchar)src[i]))
{
i++;
}
for (;i<l;i++)
{
c=src[i];
char nc=i<l-1 ? src[i+1] : ' ';
// search for "const"
if (csp<6 && c==constScope[csp] && // character matches substring "const"
(csp>0 || // inside search string
i==0 || // if it is the first character
!isId(pc) // the previous may not be a digit
)
)
csp++;
else // reset counter
csp=0;
if (vosp<6 && c==volatileScope[vosp] && // character matches substring "volatile"
(vosp>0 || // inside search string
i==0 || // if it is the first character
!isId(pc) // the previous may not be a digit
)
)
vosp++;
else // reset counter
vosp=0;
// search for "virtual"
if (vsp<8 && c==virtualScope[vsp] && // character matches substring "virtual"
(vsp>0 || // inside search string
i==0 || // if it is the first character
!isId(pc) // the previous may not be a digit
)
)
vsp++;
else // reset counter
vsp=0;
// search for "operator"
if (osp<11 && (osp>=8 || c==operatorScope[osp]) && // character matches substring "operator" followed by 3 arbitrary characters
(osp>0 || // inside search string
i==0 || // if it is the first character
!isId(pc) // the previous may not be a digit
)
)
osp++;
else // reset counter
osp=0;
switch(c)
{
case '"': // quoted string
{
*dst++=c;
pc = c;
i++;
for (;i<l;i++) // find end of string
{
c = src[i];
*dst++=c;
if (c=='\\' && i+1<l)
{
pc = c;
i++;
c = src[i];
*dst++=c;
}
else if (c=='"')
{
break;
}
pc = c;
}
}
break;
case '<': // current char is a <
*dst++=c;
if (i<l-1 &&
(isId(nc)) && // next char is an id char
(osp<8) // string in front is not "operator"
)
{
*dst++=' '; // add extra space
}
break;
case '>': // current char is a >
if (i>0 && !isspace((uchar)pc) &&
(isId(pc) || pc=='*' || pc=='&' || pc=='.' || pc=='>') && // prev char is an id char or space or *&.
(osp<8 || (osp==8 && pc!='-')) // string in front is not "operator>" or "operator->"
)
{
*dst++=' '; // add extra space in front
}
*dst++=c;
if (i<l-1 && (nc=='-' || nc=='&')) // '>-' -> '> -'
{
*dst++=' '; // add extra space after
}
break;
case ',': // current char is a ,
*dst++=c;
if (i>0 && !isspace((uchar)pc) &&
((i<l-1 && (isId(nc) || nc=='[')) || // the [ is for attributes (see bug702170)
(i<l-2 && nc=='$' && isId(src[i+2])) || // for PHP: ',$name' -> ', $name'
(i<l-3 && nc=='&' && src[i+2]=='$' && isId(src[i+3])) // for PHP: ',&$name' -> ', &$name'
)
)
{
*dst++=' '; // add extra space after
}
break;
case '^': // CLI 'Type^name' -> 'Type^ name'
case '%': // CLI 'Type%name' -> 'Type% name'
*dst++=c;
if (cliSupport && i<l-1 && (isId(nc) || nc=='-'))
{
*dst++=' '; // add extra space after
}
break;
case ')': // current char is a ) -> ')name' -> ') name'
*dst++=c;
if (i<l-1 && (isId(nc) || nc=='-'))
{
*dst++=' '; // add extra space after
}
break;
case '*':
if (i>0 && pc!=' ' && pc!='\t' && pc!=':' &&
pc!='*' && pc!='&' && pc!='(' && pc!='/' &&
pc!='.' && osp<9
)
// avoid splitting &&, **, .*, operator*, operator->*
{
*dst++=' ';
}
*dst++=c;
break;
case '&':
if (i>0 && isId(pc) && osp<9)
{
if (nc != '=')
// avoid splitting operator&=
{
*dst++=' ';
}
}
*dst++=c;
break;
case '$': // '$name' -> ' $name'
// 'name$name' -> 'name$name'
if (isId(pc))
{
*dst++=c;
break;
}
// else fallthrough
case '@': // '@name' -> ' @name'
case '\'': // ''name' -> '' name'
if (i>0 && i<l-1 && pc!='=' && pc!=':' && !isspace((uchar)pc) &&
isId(nc) && osp<8) // ")id" -> ") id"
{
*dst++=' ';
}
*dst++=c;
break;
case ':': // current char is a :
if (csp==6) // replace const::A by const ::A
{
*dst++=' ';
csp=0;
}
else if (vosp==9) // replace volatile::A by volatile ::A
{
*dst++=' ';
vosp=0;
}
else if (vsp==8) // replace virtual::A by virtual ::A
{
*dst++=' ';
vsp=0;
}
*dst++=c;
break;
case ' ': // fallthrough
case '\n': // fallthrough
case '\t':
{
if (g_charAroundSpace.charMap[(uchar)pc].before &&
g_charAroundSpace.charMap[(uchar)nc].after &&
!(pc==',' && nc=='.') &&
(osp<8 || (osp>=8 && isId(pc) && isId(nc)))
// e.g. 'operator >>' -> 'operator>>',
// 'operator "" _x' -> 'operator""_x',
// but not 'operator int' -> 'operatorint'
)
{ // keep space
*dst++=' ';
}
else if ((pc=='*' || pc=='&' || pc=='.') && nc=='>')
{
*dst++=' ';
}
}
break;
default:
*dst++=c;
if (c=='t' && csp==5 && i<l-1 && // found 't' in 'const'
!(isId(nc) || nc==')' || nc==',' || isspace((uchar)nc))
) // prevent const ::A from being converted to const::A
{
*dst++=' ';
csp=0;
}
else if (c=='e' && vosp==8 && i<l-1 && // found 'e' in 'volatile'
!(isId(nc) || nc==')' || nc==',' || isspace((uchar)nc))
) // prevent volatile ::A from being converted to volatile::A
{
*dst++=' ';
vosp=0;
}
else if (c=='l' && vsp==7 && i<l-1 && // found 'l' in 'virtual'
!(isId(nc) || nc==')' || nc==',' || isspace((uchar)nc))
) // prevent virtual ::A from being converted to virtual::A
{
*dst++=' ';
vsp=0;
}
break;
}
pc=c;
}
*dst++='\0';
//printf("removeRedundantWhitespace(%s)->%s\n",qPrint(s),growBuf);
return growBuf;
}
/**
* Returns the position in the string where a function parameter list
* begins, or -1 if one is not found.
*/
int findParameterList(const QCString &name)
{
int pos=-1;
int templateDepth=0;
do
{
if (templateDepth > 0)
{
int nextOpenPos=name.findRev('>', pos);
int nextClosePos=name.findRev('<', pos);
if (nextOpenPos!=-1 && nextOpenPos>nextClosePos)
{
++templateDepth;
pos=nextOpenPos-1;
}
else if (nextClosePos!=-1)
{
--templateDepth;
pos=nextClosePos-1;
}
else // more >'s than <'s, see bug701295
{
return -1;
}
}
else
{
int lastAnglePos=name.findRev('>', pos);
int bracePos=name.findRev('(', pos);
if (lastAnglePos!=-1 && lastAnglePos>bracePos)
{
++templateDepth;
pos=lastAnglePos-1;
}
else
{
int bp = bracePos>0 ? name.findRev('(',bracePos-1) : -1;
// bp test is to allow foo(int(&)[10]), but we need to make an exception for operator()
return bp==-1 || (bp>=8 && name.mid(bp-8,10)=="operator()") ? bracePos : bp;
}
}
} while (pos!=-1);
return -1;
}
bool rightScopeMatch(const QCString &scope, const QCString &name)
{
int sl=scope.length();
int nl=name.length();
return (name==scope || // equal
(scope.right(nl)==name && // substring
sl-nl>1 && scope.at(sl-nl-1)==':' && scope.at(sl-nl-2)==':' // scope
)
);
}
bool leftScopeMatch(const QCString &scope, const QCString &name)
{
int sl=scope.length();
int nl=name.length();
return (name==scope || // equal
(scope.left(nl)==name && // substring
sl>nl+1 && scope.at(nl)==':' && scope.at(nl+1)==':' // scope
)
);
}
void linkifyText(const TextGeneratorIntf &out, const Definition *scope,
const FileDef *fileScope,const Definition *self,
const QCString &text, bool autoBreak,bool external,
bool keepSpaces,int indentLevel)
{
if (text.isEmpty()) return;
//printf("linkify='%s'\n",text);
std::string txtStr=text.str();
size_t strLen = txtStr.length();
if (strLen==0) return;
static const reg::Ex regExp(R"(\a[\w~!\\.:$]*)");
reg::Iterator it(txtStr,regExp);
reg::Iterator end;
//printf("linkifyText scope=%s fileScope=%s strtxt=%s strlen=%d external=%d\n",
// scope ? qPrint(scope->name()):"<none>",
// fileScope ? qPrint(fileScope->name()) : "<none>",
// qPrint(txtStr),strLen,external);
size_t index=0;
size_t skipIndex=0;
size_t floatingIndex=0;
for (; it!=end ; ++it) // for each word from the text string
{
const auto &match = *it;
size_t newIndex = match.position();
size_t matchLen = match.length();
floatingIndex+=newIndex-skipIndex+matchLen;
if (newIndex>0 && txtStr.at(newIndex-1)=='0') // ignore hex numbers (match x00 in 0x00)
{
std::string part = txtStr.substr(skipIndex,newIndex+matchLen-skipIndex);
out.writeString(part.c_str(),keepSpaces);
skipIndex=index=newIndex+matchLen;
continue;
}
// add non-word part to the result
bool insideString=FALSE;
for (size_t i=index;i<newIndex;i++)
{
if (txtStr.at(i)=='"') insideString=!insideString;
}
//printf("floatingIndex=%d strlen=%d autoBreak=%d\n",floatingIndex,strLen,autoBreak);
if (strLen>35 && floatingIndex>30 && autoBreak) // try to insert a split point
{
std::string splitText = txtStr.substr(skipIndex,newIndex-skipIndex);
size_t splitLength = splitText.length();
size_t offset=1;
size_t i = splitText.find(',');
if (i==std::string::npos) { i=splitText.find('<'); if (i!=std::string::npos) offset=0; }
if (i==std::string::npos) i=splitText.find('>');
if (i==std::string::npos) i=splitText.find(' ');
//printf("splitText=[%s] len=%d i=%d offset=%d\n",qPrint(splitText),splitLength,i,offset);
if (i!=std::string::npos) // add a link-break at i in case of Html output
{
std::string part1 = splitText.substr(0,i+offset);
out.writeString(part1.c_str(),keepSpaces);
out.writeBreak(indentLevel==0 ? 0 : indentLevel+1);
std::string part2 = splitText.substr(i+offset);
out.writeString(part2.c_str(),keepSpaces);
floatingIndex=splitLength-i-offset+matchLen;
}
else
{
out.writeString(splitText.c_str(),keepSpaces);
}
}
else
{
//ol.docify(txtStr.mid(skipIndex,newIndex-skipIndex));
std::string part = txtStr.substr(skipIndex,newIndex-skipIndex);
out.writeString(part.c_str(),keepSpaces);
}
// get word from string
std::string word=txtStr.substr(newIndex,matchLen);
QCString matchWord = substitute(substitute(QCString(word),"\\","::"),".","::");
//printf("linkifyText word=%s matchWord=%s scope=%s\n",
// qPrint(word),qPrint(matchWord),scope ? qPrint(scope->name()) : "<none>");
bool found=FALSE;
if (!insideString)
{
const MemberDef *md=0;
const ClassDef *cd=0;
const FileDef *fd=0;
const NamespaceDef *nd=0;
const GroupDef *gd=0;
const ConceptDef *cnd=0;
//printf("** Match word '%s'\n",qPrint(matchWord));
SymbolResolver resolver(fileScope);
cd=resolver.resolveClass(scope,matchWord);
const MemberDef *typeDef = resolver.getTypedef();
if (typeDef) // First look at typedef then class, see bug 584184.
{
//printf("Found typedef %s\n",qPrint(typeDef->name()));
if (external ? typeDef->isLinkable() : typeDef->isLinkableInProject())
{
if (typeDef->getOuterScope()!=self)
{
out.writeLink(typeDef->getReference(),
typeDef->getOutputFileBase(),
typeDef->anchor(),
word.c_str());
found=TRUE;
}
}
}
if (!found && (cd || (cd=getClass(matchWord))))
{
//printf("Found class %s\n",qPrint(cd->name()));
// add link to the result
if (external ? cd->isLinkable() : cd->isLinkableInProject())
{
if (cd!=self)