Skip to content

Commit 0e63ff3

Browse files
dmcppcheckclaude
andcommitted
#if: and, or, bitand, bitor and xor short-circuit like &&, ||, &, | and ^
isAlternativeBinaryOp() did not accept an error value as operand, so "0 and (1/0)" was not converted to "0 && (1/0)" and the error was reported. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 78981c6 commit 0e63ff3

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,31 @@ static bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2)
160160
return tok1 && tok2 && tok1->location.sameline(tok2->location);
161161
}
162162

163+
/*
164+
* When an operation can't be evaluated (division by zero), its result is an
165+
* error value. The error is only reported if the error value is used, so an
166+
* operand that is not evaluated, as in "0 && (1/0)", does not cause an error.
167+
*/
168+
static const std::string ERROR_VALUE("#error:");
169+
170+
static bool isErrorValue(const simplecpp::Token *tok)
171+
{
172+
return tok && tok->str().compare(0, ERROR_VALUE.size(), ERROR_VALUE) == 0;
173+
}
174+
175+
static bool isValue(const simplecpp::Token *tok)
176+
{
177+
return tok && (tok->number || isErrorValue(tok));
178+
}
179+
163180
static bool isAlternativeBinaryOp(const simplecpp::Token *tok, const std::string &alt)
164181
{
165182
return (tok->name &&
166183
tok->str() == alt &&
167184
tok->previous &&
168185
tok->next &&
169-
(tok->previous->number || tok->previous->name || tok->previous->op == ')') &&
170-
(tok->next->number || tok->next->name || tok->next->op == '('));
186+
(isValue(tok->previous) || tok->previous->name || tok->previous->op == ')') &&
187+
(isValue(tok->next) || tok->next->name || tok->next->op == '('));
171188
}
172189

173190
static bool isAlternativeUnaryOp(const simplecpp::Token *tok, const std::string &alt)
@@ -1053,23 +1070,6 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
10531070
combineOperators();
10541071
}
10551072

1056-
/*
1057-
* When an operation can't be evaluated (division by zero), its result is an
1058-
* error value. The error is only reported if the error value is used, so an
1059-
* operand that is not evaluated, as in "0 && (1/0)", does not cause an error.
1060-
*/
1061-
static const std::string ERROR_VALUE("#error:");
1062-
1063-
static bool isErrorValue(const simplecpp::Token *tok)
1064-
{
1065-
return tok && tok->str().compare(0, ERROR_VALUE.size(), ERROR_VALUE) == 0;
1066-
}
1067-
1068-
static bool isValue(const simplecpp::Token *tok)
1069-
{
1070-
return tok && (tok->number || isErrorValue(tok));
1071-
}
1072-
10731073
/**
10741074
* If one operand of the binary operator tok is an error value then
10751075
* "lhs op rhs" is replaced by that error value.

‎test.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2786,7 +2786,13 @@ static void ifShortCircuit()
27862786
"0 && 1 % 0 && 1",
27872787
"(1 || 1/0) && !(0 && 1/0) && 0",
27882788
"0 && (2 + (3 * (1/0)))",
2789-
"0 && -(1/0)"
2789+
"0 && -(1/0)",
2790+
"0 and (1/0)",
2791+
"!(1 or 1/0)",
2792+
"defined(X) and X/0",
2793+
"0 && 1 bitor (1/0)",
2794+
"!(1 || (1/0) bitand 1)",
2795+
"0 and 1 xor (1/0)"
27902796
};
27912797
for (const char * const expr : exprs) {
27922798
const std::string code = std::string("#if ") + expr + "\n"

0 commit comments

Comments
 (0)