Fixed #4019 (false positive: (style) Variable 'dest' is assigned a value that is never used)

This commit is contained in:
Daniel Marjamäki 2012-08-17 16:37:25 +02:00
parent 1601baa974
commit 6a5cc4727d
2 changed files with 32 additions and 2 deletions

View File

@ -4156,7 +4156,7 @@ void Tokenizer::simplifyConditionOperator()
bool isReturn = false;
if (tok->next()->str() == "*") {
tok = tok->next();
var += " " + tok->next()->str();
var = tok->next()->str();
isPointer = true;
} else if (tok->next()->str() == "return") {
isReturn = true;
@ -4189,10 +4189,17 @@ void Tokenizer::simplifyConditionOperator()
if (isReturn)
str = "if ( condition ) { return value1 ; } return value2 ;";
else
str = "if ( condition ) { var = value1 ; } else { var = value2 ; }";
str = "if ( condition ) { * var = value1 ; } else { * var = value2 ; }";
std::string::size_type pos1 = 0;
while (pos1 != std::string::npos) {
if (str[pos1] == '*') {
pos1 += 2;
if (isPointer) {
tok->insertToken("*");
tok = tok->next();
}
}
std::string::size_type pos2 = str.find(" ", pos1);
if (pos2 == std::string::npos) {
tok->insertToken(str.substr(pos1));

View File

@ -457,6 +457,21 @@ private:
return tokenizer.tokens()->stringifyList(0, false);
}
std::string tokenizeDebugListing(const std::string &code, bool simplify = false, const char filename[] = "test.cpp") {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, filename);
if (simplify)
tokenizer.simplifyTokenList();
// result..
return tokenizer.tokens()->stringifyList(true);
}
void simplifyTokenList1() {
// #1717 : The simplifyErrNoInWhile needs to be used before simplifyIfAssign..
@ -2769,6 +2784,14 @@ private:
}
// Ticket #3572 (segmentation fault)
ASSERT_EQUALS("0 ; x = { ? y : z ; }", tok("0; x = { ? y : z; }"));
{
// #4019 - varid
const char code[] = "; char *p; *p = a ? 1 : 0;";
const char expected[] = "\n\n##file 0\n"
"1: ; char * p@1 ; if ( a ) { * p@1 = 1 ; } else { * p@1 = 0 ; }\n";
ASSERT_EQUALS(expected, tokenizeDebugListing(code, true));
}
}
void calculations() {