diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1bba8bf8d..314ec2d61 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7588,7 +7588,10 @@ bool Tokenizer::isFunctionParameterPassedByValue(const Token *fpar) const // Is this a function call? if (ftok && Token::Match(ftok->tokAt(-2), "[;{}=] %var% (")) { - const std::string functionName(ftok->previous()->str() + " ("); + const std::string functionName(ftok->previous()->str()); + + if (functionName == "return") + return true; // Locate function declaration.. unsigned int indentlevel = 0; @@ -7597,7 +7600,7 @@ bool Tokenizer::isFunctionParameterPassedByValue(const Token *fpar) const ++indentlevel; else if (tok->str() == "}") indentlevel = (indentlevel > 0) ? indentlevel - 1U : 0U; - else if (indentlevel == 0 && tok->isName() && Token::simpleMatch(tok, functionName.c_str())) { + else if (indentlevel == 0 && Token::Match(tok, "%type% (") && tok->str() == functionName) { // Goto parameter tok = tok->tokAt(2); unsigned int par = 1; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 3f4861f88..e282e3fd7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -180,6 +180,7 @@ private: TEST_CASE(simplifyKnownVariablesFloat); // #2454 - float variable TEST_CASE(simplifyKnownVariablesClassMember); // #2815 - value of class member may be changed by function call TEST_CASE(simplifyKnownVariablesFunctionCalls); // Function calls (don't assume pass by reference) + TEST_CASE(simplifyKnownVariablesReturn); // 3500 - return TEST_CASE(simplifyExternC); TEST_CASE(varid1); @@ -2782,6 +2783,14 @@ private: } } + void simplifyKnownVariablesReturn() { + const char code[] = "int a() {" + " int x = 123;" + " return (x);" + "}"; + ASSERT_EQUALS("int a ( ) { return 123 ; }", tokenizeAndStringify(code,true)); + } + void simplifyKnownVariablesClassMember() { // Ticket #2815 {