diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 3aa5e64e6..9c3c28813 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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 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()]); } } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 5d6715c69..c6bcd068a 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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() {