Incomplete statement: Check for statements that begin with a constant

This commit is contained in:
Daniel Marjamäki 2008-09-20 17:34:37 +00:00
parent c77b81fddd
commit 815dd364ed
4 changed files with 53 additions and 0 deletions

View File

@ -658,3 +658,46 @@ void CheckCharVariable()
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Incomplete statement..
//---------------------------------------------------------------------------
void CheckIncompleteStatement()
{
int parlevel = 0;
for ( const TOKEN *tok = tokens; tok; tok = tok->next )
{
if ( Match(tok,"; %str%") && !Match(gettok(tok,2), ",") )
{
std::ostringstream errmsg;
errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that begins with string constant";
ReportErr(errmsg.str());
}
if ( Match(tok,"; %num%") && !Match(gettok(tok,2), ",") )
{
std::ostringstream errmsg;
errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that begins with numeric constant";
ReportErr(errmsg.str());
}
/*
if ( tok->str[0] == '(' )
++parlevel;
else if ( tok->str[0] == ')' )
--parlevel;
if ( parlevel == 0 && Match(tok, "[;{}] %var% ;") )
{
std::ostringstream errmsg;
errmsg << FileLine(tok->next) << ": Redundant code: Found a statement that only contains a variable";
ReportErr(errmsg.str());
}
*/
}
}

View File

@ -43,6 +43,8 @@ void CheckStructMemberUsage();
// Using char variable as array index / as operand in bit operation
void CheckCharVariable();
// Incomplete statement. A statement that only contains a constant or variable
void CheckIncompleteStatement();
//---------------------------------------------------------------------------
#endif

View File

@ -343,6 +343,10 @@ static void CppCheck(const char FileName[], unsigned int FileId)
// Unused struct members..
CheckStructMemberUsage();
// Check for various types of incomplete statements that could for example
// mean that an ';' has been added by accident
CheckIncompleteStatement();
}
// Clean up tokens..

View File

@ -305,6 +305,10 @@ void TokenizeCode(std::istream &code, const unsigned int FileIndex)
char *pToken = CurrentToken;
for (char ch = (char)code.get(); !code.eof(); ch = (char)code.get())
{
// Todo
if ( ch < 0 )
continue;
// Preprocessor stuff?
if (ch == '#' && !CurrentToken[0])
{