diff --git a/CheckBufferOverrun.cpp b/CheckBufferOverrun.cpp index 39526d0dd..4b2140e5e 100644 --- a/CheckBufferOverrun.cpp +++ b/CheckBufferOverrun.cpp @@ -16,7 +16,7 @@ static const TOKEN *findfunction(const TOKEN *tok) int indentlevel = 0, parlevel = 0; for (; tok; tok = tok->next) { - setindentlevel( tok, indentlevel ); + setindentlevel( tok, indentlevel, -1 ); if (tok->str[0] == '(') parlevel++; @@ -111,10 +111,9 @@ static void CheckBufferOverrun_DynamicData() int indentlevel = 0; for (const TOKEN *tok = ftok; tok; tok = tok->next) { - if (setindentlevel(tok, indentlevel)) + if (setindentlevel(tok, indentlevel, 0)) { - if (indentlevel <= 0) - break; + break; } @@ -155,8 +154,7 @@ static void CheckBufferOverrun_LocalVariable() int indentlevel = 0; for (const TOKEN *tok = tokens; tok; tok = tok->next) { - setindentlevel( tok, indentlevel ); - if (indentlevel < 0) + if (setindentlevel( tok, indentlevel, -1 )) break; // Declaring array.. @@ -172,8 +170,7 @@ static void CheckBufferOverrun_LocalVariable() int _indentlevel = 0; for (const TOKEN *tok2 = gettok(tok,5); tok2; tok2 = tok2->next) { - setindentlevel(tok2, _indentlevel); - if ( _indentlevel < 0 ) + if ( setindentlevel(tok2, _indentlevel, -1) ) break; // Array index.. diff --git a/CheckHeaders.cpp b/CheckHeaders.cpp index 8ad03ce1e..a93b10195 100644 --- a/CheckHeaders.cpp +++ b/CheckHeaders.cpp @@ -91,12 +91,7 @@ void WarningIncludeHeader() continue; // I'm only interested in stuff that is declared at indentlevel 0 - if (tok1->str[0] == '{') - indentlevel++; - - else if (tok1->str[0] == '}') - indentlevel--; - + setindentlevel( tok1, indentlevel, -1 ); if (indentlevel != 0) continue; diff --git a/CheckOther.cpp b/CheckOther.cpp index 198a5b836..45ad8bb74 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -305,17 +305,10 @@ static const TOKEN *GetFunction( const TOKEN *content ) int indentlevel = 0; for (const TOKEN *tok = tokens; tok; tok = tok->next) { - if ( tok->str[0] == '{' ) - indentlevel++; + if (setindentlevel(tok, indentlevel, 0)) + func = NULL; - else if ( tok->str[0] == '}' ) - { - indentlevel--; - if (indentlevel == 0) - func = NULL; - } - - else if (indentlevel == 0) + if (indentlevel == 0) { if (tok->str[0] == ';') func = NULL; @@ -366,34 +359,27 @@ void WarningStrTok() int indentlevel = 0; for ( const TOKEN *tok = *it1; tok; tok = tok->next ) { - if ( tok->str[0] == '{' ) - indentlevel++; + if (setindentlevel(tok, indentlevel, 0)) + break; - else if ( tok->str[0] == '}' ) - { - if ( indentlevel <= 1 ) - break; - indentlevel--; - } + if ( indentlevel == 0 ) + continue; - else if ( indentlevel >= 1 ) + // Only interested in function calls.. + if (! match(tok, "var (")) + continue; + + // Check if function name is in funclist.. + std::list::const_iterator it2; + for (it2 = funclist.begin(); it2 != funclist.end(); it2++) { - // Only interested in function calls.. - if (!(IsName(tok->str) && strcmp(getstr(tok,1), "(") == 0)) + if ( strcmp( tok->str, (*it2)->str ) ) continue; - // Check if function name is in funclist.. - std::list::const_iterator it2; - for (it2 = funclist.begin(); it2 != funclist.end(); it2++) - { - if ( strcmp( tok->str, (*it2)->str ) ) - continue; - - std::ostringstream ostr; - ostr << FileLine(tok) << ": Possible bug. Both '" << (*it1)->str << "' and '" << (*it2)->str << "' uses strtok."; - ReportErr(ostr.str()); - break; - } + std::ostringstream ostr; + ostr << FileLine(tok) << ": Possible bug. Both '" << (*it1)->str << "' and '" << (*it2)->str << "' uses strtok."; + ReportErr(ostr.str()); + break; } } } @@ -436,18 +422,13 @@ void CheckCaseWithoutBreak() int indentlevel = 0; for (const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next) { - if (tok2->str[0] == '{') - indentlevel++; - else if (tok2->str[0] == '}') + if ( setindentlevel( tok2, indentlevel, -1 ) ) { - indentlevel--; - if (indentlevel < 0) - { - std::ostringstream ostr; - ostr << FileLine(tok) << ": 'case' without 'break'."; - ReportErr(ostr.str()); - } + std::ostringstream ostr; + ostr << FileLine(tok) << ": 'case' without 'break'."; + ReportErr(ostr.str()); } + if (indentlevel==0) { if (strcmp(tok2->str,"break")==0) @@ -464,7 +445,6 @@ void CheckCaseWithoutBreak() } } } - } //--------------------------------------------------------------------------- @@ -561,18 +541,10 @@ void CheckVariableScope() tok = tok2; for (tok = tok2; tok; tok = tok->next) { - if ( tok->str[0] == '{' ) + if ( setindentlevel( tok, _indentlevel, 0 ) ) { - _indentlevel++; - } - if ( tok->str[0] == '}' ) - { - _indentlevel--; - if ( _indentlevel <= 0 ) - { - tok = tok->next; - break; - } + tok = tok->next; + break; } } break; @@ -586,16 +558,11 @@ void CheckVariableScope() break; } - if ( tok->str[0] == '{' ) + if ( setindentlevel( tok, indentlevel, 0 ) ) { - indentlevel++; - } - if ( tok->str[0] == '}' ) - { - indentlevel--; - if ( indentlevel == 0 ) - func = false; + func = false; } + if ( indentlevel == 0 && match(tok, ") {") ) { func = true; @@ -632,25 +599,18 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[ // Check if the variable is used in this indentlevel.. bool used = false, used1 = false; - int indentlevel = 0; bool for_or_while = false; - while ( indentlevel >= 0 && tok ) + int indentlevel = 0; + for (; tok; tok = tok->next ) { - if ( tok->str[0] == '{' ) + if ( setindentlevel( tok, indentlevel, 0 ) ) { - indentlevel++; - } - - else if ( tok->str[0] == '}' ) - { - indentlevel--; - if ( indentlevel == 0 ) - { - if ( for_or_while && used ) - return; - used1 = used; - used = false; - } + if ( for_or_while && used ) + return; + used1 = used; + used = false; + if ( indentlevel < 0 ) + break; } else if ( strcmp(tok->str, varname) == 0 ) @@ -667,8 +627,6 @@ static void CheckVariableScope_LookupVar( const TOKEN *tok1, const char varname[ if ( tok->str[0] == ';' ) for_or_while = false; } - - tok = tok->next; } // Warning if "used" is true diff --git a/CommonCheck.cpp b/CommonCheck.cpp index 2c5f287f4..7db30cd9d 100644 --- a/CommonCheck.cpp +++ b/CommonCheck.cpp @@ -58,7 +58,7 @@ bool IsStandardType(const char str[]) } //--------------------------------------------------------------------------- -bool setindentlevel( const TOKEN *tok, int &indentlevel ) +bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel ) { if ( tok->str[0] == '{' ) indentlevel++; @@ -66,6 +66,6 @@ bool setindentlevel( const TOKEN *tok, int &indentlevel ) else if ( tok->str[0] == '}' ) indentlevel--; - return bool(tok->str[0] == '}'); + return bool(tok->str[0]=='}' && indentlevel<=endlevel); } //--------------------------------------------------------------------------- diff --git a/CommonCheck.h b/CommonCheck.h index ba34239b3..5c2dbc1bc 100644 --- a/CommonCheck.h +++ b/CommonCheck.h @@ -34,7 +34,7 @@ bool IsStandardType(const char str[]); // Iterating through tokens.. //--------------------------------------------------------------------------- -bool setindentlevel( const TOKEN *tok, int &indentlevel ); +bool setindentlevel( const TOKEN *tok, int &indentlevel, int endlevel ); //--------------------------------------------------------------------------- diff --git a/tests.cpp b/tests.cpp index 801efc313..dcfee85e8 100644 --- a/tests.cpp +++ b/tests.cpp @@ -494,6 +494,16 @@ static void constructors() "}\n"; check( CheckConstructors, __LINE__, test4, "[test.cpp:8] Uninitialized member variable 'Fred::i'\n" ); + + + const char test5[] = "class Fred\n" + "{\n" + "public:\n" + " unsigned int i;\n" + "};\n"; + check( CheckConstructors, __LINE__, test5, "[test.cpp:1] The class 'Fred' has no constructor\n" ); + + } //---------------------------------------------------------------------------