Formatting: uniformize end of lines (part 2)

This commit is contained in:
Nicolas Le Cam 2008-12-08 23:10:13 +00:00
parent 625b77ec33
commit e8d2c7cfb3
29 changed files with 338 additions and 338 deletions

View File

@ -217,7 +217,7 @@ void CheckBufferOverrunClass::CheckBufferOverrun_CheckScope( const TOKEN *tok, c
// Don't make recursive checking.. // Don't make recursive checking..
if (std::find(_callStack.begin(), _callStack.end(), tok) != _callStack.end()) if (std::find(_callStack.begin(), _callStack.end(), tok) != _callStack.end())
continue; continue;
// Only perform this checking if showAll setting is enabled.. // Only perform this checking if showAll setting is enabled..
if ( ! _settings._showAll ) if ( ! _settings._showAll )
continue; continue;

View File

@ -113,16 +113,16 @@ const TOKEN * CheckClass::FindClassFunction( const TOKEN *tok, const char classn
{ {
if ( indentlevel < 0 || tok == NULL ) if ( indentlevel < 0 || tok == NULL )
return NULL; return NULL;
std::ostringstream classPattern; std::ostringstream classPattern;
classPattern << "class " << classname << " :|{"; classPattern << "class " << classname << " :|{";
std::ostringstream internalPattern; std::ostringstream internalPattern;
internalPattern << funcname << " ("; internalPattern << funcname << " (";
std::ostringstream externalPattern; std::ostringstream externalPattern;
externalPattern << classname << " :: " << funcname << " ("; externalPattern << classname << " :: " << funcname << " (";
for ( ;tok; tok = tok->next() ) for ( ;tok; tok = tok->next() )
{ {
if ( indentlevel == 0 && TOKEN::Match(tok, classPattern.str().c_str()) ) if ( indentlevel == 0 && TOKEN::Match(tok, classPattern.str().c_str()) )
@ -406,7 +406,7 @@ void CheckClass::CheckConstructors()
// Check constructors // Check constructors
CheckConstructors( tok1, varlist, className[0] ); CheckConstructors( tok1, varlist, className[0] );
// Check assignment operators // Check assignment operators
CheckConstructors( tok1, varlist, "operator =" ); CheckConstructors( tok1, varlist, "operator =" );
@ -425,7 +425,7 @@ void CheckClass::CheckConstructors()
void CheckClass::CheckConstructors(const TOKEN *tok1, struct VAR *varlist, const char funcname[]) void CheckClass::CheckConstructors(const TOKEN *tok1, struct VAR *varlist, const char funcname[])
{ {
const char * const className = tok1->strAt(1); const char * const className = tok1->strAt(1);
int indentlevel = 0; int indentlevel = 0;
const TOKEN *constructor_token = FindClassFunction( tok1, className, funcname, indentlevel ); const TOKEN *constructor_token = FindClassFunction( tok1, className, funcname, indentlevel );
std::list<std::string> callstack; std::list<std::string> callstack;
@ -437,23 +437,23 @@ void CheckClass::CheckConstructors(const TOKEN *tok1, struct VAR *varlist, const
{ {
if ( var->init ) if ( var->init )
continue; continue;
// Is it a static member variable? // Is it a static member variable?
std::ostringstream pattern; std::ostringstream pattern;
pattern << className << "::" << var->name << "="; pattern << className << "::" << var->name << "=";
if (TOKEN::findmatch(_tokenizer->tokens(), pattern.str().c_str())) if (TOKEN::findmatch(_tokenizer->tokens(), pattern.str().c_str()))
continue; continue;
// It's non-static and it's not initialized => error // It's non-static and it's not initialized => error
std::ostringstream ostr; std::ostringstream ostr;
ostr << _tokenizer->fileLine(constructor_token); ostr << _tokenizer->fileLine(constructor_token);
ostr << " Uninitialized member variable '" << className << "::" << var->name << "'"; ostr << " Uninitialized member variable '" << className << "::" << var->name << "'";
_errorLogger->reportErr(ostr.str()); _errorLogger->reportErr(ostr.str());
} }
for ( struct VAR *var = varlist; var; var = var->next ) for ( struct VAR *var = varlist; var; var = var->next )
var->init = false; var->init = false;
constructor_token = FindClassFunction( constructor_token->next(), className, funcname, indentlevel ); constructor_token = FindClassFunction( constructor_token->next(), className, funcname, indentlevel );
callstack.clear(); callstack.clear();
ClassChecking_VarList_Initialize(tok1, constructor_token, varlist, className, callstack); ClassChecking_VarList_Initialize(tok1, constructor_token, varlist, className, callstack);
@ -694,7 +694,7 @@ void CheckClass::virtualDestructor()
while ( TOKEN::Match(derived, "%var%") ) while ( TOKEN::Match(derived, "%var%") )
{ {
bool isPublic = TOKEN::Match(derived, "public"); bool isPublic = TOKEN::Match(derived, "public");
// What kind of inheritance is it.. public|protected|private // What kind of inheritance is it.. public|protected|private
if ( TOKEN::Match( derived, "public|protected|private" ) ) if ( TOKEN::Match( derived, "public|protected|private" ) )
derived = derived->next(); derived = derived->next();
@ -703,12 +703,12 @@ void CheckClass::virtualDestructor()
const char *baseName[2]; const char *baseName[2];
baseName[0] = derived->strAt(0); baseName[0] = derived->strAt(0);
baseName[1] = 0; baseName[1] = 0;
// Update derived so it's ready for the next loop. // Update derived so it's ready for the next loop.
derived = derived->next(); derived = derived->next();
if ( TOKEN::Match(derived, ",") ) if ( TOKEN::Match(derived, ",") )
derived = derived->next(); derived = derived->next();
// If not public inheritance, skip checking of this base class.. // If not public inheritance, skip checking of this base class..
if ( ! isPublic ) if ( ! isPublic )
continue; continue;

View File

@ -49,7 +49,7 @@ private:
bool init; bool init;
struct VAR *next; struct VAR *next;
}; };
void ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list<std::string> &callstack); void ClassChecking_VarList_Initialize(const TOKEN *tok1, const TOKEN *ftok, struct VAR *varlist, const char classname[], std::list<std::string> &callstack);
void InitVar(struct VAR *varlist, const char varname[]); void InitVar(struct VAR *varlist, const char varname[]);
const TOKEN *FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel ); const TOKEN *FindClassFunction( const TOKEN *tok, const char classname[], const char funcname[], int &indentlevel );

View File

@ -136,7 +136,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType( const T
} }
CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetReallocationType( const TOKEN *tok2 ) CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetReallocationType( const TOKEN *tok2 )
{ {
// What we may have... // What we may have...
@ -149,17 +149,17 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetReallocationType( const
} }
if ( ! tok2 ) if ( ! tok2 )
return No; return No;
if ( TOKEN::Match(tok2, "realloc") ) if ( TOKEN::Match(tok2, "realloc") )
return Malloc; return Malloc;
// GTK memory reallocation.. // GTK memory reallocation..
if ( TOKEN::Match(tok2, "g_realloc|g_try_realloc|g_renew|g_try_renew") ) if ( TOKEN::Match(tok2, "g_realloc|g_try_realloc|g_renew|g_try_renew") )
return gMalloc; return gMalloc;
return No; return No;
} }
CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const char *varnames[] ) CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const TOKEN *tok, const char *varnames[] )
{ {
@ -243,7 +243,7 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
{ {
std::cout << t->str() << "\n"; std::cout << t->str() << "\n";
}*/ }*/
const char *ret = 0; const char *ret = 0;
if (TOKEN::findmatch(func_, "goto")) if (TOKEN::findmatch(func_, "goto"))
{ {
@ -257,7 +257,7 @@ const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const
ret = "dealloc"; ret = "dealloc";
else if ( TOKEN::findmatch(func_, "use") ) else if ( TOKEN::findmatch(func_, "use") )
ret = "use"; ret = "use";
Tokenizer::deleteTokens(func); Tokenizer::deleteTokens(func);
return ret; return ret;
} }
@ -326,10 +326,10 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
rettail=newtok; \ rettail=newtok; \
} }
// The first token should be ";" // The first token should be ";"
addtoken(";"); addtoken(";");
bool isloop = false; bool isloop = false;
@ -362,7 +362,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
if (TOKEN::Match(tok, "[(;{}] %var1% =", varnames)) if (TOKEN::Match(tok, "[(;{}] %var1% =", varnames))
{ {
AllocType alloc = GetAllocationType(tok->tokAt(3)); AllocType alloc = GetAllocationType(tok->tokAt(3));
if ( alloc == No ) if ( alloc == No )
{ {
alloc = GetReallocationType( tok->tokAt(3) ); alloc = GetReallocationType( tok->tokAt(3) );
@ -392,7 +392,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
MismatchError(tok, callstack, varname); MismatchError(tok, callstack, varname);
alloctype = alloc; alloctype = alloc;
} }
// assignment.. // assignment..
else else
{ {
@ -417,7 +417,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
TOKEN::Match(tok, "if ( 0 != %var1% )", varnames) ) TOKEN::Match(tok, "if ( 0 != %var1% )", varnames) )
{ {
addtoken("if(var)"); addtoken("if(var)");
// Make sure the "use" will not be added // Make sure the "use" will not be added
while ( tok->str() != ")" ) while ( tok->str() != ")" )
tok = tok->next(); tok = tok->next();
@ -526,7 +526,7 @@ TOKEN *CheckMemoryLeakClass::getcode(const TOKEN *tok, std::list<const TOKEN *>
if ( str ) if ( str )
addtoken( str ); addtoken( str );
} }
// Callback.. // Callback..
if ( TOKEN::Match(tok, "( * %var% ) (") ) if ( TOKEN::Match(tok, "( * %var% ) (") )
{ {
@ -621,7 +621,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase( tok2, tok2->tokAt(4) ); erase( tok2, tok2->tokAt(4) );
done = false; done = false;
} }
// TODO Make this more generic. Delete "if ; else use ; use" // TODO Make this more generic. Delete "if ; else use ; use"
if ( TOKEN::Match(tok2, "; if ; else use ; use") || if ( TOKEN::Match(tok2, "; if ; else use ; use") ||
TOKEN::Match(tok2, "; if use ; else ; use") ) TOKEN::Match(tok2, "; if use ; else ; use") )
@ -629,7 +629,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase( tok2, tok2->tokAt(4) ); erase( tok2, tok2->tokAt(4) );
done = false; done = false;
} }
// Reduce "if dealloc ;" and "if use ;" that is not followed by an else.. // Reduce "if dealloc ;" and "if use ;" that is not followed by an else..
// If "--all" has been given these are deleted // If "--all" has been given these are deleted
// Otherwise, ony the "if" will be deleted // Otherwise, ony the "if" will be deleted
@ -657,14 +657,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase(tok2, tok2->tokAt(2)); erase(tok2, tok2->tokAt(2));
done = false; done = false;
} }
// Reduce "else ;" => ";" // Reduce "else ;" => ";"
if ( TOKEN::Match(tok2->next(), "else ;") ) if ( TOKEN::Match(tok2->next(), "else ;") )
{ {
erase(tok2, tok2->tokAt(2)); erase(tok2, tok2->tokAt(2));
done = false; done = false;
} }
// Delete if block: "alloc; if return use ;" // Delete if block: "alloc; if return use ;"
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else")) if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else"))
{ {
@ -706,7 +706,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase(tok2,tok2->tokAt(8)); erase(tok2,tok2->tokAt(8));
done = false; done = false;
} }
// Reduce "if ; else %var% ;" => "if %var% ;" // Reduce "if ; else %var% ;" => "if %var% ;"
if ( TOKEN::Match(tok2, "if ; else %var% ;") ) if ( TOKEN::Match(tok2, "if ; else %var% ;") )
{ {
@ -738,14 +738,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase( tok2, tok2->tokAt(4) ); erase( tok2, tok2->tokAt(4) );
done = false; done = false;
} }
// Reduce "if(true) X ;" => "X ;" // Reduce "if(true) X ;" => "X ;"
if (TOKEN::Match(tok2->next(), "if(true) %var% ;") && !TOKEN::Match(tok2->tokAt(4),"else")) if (TOKEN::Match(tok2->next(), "if(true) %var% ;") && !TOKEN::Match(tok2->tokAt(4),"else"))
{ {
erase( tok2, tok2->tokAt(2) ); erase( tok2, tok2->tokAt(2) );
done = false; done = false;
} }
// Replace "loop { X ; break ; }" with "X ;" // Replace "loop { X ; break ; }" with "X ;"
if ( TOKEN::Match(tok2->next(), "loop { %var% ; break ; }") ) if ( TOKEN::Match(tok2->next(), "loop { %var% ; break ; }") )
{ {
@ -781,7 +781,7 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase(tok2, tok2->tokAt(4)); erase(tok2, tok2->tokAt(4));
done = false; done = false;
} }
// Delete if block: "alloc; if return use ;" // Delete if block: "alloc; if return use ;"
if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else")) if (TOKEN::Match(tok2,"alloc ; if return use ;") && !TOKEN::Match(tok2->tokAt(6),"else"))
{ {
@ -795,21 +795,21 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase( tok2, tok2->tokAt(4) ); erase( tok2, tok2->tokAt(4) );
done = false; done = false;
} }
// Reduce "[;{}] return ; %var%" => "[;{}] return ;" // Reduce "[;{}] return ; %var%" => "[;{}] return ;"
if ( TOKEN::Match(tok2, "[;{}] return ; %var%") ) if ( TOKEN::Match(tok2, "[;{}] return ; %var%") )
{ {
erase( tok2->next()->next(), tok2->tokAt(4) ); erase( tok2->next()->next(), tok2->tokAt(4) );
done = false; done = false;
} }
// Reduce "[;{}] return use ; %var%" => "[;{}] return use ;" // Reduce "[;{}] return use ; %var%" => "[;{}] return use ;"
if ( TOKEN::Match(tok2, "[;{}] return use ; %var%") ) if ( TOKEN::Match(tok2, "[;{}] return use ; %var%") )
{ {
erase( tok2->next()->next()->next(), tok2->tokAt(5) ); erase( tok2->next()->next()->next(), tok2->tokAt(5) );
done = false; done = false;
} }
// Reduce "if(var) return use ;" => "return use ;" // Reduce "if(var) return use ;" => "return use ;"
if ( TOKEN::Match(tok2->next(), "if(var) return use ;") && !TOKEN::Match(tok2->tokAt(5),"else")) if ( TOKEN::Match(tok2->next(), "if(var) return use ;") && !TOKEN::Match(tok2->tokAt(5),"else"))
{ {
@ -823,14 +823,14 @@ void CheckMemoryLeakClass::simplifycode(TOKEN *tok)
erase( tok2, tok2->tokAt(2) ); erase( tok2, tok2->tokAt(2) );
done = false; done = false;
} }
// Reduce "[;{}] alloc ; dealloc ;" => "[;{}]" // Reduce "[;{}] alloc ; dealloc ;" => "[;{}]"
if ( TOKEN::Match(tok2, "[;{}] alloc ; dealloc ;") ) if ( TOKEN::Match(tok2, "[;{}] alloc ; dealloc ;") )
{ {
erase( tok2, tok2->tokAt(5) ); erase( tok2, tok2->tokAt(5) );
done = false; done = false;
} }
// Reduce "if* alloc ; dealloc ;" => ";" // Reduce "if* alloc ; dealloc ;" => ";"
if ( TOKEN::Match(tok2->tokAt(2), "alloc ; dealloc ;") && if ( TOKEN::Match(tok2->tokAt(2), "alloc ; dealloc ;") &&
tok2->next()->str().find("if") == 0 ) tok2->next()->str().find("if") == 0 )
@ -1011,7 +1011,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
TOKEN *first = tok; TOKEN *first = tok;
while ( first && first->str() == ";" ) while ( first && first->str() == ";" )
first = first->next(); first = first->next();
bool noerr = false; bool noerr = false;
noerr |= TOKEN::Match( first, "alloc ; }" ); noerr |= TOKEN::Match( first, "alloc ; }" );
noerr |= TOKEN::Match( first, "alloc ; dealloc ; }" ); noerr |= TOKEN::Match( first, "alloc ; dealloc ; }" );
@ -1023,7 +1023,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
noerr |= TOKEN::Match( first, "if alloc ; use ; }" ); noerr |= TOKEN::Match( first, "if alloc ; use ; }" );
noerr |= TOKEN::Match( first, "alloc ; ifv return ; dealloc ; }" ); noerr |= TOKEN::Match( first, "alloc ; ifv return ; dealloc ; }" );
noerr |= TOKEN::Match( first, "alloc ; if return ; dealloc; }" ); noerr |= TOKEN::Match( first, "alloc ; if return ; dealloc; }" );
// Unhandled case.. // Unhandled case..
if ( ! noerr ) if ( ! noerr )
{ {
@ -1033,7 +1033,7 @@ void CheckMemoryLeakClass::CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const
std::cout << "\n"; std::cout << "\n";
} }
} }
Tokenizer::deleteTokens(tok); Tokenizer::deleteTokens(tok);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -67,7 +67,7 @@ private:
* @param tok first token. The tokens list can be modified. * @param tok first token. The tokens list can be modified.
*/ */
void simplifycode(TOKEN *tok); void simplifycode(TOKEN *tok);
/** /**
* Delete tokens between begin and end. E.g. if begin = 1 * Delete tokens between begin and end. E.g. if begin = 1
* and end = 5, tokens 2,3 and 4 would be erased. * and end = 5, tokens 2,3 and 4 would be erased.

View File

@ -785,16 +785,16 @@ void CheckOther::CheckIncompleteStatement()
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Unreachable code below a 'return' // Unreachable code below a 'return'
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CheckOther::unreachableCode() void CheckOther::unreachableCode()
{ {
const TOKEN *tok = TOKEN::findmatch( _tokenizer->tokens(), "[;{}] return" ); const TOKEN *tok = TOKEN::findmatch( _tokenizer->tokens(), "[;{}] return" );
@ -802,13 +802,13 @@ void CheckOther::unreachableCode()
{ {
// Goto the 'return' token // Goto the 'return' token
tok = tok->next(); tok = tok->next();
// Locate the end of the 'return' statement // Locate the end of the 'return' statement
while ( tok && ! TOKEN::Match(tok, ";") ) while ( tok && ! TOKEN::Match(tok, ";") )
tok = tok->next(); tok = tok->next();
while ( tok && TOKEN::Match(tok->next(), ";") ) while ( tok && TOKEN::Match(tok->next(), ";") )
tok = tok->next(); tok = tok->next();
// If there is a statement below the return it is unreachable // If there is a statement below the return it is unreachable
if (!TOKEN::Match(tok, "; case|default|}|#") && !TOKEN::Match(tok, "; %var% :")) if (!TOKEN::Match(tok, "; case|default|}|#") && !TOKEN::Match(tok, "; %var% :"))
{ {
@ -816,22 +816,22 @@ void CheckOther::unreachableCode()
errmsg << _tokenizer->fileLine(tok->next()) << ": Unreachable code below a 'return'"; errmsg << _tokenizer->fileLine(tok->next()) << ": Unreachable code below a 'return'";
_errorLogger->reportErr(errmsg.str()); _errorLogger->reportErr(errmsg.str());
} }
// Find the next 'return' statement // Find the next 'return' statement
tok = TOKEN::findmatch( tok, "[;{}] return" ); tok = TOKEN::findmatch( tok, "[;{}] return" );
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Usage of function variables // Usage of function variables
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
static bool isOp(const TOKEN *tok) static bool isOp(const TOKEN *tok)
{ {
return bool(tok && return bool(tok &&
@ -846,7 +846,7 @@ static bool isOp(const TOKEN *tok)
tok->str() == "<<" || tok->str() == "<<" ||
TOKEN::Match(tok, "[+-*/&|,]"))); TOKEN::Match(tok, "[+-*/&|,]")));
} }
void CheckOther::functionVariableUsage() void CheckOther::functionVariableUsage()
{ {
// Parse all executing scopes.. // Parse all executing scopes..
@ -858,7 +858,7 @@ void CheckOther::functionVariableUsage()
static const unsigned int USAGE_DECLARE = 1; static const unsigned int USAGE_DECLARE = 1;
static const unsigned int USAGE_READ = 2; static const unsigned int USAGE_READ = 2;
static const unsigned int USAGE_WRITE = 4; static const unsigned int USAGE_WRITE = 4;
int indentlevel = 0; int indentlevel = 0;
for ( const TOKEN *tok = tok1; tok; tok = tok->next() ) for ( const TOKEN *tok = tok1; tok; tok = tok->next() )
{ {
@ -870,61 +870,61 @@ void CheckOther::functionVariableUsage()
if ( indentlevel <= 0 ) if ( indentlevel <= 0 )
break; break;
} }
if ( TOKEN::Match(tok, "[;{}] bool|char|short|int|long|float|double %var% ;|=") ) if ( TOKEN::Match(tok, "[;{}] bool|char|short|int|long|float|double %var% ;|=") )
varUsage[ tok->strAt(2) ] = USAGE_DECLARE; varUsage[ tok->strAt(2) ] = USAGE_DECLARE;
if ( TOKEN::Match(tok, "[;{}] bool|char|short|int|long|float|double * %var% ;|=") ) if ( TOKEN::Match(tok, "[;{}] bool|char|short|int|long|float|double * %var% ;|=") )
varUsage[ tok->strAt(3) ] = USAGE_DECLARE; varUsage[ tok->strAt(3) ] = USAGE_DECLARE;
if ( TOKEN::Match(tok, "delete|return %var%") ) if ( TOKEN::Match(tok, "delete|return %var%") )
varUsage[ tok->strAt(1) ] |= USAGE_READ; varUsage[ tok->strAt(1) ] |= USAGE_READ;
if ( TOKEN::Match(tok, "%var% =") ) if ( TOKEN::Match(tok, "%var% =") )
varUsage[ tok->str() ] |= USAGE_WRITE; varUsage[ tok->str() ] |= USAGE_WRITE;
if ( TOKEN::Match(tok, "else %var% =") ) if ( TOKEN::Match(tok, "else %var% =") )
varUsage[ tok->strAt(1) ] |= USAGE_WRITE; varUsage[ tok->strAt(1) ] |= USAGE_WRITE;
if ( TOKEN::Match(tok, ">>|& %var%") ) if ( TOKEN::Match(tok, ">>|& %var%") )
varUsage[ tok->strAt(1) ] |= USAGE_WRITE; varUsage[ tok->strAt(1) ] |= USAGE_WRITE;
if ((TOKEN::Match(tok,"[(=&!]") || isOp(tok)) && TOKEN::Match(tok->next(), "%var%")) if ((TOKEN::Match(tok,"[(=&!]") || isOp(tok)) && TOKEN::Match(tok->next(), "%var%"))
varUsage[ tok->strAt(1) ] |= USAGE_READ; varUsage[ tok->strAt(1) ] |= USAGE_READ;
if (TOKEN::Match(tok, "%var%") && (tok->next()->str()==")" || isOp(tok->next()))) if (TOKEN::Match(tok, "%var%") && (tok->next()->str()==")" || isOp(tok->next())))
varUsage[ tok->str() ] |= USAGE_READ; varUsage[ tok->str() ] |= USAGE_READ;
if ( TOKEN::Match(tok, "[(,] %var% [,)]") ) if ( TOKEN::Match(tok, "[(,] %var% [,)]") )
varUsage[ tok->strAt(1) ] |= USAGE_WRITE; varUsage[ tok->strAt(1) ] |= USAGE_WRITE;
} }
// Check usage of all variables in the current scope.. // Check usage of all variables in the current scope..
for ( std::map<std::string, unsigned int>::const_iterator it = varUsage.begin(); it != varUsage.end(); ++it ) for ( std::map<std::string, unsigned int>::const_iterator it = varUsage.begin(); it != varUsage.end(); ++it )
{ {
std::string varname = it->first; std::string varname = it->first;
unsigned int usage = it->second; unsigned int usage = it->second;
if (!isalpha(varname[0])) if (!isalpha(varname[0]))
continue; continue;
if ( ! ( usage & USAGE_DECLARE ) ) if ( ! ( usage & USAGE_DECLARE ) )
continue; continue;
if ( usage == USAGE_DECLARE ) if ( usage == USAGE_DECLARE )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok1->next()) << ": Unused variable '" << varname << "'"; errmsg << _tokenizer->fileLine(tok1->next()) << ": Unused variable '" << varname << "'";
_errorLogger->reportErr(errmsg.str()); _errorLogger->reportErr(errmsg.str());
} }
else if ( ! (usage & USAGE_READ) ) else if ( ! (usage & USAGE_READ) )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok1->next()) << ": Variable '" << varname << "' is assigned a value that is never used"; errmsg << _tokenizer->fileLine(tok1->next()) << ": Variable '" << varname << "' is assigned a value that is never used";
_errorLogger->reportErr(errmsg.str()); _errorLogger->reportErr(errmsg.str());
} }
else if ( ! (usage & USAGE_WRITE) ) else if ( ! (usage & USAGE_WRITE) )
{ {
std::ostringstream errmsg; std::ostringstream errmsg;
@ -932,7 +932,7 @@ void CheckOther::functionVariableUsage()
_errorLogger->reportErr(errmsg.str()); _errorLogger->reportErr(errmsg.str());
} }
} }
// Goto next executing scope.. // Goto next executing scope..
tok1 = TOKEN::findmatch( tok1->next(), ") const| {" ); tok1 = TOKEN::findmatch( tok1->next(), ") const| {" );
} }

