errmsg: Added message 'function parameter is passed by value'

This commit is contained in:
Daniel Marjamäki 2009-01-12 17:32:53 +00:00
parent df689b454c
commit 13e2396345
4 changed files with 15 additions and 9 deletions

View File

@ -619,9 +619,7 @@ void CheckOther::CheckConstantFunctionParameter()
{ {
if (Token::Match(tok, "[,(] const std :: %type% %var% [,)]")) if (Token::Match(tok, "[,(] const std :: %type% %var% [,)]"))
{ {
std::ostringstream errmsg; _errorLogger->reportErr(ErrorMessage::passedByValue(_tokenizer, tok, tok->strAt(5)));
errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(5) << " is passed by value, it could be passed by reference/pointer instead";
_errorLogger->reportErr(errmsg.str());
} }
else if (Token::Match(tok, "[,(] const %type% %var% [,)]")) else if (Token::Match(tok, "[,(] const %type% %var% [,)]"))
@ -630,9 +628,7 @@ void CheckOther::CheckConstantFunctionParameter()
const std::string pattern(std::string("class|struct ") + tok->strAt(2)); const std::string pattern(std::string("class|struct ") + tok->strAt(2));
if (Token::findmatch(_tokenizer->tokens(), pattern.c_str())) if (Token::findmatch(_tokenizer->tokens(), pattern.c_str()))
{ {
std::ostringstream errmsg; _errorLogger->reportErr(ErrorMessage::passedByValue(_tokenizer, tok, tok->strAt(3)));
errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(3) << " is passed by value, it could be passed by reference/pointer instead";
_errorLogger->reportErr(errmsg.str());
} }
} }
} }

View File

@ -327,6 +327,9 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
if (ErrorMessage::unreachableCode(_settings)) if (ErrorMessage::unreachableCode(_settings))
checkOther.unreachableCode(); checkOther.unreachableCode();
// Check if a constant function parameter is passed by value
if (ErrorMessage::passedByValue(_settings))
checkOther.CheckConstantFunctionParameter();
if (_settings._checkCodingStyle) if (_settings._checkCodingStyle)
@ -334,9 +337,6 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// 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
checkOther.CheckConstantFunctionParameter();
// 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();

View File

@ -237,5 +237,14 @@ public:
return true; return true;
} }
static std::string passedByValue(const Tokenizer *tokenizer, const Token *Location, const std::string &parname)
{
return msg1(tokenizer, Location) + "Function parameter '" + parname + "' is passed by value. It could be passed by reference instead.";
}
static bool passedByValue(const Settings &s)
{
return true;
}
}; };
#endif #endif

View File

@ -85,6 +85,7 @@ int main()
err.push_back(Message("udivWarning", Message::STYLE | Message::ALL, "Warning: Division with signed and unsigned operators")); err.push_back(Message("udivWarning", Message::STYLE | Message::ALL, "Warning: Division with signed and unsigned operators"));
err.push_back(Message("unusedStructMember", Message::STYLE, "struct or union member '%1::%2' is never used", "structname", "varname")); err.push_back(Message("unusedStructMember", Message::STYLE, "struct or union member '%1::%2' is never used", "structname", "varname"));
err.push_back(Message("unreachableCode", 0, "Unreachable code below a 'return'")); err.push_back(Message("unreachableCode", 0, "Unreachable code below a 'return'"));
err.push_back(Message("passedByValue", 0, "Function parameter '%1' is passed by value. It could be passed by reference instead.", "parname"));
// Generate code.. // Generate code..
std::cout << "Generate code.." << std::endl; std::cout << "Generate code.." << std::endl;