forked from Kitware/KWStyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwsCheckIndent.cxx
1100 lines (1002 loc) · 32.5 KB
/
kwsCheckIndent.cxx
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
/*=========================================================================
Program: KWStyle - Kitware Style Checker
Module: kwsCheckIndent.cxx
Copyright (c) Kitware, Inc. All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "kwsParser.h"
namespace kws {
#define ALIGN_LEFT -99999
/** Extract the current line from pos to \n */
std::string Parser::ExtractLine(size_t pos)
{
size_t p = m_Buffer.find("\n",pos);
if(p>pos)
{
return m_Buffer.substr(pos,p-pos-1);
}
return "";
}
/** Return the current ident */
int Parser::GetCurrentIdent(std::string line,char type)
{
int indent = 0;
std::string::const_iterator it = line.begin();
while(it != line.end() && (*it)== type)
{
indent++;
it++;
}
return indent;
}
/** Check the indent size */
bool Parser::CheckIndent(IndentType itype,
unsigned long size,
bool doNotCheckHeader,
bool allowBlockLine,
unsigned int maxLength,
bool allowCommaIndent)
{
m_TestsDone[INDENT] = true;
m_TestsDescription[INDENT] = "The Indent should respect: ";
char* val = new char[10];
sprintf(val,"%ld ",size);
m_TestsDescription[INDENT] += val;
if(itype == (IndentType)TABS)
{
m_TestsDescription[INDENT] += "tabs";
}
else
{
m_TestsDescription[INDENT] += "spaces";
}
delete [] val;
if(doNotCheckHeader)
{
m_TestsDescription[INDENT] += " (not checking header, ";
}
else
{
m_TestsDescription[INDENT] += " (checking header, ";
}
if(allowBlockLine)
{
m_TestsDescription[INDENT] += "blockline allowed)";
}
else
{
m_TestsDescription[INDENT] += "blockline not allowed)";
}
bool hasError = false;
unsigned long pos = 0;
std::string::const_iterator it = m_Buffer.begin();
// Variable to check if we are in a comment or not
bool isCheckingComment = false;
// In the case we have #if/#else/#endif we want to ignore the #else section
// if we have some '{' not closed
// #if
// {
// #else
// {
// #endif
this->ComputeIfElseEndifList();
// Create the indentation
this->InitIndentation();
// Print the initial indentation
/*std::vector<IndentPosition>::const_iterator itIndent = m_IdentPositionVector.begin();
while(itIndent != m_IdentPositionVector.end())
{
std::cout << this->GetLineNumber((*itIndent).position) << std::endl;
std::cout << (*itIndent).current << std::endl;
std::cout << (*itIndent).after << std::endl;
std::cout << (*itIndent).name.c_str() << std::endl;
itIndent++;
}*/
// If we do not want to check the header
if(doNotCheckHeader)
{
unsigned long fileSize = 0;
// if the file is specified
if(m_HeaderFilename.size() > 0)
{
std::ifstream file;
file.open(m_HeaderFilename.c_str(), std::ios::binary | std::ios::in);
if(!file.is_open())
{
std::cout << "Cannot open file: " << m_HeaderFilename << std::endl;
return false;
}
file.seekg(0,std::ios::end);
fileSize = file.tellg();
file.close();
}
else
{
// we look at the first '*/' in the file which indicated the end of the current header
// This assume that there is an header at some point
long int endHeader = m_Buffer.find("*/",0);
if(endHeader>0)
{
fileSize = endHeader;
}
}
// We skip the header
for(unsigned int i=0;i<fileSize;i++)
{
if(it != m_Buffer.end())
{
pos++;
it++;
}
}
}
char type = ' ';
if(itype == (IndentType)TABS) {type = '\t';}
int wantedIndent = 0;
// We extract the firt line and compute the number of spaces/tabs at the beginning
std::string line = this->ExtractLine(pos);
int currentIndent = this->GetCurrentIdent(line,type);
bool firstChar = true;
// We start to check
while(it != m_Buffer.end())
{
// If we should skip the line
bool skip = this->IsInElseForbiddenSection(pos);
if((*it) == type || (*it)=='\r' || skip)
{
it++;
pos++;
continue;
}
if((*it) == '\n')
{
it++;
pos++;
// We extract the next line
std::string line = this->ExtractLine(pos);
currentIndent = this->GetCurrentIdent(line,type);
firstChar = true;
continue;
}
// Check if pos is in the list of positions
std::vector<IndentPosition>::iterator itIdentPos = m_IdentPositionVector.begin();
IndentPosition* sindent = NULL;
while(itIdentPos != m_IdentPositionVector.end())
{
if((*itIdentPos).position == pos)
{
sindent = &(*itIdentPos);
break;
}
itIdentPos++;
}
// We check if we have the right indent
if(sindent)
{
long int wanted = wantedIndent+size*sindent->current;
if(sindent->current == ALIGN_LEFT)
{
wanted = 0;
}
else if(currentIndent != wanted)
{
bool returnError = true;
// We check that the previous line is not ending with a semicolon
// and that the sum of the two lines is more than maxLength
std::string previousLine = this->GetLine(this->GetLineNumber(pos)-2);
std::string currentLine = this->GetLine(this->GetLineNumber(pos)-1);
if( (previousLine[previousLine.size()-1] != ';')
&& (previousLine.size()+currentLine.size()-currentIndent>maxLength)
)
{
returnError = false;
}
// Check for special characters
if(previousLine[previousLine.size()-1] != ';')
{
// Check if we have a << at the beginning of the current line
long int posSpecialChar = currentLine.find("<<");
if(posSpecialChar != -1)
{
returnError = false;
for(long int i=0;i<posSpecialChar;i++)
{
if(currentLine[i] != '\r' && currentLine[i] != '\n' && currentLine[i] != ' ' && currentLine[i] != '\t')
{
returnError = true;
break;
}
}
}
}
// We check that any definitions of public:, private: and protected:
// are not within a double class (class inside a class)
// WARNING: We just ignore the error at that point (maybe more checking
// will be necessary)
if(sindent->name == "public:"
|| sindent->name == "protected:"
|| sindent->name == "private:"
|| sindent->name == "signals:"
|| sindent->name == "public slots:"
)
{
int inClass = this->IsInClass(this->GetPositionWithoutComments(pos));
if(inClass>1)
{
returnError = false;
}
}
// Check if we are inside a macro. If yes we disable the checking of the ident
// (This is too complex for macros)
if(returnError)
{
// We are in a macro if we have
// '#define foo' and the line finishs with '\'
long int begMacro = m_Buffer.find("#define",0);
while(begMacro!=-1)
{
// Find the end of the macro
long int endMacro = m_Buffer.find("\r",begMacro);
while(endMacro>0 && m_Buffer[endMacro-1]=='\\')
{
endMacro = m_Buffer.find("\r",endMacro+1);
}
if(endMacro!=-1 && (long int)pos<endMacro && (long int)pos>begMacro)
{
returnError = false;
break;
}
begMacro = m_Buffer.find("#define",endMacro);
}
}
if(returnError)
{
Error error;
error.line = this->GetLineNumber(pos);
error.line2 = error.line;
error.number = INDENT;
error.description = "Special Indent is wrong ";
char* val = new char[10];
sprintf(val,"%d",currentIndent);
error.description += val;
error.description += " (should be ";
delete [] val;
val = new char[10];
sprintf(val,"%ld",wanted);
error.description += val;
error.description += ")";
delete [] val;
m_ErrorList.push_back(error);
hasError = true;
}
}
wantedIndent += size*sindent->after;
firstChar = false;
}
else if((it != m_Buffer.end()) && ((*it) == '{')
//&& !this->IsInComments(pos)
&& !isCheckingComment
//&& !this->IsBetweenQuote(pos,true)
&& !(
(!this->IsInAnyComments(pos) && this->IsBetweenQuote(this->GetPositionWithoutComments(pos),false))
|| (this->IsInAnyComments(pos) && this->IsBetweenQuote(pos,true)))
) // openning bracket
{
bool check = true;
// Check if { is after // [THIS CHECK IS NOT USEFULL ANYMORE]
long int doubleslash = m_Buffer.rfind("//",pos);
if(doubleslash != -1)
{
if(this->GetLineNumber(doubleslash) == this->GetLineNumber(pos))
{
check = false;
}
}
if(check)
{
wantedIndent += size;
}
}
if(firstChar) // general case
{
// If we are in a comment
if(this->IsInComments(pos))
{
if(this->IsInAnyComments(pos))
{
isCheckingComment = true;
}
// We check how much space we have in the middle section
unsigned int nSpaceMiddle = 0;
while(m_CommentMiddle[nSpaceMiddle] == type)
{
nSpaceMiddle++;
}
if((*it) == m_CommentMiddle[nSpaceMiddle])
{
if(currentIndent>0)
{
currentIndent -= nSpaceMiddle;
}
}
else
{
// We check how much space we have in the end section
unsigned int nSpaceEnd = 0;
while(m_CommentEnd[nSpaceEnd] == type)
{
nSpaceEnd++;
}
if((*it) == m_CommentEnd[nSpaceEnd])
{
currentIndent -= nSpaceEnd;
isCheckingComment = false;
}
}
}
else
{
isCheckingComment = false;
}
bool inComment = this->IsInAnyComments(pos);
if(isCheckingComment && !inComment)
{
Warning warning;
warning.line = this->GetLineNumber(pos);
warning.line2 = this->GetLineNumber(pos);
warning.description = "There is a problem with the comments";
warning.number = INDENT;
m_WarningList.push_back(warning);
// We check how much space we have in the end section
unsigned int nSpaceEnd = 0;
while(m_CommentEnd[nSpaceEnd] == type)
{
nSpaceEnd++;
}
currentIndent -= nSpaceEnd;
isCheckingComment = false;
}
unsigned int poswithoutcomment = this->GetPositionWithoutComments(pos);
if((currentIndent != wantedIndent)
&& ((inComment
&& !this->IsBetweenCharsFast('<','>',pos,true)
&& !this->IsBetweenCharsFast('(',')',pos,true)
&& !this->IsBetweenChars('(',')',pos,true)
&& !this->IsBetweenChars('<','>',pos,true))
||
(
!inComment
&& !this->IsBetweenCharsFast('<','>',poswithoutcomment,false)
&& !this->IsBetweenCharsFast('(',')',poswithoutcomment,false)
&& !this->IsBetweenChars('(',')',poswithoutcomment,false)
&& !this->IsBetweenChars('<','>',poswithoutcomment,false)))
)
{
// If we are inside an enum we do not check indent
bool isInsideEnum = false;
long int bracket = m_Buffer.find_last_of('{',pos);
if(bracket != -1)
{
unsigned int l = this->FindPreviousWord(bracket-1,true).size();
if(this->FindPreviousWord(bracket-l-2,true) == "enum")
{
isInsideEnum = true;
}
}
bool reportError = true;
// We check that the previous line is not ending with a semicolon
// and that the sum of the two lines is more than maxLength
std::string previousLine = this->GetLine(this->GetLineNumber(pos)-2);
std::string currentLine = this->GetLine(this->GetLineNumber(pos)-1);
if(( (previousLine[previousLine.size()-1] != ';')
&& (previousLine.size()+currentLine.size()-currentIndent>maxLength))
|| (isInsideEnum)
)
{
reportError = false;
}
// Check if the line start with '<<' if this is the case we ignore it
else if((*it) == '<' && (*(it+1) == '<'))
{
long int posInf = previousLine.find("<<");
if((posInf != -1) && (posInf == currentIndent+1))
{
reportError = false;
}
}
// We don't care about everything between the class and '{'
else
{
long int classPos = m_Buffer.find("class",0);
while(classPos!=-1)
{
long int endClass = m_Buffer.find("{",classPos);
if(endClass!=-1 && (long int)pos<endClass && (long int)pos>classPos)
{
reportError = false;
break;
}
classPos = m_Buffer.find("class",classPos+1);
}
}
// If the ident is between a '=' and a ';' we ignore
// This is for lists. THIS IS NOT A STRICT CHECK. Might be missing some.
if(reportError)
{
long int classPos = m_BufferNoComment.find("=",0);
while(classPos!=-1)
{
long int posNoCommments = this->GetPositionWithoutComments(pos);
long int endClass = m_BufferNoComment.find(";",classPos);
if(endClass!=-1 && (long int)posNoCommments<endClass && (long int)posNoCommments>classPos)
{
reportError = false;
break;
}
classPos = m_BufferNoComment.find("=",classPos+1);
}
}
// If the ident is between a ':' and a '{' we ignore
// This is for the constructor.
if(reportError)
{
long int classPos = m_BufferNoComment.find(":",0);
while(classPos!=-1)
{
long int posNoCommments = this->GetPositionWithoutComments(pos);
long int endConstructor = m_BufferNoComment.find("{",classPos);
if(endConstructor!=-1 && (long int)posNoCommments<endConstructor && (long int)posNoCommments>classPos)
{
reportError = false;
break;
}
classPos = m_BufferNoComment.find(":",classPos+1);
}
}
// If the ident is between 'return' and ';' we ignore
// Ideally we should have a strict check
if(reportError)
{
long int classPos = m_BufferNoComment.find("return",0);
while(classPos!=-1)
{
long int posNoCommments = this->GetPositionWithoutComments(pos);
long int endConstructor = m_BufferNoComment.find(";",classPos);
if(endConstructor!=-1 && (long int)posNoCommments<endConstructor && (long int)posNoCommments>classPos)
{
reportError = false;
break;
}
classPos = m_BufferNoComment.find("return",classPos+1);
}
}
// Check if we are inside a macro. If yes we disable the checking of the ident
// (This is too complex for macros)
if(reportError)
{
// We are in a macro if we have
// '#define foo' and the line finishs with '\'
long int begMacro = m_Buffer.find("#define",0);
while(begMacro!=-1)
{
// Find the end of the macro
long int endMacro = m_Buffer.find("\r",begMacro);
while(endMacro>0 && m_Buffer[endMacro-1]=='\\')
{
endMacro = m_Buffer.find("\r",endMacro+1);
}
if(endMacro!=-1 && (long int)pos<endMacro && (long int)pos>begMacro)
{
reportError = false;
break;
}
begMacro = m_Buffer.find("#define",endMacro);
}
}
// If we allowCommaIndentation:
// if(myvalue,
// myvalue2)
if(allowCommaIndent && reportError)
{
// Check the end of the previous line if we have a comma
long int j = previousLine.size()-1;
while(j>0)
{
if(previousLine[j] != ' '
&& previousLine[j] != '\n'
&& previousLine[j] != '\r')
{
break;
}
j--;
}
if(previousLine[j] == ',')
{
reportError = false;
}
}
if(reportError)
{
Error error;
error.line = this->GetLineNumber(pos);
error.line2 = error.line;
error.number = INDENT;
error.description = "Indent is wrong ";
char* val = new char[10];
sprintf(val,"%d",currentIndent);
error.description += val;
error.description += " (should be ";
delete [] val;
val = new char[10];
sprintf(val,"%d",wantedIndent);
error.description += val;
error.description += ")";
delete [] val;
m_ErrorList.push_back(error);
hasError = true;
}
}
}
if((it != m_Buffer.end()) && ((*it) == '}')
&& !sindent
//&& !this->IsInComments(pos)
&& !isCheckingComment
//&& !this->IsBetweenQuote(pos,true)
&& !(
(!this->IsInAnyComments(pos) && this->IsBetweenQuote(this->GetPositionWithoutComments(pos),false))
|| (this->IsInAnyComments(pos) && this->IsBetweenQuote(pos,true)))
) // closing bracket
{
bool check = true;
// Check if { is after //
long int doubleslash = m_Buffer.rfind("//",pos);
if(doubleslash != -1)
{
if(this->GetLineNumber(doubleslash) == this->GetLineNumber(pos))
{
check = false;
}
}
if(check)
{
wantedIndent -= size;
}
}
firstChar = false;
if(it != m_Buffer.end())
{
it++;
pos++;
}
}
return !hasError;
}
/** Check if the current position is a valid switch statement */
bool Parser::CheckValidSwitchStatement(unsigned int posSwitch)
{
if((m_BufferNoComment[posSwitch-1]!='\n'
&& m_BufferNoComment[posSwitch-1]!=' '
&& posSwitch-1 != 0) ||
(m_BufferNoComment.size() > posSwitch+6
&& m_BufferNoComment[posSwitch+6] != ' '
&& m_BufferNoComment[posSwitch+6] != '('))
{
return false;
}
return true;
}
/** Init the indentation */
bool Parser::InitIndentation()
{
m_IdentPositionVector.clear();
// namespace
std::vector<size_t> namespacevec;
size_t posNamespace = m_BufferNoComment.find("namespace",0);
while(posNamespace!=std::string::npos)
{
size_t posNamespace1 = m_BufferNoComment.find("{",posNamespace);
if(posNamespace1 != std::string::npos)
{
size_t posNamespace2 = m_BufferNoComment.find(";",posNamespace);
if((posNamespace2 == std::string::npos) || (posNamespace2 > posNamespace1))
{
size_t posNamespaceComments = this->GetPositionWithComments(posNamespace1);
IndentPosition ind;
ind.position = posNamespaceComments;
ind.current = 0;
ind.after = 0;
namespacevec.push_back(posNamespaceComments);
//std::cout << "Found Namespace at: " << this->GetLineNumber(posNamespaceComments) << std::endl;
m_IdentPositionVector.push_back(ind);
ind.position = this->FindClosingChar('{','}',posNamespaceComments);
namespacevec.push_back(ind.position);
m_IdentPositionVector.push_back(ind);
}
}
posNamespace = m_BufferNoComment.find("namespace",posNamespace+1);
}
// Create a list of position specific for namespaces
std::vector<size_t> namespacePos;
std::vector<IndentPosition>::iterator itIdentPos = m_IdentPositionVector.begin();
while(itIdentPos != m_IdentPositionVector.end())
{
namespacePos.push_back((*itIdentPos).position);
itIdentPos++;
}
// Check if the { is the first in the file/function or in a namespace
size_t posClass = m_BufferNoComment.find('{',0);
while(posClass!= std::string::npos && this->IsInElseForbiddenSection(this->GetPositionWithComments(posClass)))
{
posClass = m_BufferNoComment.find('{',posClass+1);
}
while(posClass != std::string::npos)
{
// We count the number of { and } before posClass
int nOpen = 0;
int nClose = 0;
size_t open = m_BufferNoComment.find('{',0);
while(open!=std::string::npos && open<posClass)
{
if(!this->IsInElseForbiddenSection(this->GetPositionWithComments(open))
&& !this->IsBetweenQuote(open)
)
{
bool isNamespace = false;
// Remove the potential namespaces
std::vector<size_t>::const_iterator itN = namespacePos.begin();
while(itN != namespacePos.end())
{
if((*itN)==this->GetPositionWithComments(open))
{
isNamespace = true;
}
itN++;
}
if(!isNamespace)
{
nOpen++;
}
}
open = m_BufferNoComment.find('{',open+1);
}
size_t close = m_BufferNoComment.find('}',0);
while(close!=std::string::npos && close<posClass)
{
if(!this->IsInElseForbiddenSection(this->GetPositionWithComments(close))
&& !this->IsBetweenQuote(close)
)
{
bool isNamespace = false;
// Remove the potential namespaces
std::vector<size_t>::const_iterator itN = namespacePos.begin();
while(itN != namespacePos.end())
{
if((*itN)==this->GetPositionWithComments(close))
{
isNamespace = true;
}
itN++;
}
if(!isNamespace)
{
nClose++;
}
}
close = m_BufferNoComment.find('}',close+1);
}
bool defined = false;
if(nClose == nOpen)
{
// Check if this is not the namespace previously defined
std::vector<size_t>::iterator itname = namespacevec.begin();
while(itname != namespacevec.end())
{
if((*itname) == this->GetPositionWithComments(posClass))
{
defined = true;
break;
}
itname++;
}
}
if((nClose == nOpen) && !defined)
{
// translate the position in the buffer position;
size_t posClassComments = this->GetPositionWithComments(posClass);
IndentPosition ind;
ind.position = posClassComments;
ind.current = 0;
ind.after = 1;
m_IdentPositionVector.push_back(ind);
ind.position = this->FindClosingChar('{','}',posClassComments);
while(this->IsBetweenQuote(ind.position,true))
{
ind.position = this->FindClosingChar('{','}',ind.position+1);
}
ind.current = -1;
ind.after = -1;
m_IdentPositionVector.push_back(ind);
}
posClass = m_BufferNoComment.find('{',posClass+1);
}
// int main()
size_t posMain = m_BufferNoComment.find("main",0);
while(posMain != std::string::npos)
{
// Check if the next char is '('
bool valid = true;
unsigned long pos = posMain+5;
while(pos<m_BufferNoComment.size()
&& (m_BufferNoComment[pos]==' '
|| m_BufferNoComment[pos]=='\r'
|| m_BufferNoComment[pos]=='\n')
)
{
pos++;
}
if(m_BufferNoComment[pos]!='(')
{
valid = false;
}
if(valid)
{
size_t bracket = m_BufferNoComment.find('{',posMain+4);
if(bracket != std::string::npos)
{
// translate the position in the buffer position;
size_t posMainComments = this->GetPositionWithComments(bracket);
IndentPosition ind;
ind.position = posMainComments;
ind.current = 0;
ind.after = 1;
m_IdentPositionVector.push_back(ind);
ind.position = this->FindClosingChar('{','}',posMainComments);
ind.current = -1;
ind.after = -1;
m_IdentPositionVector.push_back(ind);
}
}
posMain = m_BufferNoComment.find("main",posMain+4);
}
// switch/case statement
// for the moment break; restore the indentation
size_t posSwitch = m_BufferNoComment.find("switch",0);
while(posSwitch != std::string::npos)
{
// Check that it is a valid switch statement
if(!this->CheckValidSwitchStatement(posSwitch))
{
posSwitch = m_BufferNoComment.find("switch",posSwitch+1);
continue;
}
// If this is the first case we find the openning { in order to
// find the closing } of the switch statement
size_t openningBracket = m_BufferNoComment.find("{",posSwitch);
size_t closingBracket = this->FindClosingChar('{','}',openningBracket,true);
size_t posColumnComments = this->GetPositionWithComments(closingBracket);
IndentPosition ind;
ind.position = posColumnComments;
ind.current = -1;
ind.after = -2;
m_IdentPositionVector.push_back(ind);
// Do the default case
size_t defaultPos = m_BufferNoComment.find("default",openningBracket);
if(defaultPos > closingBracket)
{
defaultPos = std::string::npos;
}
// We need to make sure that there is no "switch" statement nested
size_t nestedSwitch = m_BufferNoComment.find("switch",posSwitch+1);
while(nestedSwitch != std::string::npos)
{
if(!this->CheckValidSwitchStatement(nestedSwitch))
{
nestedSwitch = m_BufferNoComment.find("switch",nestedSwitch+1);
continue;
}
if(nestedSwitch < defaultPos)
{
defaultPos = m_BufferNoComment.find("default",defaultPos+1);
}
else
{
break;
}
nestedSwitch = m_BufferNoComment.find("switch",nestedSwitch+1);
}
if(defaultPos != std::string::npos)
{
size_t posColumnComments = this->GetPositionWithComments(defaultPos);
IndentPosition ind;
ind.position = posColumnComments;
// The current indent should be -1 unless we are right after the openning
// bracket. In that case the current indent should be 0;
size_t j=defaultPos-1;
while(j!=std::string::npos)
{
if(m_BufferNoComment[j] != ' '
&& m_BufferNoComment[j] != '\n'
&& m_BufferNoComment[j] != '\r'
)
{
break;
}
j--;
}
if(j == openningBracket)
{
ind.current = 0;
}
else
{
ind.current = -1;
}
ind.after = 0;
m_IdentPositionVector.push_back(ind);
// Find the ':' after the default
size_t column = m_BufferNoComment.find(":",defaultPos+1);
column = this->GetPositionWithComments(column);
// Sometimes there is a { right after the : we skip it if this is
// the case
size_t ic = column+1;
while(ic<m_Buffer.size()
&& (m_Buffer[ic] == ' '
|| m_Buffer[ic] == '\r'
|| m_Buffer[ic] == '\n'))
{
ic++;
}
if(m_Buffer[ic] == '{')
{
IndentPosition ind;
ind.position = ic;
ind.current = 0;
ind.after = 0;
m_IdentPositionVector.push_back(ind);
ind.position = this->FindClosingChar('{','}',ic);
ind.current = 0;
ind.after = 0;
m_IdentPositionVector.push_back(ind);
}
}
size_t posCase = m_BufferNoComment.find("case",openningBracket);
bool firstCase = true;
size_t previousCase = openningBracket;
while(posCase!= std::string::npos && posCase<closingBracket)
{
// Check if we don't have any switch statement inside
size_t insideSwitch = m_BufferNoComment.find("switch",previousCase);
if(insideSwitch>openningBracket && insideSwitch<posCase)
{
// jump to the end of the inside switch/case
size_t insideBracket = m_BufferNoComment.find("{",insideSwitch);
posCase = this->FindClosingChar('{','}',insideBracket,true);
}
else
{
size_t posColumnComments = this->GetPositionWithComments(posCase);
IndentPosition ind;
ind.position = posColumnComments;
if(firstCase)
{
ind.current = 0;
}
else
{
ind.current = -1;
}
ind.after = 0;
m_IdentPositionVector.push_back(ind);
size_t column = m_BufferNoComment.find(':',posCase+3);
// Make sure that we are not checing '::'
while(column+1<m_BufferNoComment.size()
&& m_BufferNoComment[column+1]==':')
{
column = m_BufferNoComment.find(':',column+2);
}
if(column != std::string::npos)
{
// translate the position in the buffer position;
size_t posColumnComments = this->GetPositionWithComments(column);
IndentPosition ind;
ind.position = posColumnComments;
if(firstCase)
{
ind.current = 0;
ind.after = 1;
}
else