| 1 |
/*========================================================================= |
| 2 |
|
| 3 |
Program: KWStyle - Kitware Style Checker |
| 4 |
Module: kwsCheckStatementPerLine.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 <limits> // std::numeric_limits |
| 17 |
|
| 18 |
namespace kws { |
| 19 |
|
| 20 |
|
| 21 |
/** Check the number of statements per line */ |
| 22 |
bool Parser::CheckStatementPerLine(unsigned long max,bool checkInlineFunctions) |
| 23 |
{ |
| 24 |
this->CheckLineLength(std::numeric_limits<unsigned long>::max(),false,false); // run CheckLineLength without errors to fill m_Positions |
| 25 |
m_TestsDone[STATEMENTPERLINE] = true; |
| 26 |
constexpr size_t length = 255; |
| 27 |
char* val = new char[length]; |
| 28 |
snprintf(val,length,"Statements per line = %ld max",max); |
| 29 |
m_TestsDescription[STATEMENTPERLINE] = val; |
| 30 |
delete [] val; |
| 31 |
|
| 32 |
if(!checkInlineFunctions) |
| 33 |
{ |
| 34 |
m_TestsDescription[STATEMENTPERLINE] += " (not checking inline functions)"; |
| 35 |
} |
| 36 |
|
| 37 |
bool hasError = false; |
| 38 |
size_t posSemicolon = m_BufferNoComment.find(";",0); |
| 39 |
long int currentLine = 0; |
| 40 |
unsigned long statements = 0; |
| 41 |
bool newline = false; |
| 42 |
std::string line = ""; |
| 43 |
|
| 44 |
while(posSemicolon != std::string::npos) |
| 45 |
{ |
| 46 |
// If we are on the same line |
| 47 |
long int lineNumber = this->GetLineNumber(posSemicolon,true); |
| 48 |
|
| 49 |
if(lineNumber != currentLine) |
| 50 |
{ |
| 51 |
currentLine = lineNumber; |
| 52 |
statements = 0; |
| 53 |
} |
| 54 |
|
| 55 |
line = this->GetLine(lineNumber-1); |
| 56 |
|
| 57 |
// We need more than the current line to determine if we are between parenthesis |
| 58 |
// This is arbitrary but should work in most cases. Maybe the best will be to find the |
| 59 |
// previous semicolon but it might take a long time |
| 60 |
long int nb = static_cast<long int>(posSemicolon)-100; // 100 characters befor |
| 61 |
if(nb < 0) |
| 62 |
{ |
| 63 |
nb = 0; |
| 64 |
} |
| 65 |
|
| 66 |
size_t ne = posSemicolon+100; // 100 characters after |
| 67 |
if(ne > m_BufferNoComment.size()) |
| 68 |
{ |
| 69 |
ne = m_BufferNoComment.size(); |
| 70 |
} |
| 71 |
|
| 72 |
size_t posInLine2 = posSemicolon-nb; |
| 73 |
|
| 74 |
std::string line2 = m_BufferNoComment.substr(nb,ne-nb); |
| 75 |
|
| 76 |
if((!this->IsBetweenChars('(',')',posInLine2,false,line2)) |
| 77 |
&& (!this->IsBetweenQuote(posInLine2,false,line2)) |
| 78 |
&& line.find("case:") == std::string::npos // we allow switch/case to be in one line |
| 79 |
) |
| 80 |
{ |
| 81 |
statements++; |
| 82 |
} |
| 83 |
|
| 84 |
size_t posSemicolon2 = m_BufferNoComment.find(";",posSemicolon+1); |
| 85 |
if(this->GetLineNumber(posSemicolon2,true) == currentLine) |
| 86 |
{ |
| 87 |
newline = false; |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
newline = true; |
| 92 |
} |
| 93 |
|
| 94 |
bool reportError=true; |
| 95 |
|
| 96 |
if(!checkInlineFunctions) |
| 97 |
{ |
| 98 |
if(this->IsInFunction(posSemicolon,m_BufferNoComment.c_str()) |
| 99 |
&& this->IsInClass(posSemicolon) |
| 100 |
) |
| 101 |
{ |
| 102 |
reportError = false; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if(reportError && statements > max && (newline || posSemicolon2==std::string::npos)) |
| 107 |
{ |
| 108 |
Error error; |
| 109 |
error.line = this->GetLineNumber(posSemicolon,true); |
| 110 |
error.line2 = error.line; |
| 111 |
error.number = STATEMENTPERLINE; |
| 112 |
error.description = "Number of statements per line exceed: "; |
| 113 |
constexpr size_t length = 10; |
| 114 |
char* localval = new char[length]; |
| 115 |
snprintf(localval,length,"%ld",statements); |
| 116 |
error.description += localval; |
| 117 |
error.description += " (max="; |
| 118 |
delete [] localval; |
| 119 |
localval = new char[length]; |
| 120 |
snprintf(localval,length,"%ld",max); |
| 121 |
error.description += localval; |
| 122 |
error.description += ")"; |
| 123 |
delete [] localval; |
| 124 |
m_ErrorList.push_back(error); |
| 125 |
hasError = true; |
| 126 |
} |
| 127 |
|
| 128 |
posSemicolon = posSemicolon2; |
| 129 |
} |
| 130 |
|
| 131 |
return !hasError; |
| 132 |
} |
| 133 |
|
| 134 |
} // end namespace kws |