View File

@ -70,10 +70,10 @@ public:
// Incomplete statement. A statement that only contains a constant or variable // Incomplete statement. A statement that only contains a constant or variable
void CheckIncompleteStatement(); void CheckIncompleteStatement();
/** Unreachable code below a 'return' */ /** Unreachable code below a 'return' */
void unreachableCode(); void unreachableCode();
/** Unused function variables */ /** Unused function variables */
void functionVariableUsage(); void functionVariableUsage();
private: private:

View File

@ -39,14 +39,14 @@ class FileLister
{ {
public: public:
static void RecursiveAddFiles( std::vector<std::string> &filenames, const std::string &path, bool recursive ); static void RecursiveAddFiles( std::vector<std::string> &filenames, const std::string &path, bool recursive );
private: private:
static bool AcceptFile( const std::string &filename ); static bool AcceptFile( const std::string &filename );
#ifdef __BORLANDC__ #ifdef __BORLANDC__
static void AddFiles( std::vector<std::string> &filenames, const std::string &path, const std::string &pattern ); static void AddFiles( std::vector<std::string> &filenames, const std::string &path, const std::string &pattern );
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
static void AddFiles( std::vector<std::string> &filenames, const std::string &path, const std::string &pattern ); static void AddFiles( std::vector<std::string> &filenames, const std::string &path, const std::string &pattern );
#endif #endif

View File

@ -16,10 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#include "cppcheck.h" #include "cppcheck.h"
#include "preprocessor.h" // preprocessor. #include "preprocessor.h" // preprocessor.
#include "tokenize.h" // <- Tokenizer #include "tokenize.h" // <- Tokenizer
#include "CheckMemoryLeak.h" #include "CheckMemoryLeak.h"
#include "CheckBufferOverrun.h" #include "CheckBufferOverrun.h"
#include "CheckClass.h" #include "CheckClass.h"
@ -27,76 +27,76 @@
#include "CheckOther.h" #include "CheckOther.h"
#include "CheckFunctionUsage.h" #include "CheckFunctionUsage.h"
#include "FileLister.h" #include "FileLister.h"
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <map> #include <map>
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
CppCheck::CppCheck( ErrorLogger &errorLogger ) : _checkFunctionUsage( this ) CppCheck::CppCheck( ErrorLogger &errorLogger ) : _checkFunctionUsage( this )
{ {
_errorLogger = &errorLogger; _errorLogger = &errorLogger;
} }
CppCheck::~CppCheck() CppCheck::~CppCheck()
{ {
} }
void CppCheck::settings( const Settings &settings ) void CppCheck::settings( const Settings &settings )
{ {
_settings = settings; _settings = settings;
} }
void CppCheck::addFile( const std::string &path ) void CppCheck::addFile( const std::string &path )
{ {
_filenames.push_back( path ); _filenames.push_back( path );
} }
void CppCheck::addFile( const std::string &path, const std::string &content ) void CppCheck::addFile( const std::string &path, const std::string &content )
{ {
_filenames.push_back( path ); _filenames.push_back( path );
_fileContents[ path ] = content; _fileContents[ path ] = content;
} }
std::string CppCheck::parseFromArgs( int argc, const char* const argv[] ) std::string CppCheck::parseFromArgs( int argc, const char* const argv[] )
{ {
std::vector<std::string> pathnames; std::vector<std::string> pathnames;
bool Recursive = false; bool Recursive = false;
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
// Flag used for various purposes during debugging // Flag used for various purposes during debugging
if (strcmp(argv[i],"--debug") == 0) if (strcmp(argv[i],"--debug") == 0)
_settings._debug = true; _settings._debug = true;
// Show all messages // Show all messages
else if (strcmp(argv[i],"--all") == 0) else if (strcmp(argv[i],"--all") == 0)
_settings._showAll = true; _settings._showAll = true;
// Only print something when there are errors // Only print something when there are errors
else if (strcmp(argv[i],"--errorsonly")==0) else if (strcmp(argv[i],"--errorsonly")==0)
_settings._errorsOnly = true; _settings._errorsOnly = true;
// Checking coding style // Checking coding style
else if (strcmp(argv[i],"--style")==0) else if (strcmp(argv[i],"--style")==0)
_settings._checkCodingStyle = true; _settings._checkCodingStyle = true;
// Recursively check source files // Recursively check source files
else if (strcmp(argv[i],"--recursive")==0) else if (strcmp(argv[i],"--recursive")==0)
Recursive = true; Recursive = true;
// Verbose error messages (configuration info) // Verbose error messages (configuration info)
else if (strcmp(argv[i],"--verbose")==0) else if (strcmp(argv[i],"--verbose")==0)
_settings._verbose = true; _settings._verbose = true;
else else
pathnames.push_back( argv[i] ); pathnames.push_back( argv[i] );
} }
// --recursive was used // --recursive was used
if ( Recursive ) if ( Recursive )
{ {
@ -108,7 +108,7 @@ std::string CppCheck::parseFromArgs( int argc, const char* const argv[] )
else else
{ {
// Handle situation: cppcheck --recursive path1 path2 // Handle situation: cppcheck --recursive path1 path2
// Execute RecursiveAddFiles() to each given file parameter // Execute RecursiveAddFiles() to each given file parameter
std::vector<std::string>::const_iterator iter; std::vector<std::string>::const_iterator iter;
for(iter=pathnames.begin(); iter!=pathnames.end(); iter++) for(iter=pathnames.begin(); iter!=pathnames.end(); iter++)
@ -121,7 +121,7 @@ std::string CppCheck::parseFromArgs( int argc, const char* const argv[] )
for(iter=pathnames.begin(); iter!=pathnames.end(); iter++) for(iter=pathnames.begin(); iter!=pathnames.end(); iter++)
FileLister::RecursiveAddFiles( _filenames, iter->c_str(), false ); FileLister::RecursiveAddFiles( _filenames, iter->c_str(), false );
} }
if (_filenames.empty()) if (_filenames.empty())
{ {
std::ostringstream oss; std::ostringstream oss;
@ -141,15 +141,15 @@ std::string CppCheck::parseFromArgs( int argc, const char* const argv[] )
" --verbose More detailed error reports\n"; " --verbose More detailed error reports\n";
return oss.str(); return oss.str();
} }
// Check function usage if "--style" and "--all" was given. // Check function usage if "--style" and "--all" was given.
// There will be false positives for exported library functions // There will be false positives for exported library functions
if ( _settings._showAll && _settings._checkCodingStyle ) if ( _settings._showAll && _settings._checkCodingStyle )
_settings._checkFunctionUsage = true; _settings._checkFunctionUsage = true;
return ""; return "";
} }
void CppCheck::check() void CppCheck::check()
{ {
std::sort( _filenames.begin(), _filenames.end() ); std::sort( _filenames.begin(), _filenames.end() );
@ -157,11 +157,11 @@ void CppCheck::check()
{ {
_errout.str(""); _errout.str("");
std::string fname = _filenames[c]; std::string fname = _filenames[c];
// If only errors are printed, print filename after the check // If only errors are printed, print filename after the check
if ( _settings._errorsOnly == false ) if ( _settings._errorsOnly == false )
_errorLogger->reportOut( std::string( "Checking " ) + fname + std::string( "..." ) ); _errorLogger->reportOut( std::string( "Checking " ) + fname + std::string( "..." ) );
Preprocessor preprocessor( this ); Preprocessor preprocessor( this );
std::map<std::string, std::string> code; std::map<std::string, std::string> code;
if( _fileContents.size() > 0 && _fileContents.find( _filenames[c] ) != _fileContents.end() ) if( _fileContents.size() > 0 && _fileContents.find( _filenames[c] ) != _fileContents.end() )
@ -176,17 +176,17 @@ void CppCheck::check()
std::ifstream fin( fname.c_str() ); std::ifstream fin( fname.c_str() );
preprocessor.preprocess(fin, code, fname); preprocessor.preprocess(fin, code, fname);
} }
for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it ) for ( std::map<std::string,std::string>::const_iterator it = code.begin(); it != code.end(); ++it )
{ {
cfg = it->first; cfg = it->first;
checkFile(it->second, _filenames[c].c_str()); checkFile(it->second, _filenames[c].c_str());
} }
if ( _settings._errorsOnly == false && _errout.str().empty() ) if ( _settings._errorsOnly == false && _errout.str().empty() )
_errorLogger->reportOut( "No errors found" ); _errorLogger->reportOut( "No errors found" );
} }
// This generates false positives - especially for libraries // This generates false positives - especially for libraries
_settings._verbose = false; _settings._verbose = false;
if ( _settings._checkFunctionUsage ) if ( _settings._checkFunctionUsage )
@ -194,47 +194,47 @@ void CppCheck::check()
_errout.str(""); _errout.str("");
if( _settings._errorsOnly == false ) if( _settings._errorsOnly == false )
_errorLogger->reportOut( "Checking usage of global functions (this may take several minutes).." ); _errorLogger->reportOut( "Checking usage of global functions (this may take several minutes).." );
_checkFunctionUsage.check(); _checkFunctionUsage.check();
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// CppCheck - A function that checks a specified file // CppCheck - A function that checks a specified file
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CppCheck::checkFile(const std::string &code, const char FileName[]) void CppCheck::checkFile(const std::string &code, const char FileName[])
{ {
Tokenizer _tokenizer; Tokenizer _tokenizer;
// Tokenize the file // Tokenize the file
{ {
std::istringstream istr(code); std::istringstream istr(code);
_tokenizer.tokenize(istr, FileName); _tokenizer.tokenize(istr, FileName);
} }
_tokenizer.fillFunctionList(); _tokenizer.fillFunctionList();
// Check that the memsets are valid. // Check that the memsets are valid.
// The 'memset' function can do dangerous things if used wrong. // The 'memset' function can do dangerous things if used wrong.
// Important: The checking doesn't work on simplified tokens list. // Important: The checking doesn't work on simplified tokens list.
CheckClass checkClass( &_tokenizer, _settings, this ); CheckClass checkClass( &_tokenizer, _settings, this );
checkClass.CheckMemset(); checkClass.CheckMemset();
// Check for unsigned divisions where one operand is signed // Check for unsigned divisions where one operand is signed
// Very important to run it before 'SimplifyTokenList' // Very important to run it before 'SimplifyTokenList'
CheckOther checkOther( &_tokenizer, this ); CheckOther checkOther( &_tokenizer, this );
if ( _settings._checkCodingStyle ) if ( _settings._checkCodingStyle )
checkOther.CheckUnsignedDivision(); checkOther.CheckUnsignedDivision();
// Give warning when using char variable as array index // Give warning when using char variable as array index
// Doesn't work on simplified token list ('unsigned') // Doesn't work on simplified token list ('unsigned')
if ( _settings._checkCodingStyle ) if ( _settings._checkCodingStyle )
checkOther.CheckCharVariable(); checkOther.CheckCharVariable();
// Including header which is not needed (too many false positives) // Including header which is not needed (too many false positives)
// if ( _settings._checkCodingStyle ) // if ( _settings._checkCodingStyle )
@ -242,96 +242,96 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// CheckHeaders checkHeaders( &tokenizer ); // CheckHeaders checkHeaders( &tokenizer );
// checkHeaders.WarningIncludeHeader(); // checkHeaders.WarningIncludeHeader();
// } // }
_tokenizer.simplifyTokenList(); _tokenizer.simplifyTokenList();
if ( _settings._checkFunctionUsage ) if ( _settings._checkFunctionUsage )
_checkFunctionUsage.parseTokens(_tokenizer); _checkFunctionUsage.parseTokens(_tokenizer);
// Memory leak // Memory leak
CheckMemoryLeakClass checkMemoryLeak( &_tokenizer, _settings, this ); CheckMemoryLeakClass checkMemoryLeak( &_tokenizer, _settings, this );
checkMemoryLeak.CheckMemoryLeak(); checkMemoryLeak.CheckMemoryLeak();
// Buffer overruns.. // Buffer overruns..
CheckBufferOverrunClass checkBufferOverrun( &_tokenizer, _settings, this ); CheckBufferOverrunClass checkBufferOverrun( &_tokenizer, _settings, this );
checkBufferOverrun.CheckBufferOverrun(); checkBufferOverrun.CheckBufferOverrun();
// Check that all class constructors are ok. // Check that all class constructors are ok.
checkClass.CheckConstructors(); checkClass.CheckConstructors();
// Check that all base classes have virtual destructors // Check that all base classes have virtual destructors
checkClass.virtualDestructor(); checkClass.virtualDestructor();
if (_settings._showAll) if (_settings._showAll)
{ {
// Check for "if (a=b)" // Check for "if (a=b)"
checkOther.CheckIfAssignment(); checkOther.CheckIfAssignment();
// Check for case without break // Check for case without break
// Disabled because it generates many false positives // Disabled because it generates many false positives
// CheckCaseWithoutBreak(); // CheckCaseWithoutBreak();
// Dangerous usage of strtok // Dangerous usage of strtok
// Disabled because it generates false positives // Disabled because it generates false positives
//WarningStrTok(); //WarningStrTok();
} }
// Dangerous functions, such as 'gets' and 'scanf' // Dangerous functions, such as 'gets' and 'scanf'
checkBufferOverrun.WarningDangerousFunctions(); checkBufferOverrun.WarningDangerousFunctions();
// Invalid function usage.. // Invalid function usage..
checkOther.InvalidFunctionUsage(); checkOther.InvalidFunctionUsage();
if (_settings._checkCodingStyle) if (_settings._checkCodingStyle)
{ {
// Check that all private functions are called. // Check that all private functions are called.
checkClass.CheckUnusedPrivateFunctions(); checkClass.CheckUnusedPrivateFunctions();
// Warning upon c-style pointer casts // Warning upon c-style pointer casts
const char *ext = strrchr(FileName, '.'); const char *ext = strrchr(FileName, '.');
if (ext && strcmp(ext,".cpp")==0) if (ext && strcmp(ext,".cpp")==0)
checkOther.WarningOldStylePointerCast(); checkOther.WarningOldStylePointerCast();
// Use standard functions instead // Use standard functions instead
checkOther.WarningIsDigit(); checkOther.WarningIsDigit();
checkOther.WarningIsAlpha(); checkOther.WarningIsAlpha();
checkClass.CheckOperatorEq1(); checkClass.CheckOperatorEq1();
// if (a) delete a; // if (a) delete a;
checkOther.WarningRedundantCode(); checkOther.WarningRedundantCode();
// if (condition); // if (condition);
checkOther.WarningIf(); checkOther.WarningIf();
// Variable scope (check if the scope could be limited) // Variable scope (check if the scope could be limited)
//CheckVariableScope(); //CheckVariableScope();
// Check if a constant function parameter is passed by value // Check if a constant function parameter is passed by value
checkOther.CheckConstantFunctionParameter(); checkOther.CheckConstantFunctionParameter();
// Unused struct members.. // Unused struct members..
checkOther.CheckStructMemberUsage(); checkOther.CheckStructMemberUsage();
// Check for various types of incomplete statements that could for example // Check for various types of incomplete statements that could for example
// mean that an ';' has been added by accident // mean that an ';' has been added by accident
checkOther.CheckIncompleteStatement(); checkOther.CheckIncompleteStatement();
// Unreachable code below a 'return' statement // Unreachable code below a 'return' statement
checkOther.unreachableCode(); checkOther.unreachableCode();
// Usage of local functions // Usage of local functions
checkOther.functionVariableUsage(); checkOther.functionVariableUsage();
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void CppCheck::reportErr( const std::string &errmsg) void CppCheck::reportErr( const std::string &errmsg)
{ {
if ( /*OnlyReportUniqueErrors*/ true ) if ( /*OnlyReportUniqueErrors*/ true )
@ -340,19 +340,19 @@ void CppCheck::reportErr( const std::string &errmsg)
return; return;
_errorList.push_back( errmsg ); _errorList.push_back( errmsg );
} }
std::string errmsg2( errmsg ); std::string errmsg2( errmsg );
if ( _settings._verbose ) if ( _settings._verbose )
{ {
errmsg2 += "\n Defines=\'" + cfg + "\'\n"; errmsg2 += "\n Defines=\'" + cfg + "\'\n";
} }
_errorLogger->reportErr( errmsg2 ); _errorLogger->reportErr( errmsg2 );
_errout << errmsg2 << std::endl; _errout << errmsg2 << std::endl;
} }
void CppCheck::reportOut( const std::string &outmsg) void CppCheck::reportOut( const std::string &outmsg)
{ {
// This is currently never called. It is here just to comply with // This is currently never called. It is here just to comply with

View File

@ -15,10 +15,10 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#ifndef CPPCHECK_H #ifndef CPPCHECK_H
#define CPPCHECK_H #define CPPCHECK_H
#include <string> #include <string>
#include <list> #include <list>
#include <sstream> #include <sstream>
@ -27,7 +27,7 @@
#include "settings.h" #include "settings.h"
#include "errorlogger.h" #include "errorlogger.h"
#include "CheckFunctionUsage.h" #include "CheckFunctionUsage.h"
/** /**
* This is the base class which will use other classes to do * This is the base class which will use other classes to do
* static code analysis for C and C++ code to find possible * static code analysis for C and C++ code to find possible
@ -41,18 +41,18 @@ class CppCheck : public ErrorLogger
* Constructor. * Constructor.
*/ */
CppCheck( ErrorLogger &errorLogger ); CppCheck( ErrorLogger &errorLogger );
/** /**
* Destructor. * Destructor.
*/ */
virtual ~CppCheck(); virtual ~CppCheck();
/** /**
* This starts the actual checking. Note that you must call * This starts the actual checking. Note that you must call
* parseFromArgs() or settings() and addFile() before calling this. * parseFromArgs() or settings() and addFile() before calling this.
*/ */
void check(); void check();
/** /**
* Adjust the settings before doing the check. E.g. show only * Adjust the settings before doing the check. E.g. show only
* actual bugs or also coding style issues. * actual bugs or also coding style issues.
@ -60,7 +60,7 @@ class CppCheck : public ErrorLogger
* @param settings New settings which will overwrite the old. * @param settings New settings which will overwrite the old.
*/ */
void settings( const Settings &settings ); void settings( const Settings &settings );
/** /**
* Add new file to be checked. * Add new file to be checked.
* *
@ -69,7 +69,7 @@ class CppCheck : public ErrorLogger
* should be added to the list. Include filese are gathered automatically. * should be added to the list. Include filese are gathered automatically.
*/ */
void addFile( const std::string &path ); void addFile( const std::string &path );
/** /**
* Add new unreal file to be checked. * Add new unreal file to be checked.
* *
@ -78,7 +78,7 @@ class CppCheck : public ErrorLogger
* the content of the file. * the content of the file.
*/ */
void addFile( const std::string &path, const std::string &content ); void addFile( const std::string &path, const std::string &content );
/** /**
* Parse command line args and get settings and file lists * Parse command line args and get settings and file lists
* from there. * from there.
@ -90,10 +90,10 @@ class CppCheck : public ErrorLogger
* checked. * checked.
*/ */
std::string parseFromArgs( int argc, const char* const argv[] ); std::string parseFromArgs( int argc, const char* const argv[] );
private: private:
void checkFile(const std::string &code, const char FileName[]); void checkFile(const std::string &code, const char FileName[]);
/** /**
* Errors and warnings are directed here. * Errors and warnings are directed here.
* *
@ -102,14 +102,14 @@ class CppCheck : public ErrorLogger
* "[main.cpp:4] Uninitialized member variable" * "[main.cpp:4] Uninitialized member variable"
*/ */
virtual void reportErr( const std::string &errmsg); virtual void reportErr( const std::string &errmsg);
/** /**
* Information about progress is directed here. * Information about progress is directed here.
* *
* @param outmsg, E.g. "Checking main.cpp..." * @param outmsg, E.g. "Checking main.cpp..."
*/ */
virtual void reportOut( const std::string &outmsg); virtual void reportOut( const std::string &outmsg);
std::list<std::string> _errorList; std::list<std::string> _errorList;
std::ostringstream _errout; std::ostringstream _errout;
Settings _settings; Settings _settings;
@ -118,9 +118,9 @@ class CppCheck : public ErrorLogger
std::map<std::string,std::string> _fileContents; std::map<std::string,std::string> _fileContents;
CheckFunctionUsage _checkFunctionUsage; CheckFunctionUsage _checkFunctionUsage;
ErrorLogger *_errorLogger; ErrorLogger *_errorLogger;
/** Current configuration */ /** Current configuration */
std::string cfg; std::string cfg;
}; };
#endif // CPPCHECK_H #endif // CPPCHECK_H

View File

@ -15,21 +15,21 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#include "cppcheckexecutor.h" #include "cppcheckexecutor.h"
#include "cppcheck.h" #include "cppcheck.h"
#include <iostream> #include <iostream>
CppCheckExecutor::CppCheckExecutor() CppCheckExecutor::CppCheckExecutor()
{ {
//ctor //ctor
} }
CppCheckExecutor::~CppCheckExecutor() CppCheckExecutor::~CppCheckExecutor()
{ {
//dtor //dtor
} }
void CppCheckExecutor::check( int argc, const char* const argv[] ) void CppCheckExecutor::check( int argc, const char* const argv[] )
{ {
CppCheck cppCheck( *this ); CppCheck cppCheck( *this );
@ -39,12 +39,12 @@ void CppCheckExecutor::check( int argc, const char* const argv[] )
else else
std::cout << result; std::cout << result;
} }
void CppCheckExecutor::reportErr( const std::string &errmsg) void CppCheckExecutor::reportErr( const std::string &errmsg)
{ {
std::cerr << errmsg << std::endl; std::cerr << errmsg << std::endl;
} }
void CppCheckExecutor::reportOut( const std::string &outmsg) void CppCheckExecutor::reportOut( const std::string &outmsg)
{ {
std::cout << outmsg << std::endl; std::cout << outmsg << std::endl;

View File

@ -15,12 +15,12 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#ifndef CPPCHECKEXECUTOR_H #ifndef CPPCHECKEXECUTOR_H
#define CPPCHECKEXECUTOR_H #define CPPCHECKEXECUTOR_H
#include "errorlogger.h" #include "errorlogger.h"
/** /**
* This class works as an example of how CppCheck can be used in external * This class works as an example of how CppCheck can be used in external
* programs without very little knowledge of the internal parts of the * programs without very little knowledge of the internal parts of the
@ -35,12 +35,12 @@ public:
* Constructor * Constructor
*/ */
CppCheckExecutor(); CppCheckExecutor();
/** /**
* Destructor * Destructor
*/ */
virtual ~CppCheckExecutor(); virtual ~CppCheckExecutor();
/** /**
* Starts the checking. * Starts the checking.
* *
@ -48,7 +48,7 @@ public:
* @param argv from main() * @param argv from main()
*/ */
void check( int argc, const char* const argv[] ); void check( int argc, const char* const argv[] );
/** /**
* Errors and warnings are directed here. This should be * Errors and warnings are directed here. This should be
* called by the CppCheck class only. * called by the CppCheck class only.
@ -58,7 +58,7 @@ public:
* "[main.cpp:4] Uninitialized member variable" * "[main.cpp:4] Uninitialized member variable"
*/ */
virtual void reportErr( const std::string &errmsg); virtual void reportErr( const std::string &errmsg);
/** /**
* Information about progress is directed here. This should be * Information about progress is directed here. This should be
* called by the CppCheck class only. * called by the CppCheck class only.
@ -67,5 +67,5 @@ public:
*/ */
virtual void reportOut( const std::string &outmsg); virtual void reportOut( const std::string &outmsg);
}; };
#endif // CPPCHECKEXECUTOR_H #endif // CPPCHECKEXECUTOR_H

View File

@ -15,12 +15,12 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#ifndef ERRORLOGGER_H #ifndef ERRORLOGGER_H
#define ERRORLOGGER_H #define ERRORLOGGER_H
#include <string> #include <string>
/** /**
* This is an interface, which the class responsible of error logging * This is an interface, which the class responsible of error logging
* should implement. * should implement.
@ -29,7 +29,7 @@ class ErrorLogger
{ {
public: public:
virtual ~ErrorLogger() {} virtual ~ErrorLogger() {}
/** /**
* Errors and warnings are directed here. * Errors and warnings are directed here.
* *
@ -38,7 +38,7 @@ public:
* "[main.cpp:4] Uninitialized member variable" * "[main.cpp:4] Uninitialized member variable"
*/ */
virtual void reportErr( const std::string &errmsg) = 0; virtual void reportErr( const std::string &errmsg) = 0;
/** /**
* Information about progress is directed here. * Information about progress is directed here.
* *
@ -46,5 +46,5 @@ public:
*/ */
virtual void reportOut( const std::string &outmsg) = 0; virtual void reportOut( const std::string &outmsg) = 0;
}; };
#endif // #ifndef ERRORLOGGER_H #endif // #ifndef ERRORLOGGER_H

View File

@ -114,7 +114,7 @@ std::string Preprocessor::read(std::istream &istr, const std::string &filename)
{ {
ch = (char)istr.get(); ch = (char)istr.get();
code << std::string(1,ch); code << std::string(1,ch);
// Avoid exiting loop if string contains "-characters // Avoid exiting loop if string contains "-characters
ch = 0; ch = 0;
} }
@ -142,7 +142,7 @@ std::string Preprocessor::read(std::istream &istr, const std::string &filename)
code << std::string(1, ch); code << std::string(1, ch);
} }
} }
return code.str(); return code.str();
} }
@ -159,7 +159,7 @@ void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::str
std::string::size_type loc = 0; std::string::size_type loc = 0;
while ( (loc = codestr.find("\t", loc)) != std::string::npos ) while ( (loc = codestr.find("\t", loc)) != std::string::npos )
codestr[loc] = ' '; codestr[loc] = ' ';
// Remove all indentation.. // Remove all indentation..
if ( !codestr.empty() && codestr[0] == ' ' ) if ( !codestr.empty() && codestr[0] == ' ' )
codestr.erase( 0, codestr.find_first_not_of(" ") ); codestr.erase( 0, codestr.find_first_not_of(" ") );

