Fixed #8176 (ValueFlow: variable might be changed if it's passed by reference to method)
This commit is contained in:
parent
5f4b06c0f4
commit
e61222126f
|
@ -408,7 +408,7 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
|
||||||
tok = tok ? tok->previous() : nullptr;
|
tok = tok ? tok->previous() : nullptr;
|
||||||
if (tok && tok->link() && tok->str() == ">")
|
if (tok && tok->link() && tok->str() == ">")
|
||||||
tok = tok->link()->previous();
|
tok = tok->link()->previous();
|
||||||
if (!Token::Match(tok, "%name% ("))
|
if (!Token::Match(tok, "%name% [(<]"))
|
||||||
return false; // not a function => variable not changed
|
return false; // not a function => variable not changed
|
||||||
|
|
||||||
// Constructor call
|
// Constructor call
|
||||||
|
|
|
@ -35,6 +35,7 @@ private:
|
||||||
void run() {
|
void run() {
|
||||||
TEST_CASE(isReturnScope);
|
TEST_CASE(isReturnScope);
|
||||||
TEST_CASE(isVariableChanged);
|
TEST_CASE(isVariableChanged);
|
||||||
|
TEST_CASE(isVariableChangedByFunctionCall);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isReturnScope(const char code[], int offset) {
|
bool isReturnScope(const char code[], int offset) {
|
||||||
|
@ -74,6 +75,28 @@ private:
|
||||||
" if (b) { (int)((INTOF(8))result >> b); }\n"
|
" if (b) { (int)((INTOF(8))result >> b); }\n"
|
||||||
"}", "if", "}"));
|
"}", "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)
|
REGISTER_TEST(TestAstUtils)
|
||||||
|
|
Loading…
Reference in New Issue