| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckIfWhileForUntil.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 |
#include <string.h> // strlen |
| 16 |
|
| 17 |
namespace kws { |
| 18 |
|
| 19 |
/** Check the number of spaces after the if keyword */ |
| 20 |
bool Parser::CheckIfWhileForUntil(unsigned int after) |
| 21 |
{ |
| 22 |
m_TestsDone[IFWHILEFORUNTIL] = true; |
| 23 |
m_TestsDescription[IFWHILEFORUNTIL] = |
| 24 |
"Number of spaces after If/While/For should be"; |
| 25 |
constexpr size_t length = 200; |
| 26 |
char* val = new char[length]; |
| 27 |
snprintf(val,length," = %d",after); |
| 28 |
m_TestsDescription[IFWHILEFORUNTIL] += val; |
| 29 |
delete [] val; |
| 30 |
|
| 31 |
bool hasErrors = false; |
| 32 |
|
| 33 |
if(!this->FindIfWhileForUntil("if",after)) |
| 34 |
{ |
| 35 |
hasErrors = true; |
| 36 |
} |
| 37 |
if(!this->FindIfWhileForUntil("while",after)) |
| 38 |
{ |
| 39 |
hasErrors = true; |
| 40 |
} |
| 41 |
if(!this->FindIfWhileForUntil("for",after)) |
| 42 |
{ |
| 43 |
hasErrors = true; |
| 44 |
} |
| 45 |
if(!this->FindIfWhileForUntil("until",after)) |
| 46 |
{ |
| 47 |
hasErrors = true; |
| 48 |
} |
| 49 |
|
| 50 |
return !hasErrors; |
| 51 |
} |
| 52 |
|
| 53 |
/** Check the operator */ |
| 54 |
bool Parser::FindIfWhileForUntil(const char* op, |
| 55 |
unsigned int after) |
| 56 |
{ |
| 57 |
bool hasErrors = false; |
| 58 |
|
| 59 |
long int prevOperatorPos = 0; |
| 60 |
size_t operatorPos = m_BufferNoComment.find(op,prevOperatorPos); |
| 61 |
while(operatorPos != std::string::npos) |
| 62 |
{ |
| 63 |
bool checkError = true; |
| 64 |
if( operatorPos > 0 && !isspace( m_BufferNoComment[operatorPos-1] ) ) |
| 65 |
{ |
| 66 |
checkError = false; |
| 67 |
} |
| 68 |
if( operatorPos+strlen(op) < m_BufferNoComment.size() && |
| 69 |
!isspace( m_BufferNoComment[operatorPos+strlen(op)] ) && |
| 70 |
m_BufferNoComment[operatorPos+strlen(op)] != '(' ) |
| 71 |
{ |
| 72 |
checkError = false; |
| 73 |
} |
| 74 |
|
| 75 |
if( checkError ) |
| 76 |
{ |
| 77 |
// Check number of space after |
| 78 |
unsigned int aft = 0; |
| 79 |
auto i = static_cast<long int>(operatorPos + strlen(op)); |
| 80 |
while( (i<(long int)m_BufferNoComment.size()) |
| 81 |
&& m_BufferNoComment[i] == ' ' ) |
| 82 |
{ |
| 83 |
aft++; |
| 84 |
i++; |
| 85 |
} |
| 86 |
if( i<(long int)m_BufferNoComment.size() && |
| 87 |
( m_BufferNoComment[i] == '\n' || m_BufferNoComment[i] == '\r' ) ) |
| 88 |
{ |
| 89 |
aft = after + 1; |
| 90 |
} |
| 91 |
|
| 92 |
if( aft != after ) |
| 93 |
{ |
| 94 |
if( !this->IsBetweenQuote(operatorPos,false) ) |
| 95 |
{ |
| 96 |
Error error; |
| 97 |
error.line = this->GetLineNumber(operatorPos,true); |
| 98 |
error.line2 = error.line; |
| 99 |
error.number = IFWHILEFORUNTIL; |
| 100 |
error.description = "Spaces after "; |
| 101 |
error.description += op; |
| 102 |
error.description += " = "; |
| 103 |
constexpr size_t length = 200; |
| 104 |
char* errorVal = new char[length]; |
| 105 |
snprintf(errorVal,length,"%d",aft); |
| 106 |
error.description += errorVal; |
| 107 |
error.description += " v.s "; |
| 108 |
snprintf(errorVal,length,"%d",after); |
| 109 |
error.description += errorVal; |
| 110 |
m_ErrorList.push_back(error); |
| 111 |
delete [] errorVal; |
| 112 |
hasErrors = true; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
prevOperatorPos = static_cast<long int>( operatorPos ) + 1; |
| 117 |
if( prevOperatorPos < static_cast<long int>( m_BufferNoComment.size() ) |
| 118 |
- 1 ) |
| 119 |
{ |
| 120 |
operatorPos = m_BufferNoComment.find(op,prevOperatorPos); |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
operatorPos = std::string::npos; |
| 125 |
} |
| 126 |
} |
| 127 |
return !hasErrors; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
} // end namespace kws |