View File

@ -35,22 +35,22 @@ public:
/** Just read the code into a string. Perform simple cleanup of the code */ /** Just read the code into a string. Perform simple cleanup of the code */
std::string read(std::istream &istr, const std::string &filename); std::string read(std::istream &istr, const std::string &filename);
private: private:
/** /**
* Get preprocessed code for a given configuration * Get preprocessed code for a given configuration
*/ */
std::string getcode(const std::string &filedata, std::string cfg); std::string getcode(const std::string &filedata, std::string cfg);
/** /**
* Get all possible configurations. By looking at the ifdefs and ifndefs in filedata * Get all possible configurations. By looking at the ifdefs and ifndefs in filedata
*/ */
std::list<std::string> getcfgs( const std::string &filedata ); std::list<std::string> getcfgs( const std::string &filedata );
std::string getdef(std::string line, bool def); std::string getdef(std::string line, bool def);
bool match_cfg_def( std::string cfg, const std::string &def ); bool match_cfg_def( std::string cfg, const std::string &def );
ErrorLogger *_errorLogger; ErrorLogger *_errorLogger;
}; };

View File

@ -15,9 +15,9 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#include "settings.h" #include "settings.h"
Settings::Settings() Settings::Settings()
{ {
_debug = false; _debug = false;
@ -27,8 +27,8 @@ Settings::Settings()
_checkFunctionUsage = false; _checkFunctionUsage = false;
_verbose = false; _verbose = false;
} }
Settings::~Settings() Settings::~Settings()
{ {
} }

