Formatting: uniformize end of lines.

This commit is contained in:
Nicolas Le Cam 2008-12-14 17:05:21 +00:00
parent 8882ff9509
commit fec777057d
5 changed files with 363 additions and 363 deletions

View File

@ -189,26 +189,26 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetDeallocationType( const
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const TOKEN *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype ) const char * CheckMemoryLeakClass::call_func( const TOKEN *tok, std::list<const TOKEN *> callstack, const char *varnames[], AllocType &alloctype, AllocType &dealloctype )
{ {
// Keywords that are not function calls.. // Keywords that are not function calls..
if (TOKEN::Match(tok,"if|for|while")) if (TOKEN::Match(tok,"if|for|while"))
return 0;
// String functions that are not allocating nor deallocating memory..
if (TOKEN::Match(tok, "strcpy|strncpy|strcat|strncat|strcmp|strncmp|strcasecmp|stricmp|sprintf|strchr|strrchr|strstr"))
return 0;
// Memory functions that are not allocating nor deallocating memory..
if (TOKEN::Match(tok, "memset|memcpy|memmove|memchr"))
return 0;
// I/O functions that are not allocating nor deallocating memory..
if (TOKEN::Match(tok, "fgets|fgetc|fputs|fputc|printf"))
return 0; return 0;
// Convert functions that are not allocating nor deallocating memory.. // String functions that are not allocating nor deallocating memory..
if (TOKEN::Match(tok, "atoi|atof|atol|strtol|strtoul|strtod")) if (TOKEN::Match(tok, "strcpy|strncpy|strcat|strncat|strcmp|strncmp|strcasecmp|stricmp|sprintf|strchr|strrchr|strstr"))
return 0; return 0;
// Memory functions that are not allocating nor deallocating memory..
if (TOKEN::Match(tok, "memset|memcpy|memmove|memchr"))
return 0;
// I/O functions that are not allocating nor deallocating memory..
if (TOKEN::Match(tok, "fgets|fgetc|fputs|fputc|printf"))
return 0;
// Convert functions that are not allocating nor deallocating memory..
if (TOKEN::Match(tok, "atoi|atof|atol|strtol|strtoul|strtod"))
return 0;
if (GetAllocationType(tok)!=No || GetReallocationType(tok)!=No || GetDeallocationType(tok,varnames)!=No) if (GetAllocationType(tok)!=No || GetReallocationType(tok)!=No || GetDeallocationType(tok,varnames)!=No)
return 0; return 0;

View File

@ -1,61 +1,61 @@
/* /*
* c++check - c/c++ syntax checking * c++check - c/c++ syntax checking
* Copyright (C) 2008 Daniel Marjamäki and Reijo Tomperi * Copyright (C) 2008 Daniel Marjamäki and Reijo Tomperi
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* 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 <cstring> #include <cstring>
#include "testsuite.h" #include "testsuite.h"
#include "token.h" #include "token.h"
extern std::ostringstream errout; extern std::ostringstream errout;
class TestTOKEN : public TestFixture class TestTOKEN : public TestFixture
{ {
public: public:
TestTOKEN() : TestFixture("TestTOKEN") TestTOKEN() : TestFixture("TestTOKEN")
{ } { }
private: private:
void run() void run()
{ {
TEST_CASE( nextprevious ); TEST_CASE( nextprevious );
} }
void nextprevious() void nextprevious()
{ {
TOKEN *token = new TOKEN; TOKEN *token = new TOKEN;
token->setstr( "1" ); token->setstr( "1" );
token->insertToken( "2" ); token->insertToken( "2" );
token->next()->insertToken( "3" ); token->next()->insertToken( "3" );
TOKEN *last = token->next()->next(); TOKEN *last = token->next()->next();
ASSERT_EQUALS( token->str(), "1" ); ASSERT_EQUALS( token->str(), "1" );
ASSERT_EQUALS( token->next()->str(), "2" ); ASSERT_EQUALS( token->next()->str(), "2" );
ASSERT_EQUALS( token->next()->next()->str(), "3" ); ASSERT_EQUALS( token->next()->next()->str(), "3" );
if( last->next() ) if( last->next() )
ASSERT_EQUALS( "Null was expected", "" ); ASSERT_EQUALS( "Null was expected", "" );
ASSERT_EQUALS( last->str(), "3" ); ASSERT_EQUALS( last->str(), "3" );
ASSERT_EQUALS( last->previous()->str(), "2" ); ASSERT_EQUALS( last->previous()->str(), "2" );
ASSERT_EQUALS( last->previous()->previous()->str(), "1" ); ASSERT_EQUALS( last->previous()->previous()->str(), "1" );
if( token->previous() ) if( token->previous() )
ASSERT_EQUALS( "Null was expected", "" ); ASSERT_EQUALS( "Null was expected", "" );
} }
}; };
REGISTER_TEST( TestTOKEN ) REGISTER_TEST( TestTOKEN )

View File

@ -46,8 +46,8 @@ private:
TEST_CASE( const_and_volatile_functions ); TEST_CASE( const_and_volatile_functions );
TEST_CASE( numeric_true_condition ); TEST_CASE( numeric_true_condition );
TEST_CASE( simplify_known_variables ); TEST_CASE( simplify_known_variables );
TEST_CASE( multi_compare ); TEST_CASE( multi_compare );
@ -218,119 +218,119 @@ private:
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 simplify_known_variables() void simplify_known_variables()
{ {
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
" int a = 10;\n" " int a = 10;\n"
" if (a);\n" " if (a);\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();
tokenizer.simplifyKnownVariables(); tokenizer.simplifyKnownVariables();
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 ( ) { int a = 10 ; if ( 10 ) ; }"), ostr.str() ); ASSERT_EQUALS( std::string(" void f ( ) { int a = 10 ; if ( 10 ) ; }"), ostr.str() );
} }
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
" int a = 10;\n" " int a = 10;\n"
" a = g();\n" " a = g();\n"
" if (a);\n" " if (a);\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();
tokenizer.simplifyKnownVariables(); tokenizer.simplifyKnownVariables();
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 ( ) { int a = 10 ; a = g ( ) ; if ( a ) ; }"), ostr.str() ); ASSERT_EQUALS( std::string(" void f ( ) { int a = 10 ; a = g ( ) ; if ( a ) ; }"), ostr.str() );
} }
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
" int a = 4;\n" " int a = 4;\n"
" while(true){\n" " while(true){\n"
" break;\n" " break;\n"
" a = 10;\n" " a = 10;\n"
" }\n" " }\n"
" if (a);\n" " if (a);\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();
tokenizer.simplifyKnownVariables(); tokenizer.simplifyKnownVariables();
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 ( ) { int a = 4 ; while ( true ) { break ; a = 10 ; } if ( a ) ; }"), ostr.str() ); ASSERT_EQUALS( std::string(" void f ( ) { int a = 4 ; while ( true ) { break ; a = 10 ; } if ( a ) ; }"), ostr.str() );
} }
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
" int a = 4;\n" " int a = 4;\n"
" if ( g(a));\n" " if ( g(a));\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();
tokenizer.simplifyKnownVariables(); tokenizer.simplifyKnownVariables();
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 ( ) { int a = 4 ; if ( g ( a ) ) ; }"), ostr.str() ); ASSERT_EQUALS( std::string(" void f ( ) { int a = 4 ; if ( g ( a ) ) ; }"), ostr.str() );
} }
{ {
const char code[] = "void f()\n" const char code[] = "void f()\n"
"{\n" "{\n"
" int a = 4;\n" " int a = 4;\n"
" if ( a = 5 );\n" " if ( a = 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.setVarId(); tokenizer.setVarId();
tokenizer.simplifyKnownVariables(); tokenizer.simplifyKnownVariables();
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 ( ) { int a = 4 ; if ( a = 5 ) ; }"), ostr.str() ); ASSERT_EQUALS( std::string(" void f ( ) { int a = 4 ; if ( a = 5 ) ; }"), ostr.str() );
} }
} }
void multi_compare() void multi_compare()
{ {

View File

@ -1011,19 +1011,19 @@ void Tokenizer::simplifyTokenList()
} }
} }
bool done = false; bool done = false;
while ( ! done ) while ( ! done )
{ {
done = true; done = true;
if( simplifyConditions() ) if( simplifyConditions() )
done = false; done = false;
if( simplifyCasts() ) if( simplifyCasts() )
done = false; done = false;
if( simplifyFunctionReturn() ) if( simplifyFunctionReturn() )
done = false; done = false;
} }
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@ -1099,139 +1099,139 @@ bool Tokenizer::simplifyConditions()
return ret; return ret;
} }
bool Tokenizer::simplifyCasts() bool Tokenizer::simplifyCasts()
{ {
bool ret = false; bool ret = false;
for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
{ {
if ( TOKEN::Match(tok->next(), "( %type% * )") ) if ( TOKEN::Match(tok->next(), "( %type% * )") )
{ {
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
ret = true; ret = true;
} }
else if ( TOKEN::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <" ) ) else if ( TOKEN::Match(tok->next(), "dynamic_cast|reinterpret_cast|const_cast|static_cast <" ) )
{ {
while ( tok->next() && tok->next()->str() != ">" ) while ( tok->next() && tok->next()->str() != ">" )
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
tok->deleteNext(); tok->deleteNext();
TOKEN *tok2 = tok; TOKEN *tok2 = tok;
int parlevel = 0; int parlevel = 0;
while ( tok2->next() && parlevel >= 0 ) while ( tok2->next() && parlevel >= 0 )
{ {
tok2 = tok2->next(); tok2 = tok2->next();
if ( TOKEN::Match(tok2->next(), "(") ) if ( TOKEN::Match(tok2->next(), "(") )
++parlevel; ++parlevel;
else if ( TOKEN::Match(tok2->next(), ")") ) else if ( TOKEN::Match(tok2->next(), ")") )
--parlevel; --parlevel;
} }
if (tok2->next()) if (tok2->next())
tok2->deleteNext(); tok2->deleteNext();
ret = true; ret = true;
} }
} }
return ret; return ret;
} }
bool Tokenizer::simplifyFunctionReturn() bool Tokenizer::simplifyFunctionReturn()
{ {
bool ret = false; bool ret = false;
int indentlevel = 0; int indentlevel = 0;
for ( const TOKEN *tok = tokens(); tok; tok = tok->next() ) for ( const TOKEN *tok = tokens(); tok; tok = tok->next() )
{ {
if ( tok->str() == "{" ) if ( tok->str() == "{" )
++indentlevel; ++indentlevel;
else if ( tok->str() == "}" ) else if ( tok->str() == "}" )
--indentlevel; --indentlevel;
else if ( indentlevel == 0 && TOKEN::Match(tok, "%var% ( ) { return %num% ; }") ) else if ( indentlevel == 0 && TOKEN::Match(tok, "%var% ( ) { return %num% ; }") )
{ {
std::ostringstream pattern; std::ostringstream pattern;
pattern << "[(=+-*/] " << tok->str() << " ( ) [;)+-*/]"; pattern << "[(=+-*/] " << tok->str() << " ( ) [;)+-*/]";
for ( TOKEN *tok2 = _tokens; tok2; tok2 = tok2->next() ) for ( TOKEN *tok2 = _tokens; tok2; tok2 = tok2->next() )
{ {
if ( TOKEN::Match(tok2, pattern.str().c_str()) ) if ( TOKEN::Match(tok2, pattern.str().c_str()) )
{ {
tok2 = tok2->next(); tok2 = tok2->next();
tok2->setstr( tok->strAt(5) ); tok2->setstr( tok->strAt(5) );
tok2->deleteNext(); tok2->deleteNext();
tok2->deleteNext(); tok2->deleteNext();
ret = true; ret = true;
} }
} }
} }
} }
return ret; return ret;
} }
bool Tokenizer::simplifyKnownVariables() bool Tokenizer::simplifyKnownVariables()
{ {
// TODO, this function should be called from simplifyTokenList() // TODO, this function should be called from simplifyTokenList()
// after the implementation is done. // after the implementation is done.
// TODO, this functions needs to be implemented. // TODO, this functions needs to be implemented.
// TODO, test // TODO, test
bool ret = false; bool ret = false;
for ( TOKEN *tok = _tokens; tok; tok = tok->next() ) for ( TOKEN *tok = _tokens; tok; tok = tok->next() )
{ {
// Search for a block of code // Search for a block of code
if ( ! TOKEN::Match(tok, ") const| {") ) if ( ! TOKEN::Match(tok, ") const| {") )
continue; continue;
// parse the block of code.. // parse the block of code..
int indentlevel = 0; int indentlevel = 0;
for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() ) for ( TOKEN *tok2 = tok; tok2; tok2 = tok2->next() )
{ {
if ( tok2->str() == "{" ) if ( tok2->str() == "{" )
++indentlevel; ++indentlevel;
else if ( tok2->str() == "}" ) else if ( tok2->str() == "}" )
{ {
--indentlevel; --indentlevel;
if ( indentlevel <= 0 ) if ( indentlevel <= 0 )
continue; continue;
} }
else if ( TOKEN::Match(tok2, "%var% = %num% ;") ) else if ( TOKEN::Match(tok2, "%var% = %num% ;") )
{ {
unsigned int varid = tok2->varId(); unsigned int varid = tok2->varId();
TOKEN *tok3 = tok2; TOKEN *tok3 = tok2;
while ( tok3 ) while ( tok3 )
{ {
tok3 = tok3->next(); tok3 = tok3->next();
// Perhaps it's a loop => bail out // Perhaps it's a loop => bail out
if ( TOKEN::Match(tok3, "[{}]") ) if ( TOKEN::Match(tok3, "[{}]") )
break; break;
// Variable is used somehow in a non-defined pattern => bail out // Variable is used somehow in a non-defined pattern => bail out
if ( tok3->varId() == varid ) if ( tok3->varId() == varid )
break; break;
// Replace variable with numeric constant.. // Replace variable with numeric constant..
if ( TOKEN::Match(tok3, "if ( %varid% )", 0, 0, varid) ) if ( TOKEN::Match(tok3, "if ( %varid% )", 0, 0, varid) )
{ {
tok3 = tok3->next()->next(); tok3 = tok3->next()->next();
tok3->setstr( tok2->strAt(2) ); tok3->setstr( tok2->strAt(2) );
ret = true; ret = true;
} }
} }
} }
} }
} }
return ret; return ret;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Helper functions for handling the tokens list // Helper functions for handling the tokens list

View File

@ -78,7 +78,7 @@ public:
void fillFunctionList(); void fillFunctionList();
const TOKEN *GetFunctionTokenByName( const char funcname[] ) const; const TOKEN *GetFunctionTokenByName( const char funcname[] ) const;
const TOKEN *tokens() const; const TOKEN *tokens() const;
#ifndef UNIT_TESTING #ifndef UNIT_TESTING
private: private:
@ -94,34 +94,34 @@ private:
void Define(const char Name[], const char Value[]); void Define(const char Name[], const char Value[]);
void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno); void addtoken(const char str[], const unsigned int lineno, const unsigned int fileno);
/** Simplify conditions /** Simplify conditions
* @return true if something is modified * @return true if something is modified
* false if nothing is done. * false if nothing is done.
*/
bool simplifyConditions();
/** Simplify casts
* @return true if something is modified
* false if nothing is done.
*/ */
bool simplifyConditions();
/** Simplify casts
* @return true if something is modified
* false if nothing is done.
*/
bool simplifyCasts(); bool simplifyCasts();
/** Simplify function calls - constant return value /** Simplify function calls - constant return value
* @return true if something is modified * @return true if something is modified
* false if nothing is done. * false if nothing is done.
*/ */
bool simplifyFunctionReturn(); bool simplifyFunctionReturn();
/** /**
* A simplify function that replaces a variable with its value in cases * A simplify function that replaces a variable with its value in cases
* when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)" * when the value is known. e.g. "x=10; if(x)" => "x=10;if(10)"
* *
* @param token The token list to check and modify. * @param token The token list to check and modify.
* @return true if modifications to token-list are done. * @return true if modifications to token-list are done.
* false if no modifications are done. * false if no modifications are done.
*/ */
bool simplifyKnownVariables(); bool simplifyKnownVariables();
TOKEN *_gettok(TOKEN *tok, int index); TOKEN *_gettok(TOKEN *tok, int index);