Fixed #8176 (ValueFlow: variable might be changed if it's passed by reference to method)

This commit is contained in:
Daniel Marjamäki 2017-09-20 22:09:09 +02:00
parent 5f4b06c0f4
commit e61222126f
2 changed files with 24 additions and 1 deletions

View File

@ -408,7 +408,7 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
tok = tok ? tok->previous() : nullptr;
if (tok && tok->link() && tok->str() == ">")
tok = tok->link()->previous();
if (!Token::Match(tok, "%name% ("))
if (!Token::Match(tok, "%name% [(<]"))
return false; // not a function => variable not changed
// Constructor call

View File

@ -35,6 +35,7 @@ private:
void run() {
TEST_CASE(isReturnScope);
TEST_CASE(isVariableChanged);
TEST_CASE(isVariableChangedByFunctionCall);
}
bool isReturnScope(const char code[], int offset) {
@ -74,6 +75,28 @@ private:
" if (b) { (int)((INTOF(8))result >> b); }\n"
"}", "if", "}"));
}
bool isVariableChangedByFunctionCall(const char code[], const char pattern[], bool *inconclusive) {
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
const Token * const argtok = Token::findmatch(tokenizer.tokens(), pattern);
return ::isVariableChangedByFunctionCall(argtok, &settings, inconclusive);
}
void isVariableChangedByFunctionCall() {
const char *code;
bool inconclusive;
// #8271 - template method
code = "void f(int x) {\n"
" a<int>(x);\n"
"}";
inconclusive = false;
ASSERT_EQUALS(false, isVariableChangedByFunctionCall(code, "x ) ;", &inconclusive));
ASSERT_EQUALS(true, inconclusive);
}
};
REGISTER_TEST(TestAstUtils)