View File

@ -15,10 +15,10 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#ifndef SETTINGS_H #ifndef SETTINGS_H
#define SETTINGS_H #define SETTINGS_H
/** /**
* This is just a container for general settings so that we don't need * This is just a container for general settings so that we don't need
* to pass individual values to functions or constructors now or in the * to pass individual values to functions or constructors now or in the
@ -29,7 +29,7 @@ class Settings
public: public:
Settings(); Settings();
virtual ~Settings(); virtual ~Settings();
bool _debug; bool _debug;
bool _showAll; bool _showAll;
bool _checkCodingStyle; bool _checkCodingStyle;
@ -37,5 +37,5 @@ public:
bool _checkFunctionUsage; bool _checkFunctionUsage;
bool _verbose; bool _verbose;
}; };
#endif // SETTINGS_H #endif // SETTINGS_H

View File

@ -68,7 +68,7 @@ private:
TEST_CASE( initvar_same_classname ); // BUG 2208157 TEST_CASE( initvar_same_classname ); // BUG 2208157
TEST_CASE( initvar_chained_assign ); // BUG 2270433 TEST_CASE( initvar_chained_assign ); // BUG 2270433
TEST_CASE( initvar_2constructors ); // BUG 2270353 TEST_CASE( initvar_2constructors ); // BUG 2270353
TEST_CASE( initvar_private_constructor ); // BUG 2354171 - private constructor TEST_CASE( initvar_private_constructor ); // BUG 2354171 - private constructor
TEST_CASE( initvar_destructor ); // No variables need to be initialized in a destructor TEST_CASE( initvar_destructor ); // No variables need to be initialized in a destructor
@ -186,7 +186,7 @@ private:
ASSERT_EQUALS( std::string(""), err ); ASSERT_EQUALS( std::string(""), err );
} }
void initvar_operator_eq2() void initvar_operator_eq2()
{ {
check( "class Fred\n" check( "class Fred\n"
@ -198,7 +198,7 @@ private:
"};\n" ); "};\n" );
ASSERT_EQUALS( std::string("[test.cpp:5] Uninitialized member variable 'Fred::i'\n"), errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:5] Uninitialized member variable 'Fred::i'\n"), errout.str() );
} }
void initvar_operator_eq3() void initvar_operator_eq3()
{ {
check( "class Fred\n" check( "class Fred\n"
@ -295,7 +295,7 @@ private:
ASSERT_EQUALS( std::string(""), err ); ASSERT_EQUALS( std::string(""), err );
} }
void initvar_private_constructor() void initvar_private_constructor()
{ {
check( "class Fred\n" check( "class Fred\n"

View File

@ -68,7 +68,7 @@ private:
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void return1() void return1()
{ {
check( "int f1()\n" check( "int f1()\n"
@ -78,7 +78,7 @@ private:
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void callback1() void callback1()
{ {
check( "void f1()\n" check( "void f1()\n"
@ -88,7 +88,7 @@ private:
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void else1() void else1()
{ {
check( "void f1()\n" check( "void f1()\n"

View File

@ -123,10 +123,10 @@ private:
TEST_CASE( linux_list_1 ); TEST_CASE( linux_list_1 );
TEST_CASE( sizeof1 ); TEST_CASE( sizeof1 );
TEST_CASE( realloc1 ); TEST_CASE( realloc1 );
TEST_CASE( realloc2 ); TEST_CASE( realloc2 );
TEST_CASE( assign ); TEST_CASE( assign );
} }
@ -807,7 +807,7 @@ private:
std::string err( errout.str() ); std::string err( errout.str() );
ASSERT_EQUALS( std::string("[test.cpp:11]: Memory leak: p\n"), err ); ASSERT_EQUALS( std::string("[test.cpp:11]: Memory leak: p\n"), err );
} }
void func8() void func8()
{ {
@ -818,7 +818,7 @@ private:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
/* /*
@ -970,7 +970,7 @@ private:
ASSERT_EQUALS( std::string("[test.cpp:12]: Memory leak: s2\n"), err ); ASSERT_EQUALS( std::string("[test.cpp:12]: Memory leak: s2\n"), err );
} }
void realloc1() void realloc1()
{ {
check( "void foo()\n" check( "void foo()\n"
@ -978,10 +978,10 @@ private:
" char *a = (char *)malloc(10);\n" " char *a = (char *)malloc(10);\n"
" a = realloc(a, 100);\n" " a = realloc(a, 100);\n"
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: a\n"), errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:5]: Memory leak: a\n"), errout.str() );
} }
void realloc2() void realloc2()
{ {
check( "void foo()\n" check( "void foo()\n"
@ -990,10 +990,10 @@ private:
" a = (char *)realloc(a, 100);\n" " a = (char *)realloc(a, 100);\n"
" free(a);\n" " free(a);\n"
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void assign() void assign()
{ {
@ -1003,25 +1003,25 @@ private:
" a = 0;\n" " a = 0;\n"
" free(a);\n" " free(a);\n"
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string("[test.cpp:3]: Memory leak: a\n"), errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:3]: Memory leak: a\n"), errout.str() );
check( "void foo()\n" check( "void foo()\n"
"{\n" "{\n"
" char *a = (char *)malloc(10);\n" " char *a = (char *)malloc(10);\n"
" char *p = a;\n" " char *p = a;\n"
" free(p);\n" " free(p);\n"
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
check( "void foo()\n" check( "void foo()\n"
"{\n" "{\n"
" char *a = (char *)malloc(10);\n" " char *a = (char *)malloc(10);\n"
" a += 10;\n" " a += 10;\n"
" free(a - 10);\n" " free(a - 10);\n"
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }

View File

@ -39,7 +39,7 @@ private:
{ {
// Just read the code into a string. Perform simple cleanup of the code // Just read the code into a string. Perform simple cleanup of the code
TEST_CASE(readCode); TEST_CASE(readCode);
// The bug that started the whole work with the new preprocessor // The bug that started the whole work with the new preprocessor
TEST_CASE( Bug2190219 ); TEST_CASE( Bug2190219 );
@ -63,7 +63,7 @@ private:
TEST_CASE( multiline ); TEST_CASE( multiline );
} }
void readCode() void readCode()
{ {
const char code[] = " \t a //\n" const char code[] = " \t a //\n"
@ -73,7 +73,7 @@ private:
std::string codestr( p.read(istr,"") ); std::string codestr( p.read(istr,"") );
ASSERT_EQUALS( "a \n#aa b \n", codestr ); ASSERT_EQUALS( "a \n#aa b \n", codestr );
} }
bool cmpmaps(const std::map<std::string, std::string> &m1, const std::map<std::string, std::string> &m2) bool cmpmaps(const std::map<std::string, std::string> &m1, const std::map<std::string, std::string> &m2)
{ {

View File

@ -15,53 +15,53 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#include "testsuite.h" #include "testsuite.h"
#include "tokenize.h" #include "tokenize.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
class TestSimplifyTokens : public TestFixture class TestSimplifyTokens : public TestFixture
{ {
public: public:
TestSimplifyTokens() : TestFixture("TestSimplifyTokens") TestSimplifyTokens() : TestFixture("TestSimplifyTokens")
{ } { }
private: private:
void run() void run()
{ {
TEST_CASE( cast0 ); TEST_CASE( cast0 );
TEST_CASE( sizeof1 ); TEST_CASE( sizeof1 );
} }
std::string tok(const char code[]) std::string tok(const char code[])
{ {
std::istringstream istr(code); std::istringstream istr(code);
Tokenizer tokenizer; Tokenizer tokenizer;
tokenizer.tokenize( istr, "test.cpp" ); tokenizer.tokenize( istr, "test.cpp" );
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
std::string ret; std::string ret;
for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() ) for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() )
{ {
ret += std::string(tok->aaaa()) + " "; ret += std::string(tok->aaaa()) + " ";
} }
return ret; return ret;
} }
void cast0() void cast0()
{ {
const char code1[] = " if ( p == (char *)0 ) "; const char code1[] = " if ( p == (char *)0 ) ";
const char code2[] = " if ( p == 0 ) "; const char code2[] = " if ( p == 0 ) ";
ASSERT_EQUALS( tok(code1), tok(code2) ); ASSERT_EQUALS( tok(code1), tok(code2) );
} }
void sizeof1() void sizeof1()
{ {
const char code1[] = " struct ABC *abc = malloc(sizeof(*abc)); "; const char code1[] = " struct ABC *abc = malloc(sizeof(*abc)); ";
@ -69,5 +69,5 @@ private:
ASSERT_EQUALS( tok(code1), tok(code2) ); ASSERT_EQUALS( tok(code1), tok(code2) );
} }
}; };
REGISTER_TEST( TestSimplifyTokens ) REGISTER_TEST( TestSimplifyTokens )

View File

@ -42,7 +42,7 @@ protected:
public: public:
virtual void reportErr( const std::string &errmsg); virtual void reportErr( const std::string &errmsg);
virtual void reportOut( const std::string &outmsg); virtual void reportOut( const std::string &outmsg);
TestFixture(const std::string &_name); TestFixture(const std::string &_name);

View File

@ -43,15 +43,15 @@ private:
TEST_CASE( inlineasm ); TEST_CASE( inlineasm );
TEST_CASE( dupfuncname ); TEST_CASE( dupfuncname );
TEST_CASE( const_and_volatile_functions ); TEST_CASE( const_and_volatile_functions );
TEST_CASE( numeric_true_condition ); TEST_CASE( numeric_true_condition );
TEST_CASE( multi_compare ); TEST_CASE( multi_compare );
TEST_CASE( match1 ); TEST_CASE( match1 );
TEST_CASE( varid1 ); TEST_CASE( varid1 );
} }
@ -157,7 +157,7 @@ private:
ASSERT_EQUALS( 1, static_cast<unsigned int>(tokenizer._functionList.size()) ); ASSERT_EQUALS( 1, static_cast<unsigned int>(tokenizer._functionList.size()) );
ASSERT_EQUALS( std::string("b"), tokenizer._functionList[0]->aaaa() ); ASSERT_EQUALS( std::string("b"), tokenizer._functionList[0]->aaaa() );
} }
void const_and_volatile_functions() void const_and_volatile_functions()
{ {
const char code[] = "class B\n\ const char code[] = "class B\n\
@ -176,15 +176,15 @@ private:
\n\ \n\
void B::c() volatile\n\ void B::c() volatile\n\
{}\n"; {}\n";
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.fillFunctionList(); tokenizer.fillFunctionList();
ASSERT_EQUALS( 3, static_cast<unsigned int>(tokenizer._functionList.size()) ); ASSERT_EQUALS( 3, static_cast<unsigned int>(tokenizer._functionList.size()) );
if( tokenizer._functionList.size() == 3 ) if( tokenizer._functionList.size() == 3 )
{ {
@ -193,7 +193,7 @@ private:
ASSERT_EQUALS( std::string("c"), tokenizer._functionList[2]->str() ); ASSERT_EQUALS( std::string("c"), tokenizer._functionList[2]->str() );
} }
} }
void numeric_true_condition() void numeric_true_condition()
{ {
@ -201,68 +201,68 @@ private:
"{\n" "{\n"
" if (5==5);\n" " if (5==5);\n"
"}\n"; "}\n";
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
std::ostringstream ostr; std::ostringstream ostr;
for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next()) for (const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str(); ostr << " " << tok->str();
ASSERT_EQUALS( std::string(" void f ( ) { if ( true ) ; }"), ostr.str() ); ASSERT_EQUALS( std::string(" void f ( ) { if ( true ) ; }"), ostr.str() );
} }
void multi_compare() void multi_compare()
{ {
// Test for found // Test for found
ASSERT_EQUALS( 1, TOKEN::multiCompare( "one|two", "one" ) ); ASSERT_EQUALS( 1, TOKEN::multiCompare( "one|two", "one" ) );
ASSERT_EQUALS( 1, TOKEN::multiCompare( "one|two", "two" ) ); ASSERT_EQUALS( 1, TOKEN::multiCompare( "one|two", "two" ) );
ASSERT_EQUALS( 1, TOKEN::multiCompare( "verybig|two|", "two" ) ); ASSERT_EQUALS( 1, TOKEN::multiCompare( "verybig|two|", "two" ) );
// Test for empty string found // Test for empty string found
ASSERT_EQUALS( 0, TOKEN::multiCompare( "|one|two", "notfound" ) ); ASSERT_EQUALS( 0, TOKEN::multiCompare( "|one|two", "notfound" ) );
ASSERT_EQUALS( 0, TOKEN::multiCompare( "one||two", "notfound" ) ); ASSERT_EQUALS( 0, TOKEN::multiCompare( "one||two", "notfound" ) );
ASSERT_EQUALS( 0, TOKEN::multiCompare( "one|two|", "notfound" ) ); ASSERT_EQUALS( 0, TOKEN::multiCompare( "one|two|", "notfound" ) );
// Test for not found // Test for not found
ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "notfound" ) ); ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "notfound" ) );
ASSERT_EQUALS( -1, TOKEN::multiCompare( "verybig|two", "s" ) ); ASSERT_EQUALS( -1, TOKEN::multiCompare( "verybig|two", "s" ) );
ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "ne" ) ); ASSERT_EQUALS( -1, TOKEN::multiCompare( "one|two", "ne" ) );
ASSERT_EQUALS( -1, TOKEN::multiCompare( "abc|def", "a" ) ); ASSERT_EQUALS( -1, TOKEN::multiCompare( "abc|def", "a" ) );
} }
void match1() void match1()
{ {
// Match "%var% | %var%" // Match "%var% | %var%"
{ {
const std::string code("abc|def"); const std::string code("abc|def");
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
// Match.. // Match..
ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "%var% | %var%") ); ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "%var% | %var%") );
} }
// Match "%var% || %var%" // Match "%var% || %var%"
{ {
const std::string code("abc||def"); const std::string code("abc||def");
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
// Match.. // Match..
ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "%var% || %var%") ); ASSERT_EQUALS( true, TOKEN::Match(tokenizer.tokens(), "%var% || %var%") );
} }
} }
void varid1() void varid1()
{ {
@ -274,13 +274,13 @@ private:
" i = 3;\n" " i = 3;\n"
" i = 4;\n" " i = 4;\n"
"}\n" ); "}\n" );
// tokenize.. // tokenize..
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId(); tokenizer.setVarId();
for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() ) for ( const TOKEN *tok = tokenizer.tokens(); tok; tok = tok->next() )
{ {
if ( tok->str() != "i" ) if ( tok->str() != "i" )

View File

@ -15,25 +15,25 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
// Check for unused variables.. // Check for unused variables..
#define UNIT_TESTING #define UNIT_TESTING
#include "testsuite.h" #include "testsuite.h"
#include "tokenize.h" #include "tokenize.h"
#include "CheckOther.h" #include "CheckOther.h"
#include <sstream> #include <sstream>
extern std::ostringstream errout; extern std::ostringstream errout;
class TestUnusedVar : public TestFixture class TestUnusedVar : public TestFixture
{ {
public: public:
TestUnusedVar() : TestFixture("TestUnusedVar") TestUnusedVar() : TestFixture("TestUnusedVar")
{ } { }
private: private:
void check( const char code[] ) void check( const char code[] )
{ {
@ -42,27 +42,27 @@ private:
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize( istr, "test.cpp" ); tokenizer.tokenize( istr, "test.cpp" );
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
// Check for unused variables.. // Check for unused variables..
CheckOther checkOther( &tokenizer, this ); CheckOther checkOther( &tokenizer, this );
checkOther.CheckStructMemberUsage(); checkOther.CheckStructMemberUsage();
} }
void run() void run()
{ {
TEST_CASE( structmember1 ); TEST_CASE( structmember1 );
TEST_CASE( structmember2 ); TEST_CASE( structmember2 );
TEST_CASE( structmember3 ); TEST_CASE( structmember3 );
TEST_CASE( localvar1 ); TEST_CASE( localvar1 );
TEST_CASE( localvar2 ); TEST_CASE( localvar2 );
TEST_CASE( localvar3 ); TEST_CASE( localvar3 );
TEST_CASE( localvar4 ); TEST_CASE( localvar4 );
} }
void structmember1() void structmember1()
{ {
check( "struct abc\n" check( "struct abc\n"
@ -75,7 +75,7 @@ private:
"[test.cpp:3]: struct member 'abc::b' is never read\n" "[test.cpp:3]: struct member 'abc::b' is never read\n"
"[test.cpp:4]: struct member 'abc::c' is never read\n"), errout.str() ); "[test.cpp:4]: struct member 'abc::c' is never read\n"), errout.str() );
} }
void structmember2() void structmember2()
{ {
check( "struct ABC\n" check( "struct ABC\n"
@ -94,7 +94,7 @@ private:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void structmember3() void structmember3()
{ {
check( "struct ABC\n" check( "struct ABC\n"
@ -114,11 +114,11 @@ private:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
void functionVariableUsage( const char code[] ) void functionVariableUsage( const char code[] )
{ {
// Tokenize.. // Tokenize..
@ -126,15 +126,15 @@ private:
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize( istr, "test.cpp" ); tokenizer.tokenize( istr, "test.cpp" );
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
// Check for unused variables.. // Check for unused variables..
CheckOther checkOther( &tokenizer, this ); CheckOther checkOther( &tokenizer, this );
checkOther.functionVariableUsage(); checkOther.functionVariableUsage();
} }
void localvar1() void localvar1()
{ {
functionVariableUsage( "void foo()\n" functionVariableUsage( "void foo()\n"
@ -143,7 +143,7 @@ private:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string("[test.cpp:2]: Variable 'i' is assigned a value that is never used\n"), errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:2]: Variable 'i' is assigned a value that is never used\n"), errout.str() );
} }
void localvar2() void localvar2()
{ {
functionVariableUsage( "void foo()\n" functionVariableUsage( "void foo()\n"
@ -153,7 +153,7 @@ private:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string("[test.cpp:2]: Variable 'i' is not assigned a value\n"), errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:2]: Variable 'i' is not assigned a value\n"), errout.str() );
} }
void localvar3() void localvar3()
{ {
functionVariableUsage( "void foo()\n" functionVariableUsage( "void foo()\n"
@ -165,7 +165,7 @@ private:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string("[test.cpp:2]: Variable 'i' is assigned a value that is never used\n"), errout.str() ); ASSERT_EQUALS( std::string("[test.cpp:2]: Variable 'i' is assigned a value that is never used\n"), errout.str() );
} }
void localvar4() void localvar4()
{ {
functionVariableUsage( "void foo()\n" functionVariableUsage( "void foo()\n"
@ -175,10 +175,10 @@ private:
"}\n" ); "}\n" );
ASSERT_EQUALS( std::string(""), errout.str() ); ASSERT_EQUALS( std::string(""), errout.str() );
} }
}; };
REGISTER_TEST( TestUnusedVar ) REGISTER_TEST( TestUnusedVar )

View File

@ -103,19 +103,19 @@ int TOKEN::multiCompare( const char *needle, const char *haystack )
// If needle and haystack are both at the end, we have a match. // If needle and haystack are both at the end, we have a match.
if( *haystackPointer == 0 ) if( *haystackPointer == 0 )
return 1; return 1;
haystackPointer = haystack; haystackPointer = haystack;
if( findNextOr ) if( findNextOr )
findNextOr = false; findNextOr = false;
else else
emptyStringFound = true; emptyStringFound = true;
continue; continue;
} }
if( findNextOr ) if( findNextOr )
continue; continue;
// If haystack and needle don't share the same character, reset // If haystack and needle don't share the same character, reset
// haystackpointer and find next '|' character. // haystackpointer and find next '|' character.
if( *haystackPointer != *needle ) if( *haystackPointer != *needle )
@ -124,22 +124,22 @@ int TOKEN::multiCompare( const char *needle, const char *haystack )
findNextOr = true; findNextOr = true;
continue; continue;
} }
// All characters in haystack and needle have matched this far // All characters in haystack and needle have matched this far
haystackPointer++; haystackPointer++;
} }
// If both needle and haystack are at the end, then we have a match. // If both needle and haystack are at the end, then we have a match.
if( *haystackPointer == 0 ) if( *haystackPointer == 0 )
return 1; return 1;
// If empty string was found or if last character in needle was '|' // If empty string was found or if last character in needle was '|'
if( emptyStringFound || findNextOr == false ) if( emptyStringFound || findNextOr == false )
return 0; return 0;
return -1; return -1;
} }
bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[]) bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[], const char *varname2[])
{ {
@ -176,11 +176,11 @@ bool TOKEN::Match(const TOKEN *tok, const char pattern[], const char *varname1[]
if (!tok->isName()) if (!tok->isName())
return false; return false;
} }
// Accept any token // Accept any token
else if (strcmp(str,"%any%")==0 ) else if (strcmp(str,"%any%")==0 )
{ {
} }
// Variable name.. // Variable name..
@ -314,37 +314,37 @@ unsigned int TOKEN::varId() const
{ {
return _varId; return _varId;
} }
void TOKEN::varId( unsigned int id ) void TOKEN::varId( unsigned int id )
{ {
_varId = id; _varId = id;
} }
TOKEN *TOKEN::next() const TOKEN *TOKEN::next() const
{ {
return _next; return _next;
} }
void TOKEN::next( TOKEN *next ) void TOKEN::next( TOKEN *next )
{ {
_next = next; _next = next;
} }
unsigned int TOKEN::fileIndex() const unsigned int TOKEN::fileIndex() const
{ {
return _fileIndex; return _fileIndex;
} }
void TOKEN::fileIndex( unsigned int fileIndex ) void TOKEN::fileIndex( unsigned int fileIndex )
{ {
_fileIndex = fileIndex; _fileIndex = fileIndex;
} }
unsigned int TOKEN::linenr() const unsigned int TOKEN::linenr() const
{ {
return _linenr; return _linenr;
} }
void TOKEN::linenr( unsigned int linenr ) void TOKEN::linenr( unsigned int linenr )
{ {
_linenr = linenr; _linenr = linenr;

40
token.h
View File

@ -15,52 +15,52 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/ * along with this program. If not, see <http://www.gnu.org/licenses/
*/ */
#ifndef TOKEN_H #ifndef TOKEN_H
#define TOKEN_H #define TOKEN_H
#include <string> #include <string>
class TOKEN class TOKEN
{ {
public: public:
TOKEN(); TOKEN();
~TOKEN(); ~TOKEN();
void setstr( const char s[] ); void setstr( const char s[] );
const std::string &str() const const std::string &str() const
{ return _str; } { return _str; }
const char *aaaa() const const char *aaaa() const
{ return _cstr; } { return _cstr; }
char aaaa0() const char aaaa0() const
{ return _cstr[0]; } { return _cstr[0]; }
char aaaa1() const char aaaa1() const
{ return _cstr[1]; } { return _cstr[1]; }
/** /**
* Combine two tokens that belong to each other. * Combine two tokens that belong to each other.
* Ex: "<" and "=" may become "<=" * Ex: "<" and "=" may become "<="
*/ */
void combineWithNext(const char str1[], const char str2[]); void combineWithNext(const char str1[], const char str2[]);
/** /**
* Unlink and delete next token. * Unlink and delete next token.
*/ */
void deleteNext(); void deleteNext();
/** /**
* Returns token in given index, related to this token. * Returns token in given index, related to this token.
* For example index 1 would return next token, and 2 * For example index 1 would return next token, and 2
* would return next from that one. * would return next from that one.
*/ */
const TOKEN *tokAt(int index) const; const TOKEN *tokAt(int index) const;
const char *strAt(int index) const; const char *strAt(int index) const;
/** /**
* Match given token (or list of tokens) to a pattern list. * Match given token (or list of tokens) to a pattern list.
* *
@ -89,13 +89,13 @@ public:
* false if given token does not match with given pattern * false if given token does not match with given pattern
*/ */
static bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0); static bool Match(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
bool isName() const; bool isName() const;
bool isNumber() const; bool isNumber() const;
bool isStandardType() const; bool isStandardType() const;
static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0); static const TOKEN *findmatch(const TOKEN *tok, const char pattern[], const char *varname1[]=0, const char *varname2[]=0);
static const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]); static const TOKEN *findtoken(const TOKEN *tok1, const char *tokenstr[]);
/** /**
* Needle is build from multiple alternatives. If one of * Needle is build from multiple alternatives. If one of
* them is equal to haystack, return value is 1. If there * them is equal to haystack, return value is 1. If there
@ -110,20 +110,20 @@ public:
* -1 if needle was not found * -1 if needle was not found
*/ */
static int multiCompare( const char *needle, const char *haystack ); static int multiCompare( const char *needle, const char *haystack );
unsigned int linenr() const; unsigned int linenr() const;
void linenr( unsigned int linenr ); void linenr( unsigned int linenr );
unsigned int fileIndex() const; unsigned int fileIndex() const;
void fileIndex( unsigned int fileIndex ); void fileIndex( unsigned int fileIndex );
TOKEN *next() const; TOKEN *next() const;
void next( TOKEN *next ); void next( TOKEN *next );
unsigned int varId() const; unsigned int varId() const;
void varId( unsigned int id ); void varId( unsigned int id );
private: private:
std::string _str; std::string _str;
char * _cstr; char * _cstr;
@ -134,5 +134,5 @@ private:
unsigned int _fileIndex; unsigned int _fileIndex;
unsigned int _linenr; unsigned int _linenr;
}; };
#endif // TOKEN_H #endif // TOKEN_H

