CheckConstantFunctionParameters: Updated the check to handle all std::.. struct and class

This commit is contained in:
Daniel Marjamäki 2008-05-03 09:44:20 +00:00
parent cae2e190da
commit a4486fb883
2 changed files with 47 additions and 7 deletions

View File

@ -704,12 +704,32 @@ void CheckConstantFunctionParameter()
{
for (const TOKEN *tok = tokens; tok; tok = tok->next)
{
if ( Match(tok,"[,(] const std :: string %var% [,)]") )
if ( Match(tok,"[,(] const std :: %type% %var% [,)]") )
{
std::ostringstream errmsg;
errmsg << FileLine(tok) << " looks like a constant function parameter that is passed by value";
errmsg << FileLine(tok) << " " << getstr(tok,5) << " is passed by value, it could be passed by reference/pointer instead";
ReportErr( errmsg.str() );
}
else if ( Match(tok,"[,(] const %type% %var% [,)]") )
{
// Check if type is a struct or class.
const char *pattern[3] = {"class","type",0};
pattern[1] = getstr(tok, 2);
if ( findtoken(tokens, pattern) )
{
std::ostringstream errmsg;
errmsg << FileLine(tok) << " " << getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
ReportErr( errmsg.str() );
}
pattern[0] = "struct";
if ( findtoken(tokens, pattern) )
{
std::ostringstream errmsg;
errmsg << FileLine(tok) << " " << getstr(tok,3) << " is passed by value, it could be passed by reference/pointer instead";
ReportErr( errmsg.str() );
}
}
}
}

View File

@ -28,6 +28,7 @@ static void memleak_in_function();
static void memleak_in_class();
static void division();
static void unused_variable();
static void fpar_byvalue();
//---------------------------------------------------------------------------
int main()
@ -59,6 +60,8 @@ int main()
// unused variable..
unused_variable();
fpar_byvalue();
std::cout << "Success Rate: "
<< SuccessCount
<< " / "
@ -875,7 +878,7 @@ static void unused_variable()
const char test10[] = "static void f()\n"
"{\n"
" TPoint p1;\n"
@ -885,9 +888,26 @@ static void unused_variable()
" }\n"
"}\n";
check( CheckVariableScope, __LINE__, test10, "" );
}
//---------------------------------------------------------------------------
static void fpar_byvalue()
{
check( CheckConstantFunctionParameter,
__LINE__,
"void f(const std::string str);",
"[test.cpp:1] str is passed by value, it could be passed by reference/pointer instead\n" );
check( CheckConstantFunctionParameter,
__LINE__,
"void f(const int a, const std::vector v, const int b);",
"[test.cpp:1] v is passed by value, it could be passed by reference/pointer instead\n" );
check( CheckConstantFunctionParameter,
__LINE__,
"class Fred;\n"
"void f(const Fred f);",
"[test.cpp:2] f is passed by value, it could be passed by reference/pointer instead\n" );
}
//---------------------------------------------------------------------------