Skip to content

Commit 9fedb7d

Browse files
authored
Fix #716 (#if: && and || are evaluated with the same precedence) (#717)
1 parent 4969f5c commit 9fedb7d

2 files changed

Lines changed: 49 additions & 21 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,30 +1422,36 @@ void simplecpp::TokenList::constFoldBitwise(Token *tok)
14221422
static const std::string OR("or");
14231423
void simplecpp::TokenList::constFoldLogicalOp(Token *tok)
14241424
{
1425-
for (; tok && tok->op != ')'; tok = tok->next) {
1426-
if (tok->name) {
1427-
if (isAlternativeBinaryOp(tok,AND))
1428-
tok->setstr("&&");
1429-
else if (isAlternativeBinaryOp(tok,OR))
1430-
tok->setstr("||");
1425+
for (Token *t = tok; t && t->op != ')'; t = t->next) {
1426+
if (t->name) {
1427+
if (isAlternativeBinaryOp(t,AND))
1428+
t->setstr("&&");
1429+
else if (isAlternativeBinaryOp(t,OR))
1430+
t->setstr("||");
14311431
}
1432-
if (tok->str() != "&&" && tok->str() != "||")
1433-
continue;
1434-
if (!tok->previous || !tok->previous->number)
1435-
continue;
1436-
if (!tok->next || !tok->next->number)
1437-
continue;
1432+
}
14381433

1439-
int result;
1440-
if (tok->str() == "||")
1441-
result = (stringToLL(tok->previous->str()) || stringToLL(tok->next->str()));
1442-
else /*if (tok->str() == "&&")*/
1443-
result = (stringToLL(tok->previous->str()) && stringToLL(tok->next->str()));
1434+
// && has higher precedence than ||
1435+
for (const char * const op : {"&&", "||"}) {
1436+
for (Token *t = tok; t && t->op != ')'; t = t->next) {
1437+
if (t->str() != op)
1438+
continue;
1439+
if (!t->previous || !t->previous->number)
1440+
continue;
1441+
if (!t->next || !t->next->number)
1442+
continue;
14441443

1445-
tok = tok->previous;
1446-
tok->setstr(toString(result));
1447-
deleteToken(tok->next);
1448-
deleteToken(tok->next);
1444+
int result;
1445+
if (t->str() == "||")
1446+
result = (stringToLL(t->previous->str()) || stringToLL(t->next->str()));
1447+
else /*if (t->str() == "&&")*/
1448+
result = (stringToLL(t->previous->str()) && stringToLL(t->next->str()));
1449+
1450+
t = t->previous;
1451+
t->setstr(toString(result));
1452+
deleteToken(t->next);
1453+
deleteToken(t->next);
1454+
}
14491455
}
14501456
}
14511457

‎test.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,27 @@ static void ifLogical()
25322532
ASSERT_EQUALS("\nX", preprocess(code, dui));
25332533
}
25342534

2535+
static void ifLogicalPrecedence()
2536+
{
2537+
// && has higher precedence than ||
2538+
const char code1[] = "#if 1 || 1 && 0\n"
2539+
"X\n"
2540+
"#endif";
2541+
ASSERT_EQUALS("\nX", preprocess(code1));
2542+
const char code2[] = "#if 1 or 1 and 0\n"
2543+
"X\n"
2544+
"#endif";
2545+
ASSERT_EQUALS("\nX", preprocess(code2));
2546+
const char code3[] = "#if 0 && 1 || 1\n"
2547+
"X\n"
2548+
"#endif";
2549+
ASSERT_EQUALS("\nX", preprocess(code3));
2550+
const char code4[] = "#if 0 || 1 && 0\n"
2551+
"X\n"
2552+
"#endif";
2553+
ASSERT_EQUALS("", preprocess(code4));
2554+
}
2555+
25352556
static void ifSizeof()
25362557
{
25372558
const char code[] = "#if sizeof(unsigned short)==2\n"
@@ -4887,6 +4908,7 @@ static void runTests(int argc, char **argv, Input input)
48874908
TEST_CASE(ifDefinedHashHash);
48884909
TEST_CASE(ifDefinedHashHash2);
48894910
TEST_CASE(ifLogical);
4911+
TEST_CASE(ifLogicalPrecedence);
48904912
TEST_CASE(ifSizeof);
48914913
TEST_CASE(elif);
48924914
TEST_CASE(elifdef);

0 commit comments

Comments
 (0)