View File

@ -73,7 +73,7 @@ TOKEN *Tokenizer::_gettok(TOKEN *tok, int index)
} }
return tok; return tok;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const TOKEN *Tokenizer::tokens() const const TOKEN *Tokenizer::tokens() const
@ -455,7 +455,7 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex)
{ {
// Append token.. // Append token..
CurrentToken += c; CurrentToken += c;
if ( c == '\n' ) if ( c == '\n' )
++lineno; ++lineno;
@ -600,7 +600,7 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex)
} }
} }
} }
// Remove "volatile" // Remove "volatile"
while ( TOKEN::Match(_tokens, "volatile") ) while ( TOKEN::Match(_tokens, "volatile") )
{ {
@ -619,20 +619,20 @@ void Tokenizer::tokenizeCode(std::istream &code, const unsigned int FileIndex)
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void Tokenizer::setVarId() void Tokenizer::setVarId()
{ {
// Clear all variable ids // Clear all variable ids
for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
tok->varId( 0 ); tok->varId( 0 );
// Set variable ids.. // Set variable ids..
unsigned int _varId = 0; unsigned int _varId = 0;
for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
{ {
if ( ! TOKEN::Match(tok, "[;{}(] %type% %var%") ) if ( ! TOKEN::Match(tok, "[;{}(] %type% %var%") )
continue; continue;
// Determine name of declared variable.. // Determine name of declared variable..
const char *varname = 0; const char *varname = 0;
TOKEN *tok2 = tok->next(); TOKEN *tok2 = tok->next();
@ -644,7 +644,7 @@ void Tokenizer::setVarId()
break; break;
tok2 = tok2->next(); tok2 = tok2->next();
} }
// Variable declaration found => Set variable ids // Variable declaration found => Set variable ids
if ( TOKEN::Match(tok2, "[;[=]") && varname ) if ( TOKEN::Match(tok2, "[;[=]") && varname )
{ {
@ -669,7 +669,7 @@ void Tokenizer::setVarId()
} }
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Simplify token list // Simplify token list
@ -1059,7 +1059,7 @@ bool Tokenizer::simplifyConditions()
tok->next()->setstr((tok->next()->str() != "0") ? "true" : "false"); tok->next()->setstr((tok->next()->str() != "0") ? "true" : "false");
ret = false; ret = false;
} }
// Reduce "(%num% == %num%)" => "(true)"/"(false)" // Reduce "(%num% == %num%)" => "(true)"/"(false)"
if ( (TOKEN::Match(tok, "&&") || TOKEN::Match(tok, "||") || TOKEN::Match(tok, "(")) && if ( (TOKEN::Match(tok, "&&") || TOKEN::Match(tok, "||") || TOKEN::Match(tok, "(")) &&
TOKEN::Match(tok->tokAt(1), "%num% %any% %num%") && TOKEN::Match(tok->tokAt(1), "%num% %any% %num%") &&
@ -1068,7 +1068,7 @@ bool Tokenizer::simplifyConditions()
double op1 = (strstr(tok->strAt(1), "0x")) ? strtol(tok->strAt(1),0,16) : atof( tok->strAt(1) ); double op1 = (strstr(tok->strAt(1), "0x")) ? strtol(tok->strAt(1),0,16) : atof( tok->strAt(1) );
double op2 = (strstr(tok->strAt(3), "0x")) ? strtol(tok->strAt(3),0,16) : atof( tok->strAt(3) ); double op2 = (strstr(tok->strAt(3), "0x")) ? strtol(tok->strAt(3),0,16) : atof( tok->strAt(3) );
std::string cmp = tok->strAt(2); std::string cmp = tok->strAt(2);
bool result = false; bool result = false;
if ( cmp == "==" ) if ( cmp == "==" )
result = (op1 == op2); result = (op1 == op2);
@ -1084,13 +1084,13 @@ bool Tokenizer::simplifyConditions()
result = (op1 < op2); result = (op1 < op2);
else else
cmp = ""; cmp = "";
if ( ! cmp.empty() ) if ( ! cmp.empty() )
{ {
tok = tok->next(); tok = tok->next();
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
tok->setstr( result ? "true" : "false" ); tok->setstr( result ? "true" : "false" );
ret = false; ret = false;
} }

View File

@ -39,7 +39,7 @@ private:
* Helper function for "tokenize". This recursively parses into included header files. * Helper function for "tokenize". This recursively parses into included header files.
*/ */
void tokenizeCode(std::istream &code, const unsigned int FileIndex=0); void tokenizeCode(std::istream &code, const unsigned int FileIndex=0);
public: public:
Tokenizer(); Tokenizer();
~Tokenizer(); ~Tokenizer();
@ -50,7 +50,7 @@ public:
* @param FileName The filename * @param FileName The filename
*/ */
void tokenize(std::istream &code, const char FileName[]); void tokenize(std::istream &code, const char FileName[]);
/** Set variable id */ /** Set variable id */
void setVarId(); void setVarId();