Fixed #4225 (False positive: uninitialized variable (assignemnt in ternary expression with cast))
This commit is contained in:
parent
a64669b1ec
commit
9a462d8a0a
|
@ -4226,6 +4226,14 @@ void Tokenizer::simplifyConditionOperator()
|
|||
|
||||
if (Token::Match(tok, "[{};] *| %var% = %any% ? %any% : %any% ;") ||
|
||||
Token::Match(tok, "[{};] return %any% ? %any% : %any% ;")) {
|
||||
|
||||
// backup varids so they can be set properly
|
||||
std::map<std::string, unsigned int> varid;
|
||||
for (const Token *tok2 = tok->next(); tok2->str() != ";"; tok2 = tok2->next()) {
|
||||
if (tok2->varId())
|
||||
varid[tok2->str()] = tok2->varId();
|
||||
}
|
||||
|
||||
std::string var(tok->next()->str());
|
||||
bool isPointer = false;
|
||||
bool isReturn = false;
|
||||
|
@ -4299,6 +4307,10 @@ void Tokenizer::simplifyConditionOperator()
|
|||
tok->str(value1);
|
||||
else if (tok->str() == "value2")
|
||||
tok->str(value2);
|
||||
|
||||
// set varid.
|
||||
if (varid.find(tok->str()) != varid.end())
|
||||
tok->varId(varid[tok->str()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -162,7 +162,7 @@ private:
|
|||
TEST_CASE(remove_comma);
|
||||
|
||||
// Simplify "?:"
|
||||
TEST_CASE(conditionOperator);
|
||||
TEST_CASE(simplifyConditionOperator);
|
||||
|
||||
// Simplify calculations
|
||||
TEST_CASE(calculations);
|
||||
|
@ -2719,7 +2719,7 @@ private:
|
|||
}
|
||||
}
|
||||
|
||||
void conditionOperator() {
|
||||
void simplifyConditionOperator() {
|
||||
{
|
||||
const char code[] = "; x = a ? b : c;";
|
||||
ASSERT_EQUALS("; if ( a ) { x = b ; } else { x = c ; }", tok(code));
|
||||
|
@ -2832,6 +2832,11 @@ private:
|
|||
ASSERT_EQUALS("; x = * b ;", tok("; x = (false)?*a:*b;"));
|
||||
ASSERT_EQUALS("void f ( ) { return 1 ; }", tok("void f() { char *p=0; return (p==0)?1:2; }"));
|
||||
}
|
||||
|
||||
// 4225 - varid gets lost
|
||||
ASSERT_EQUALS("\n\n##file 0\n"
|
||||
"1: int a@1 ; int b@2 ; int c@3 ; int d@4 ; if ( b@2 ) { a@1 = c@3 ; } else { a@1 = d@4 ; }\n",
|
||||
tokenizeDebugListing("int a, b, c, d; a = b ? (int *)c : d;", true));
|
||||
}
|
||||
|
||||
void calculations() {
|
||||
|
|
Loading…
Reference in New Issue