Skip to content

Commit 03faa25

Browse files
committed
Conditions: Better handling of function calls
1 parent c2f0828 commit 03faa25

3 files changed

Lines changed: 35 additions & 8 deletions

File tree

lib/astutils.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,23 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
164164
if (tok1->isSigned() != tok2->isSigned())
165165
return false;
166166
if (pure && tok1->isName() && tok1->next()->str() == "(" && tok1->str() != "sizeof") {
167-
if (!tok1->function() && !Token::Match(tok1->previous(), ".|::") && !library.isFunctionConst(tok1->str(), true) && !tok1->isAttributeConst() && !tok1->isAttributePure())
168-
return false;
169-
else if (tok1->function() && !tok1->function()->isConst() && !tok1->function()->isAttributeConst() && !tok1->function()->isAttributePure())
170-
return false;
167+
if (!tok1->function()) {
168+
if (!Token::Match(tok1->previous(), ".|::") && !library.isFunctionConst(tok1) && !tok1->isAttributeConst() && !tok1->isAttributePure())
169+
return false;
170+
if (Token::simpleMatch(tok1->previous(), ".")) {
171+
const Token *lhs = tok1->previous();
172+
while (Token::Match(lhs, "(|.|["))
173+
lhs = lhs->astOperand1();
174+
bool lhsIsConst = (lhs->variable() && lhs->variable()->isConst()) ||
175+
(lhs->valueType() && lhs->valueType()->constness > 0) ||
176+
(Token::Match(lhs, "%var% . %name% (") && library.isFunctionConst(lhs->tokAt(2)));
177+
if (!lhsIsConst)
178+
return false;
179+
}
180+
} else {
181+
if (tok1->function() && !tok1->function()->isConst() && !tok1->function()->isAttributeConst() && !tok1->function()->isAttributePure())
182+
return false;
183+
}
171184
}
172185
// templates/casts
173186
if ((Token::Match(tok1, "%name% <") && tok1->next()->link()) ||

test/testcondition.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,20 @@ class TestCondition : public TestFixture {
546546
"}");
547547
ASSERT_EQUALS("", errout.str());
548548

549+
{
550+
check("void f(Class &c) {\n"
551+
" if (c.dostuff() == 3) {}\n"
552+
" else { if (c.dostuff() == 3) {} }\n"
553+
"}");
554+
ASSERT_EQUALS("", errout.str());
555+
556+
check("void f(const Class &c) {\n"
557+
" if (c.dostuff() == 3) {}\n"
558+
" else { if (c.dostuff() == 3) {} }\n"
559+
"}");
560+
ASSERT_EQUALS("[test.cpp:3]: (style) Expression is always false because 'else if' condition matches previous condition at line 2.\n", errout.str());
561+
}
562+
549563
check("void f(int a, int &b) {\n"
550564
" x = x / 2;\n"
551565
" if (x < 100) { b = 1; }\n"
@@ -2091,7 +2105,7 @@ class TestCondition : public TestFixture {
20912105
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant condition: a. '!a || (a && b)' is equivalent to '!a || b'\n", errout.str());
20922106

20932107

2094-
check("void f() {\n"
2108+
check("void f(const Token *tok) {\n"
20952109
" if (!tok->next()->function() || \n"
20962110
" (tok->next()->function() && tok->next()->function()->isConstructor()));\n"
20972111
"}");
@@ -2109,7 +2123,7 @@ class TestCondition : public TestFixture {
21092123
"}");
21102124
ASSERT_EQUALS("", errout.str());
21112125

2112-
check("void f() {\n"
2126+
check("void f(const Token *tok) {\n"
21132127
" if (!tok->next(1)->function(1) || \n"
21142128
" (tok->next(1)->function(1) && tok->next(1)->function(1)->isConstructor()));\n"
21152129
"}");

test/testother.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3645,7 +3645,7 @@ class TestOther : public TestFixture {
36453645
"}");
36463646
ASSERT_EQUALS("", errout.str());
36473647

3648-
check("void f() {\n"
3648+
check("void f(const Bar &bar) {\n"
36493649
" bool a = bar.isSet() && bar->isSet();\n"
36503650
" bool b = bar.isSet() && bar.isSet();\n"
36513651
"}");
@@ -6124,7 +6124,7 @@ class TestOther : public TestFixture {
61246124
check("bool isInUnoIncludeFile(StringRef name) {"
61256125
" return name.startswith(SRCDIR \"/com/\") || name.startswith(SRCDIR \"/uno/\");\n"
61266126
"};", "test.cpp", false, false);
6127-
TODO_ASSERT_EQUALS("", "[test.cpp:1] -> [test.cpp:1]: (style) Same expression on both sides of '||'.\n", errout.str());
6127+
ASSERT_EQUALS("", errout.str());
61286128
}
61296129

61306130
void raceAfterInterlockedDecrement() {

0 commit comments

Comments
 (0)