ValueFlow: Handle string values in valueFlowSubFunction

This commit is contained in:
Daniel Marjamäki 2014-08-04 12:13:15 +02:00
parent ac59485e7e
commit 344016f7ab
2 changed files with 13 additions and 4 deletions

View File

@ -1329,9 +1329,8 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger,
// passing value(s) to function
if (Token::Match(tok, "[(,] %var% [,)]") && !tok->next()->values.empty())
argvalues = tok->next()->values;
else if (Token::Match(tok, "[(,] %num% [,)]") && MathLib::isInt(tok->strAt(1))) {
argvalues.clear();
argvalues.push_back(ValueFlow::Value(MathLib::toLongNumber(tok->next()->str())));
else if (Token::Match(tok, "[(,] %num%|%str% [,)]") && !tok->next()->values.empty()) {
argvalues = tok->next()->values;
} else {
// bool operator => values 1/0 are passed to function..
const Token *op = tok->next();
@ -1364,7 +1363,8 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger,
// Get function argument, and check if parameter is passed by value
const Function * const function = ftok->astOperand1()->function();
const Variable * const arg = function ? function->getArgumentVar(argnr) : nullptr;
if (!Token::Match(arg ? arg->typeStartToken() : nullptr, "%type% %var% ,|)"))
if (!Token::Match(arg ? arg->typeStartToken() : nullptr, "%type% %var% ,|)") &&
!Token::Match(arg ? arg->typeStartToken() : nullptr, "const| struct| %type% * %var% ,|)"))
continue;
// Function scope..

View File

@ -159,12 +159,21 @@ private:
void valueFlowString() {
const char *code;
// valueFlowAfterAssign
code = "const char * f() {\n"
" static const char *x;\n"
" if (a) x = \"123\";\n"
" return x;\n"
"}";
ASSERT_EQUALS(true, testValueOfX(code, 4, "\"123\""));
// valueFlowSubFunction
code = "void dostuff(const char *x) {\n"
" f(x);\n"
"}\n"
"\n"
"void test() { dostuff(\"abc\"); }";
ASSERT_EQUALS(true, testValueOfX(code, 2, "\"abc\""));
}
void valueFlowPointerAlias() {