Skip to content

Commit 9c17bdd

Browse files
committed
Tweak fix for ticket cppcheck-opensource#8297
1 parent 1428759 commit 9c17bdd

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

lib/tokenize.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,24 +3239,30 @@ void Tokenizer::createLinks2()
32393239
// Then this is probably a template instantiation if either "B" or "C" has comparisons
32403240
if (token->tokType() == Token::eLogicalOp && !type.empty() && type.top()->str() == "<") {
32413241
const Token *prev = token->previous();
3242-
while (Token::Match(prev, "%name%|%num%|%str%|%cop%|)|]")) {
3242+
bool foundComparison = false;
3243+
while (Token::Match(prev, "%name%|%num%|%str%|%cop%|)|]") && prev != type.top()) {
32433244
if (prev->str() == ")" || prev->str() == "]")
32443245
prev = prev->link();
3245-
else if (prev->tokType() == Token::eLogicalOp || prev->isComparisonOp())
3246+
else if (prev->tokType() == Token::eLogicalOp)
32463247
break;
3248+
else if (prev->isComparisonOp())
3249+
foundComparison = true;
32473250
prev = prev->previous();
32483251
}
3249-
if (prev && prev != type.top() && prev->isComparisonOp())
3252+
if (prev == type.top() && foundComparison)
32503253
continue;
32513254
const Token *next = token->next();
3252-
while (Token::Match(next, "%name%|%num%|%str%|%cop%|(|[")) {
3255+
foundComparison = false;
3256+
while (Token::Match(next, "%name%|%num%|%str%|%cop%|(|[") && next->str() != ">") {
32533257
if (next->str() == "(" || next->str() == "[")
32543258
next = next->link();
3255-
else if (next->tokType() == Token::eLogicalOp || next->isComparisonOp())
3259+
else if (next->tokType() == Token::eLogicalOp)
32563260
break;
3261+
else if (next->isComparisonOp())
3262+
foundComparison = true;
32573263
next = next->next();
32583264
}
3259-
if (next && next != type.top() && next->isComparisonOp() && next->str() != ">")
3265+
if (next && next->str() == ">" && foundComparison)
32603266
continue;
32613267
}
32623268

test/testtokenize.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,6 +4522,18 @@ class TestTokenizer : public TestFixture {
45224522
ASSERT_EQUALS(true, tok->linkAt(3) == nullptr);
45234523
}
45244524

4525+
{
4526+
// if (a < ... > d) { }
4527+
const char code[] = "if (a < b || c == 3 || d > e);";
4528+
errout.str("");
4529+
Tokenizer tokenizer(&settings0, this);
4530+
std::istringstream istr(code);
4531+
tokenizer.tokenize(istr, "test.cpp");
4532+
const Token *tok = tokenizer.tokens();
4533+
4534+
ASSERT_EQUALS(true, tok->linkAt(3) == nullptr);
4535+
}
4536+
45254537
{
45264538
// template
45274539
const char code[] = "a<b==3 || c> d;";

0 commit comments

Comments
 (0)