Fixed template bracket linkage in while loop simplification

Ran AStyle
This commit is contained in:
PKEuS 2014-08-23 12:28:54 +02:00
parent 976966fe81
commit 7f2be2f57c
5 changed files with 53 additions and 33 deletions

View File

@ -701,15 +701,15 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const
// Declaration in global scope?
if (tok->scope()->type == Scope::eGlobal)
return;
/*
{
const Token *parent = tok->astParent();
while (Token::Match(parent, "%var%|::|*|&"))
parent = parent->astParent();
if (parent && !Token::simpleMatch(parent, "="))
return;
}
*/
/*
{
const Token *parent = tok->astParent();
while (Token::Match(parent, "%var%|::|*|&"))
parent = parent->astParent();
if (parent && !Token::simpleMatch(parent, "="))
return;
}
*/
// Taking address?
bool addressOf = false;
{

View File

@ -2960,7 +2960,7 @@ void CheckOther::checkSuspiciousStringCompare()
const std::string varname = varTok->expressionString();
if (litTok->type() == Token::eString) {
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
suspiciousStringCompareError(tok, varname);
} else if (litTok->originalName() == "'\\0'" && var && var->isPointer()) {
suspiciousStringCompareError_char(tok, varname);

View File

@ -5968,18 +5968,20 @@ void Tokenizer::simplifyIfAndWhileAssign()
for (tok2 = tok2->next(); tok2 && tok2 != tok; tok2 = tok2->previous()) {
tok3->insertToken(tok2->str());
tok3->next()->varId(tok2->varId());
Token *newTok = tok3->next();
newTok->varId(tok2->varId());
newTok->fileIndex(tok2->fileIndex());
newTok->linenr(tok2->linenr());
// link() newly tokens manually
if (Token::Match(newTok, "}|)|]")) {
braces2.push(newTok);
} else if (Token::Match(newTok, "{|(|[")) {
Token::createMutualLinks(newTok, braces2.top());
braces2.pop();
// link() new tokens manually
if (tok2->link()) {
if (Token::Match(newTok, "}|)|]|>")) {
braces2.push(newTok);
} else {
Token::createMutualLinks(newTok, braces2.top());
braces2.pop();
}
}
}
}

View File

@ -5328,22 +5328,22 @@ private:
"return c == \"42\"[0];}", "test.c", false, true, false, false);
ASSERT_EQUALS("", errout.str());
// 5639 String literal compared with char buffer in a struct
check("struct Example {\n"
" char buffer[200];\n"
"};\n"
"void foo() {\n"
" struct Example example;\n"
" if (example.buffer == \"test\") ;\n"
"}\n", "test.cpp", false, true, false, false);
// 5639 String literal compared with char buffer in a struct
check("struct Example {\n"
" char buffer[200];\n"
"};\n"
"void foo() {\n"
" struct Example example;\n"
" if (example.buffer == \"test\") ;\n"
"}\n", "test.cpp", false, true, false, false);
ASSERT_EQUALS("[test.cpp:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str());
check("struct Example {\n"
" char buffer[200];\n"
"};\n"
"void foo() {\n"
" struct Example example;\n"
" if (example.buffer == \"test\") ;\n"
"}\n", "test.c", false, true, false, false);
check("struct Example {\n"
" char buffer[200];\n"
"};\n"
"void foo() {\n"
" struct Example example;\n"
" if (example.buffer == \"test\") ;\n"
"}\n", "test.c", false, true, false, false);
ASSERT_EQUALS("[test.c:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str());
}

View File

@ -157,6 +157,7 @@ private:
TEST_CASE(whileAssign1);
TEST_CASE(whileAssign2);
TEST_CASE(whileAssign3); // varid
TEST_CASE(whileAssign4); // links
TEST_CASE(doWhileAssign); // varid
TEST_CASE(test_4881); // similar to doWhileAssign (#4911), taken from #4881 with full code
@ -2727,6 +2728,23 @@ private:
"4: }\n", tokenizeDebugListing(code, true, "test.c"));
}
void whileAssign4() {
errout.str("");
Settings settings;
Tokenizer tokenizer(&settings, this);
std::istringstream istr("; while (!(m = q->push<Message>(x))) {}");
tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList2();
ASSERT_EQUALS("; m = q . push < Message > ( x ) ; while ( ! m ) { m = q . push < Message > ( x ) ; }", tokenizer.tokens()->stringifyList(0, false));
ASSERT(tokenizer.tokens()->tokAt(26) != nullptr);
if (tokenizer.tokens()->tokAt(26)) {
ASSERT(tokenizer.tokens()->tokAt(6)->link() == tokenizer.tokens()->tokAt(8));
ASSERT(tokenizer.tokens()->tokAt(24)->link() == tokenizer.tokens()->tokAt(26));
}
}
void doWhileAssign() {
ASSERT_EQUALS("; do { a = b ; } while ( a ) ;", simplifyIfAndWhileAssign(";do { } while(a=b);"));
ASSERT_EQUALS("; do { a . a = 0 ; a . b = c ; } while ( a . b ) ;", simplifyIfAndWhileAssign(";do { a.a = 0; } while(a.b=c);"));