Skip to content

Commit 7f2be2f

Browse files
committed
Fixed template bracket linkage in while loop simplification
Ran AStyle
1 parent 976966f commit 7f2be2f

5 files changed

Lines changed: 53 additions & 33 deletions

File tree

lib/checkbufferoverrun.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -701,15 +701,15 @@ void CheckBufferOverrun::valueFlowCheckArrayIndex(const Token * const tok, const
701701
// Declaration in global scope?
702702
if (tok->scope()->type == Scope::eGlobal)
703703
return;
704-
/*
705-
{
706-
const Token *parent = tok->astParent();
707-
while (Token::Match(parent, "%var%|::|*|&"))
708-
parent = parent->astParent();
709-
if (parent && !Token::simpleMatch(parent, "="))
710-
return;
711-
}
712-
*/
704+
/*
705+
{
706+
const Token *parent = tok->astParent();
707+
while (Token::Match(parent, "%var%|::|*|&"))
708+
parent = parent->astParent();
709+
if (parent && !Token::simpleMatch(parent, "="))
710+
return;
711+
}
712+
*/
713713
// Taking address?
714714
bool addressOf = false;
715715
{

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,7 @@ void CheckOther::checkSuspiciousStringCompare()
29602960
const std::string varname = varTok->expressionString();
29612961

29622962
if (litTok->type() == Token::eString) {
2963-
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
2963+
if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
29642964
suspiciousStringCompareError(tok, varname);
29652965
} else if (litTok->originalName() == "'\\0'" && var && var->isPointer()) {
29662966
suspiciousStringCompareError_char(tok, varname);

lib/tokenize.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5968,18 +5968,20 @@ void Tokenizer::simplifyIfAndWhileAssign()
59685968

59695969
for (tok2 = tok2->next(); tok2 && tok2 != tok; tok2 = tok2->previous()) {
59705970
tok3->insertToken(tok2->str());
5971-
tok3->next()->varId(tok2->varId());
5972-
59735971
Token *newTok = tok3->next();
5972+
5973+
newTok->varId(tok2->varId());
59745974
newTok->fileIndex(tok2->fileIndex());
59755975
newTok->linenr(tok2->linenr());
59765976

5977-
// link() newly tokens manually
5978-
if (Token::Match(newTok, "}|)|]")) {
5979-
braces2.push(newTok);
5980-
} else if (Token::Match(newTok, "{|(|[")) {
5981-
Token::createMutualLinks(newTok, braces2.top());
5982-
braces2.pop();
5977+
// link() new tokens manually
5978+
if (tok2->link()) {
5979+
if (Token::Match(newTok, "}|)|]|>")) {
5980+
braces2.push(newTok);
5981+
} else {
5982+
Token::createMutualLinks(newTok, braces2.top());
5983+
braces2.pop();
5984+
}
59835985
}
59845986
}
59855987
}

test/testother.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5328,22 +5328,22 @@ class TestOther : public TestFixture {
53285328
"return c == \"42\"[0];}", "test.c", false, true, false, false);
53295329
ASSERT_EQUALS("", errout.str());
53305330

5331-
// 5639 String literal compared with char buffer in a struct
5332-
check("struct Example {\n"
5333-
" char buffer[200];\n"
5334-
"};\n"
5335-
"void foo() {\n"
5336-
" struct Example example;\n"
5337-
" if (example.buffer == \"test\") ;\n"
5338-
"}\n", "test.cpp", false, true, false, false);
5331+
// 5639 String literal compared with char buffer in a struct
5332+
check("struct Example {\n"
5333+
" char buffer[200];\n"
5334+
"};\n"
5335+
"void foo() {\n"
5336+
" struct Example example;\n"
5337+
" if (example.buffer == \"test\") ;\n"
5338+
"}\n", "test.cpp", false, true, false, false);
53395339
ASSERT_EQUALS("[test.cpp:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str());
5340-
check("struct Example {\n"
5341-
" char buffer[200];\n"
5342-
"};\n"
5343-
"void foo() {\n"
5344-
" struct Example example;\n"
5345-
" if (example.buffer == \"test\") ;\n"
5346-
"}\n", "test.c", false, true, false, false);
5340+
check("struct Example {\n"
5341+
" char buffer[200];\n"
5342+
"};\n"
5343+
"void foo() {\n"
5344+
" struct Example example;\n"
5345+
" if (example.buffer == \"test\") ;\n"
5346+
"}\n", "test.c", false, true, false, false);
53475347
ASSERT_EQUALS("[test.c:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str());
53485348
}
53495349

test/testsimplifytokens.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class TestSimplifyTokens : public TestFixture {
157157
TEST_CASE(whileAssign1);
158158
TEST_CASE(whileAssign2);
159159
TEST_CASE(whileAssign3); // varid
160+
TEST_CASE(whileAssign4); // links
160161
TEST_CASE(doWhileAssign); // varid
161162
TEST_CASE(test_4881); // similar to doWhileAssign (#4911), taken from #4881 with full code
162163

@@ -2727,6 +2728,23 @@ class TestSimplifyTokens : public TestFixture {
27272728
"4: }\n", tokenizeDebugListing(code, true, "test.c"));
27282729
}
27292730

2731+
void whileAssign4() {
2732+
errout.str("");
2733+
2734+
Settings settings;
2735+
Tokenizer tokenizer(&settings, this);
2736+
std::istringstream istr("; while (!(m = q->push<Message>(x))) {}");
2737+
tokenizer.tokenize(istr, "test.cpp");
2738+
tokenizer.simplifyTokenList2();
2739+
2740+
ASSERT_EQUALS("; m = q . push < Message > ( x ) ; while ( ! m ) { m = q . push < Message > ( x ) ; }", tokenizer.tokens()->stringifyList(0, false));
2741+
ASSERT(tokenizer.tokens()->tokAt(26) != nullptr);
2742+
if (tokenizer.tokens()->tokAt(26)) {
2743+
ASSERT(tokenizer.tokens()->tokAt(6)->link() == tokenizer.tokens()->tokAt(8));
2744+
ASSERT(tokenizer.tokens()->tokAt(24)->link() == tokenizer.tokens()->tokAt(26));
2745+
}
2746+
}
2747+
27302748
void doWhileAssign() {
27312749
ASSERT_EQUALS("; do { a = b ; } while ( a ) ;", simplifyIfAndWhileAssign(";do { } while(a=b);"));
27322750
ASSERT_EQUALS("; do { a . a = 0 ; a . b = c ; } while ( a . b ) ;", simplifyIfAndWhileAssign(";do { a.a = 0; } while(a.b=c);"));

0 commit comments

Comments
 (0)