Fixed template bracket linkage in while loop simplification
Ran AStyle
This commit is contained in:
parent
976966fe81
commit
7f2be2f57c
|
@ -701,15 +701,15 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const
|
||||||
// Declaration in global scope?
|
// Declaration in global scope?
|
||||||
if (tok->scope()->type == Scope::eGlobal)
|
if (tok->scope()->type == Scope::eGlobal)
|
||||||
return;
|
return;
|
||||||
/*
|
/*
|
||||||
{
|
{
|
||||||
const Token *parent = tok->astParent();
|
const Token *parent = tok->astParent();
|
||||||
while (Token::Match(parent, "%var%|::|*|&"))
|
while (Token::Match(parent, "%var%|::|*|&"))
|
||||||
parent = parent->astParent();
|
parent = parent->astParent();
|
||||||
if (parent && !Token::simpleMatch(parent, "="))
|
if (parent && !Token::simpleMatch(parent, "="))
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
// Taking address?
|
// Taking address?
|
||||||
bool addressOf = false;
|
bool addressOf = false;
|
||||||
{
|
{
|
||||||
|
|
|
@ -2960,7 +2960,7 @@ void CheckOther::checkSuspiciousStringCompare()
|
||||||
const std::string varname = varTok->expressionString();
|
const std::string varname = varTok->expressionString();
|
||||||
|
|
||||||
if (litTok->type() == Token::eString) {
|
if (litTok->type() == Token::eString) {
|
||||||
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
|
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
|
||||||
suspiciousStringCompareError(tok, varname);
|
suspiciousStringCompareError(tok, varname);
|
||||||
} else if (litTok->originalName() == "'\\0'" && var && var->isPointer()) {
|
} else if (litTok->originalName() == "'\\0'" && var && var->isPointer()) {
|
||||||
suspiciousStringCompareError_char(tok, varname);
|
suspiciousStringCompareError_char(tok, varname);
|
||||||
|
|
|
@ -5968,18 +5968,20 @@ void Tokenizer::simplifyIfAndWhileAssign()
|
||||||
|
|
||||||
for (tok2 = tok2->next(); tok2 && tok2 != tok; tok2 = tok2->previous()) {
|
for (tok2 = tok2->next(); tok2 && tok2 != tok; tok2 = tok2->previous()) {
|
||||||
tok3->insertToken(tok2->str());
|
tok3->insertToken(tok2->str());
|
||||||
tok3->next()->varId(tok2->varId());
|
|
||||||
|
|
||||||
Token *newTok = tok3->next();
|
Token *newTok = tok3->next();
|
||||||
|
|
||||||
|
newTok->varId(tok2->varId());
|
||||||
newTok->fileIndex(tok2->fileIndex());
|
newTok->fileIndex(tok2->fileIndex());
|
||||||
newTok->linenr(tok2->linenr());
|
newTok->linenr(tok2->linenr());
|
||||||
|
|
||||||
// link() newly tokens manually
|
// link() new tokens manually
|
||||||
if (Token::Match(newTok, "}|)|]")) {
|
if (tok2->link()) {
|
||||||
braces2.push(newTok);
|
if (Token::Match(newTok, "}|)|]|>")) {
|
||||||
} else if (Token::Match(newTok, "{|(|[")) {
|
braces2.push(newTok);
|
||||||
Token::createMutualLinks(newTok, braces2.top());
|
} else {
|
||||||
braces2.pop();
|
Token::createMutualLinks(newTok, braces2.top());
|
||||||
|
braces2.pop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5328,22 +5328,22 @@ private:
|
||||||
"return c == \"42\"[0];}", "test.c", false, true, false, false);
|
"return c == \"42\"[0];}", "test.c", false, true, false, false);
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
// 5639 String literal compared with char buffer in a struct
|
// 5639 String literal compared with char buffer in a struct
|
||||||
check("struct Example {\n"
|
check("struct Example {\n"
|
||||||
" char buffer[200];\n"
|
" char buffer[200];\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"void foo() {\n"
|
"void foo() {\n"
|
||||||
" struct Example example;\n"
|
" struct Example example;\n"
|
||||||
" if (example.buffer == \"test\") ;\n"
|
" if (example.buffer == \"test\") ;\n"
|
||||||
"}\n", "test.cpp", false, true, false, false);
|
"}\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());
|
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"
|
check("struct Example {\n"
|
||||||
" char buffer[200];\n"
|
" char buffer[200];\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"void foo() {\n"
|
"void foo() {\n"
|
||||||
" struct Example example;\n"
|
" struct Example example;\n"
|
||||||
" if (example.buffer == \"test\") ;\n"
|
" if (example.buffer == \"test\") ;\n"
|
||||||
"}\n", "test.c", false, true, false, false);
|
"}\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());
|
ASSERT_EQUALS("[test.c:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -157,6 +157,7 @@ private:
|
||||||
TEST_CASE(whileAssign1);
|
TEST_CASE(whileAssign1);
|
||||||
TEST_CASE(whileAssign2);
|
TEST_CASE(whileAssign2);
|
||||||
TEST_CASE(whileAssign3); // varid
|
TEST_CASE(whileAssign3); // varid
|
||||||
|
TEST_CASE(whileAssign4); // links
|
||||||
TEST_CASE(doWhileAssign); // varid
|
TEST_CASE(doWhileAssign); // varid
|
||||||
TEST_CASE(test_4881); // similar to doWhileAssign (#4911), taken from #4881 with full code
|
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"));
|
"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() {
|
void doWhileAssign() {
|
||||||
ASSERT_EQUALS("; do { a = b ; } while ( a ) ;", simplifyIfAndWhileAssign(";do { } while(a=b);"));
|
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);"));
|
ASSERT_EQUALS("; do { a . a = 0 ; a . b = c ; } while ( a . b ) ;", simplifyIfAndWhileAssign(";do { a.a = 0; } while(a.b=c);"));
|
||||||
|
|
Loading…
Reference in New Issue