Debian Codegraph @

kwsCheckIndent.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckIndent.cxx
5
6 Copyright (c) Kitware, Inc. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
12
13 =========================================================================*/
14 #include "kwsParser.h"
15
16 #include <string.h>
17
18 namespace kws {
19
20 #define ALIGN_LEFT -99999
21
22 /** Extract the current line from pos to LF */
23 std::string Parser::ExtractLine(size_t pos)
24 {
25 size_t p = m_Buffer.find("\n",pos);
26 if(p>pos)
27 {
28 return m_Buffer.substr(pos,p-pos-1);
29 }
30 return "";
31 }
32
33 /** Return the current ident */
34 int Parser::GetCurrentIdent(std::string line,char type)
35 {
36 int indent = 0;
37 std::string::const_iterator it = line.begin();
38 while(it != line.end() && (*it)== type)
39 {
40 indent++;
41 it++;
42 }
43 return indent;
44 }
45
46 /** Check the indent size */
47 bool Parser::CheckIndent(IndentType itype,
48 unsigned long size,
49 bool doNotCheckHeader,
50 bool allowBlockLine,
51 unsigned int maxLength,
52 bool allowCommaIndent)
53 {
54 m_TestsDone[INDENT] = true;
55 m_TestsDescription[INDENT] = "The Indent should respect: ";
56 constexpr size_t length = 11;
57 char* val = new char[length];
58 snprintf(val,length,"%ld ",size);
59 m_TestsDescription[INDENT] += val;
60 if(itype == TAB)
61 {
62 m_TestsDescription[INDENT] += "tabs";
63 }
64 else
65 {
66 m_TestsDescription[INDENT] += "spaces";
67 }
68 delete [] val;
69
70 if(doNotCheckHeader)
71 {
72 m_TestsDescription[INDENT] += " (not checking header, ";
73 }
74 else
75 {
76 m_TestsDescription[INDENT] += " (checking header, ";
77 }
78
79 if(allowBlockLine)
80 {
81 m_TestsDescription[INDENT] += "blockline allowed)";
82 }
83 else
84 {
85 m_TestsDescription[INDENT] += "blockline not allowed)";
86 }
87
88 bool hasError = false;
89 unsigned long pos = 0;
90 std::string::const_iterator it = m_Buffer.begin();
91
92 // Variable to check if we are in a comment or not
93 bool isCheckingComment = false;
94
95 // In the case we have #if/#else/#endif we want to ignore the #else section
96 // if we have some '{' not closed
97 // #if
98 // {
99 // #else
100 // {
101 // #endif
102 this->ComputeIfElseEndifList();
103
104 // Create the indentation
105 this->InitIndentation();
106
107 // Print the initial indentation
108 /*std::vector<IndentPosition>::const_iterator itIndent
109 * = m_IdentPositionVector.begin();
110 while(itIndent != m_IdentPositionVector.end())
111 {
112 std::cout << this->GetLineNumber((*itIndent).position) << std::endl;
113 std::cout << (*itIndent).current << std::endl;
114 std::cout << (*itIndent).after << std::endl;
115 std::cout << (*itIndent).name.c_str() << std::endl;
116 itIndent++;
117 }*/
118
119 // If we do not want to check the header
120 if(doNotCheckHeader)
121 {
122 unsigned long fileSize = 0;
123 // if the file is specified
124 if (!m_HeaderFilename.empty()) {
125 std::ifstream file;
126 file.open(m_HeaderFilename.c_str(), std::ios::binary | std::ios::in);
127 if(!file.is_open())
128 {
129 std::cout << "Cannot open file: " << m_HeaderFilename << std::endl;
130 return false;
131 }
132
133 file.seekg(0,std::ios::end);
134 fileSize = file.tellg();
135 file.close();
136 } else {
137 // we look at the first '*/' in the file which indicated the end
138 // of the current header
139 // This assume that there is an header at some point
140 auto endHeader = static_cast<long int>(m_Buffer.find("*/", 0));
141 if(endHeader>0)
142 {
143 fileSize = endHeader;
144 }
145 }
146
147 // We skip the header
148 for(unsigned int i=0;i<fileSize;i++)
149 {
150 if(it != m_Buffer.end())
151 {
152 pos++;
153 it++;
154 }
155 }
156 }
157
158 char type = ' ';
159 if(itype == kws::TAB) {type = '\t';}
160
161 int wantedIndent = 0;
162
163 // We extract the first line and compute the number of spaces/tabs at
164 // the beginning
165 std::string line = this->ExtractLine(pos);
166 int currentIndent = this->GetCurrentIdent(line,type);
167 bool firstChar = true;
168
169 // We start to check
170 while(it != m_Buffer.end())
171 {
172 // If we should skip the line
173 bool skip = this->IsInElseForbiddenSection(pos);
174
175 if( strncmp(&(*it), "typedef", 7 ) == 0 )
176 {
177 while( it != m_Buffer.end() && (*it) != ';' )
178 {
179 ++it;
180 ++pos;
181 }
182 continue;
183 }
184
185 if((*it) == type || (*it)=='\r' || skip)
186 {
187 it++;
188 pos++;
189 continue;
190 }
191
192 if((*it) == '\n')
193 {
194 it++;
195 pos++;
196 // We extract the next line
197 std::string localline = this->ExtractLine(pos);
198 currentIndent = this->GetCurrentIdent(localline,type);
199 firstChar = true;
200 continue;
201 }
202
203 // Check if pos is in the list of positions
204 auto itIdentPos = m_IdentPositionVector.begin();
205 IndentPosition *sindent = nullptr;
206 while (itIdentPos != m_IdentPositionVector.end()) {
207 if ((*itIdentPos).position == pos) {
208 sindent = &(*itIdentPos);
209 break;
210 }
211 itIdentPos++;
212 }
213
214 // We check if we have the right indent
215 if(sindent)
216 {
217 long int wanted = wantedIndent+size*sindent->current;
218 if(sindent->current == ALIGN_LEFT)
219 {
220 wanted = 0;
221 }
222
223 else if(currentIndent != wanted)
224 {
225 bool returnError = true;
226 // We check that the previous line is not ending with a semicolon
227 // and that the sum of the two lines is more than maxLength
228 std::string previousLine = this->GetLine(this->GetLineNumber(pos)-2);
229 std::string currentLine = this->GetLine(this->GetLineNumber(pos)-1);
230 if( (previousLine[previousLine.size()-1] != ';')
231 && (previousLine.size()+currentLine.size()-currentIndent>0.9*maxLength)
232 )
233 {
234 returnError = false;
235 }
236
237 // Check for special characters
238 if(previousLine[previousLine.size()-1] != ';')
239 {
240 // Check if we have a << at the beginning of the current line
241 auto posSpecialChar = static_cast<long int>(currentLine.find("<<"));
242 if(posSpecialChar != -1)
243 {
244 returnError = false;
245 for(long int i=0;i<posSpecialChar;i++)
246 {
247 if(currentLine[i] != '\r' && currentLine[i] != '\n' && currentLine[i] != ' ' && currentLine[i] != '\t')
248 {
249 returnError = true;
250 break;
251 }
252 }
253 }
254 }
255
256 // We check that any definitions of public:, private: and protected:
257 // are not within a double class (class inside a class)
258 // WARNING: We just ignore the error at that point (maybe more checking
259 // will be necessary)
260 if(sindent->name == "public:"
261 || sindent->name == "protected:"
262 || sindent->name == "private:"
263 || sindent->name == "signals:"
264 || sindent->name == "public slots:"
265 )
266 {
267 int inClass = this->IsInClass(this->GetPositionWithoutComments(pos));
268 if(inClass>1)
269 {
270 returnError = false;
271 }
272 }
273
274 // Check if we are inside a macro. If yes we disable the checking of the ident
275 // (This is too complex for macros)
276 if(returnError)
277 {
278 // We are in a macro if we have
279 // '#define foo' and the line finishs with '\'
280 auto begMacro = static_cast<long int>(m_Buffer.find("#define", 0));
281 while(begMacro!=-1)
282 {
283 // Find the end of the macro
284 auto endMacro =
285 static_cast<long int>(m_Buffer.find("\r", begMacro));
286 while(endMacro>0 && m_Buffer[endMacro-1]=='\\')
287 {
288 endMacro = static_cast<long int>(m_Buffer.find("\r",endMacro+1));
289 }
290
291 if(endMacro!=-1 && (long int)pos<endMacro && (long int)pos>begMacro)
292 {
293 returnError = false;
294 break;
295 }
296 begMacro = static_cast<long int>(m_Buffer.find("#define",endMacro));
297 }
298 }
299
300 if(returnError)
301 {
302 Error error;
303 error.line = this->GetLineNumber(pos);
304 error.line2 = error.line;
305 error.number = INDENT;
306 error.description = "Special Indent is wrong ";
307 constexpr size_t length = 11;
308 char* localval = new char[length];
309 snprintf(localval,length,"%d",currentIndent);
310 error.description += localval;
311 error.description += " (should be ";
312 delete [] localval;
313 localval = new char[length];
314 snprintf(localval,length,"%ld",wanted);
315 error.description += localval;
316 error.description += ")";
317 delete [] localval;
318 m_ErrorList.push_back(error);
319 hasError = true;
320 }
321 }
322 wantedIndent += size*sindent->after;
323 firstChar = false;
324 }
325 else if((it != m_Buffer.end()) && ((*it) == '{')
326 //&& !this->IsInComments(pos)
327 && !isCheckingComment
328 //&& !this->IsBetweenQuote(pos,true)
329 && !(
330 (!this->IsInAnyComments(pos) && this->IsBetweenQuote(this->GetPositionWithoutComments(pos),false))
331 || (this->IsInAnyComments(pos) && this->IsBetweenQuote(pos,true)))
332 ) // openning bracket
333 {
334 bool check = true;
335 // Check if { is after // [THIS CHECK IS NOT USEFULL ANYMORE]
336 auto doubleslash = static_cast<long int>(m_Buffer.rfind("//", pos));
337 if(doubleslash != -1)
338 {
339 if(this->GetLineNumber(doubleslash) == this->GetLineNumber(pos))
340 {
341 check = false;
342 }
343 }
344
345 if(check)
346 {
347 wantedIndent += size;
348 }
349 }
350
351 if(firstChar) // general case
352 {
353 // If we are in a comment
354 if(this->IsInComments(pos))
355 {
356 if(this->IsInAnyComments(pos))
357 {
358 isCheckingComment = true;
359 }
360 // We check how much space we have in the middle section
361 unsigned int nSpaceMiddle = 0;
362 while(m_CommentMiddle[nSpaceMiddle] == type)
363 {
364 nSpaceMiddle++;
365 }
366
367 if((*it) == m_CommentMiddle[nSpaceMiddle])
368 {
369 if(currentIndent>0)
370 {
371 currentIndent -= nSpaceMiddle;
372 }
373 }
374 else
375 {
376 // We check how much space we have in the end section
377 unsigned int nSpaceEnd = 0;
378 while(m_CommentEnd[nSpaceEnd] == type)
379 {
380 nSpaceEnd++;
381 }
382
383 if((*it) == m_CommentEnd[nSpaceEnd])
384 {
385 currentIndent -= nSpaceEnd;
386 isCheckingComment = false;
387 }
388 }
389 }
390 else
391 {
392 isCheckingComment = false;
393 }
394
395 bool inComment = this->IsInAnyComments(pos);
396
397 if(isCheckingComment && !inComment)
398 {
399 Warning warning;
400 warning.line = this->GetLineNumber(pos);
401 warning.line2 = this->GetLineNumber(pos);
402 warning.description = "There is a problem with the comments";
403 warning.number = INDENT;
404 m_WarningList.push_back(warning);
405
406 // We check how much space we have in the end section
407 unsigned int nSpaceEnd = 0;
408 while(m_CommentEnd[nSpaceEnd] == type)
409 {
410 nSpaceEnd++;
411 }
412 currentIndent -= nSpaceEnd;
413 isCheckingComment = false;
414 }
415
416 size_t poswithoutcomment = this->GetPositionWithoutComments(pos);
417
418 if((currentIndent != wantedIndent)
419 && ((inComment
420 && !this->IsBetweenCharsFast('<','>',pos,true)
421 && !this->IsBetweenCharsFast('(',')',pos,true)
422 && !this->IsBetweenChars('(',')',pos,true)
423 && !this->IsBetweenChars('<','>',pos,true))
424 ||
425 (
426 !inComment
427 && !this->IsBetweenCharsFast('<','>',poswithoutcomment,false)
428 && !this->IsBetweenCharsFast('(',')',poswithoutcomment,false)
429 && !this->IsBetweenChars('(',')',poswithoutcomment,false)
430 && !this->IsBetweenChars('<','>',poswithoutcomment,false)))
431 )
432 {
433 // If we are inside an enum we do not check indent
434 bool isInsideEnum = false;
435 auto bracket = static_cast<long int>(m_Buffer.find_last_of('{', pos));
436 if(bracket != -1)
437 {
438 size_t l = this->FindPreviousWord(bracket-1,true).size();
439 if(this->FindPreviousWord(bracket-l-2,true) == "enum")
440 {
441 isInsideEnum = true;
442 }
443 }
444
445 bool reportError = true;
446
447 // We check that the previous line is not ending with a semicolon
448 // and that the sum of the two lines is more than maxLength
449 std::string previousLine = this->GetLine(this->GetLineNumber(pos)-2);
450 std::string currentLine = this->GetLine(this->GetLineNumber(pos)-1);
451 if(( (previousLine[previousLine.size()-1] != ';')
452 && (previousLine.size()+currentLine.size()-currentIndent>0.9*maxLength))
453 || (isInsideEnum)
454 )
455 {
456 reportError = false;
457 }
458 // Check if the line start with '<<' if this is the case we ignore it
459 else if( strncmp( &(*it), "<<", 2) == 0 ||
460 ( previousLine.size() > 6
461 && previousLine.compare( previousLine.size()-2, 2, "<<" ) == 0) )
462 {
463 reportError = false;
464 }
465 // Catch some instances of long variable declarations using
466 // typedefed types that end in "Type".
467 else if( previousLine.size() > 6
468 && previousLine.compare( previousLine.size()-4, 4, "Type" ) == 0 )
469 {
470 reportError = false;
471 }
472 // We don't care about everything between the class and '{'
473 else
474 {
475 auto classPos = static_cast<long int>(m_Buffer.find("class", 0));
476 while (classPos != -1) {
477 auto endClass =
478 static_cast<long int>(m_Buffer.find("{", classPos));
479 if (endClass != -1 && (long int)pos < endClass &&
480 (long int)pos > classPos) {
481 reportError = false;
482 break;
483 }
484 classPos = static_cast<long int>(m_Buffer.find("class",classPos+1));
485 }
486 }
487
488 // If the ident is between a '=' and a ';' we ignore
489 // This is for lists. THIS IS NOT A STRICT CHECK. Might be missing some.
490 if(reportError)
491 {
492 auto classPos = static_cast<long int>(m_BufferNoComment.find("=", 0));
493 while(classPos!=-1)
494 {
495 auto posNoCommments =
496 static_cast<long int>(this->GetPositionWithoutComments(pos));
497 auto endClass =
498 static_cast<long int>(m_BufferNoComment.find(";", classPos));
499 if(endClass!=-1 && (long int)posNoCommments<endClass && (long int)posNoCommments>classPos)
500 {
501 reportError = false;
502 break;
503 }
504 classPos = static_cast<long int>(m_BufferNoComment.find("=",classPos+1));
505 }
506 }
507
508 // If the ident is between a ':' and a '{' we ignore
509 // This is for the constructor.
510 if(reportError)
511 {
512 auto classPos = static_cast<long int>(m_BufferNoComment.find(":", 0));
513 while(classPos!=-1)
514 {
515 auto posNoCommments =
516 static_cast<long int>(this->GetPositionWithoutComments(pos));
517 auto endConstructor =
518 static_cast<long int>(m_BufferNoComment.find("{", classPos));
519
520 if(endConstructor!=-1 && (long int)posNoCommments<endConstructor && (long int)posNoCommments>classPos)
521 {
522 reportError = false;
523 break;
524 }
525 classPos = static_cast<long int>(m_BufferNoComment.find(":",classPos+1));
526 }
527 }
528
529 // If the ident is between 'return' and ';' we ignore
530 // Ideally we should have a strict check
531 if(reportError)
532 {
533 auto classPos =
534 static_cast<long int>(m_BufferNoComment.find("return", 0));
535 while(classPos!=-1)
536 {
537 auto posNoCommments =
538 static_cast<long int>(this->GetPositionWithoutComments(pos));
539 auto endConstructor =
540 static_cast<long int>(m_BufferNoComment.find(";", classPos));
541
542 if(endConstructor!=-1 && (long int)posNoCommments<endConstructor && (long int)posNoCommments>classPos)
543 {
544 reportError = false;
545 break;
546 }
547 classPos = static_cast<long int>(m_BufferNoComment.find("return",classPos+1));
548 }
549 }
550
551 // Check if we are inside a macro. If yes we disable the checking of the ident
552 // (This is too complex for macros)
553 if(reportError)
554 {
555 // We are in a macro if we have
556 // '#define foo' and the line finishs with '\'
557 auto begMacro = static_cast<long int>(m_Buffer.find("#define", 0));
558 while(begMacro!=-1)
559 {
560 // Find the end of the macro
561 auto endMacro =
562 static_cast<long int>(m_Buffer.find("\r", begMacro));
563 while(endMacro>0 && m_Buffer[endMacro-1]=='\\')
564 {
565 endMacro = static_cast<long int>(m_Buffer.find("\r",endMacro+1));
566 }
567
568 if(endMacro!=-1 && (long int)pos<endMacro && (long int)pos>begMacro)
569 {
570 reportError = false;
571 break;
572 }
573 begMacro = static_cast<long int>(m_Buffer.find("#define",endMacro));
574 }
575 }
576
577 // If we allowCommaIndentation:
578 // if(myvalue,
579 // myvalue2)
580 if(allowCommaIndent && reportError)
581 {
582 // Check the end of the previous line if we have a comma
583 auto j = static_cast<long int>(previousLine.size() - 1);
584 while(j>0)
585 {
586 if(previousLine[j] != ' '
587 && previousLine[j] != '\n'
588 && previousLine[j] != '\r')
589 {
590 break;
591 }
592 j--;
593 }
594
595 if(previousLine[j] == ',')
596 {
597 reportError = false;
598 }
599 }
600
601 if( currentIndent < 0 )
602 {
603 reportError = false;
604 }
605
606 if(reportError)
607 {
608 Error error;
609 error.line = this->GetLineNumber(pos);
610 error.line2 = error.line;
611 error.number = INDENT;
612 error.description = "Indent is wrong ";
613 constexpr size_t length = 11;
614 char* localval = new char[length];
615 snprintf(localval,length,"%d",currentIndent);
616 error.description += localval;
617 error.description += " (should be ";
618 delete [] localval;
619 localval = new char[length];
620 snprintf(localval,length,"%d",wantedIndent);
621 error.description += localval;
622 error.description += ")";
623 delete [] localval;
624 m_ErrorList.push_back(error);
625 hasError = true;
626 }
627 }
628 }
629
630 if((it != m_Buffer.end()) && ((*it) == '}')
631 && !sindent
632 //&& !this->IsInComments(pos)
633 && !isCheckingComment
634 //&& !this->IsBetweenQuote(pos,true)
635 && !(
636 (!this->IsInAnyComments(pos) && this->IsBetweenQuote(this->GetPositionWithoutComments(pos),false))
637 || (this->IsInAnyComments(pos) && this->IsBetweenQuote(pos,true)))
638 ) // closing bracket
639 {
640 bool check = true;
641 // Check if { is after //
642 auto doubleslash = static_cast<long int>(m_Buffer.rfind("//", pos));
643 if(doubleslash != -1)
644 {
645 if(this->GetLineNumber(doubleslash) == this->GetLineNumber(pos))
646 {
647 check = false;
648 }
649 }
650 if(check)
651 {
652 wantedIndent -= size;
653 }
654 }
655
656 firstChar = false;
657 if(it != m_Buffer.end())
658 {
659 it++;
660 pos++;
661 }
662 }
663
664 return !hasError;
665 }
666
667
668 /** Check if the current position is a valid switch statement */
669 bool Parser::CheckValidSwitchStatement(unsigned int posSwitch)
670 {
671 if((m_BufferNoComment[posSwitch-1]!='\n'
672 && m_BufferNoComment[posSwitch-1]!=' '
673 && posSwitch-1 != 0) ||
674 (m_BufferNoComment.size() > posSwitch+6
675 && m_BufferNoComment[posSwitch+6] != ' '
676 && m_BufferNoComment[posSwitch+6] != '('))
677 {
678 return false;
679 }
680 return true;
681 }
682
683
684 /** Init the indentation */
685 bool Parser::InitIndentation()
686 {
687 m_IdentPositionVector.clear();
688
689 // namespace
690 std::vector<size_t> namespacevec;
691
692 size_t posNamespace = m_BufferNoComment.find("namespace",0);
693 while(posNamespace!=std::string::npos)
694 {
695 size_t posNamespace1 = m_BufferNoComment.find("{",posNamespace);
696 if(posNamespace1 != std::string::npos)
697 {
698 size_t posNamespace2 = m_BufferNoComment.find(";",posNamespace);
699 if((posNamespace2 == std::string::npos) || (posNamespace2 > posNamespace1))
700 {
701 size_t posNamespaceComments = this->GetPositionWithComments(posNamespace1);
702 IndentPosition ind;
703 ind.position = posNamespaceComments;
704 ind.current = 0;
705 ind.after = 0;
706 namespacevec.push_back(posNamespaceComments);
707 //std::cout << "Found Namespace at: " << this->GetLineNumber(posNamespaceComments) << std::endl;
708 m_IdentPositionVector.push_back(ind);
709 ind.position = this->FindClosingChar('{','}',posNamespaceComments);
710 namespacevec.push_back(ind.position);
711 m_IdentPositionVector.push_back(ind);
712 }
713 }
714 posNamespace = m_BufferNoComment.find("namespace",posNamespace+1);
715 }
716
717 // Create a list of position specific for namespaces
718 std::vector<size_t> namespacePos;
719 auto itIdentPos = m_IdentPositionVector.begin();
720 while(itIdentPos != m_IdentPositionVector.end())
721 {
722 namespacePos.push_back((*itIdentPos).position);
723 itIdentPos++;
724 }
725
726 // Check if the { is the first in the file/function or in a namespace
727 size_t posClass = m_BufferNoComment.find('{',0);
728
729 while(posClass!= std::string::npos && this->IsInElseForbiddenSection(this->GetPositionWithComments(posClass)))
730 {
731 posClass = m_BufferNoComment.find('{',posClass+1);
732 }
733
734 while(posClass != std::string::npos)
735 {
736 // We count the number of { and } before posClass
737 unsigned int nOpen = 0;
738 unsigned int nClose = 0;
739
740 size_t open = m_BufferNoComment.find('{',0);
741 while(open!=std::string::npos && open<posClass)
742 {
743 if(!this->IsInElseForbiddenSection(this->GetPositionWithComments(open))
744 && !this->IsBetweenQuote(open)
745 )
746 {
747 bool isNamespace = false;
748 // Remove the potential namespaces
749 std::vector<size_t>::const_iterator itN = namespacePos.begin();
750 while(itN != namespacePos.end())
751 {
752 if((*itN)==this->GetPositionWithComments(open))
753 {
754 isNamespace = true;
755 }
756 itN++;
757 }
758 if(!isNamespace)
759 {
760 nOpen++;
761 }
762 }
763 open = m_BufferNoComment.find('{',open+1);
764 }
765
766 size_t close = m_BufferNoComment.find('}',0);
767 while(close!=std::string::npos && close<posClass)
768 {
769 if(!this->IsInElseForbiddenSection(this->GetPositionWithComments(close))
770 && !this->IsBetweenQuote(close)
771 )
772 {
773 bool isNamespace = false;
774 // Remove the potential namespaces
775 std::vector<size_t>::const_iterator itN = namespacePos.begin();
776 while(itN != namespacePos.end())
777 {
778 if((*itN)==this->GetPositionWithComments(close))
779 {
780 isNamespace = true;
781 }
782 itN++;
783 }
784 if(!isNamespace)
785 {
786 nClose++;
787 }
788 }
789 close = m_BufferNoComment.find('}',close+1);
790 }
791
792 bool defined = false;
793
794 if(nClose == nOpen)
795 {
796 // Check if this is not the namespace previously defined
797 auto itname = namespacevec.begin();
798 while(itname != namespacevec.end())
799 {
800 if((*itname) == this->GetPositionWithComments(posClass))
801 {
802 defined = true;
803 break;
804 }
805 itname++;
806 }
807 }
808
809 if((nClose == nOpen) && !defined)
810 {
811 // translate the position in the buffer position;
812 size_t posClassComments = this->GetPositionWithComments(posClass);
813 IndentPosition ind;
814 ind.position = posClassComments;
815 ind.current = 0;
816 ind.after = 1;
817 m_IdentPositionVector.push_back(ind);
818 ind.position = this->FindClosingChar('{','}',posClassComments);
819 while(this->IsBetweenQuote(ind.position,true))
820 {
821 ind.position = this->FindClosingChar('{','}',ind.position+1);
822 }
823 ind.current = -1;
824 ind.after = -1;
825 m_IdentPositionVector.push_back(ind);
826 }
827 posClass = m_BufferNoComment.find('{',posClass+1);
828 }
829
830 // int main()
831 size_t posMain = m_BufferNoComment.find("main",0);
832 while(posMain != std::string::npos)
833 {
834 // Check if the next char is '('
835 bool valid = true;
836 auto pos = static_cast<unsigned long>(posMain + 5);
837 while(pos<m_BufferNoComment.size()
838 && (m_BufferNoComment[pos]==' '
839 || m_BufferNoComment[pos]=='\r'
840 || m_BufferNoComment[pos]=='\n')
841 )
842 {
843 pos++;
844 }
845
846 if(m_BufferNoComment[pos]!='(')
847 {
848 valid = false;
849 }
850 if(valid)
851 {
852 size_t bracket = m_BufferNoComment.find('{',posMain+4);
853 if(bracket != std::string::npos)
854 {
855 // translate the position in the buffer position;
856 size_t posMainComments = this->GetPositionWithComments(bracket);
857 IndentPosition ind;
858 ind.position = posMainComments;
859 ind.current = 0;
860 ind.after = 1;
861 m_IdentPositionVector.push_back(ind);
862 ind.position = this->FindClosingChar('{','}',posMainComments);
863 ind.current = -1;
864 ind.after = -1;
865 m_IdentPositionVector.push_back(ind);
866 }
867 }
868 posMain = m_BufferNoComment.find("main",posMain+4);
869 }
870
871 // switch/case statement
872 // for the moment break; restore the indentation
873 size_t posSwitch = m_BufferNoComment.find("switch",0);
874 while(posSwitch != std::string::npos)
875 {
876 // Check that it is a valid switch statement
877 if(!this->CheckValidSwitchStatement(static_cast<unsigned int>(posSwitch)))
878 {
879 posSwitch = m_BufferNoComment.find("switch",posSwitch+1);
880 continue;
881 }
882
883 // If this is the first case we find the openning { in order to
884 // find the closing } of the switch statement
885 size_t openningBracket = m_BufferNoComment.find("{",posSwitch);
886 size_t closingBracket = this->FindClosingChar('{','}',openningBracket,true);
887 size_t posColumnComments = this->GetPositionWithComments(closingBracket);
888 IndentPosition ind;
889 ind.position = posColumnComments;
890 ind.current = -1;
891 ind.after = -2;
892 m_IdentPositionVector.push_back(ind);
893
894 // Do the default case
895 size_t defaultPos = m_BufferNoComment.find("default",openningBracket);
896 if(defaultPos > closingBracket)
897 {
898 defaultPos = std::string::npos;
899 }
900
901 // We need to make sure that there is no "switch" statement nested
902 size_t nestedSwitch = m_BufferNoComment.find("switch",posSwitch+1);
903 while(nestedSwitch != std::string::npos)
904 {
905 if(!this->CheckValidSwitchStatement(static_cast<unsigned int>(nestedSwitch)))
906 {
907 nestedSwitch = m_BufferNoComment.find("switch",nestedSwitch+1);
908 continue;
909 }
910
911 if(nestedSwitch < defaultPos)
912 {
913 defaultPos = m_BufferNoComment.find("default",defaultPos+1);
914 }
915 else
916 {
917 break;
918 }
919 nestedSwitch = m_BufferNoComment.find("switch",nestedSwitch+1);
920 }
921
922 if(defaultPos != std::string::npos)
923 {
924 size_t localposColumnComments = this->GetPositionWithComments(defaultPos);
925 IndentPosition localind;
926 localind.position = localposColumnComments;
927
928 // The current indent should be -1 unless we are right after the openning
929 // bracket. In that case the current indent should be 0;
930 size_t j=defaultPos-1;
931 while(j!=std::string::npos)
932 {
933 if(m_BufferNoComment[j] != ' '
934 && m_BufferNoComment[j] != '\n'
935 && m_BufferNoComment[j] != '\r'
936 )
937 {
938 break;
939 }
940 j--;
941 }
942
943 if(j == openningBracket)
944 {
945 localind.current = 0;
946 }
947 else
948 {
949 localind.current = -1;
950 }
951
952 localind.after = 0;
953 m_IdentPositionVector.push_back(localind);
954 // Find the ':' after the default
955 size_t column = m_BufferNoComment.find(":",defaultPos+1);
956 column = this->GetPositionWithComments(column);
957
958 // Sometimes there is a { right after the : we skip it if this is
959 // the case
960 size_t ic = column+1;
961 while(ic<m_Buffer.size()
962 && (m_Buffer[ic] == ' '
963 || m_Buffer[ic] == '\r'
964 || m_Buffer[ic] == '\n'))
965 {
966 ic++;
967 }
968 if(m_Buffer[ic] == '{')
969 {
970 IndentPosition tempind;
971 tempind.position = ic;
972 tempind.current = 0;
973 tempind.after = 0;
974 m_IdentPositionVector.push_back(tempind);
975 tempind.position = this->FindClosingChar('{','}',ic);
976 tempind.current = 0;
977 tempind.after = 0;
978 m_IdentPositionVector.push_back(tempind);
979 }
980 }
981
982 size_t posCase = m_BufferNoComment.find("case",openningBracket);
983 bool firstCase = true;
984 size_t previousCase = openningBracket;
985
986 while(posCase!= std::string::npos && posCase<closingBracket)
987 {
988 // Check if we don't have any switch statement inside
989 size_t insideSwitch = m_BufferNoComment.find("switch",previousCase);
990 if(insideSwitch>openningBracket && insideSwitch<posCase)
991 {
992 // jump to the end of the inside switch/case
993 size_t insideBracket = m_BufferNoComment.find("{",insideSwitch);
994 posCase = this->FindClosingChar('{','}',insideBracket,true);
995 }
996 else
997 {
998 size_t localposColumnComments = this->GetPositionWithComments(posCase);
999 IndentPosition localindtemp;
1000 localindtemp.position = localposColumnComments;
1001 if(firstCase)
1002 {
1003 localindtemp.current = 0;
1004 }
1005 else
1006 {
1007 localindtemp.current = -1;
1008 }
1009 localindtemp.after = 0;
1010
1011 m_IdentPositionVector.push_back(localindtemp);
1012
1013 size_t column = m_BufferNoComment.find(':',posCase+3);
1014 // Make sure that we are not checing '::'
1015 while(column+1<m_BufferNoComment.size()
1016 && m_BufferNoComment[column+1]==':')
1017 {
1018 column = m_BufferNoComment.find(':',column+2);
1019 }
1020
1021 if(column != std::string::npos)
1022 {
1023 // translate the position in the buffer position;
1024 localposColumnComments = this->GetPositionWithComments(column);
1025 IndentPosition localind;
1026 localind.position = localposColumnComments;
1027 if(firstCase)
1028 {
1029 localind.current = 0;
1030 localind.after = 1;
1031 }
1032 else
1033 {
1034 localind.current = -1;
1035 localind.after = 0;
1036 }
1037 m_IdentPositionVector.push_back(localind);
1038
1039 // Sometimes there is a { right after the : we skip it if this is
1040 // the case
1041 size_t ic = localposColumnComments+1;
1042 while(ic<m_Buffer.size()
1043 && (m_Buffer[ic] == ' '
1044 || m_Buffer[ic] == '\r'
1045 || m_Buffer[ic] == '\n'))
1046 {
1047 ic++;
1048 }
1049 if(m_Buffer[ic] == '{')
1050 {
1051 IndentPosition tempind;
1052 tempind.position = ic;
1053 tempind.current = 0;
1054 tempind.after = 0;
1055 m_IdentPositionVector.push_back(tempind);
1056 tempind.position = this->FindClosingChar('{','}',ic);
1057 tempind.current = 0;
1058 tempind.after = 0;
1059 m_IdentPositionVector.push_back(tempind);
1060 }
1061 }
1062 }
1063 firstCase = false;
1064 previousCase = posCase;
1065 posCase = m_BufferNoComment.find("case",posCase+1);
1066 }
1067
1068 posSwitch = m_BufferNoComment.find("switch",posSwitch+3);
1069 }
1070
1071 // Some words should be indented as the previous indent
1072 this->AddIndent("public:",-1,0);
1073 this->AddIndent("private:",-1,0);
1074 this->AddIndent("protected:",-1,0);
1075 this->AddIndent("signals:",-1,0);
1076 this->AddIndent("public slots:",-1,0);
1077 this->AddIndent("private slots:",-1,0);
1078 this->AddIndent("protected slots:",-1,0);
1079
1080 // some words should be always align left
1081 this->AddIndent("#include",ALIGN_LEFT,0);
1082 this->AddIndent("#if",ALIGN_LEFT,0);
1083 //this->AddIndent("#ifdef",ALIGN_LEFT,0); // #if is taking care of it
1084 this->AddIndent("#elif",ALIGN_LEFT,0);
1085 this->AddIndent("#else",ALIGN_LEFT,0);
1086 this->AddIndent("#endif",ALIGN_LEFT,0);
1087 //this->AddIndent("#ifndef",ALIGN_LEFT,0); // #if is taking care of it
1088 this->AddIndent("#define",ALIGN_LEFT,0);
1089 this->AddIndent("#undef",ALIGN_LEFT,0);
1090
1091 return true;
1092 }
1093
1094 void Parser::AddIndent(const char* name,int current,int after)
1095 {
1096 size_t posPrev = m_Buffer.find(name,0);
1097 while(posPrev!=std::string::npos)
1098 {
1099 IndentPosition ind;
1100 ind.position = posPrev;
1101 ind.current = current;
1102 ind.after = after;
1103 ind.name = name;
1104 m_IdentPositionVector.push_back(ind);
1105 posPrev = m_Buffer.find(name,posPrev+1);
1106 }
1107 }
1108
1109 /** Return true if the current position with comments is in the
1110 * #else section of the forbiden part.
1111 * In case we have something like:
1112 * #if
1113 * {
1114 * #else
1115 * {
1116 * #endif */
1117 bool Parser::IsInElseForbiddenSection(size_t pos)
1118 {
1119 IfElseEndifListType::const_iterator itLS = m_IfElseEndifList.begin();
1120 while(itLS != m_IfElseEndifList.end())
1121 {
1122 if(pos>(*itLS).first && pos<(*itLS).second)
1123 {
1124 return true;
1125 }
1126 itLS++;
1127 }
1128 return false;
1129 }
1130
1131
1132 } // end namespace kws