From 13e23963454188f5dd814d3bd3740200f6e77cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 12 Jan 2009 17:32:53 +0000 Subject: [PATCH] errmsg: Added message 'function parameter is passed by value' --- src/checkother.cpp | 8 ++------ src/cppcheck.cpp | 6 +++--- src/errormessage.h | 9 +++++++++ tools/errmsg.cpp | 1 + 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index c6f085a51..21b073d2b 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -619,9 +619,7 @@ void CheckOther::CheckConstantFunctionParameter() { if (Token::Match(tok, "[,(] const std :: %type% %var% [,)]")) { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(5) << " is passed by value, it could be passed by reference/pointer instead"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr(ErrorMessage::passedByValue(_tokenizer, tok, tok->strAt(5))); } 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)); if (Token::findmatch(_tokenizer->tokens(), pattern.c_str())) { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(tok) << " " << tok->strAt(3) << " is passed by value, it could be passed by reference/pointer instead"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr(ErrorMessage::passedByValue(_tokenizer, tok, tok->strAt(3))); } } } diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 8679826bf..67771aa96 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -327,6 +327,9 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) if (ErrorMessage::unreachableCode(_settings)) checkOther.unreachableCode(); + // Check if a constant function parameter is passed by value + if (ErrorMessage::passedByValue(_settings)) + checkOther.CheckConstantFunctionParameter(); 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) //CheckVariableScope(); - // Check if a constant function parameter is passed by value - checkOther.CheckConstantFunctionParameter(); - // Check for various types of incomplete statements that could for example // mean that an ';' has been added by accident checkOther.CheckIncompleteStatement(); diff --git a/src/errormessage.h b/src/errormessage.h index d75330b5e..c04f5b738 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -237,5 +237,14 @@ public: 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 diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index edf59b10c..394d0db4c 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -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("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("passedByValue", 0, "Function parameter '%1' is passed by value. It could be passed by reference instead.", "parname")); // Generate code.. std::cout << "Generate code.." << std::endl;