Skip to content

Commit a3bd7a1

Browse files
committed
#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 1f255d2 commit a3bd7a1

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
@@ -179,14 +179,31 @@ static bool isValidPunctuatorPaste(const std::string &s)
179179
return isPunctuator(s) || (s.size() >= 2 && s[0] == '.' && std::isdigit(static_cast<unsigned char>(s[1])));
180180
}
181181

182+
/*
183+
* When an operation can't be evaluated (division by zero), its result is an
184+
* error value. The error is only reported if the error value is used, so an
185+
* operand that is not evaluated, as in "0 && (1/0)", does not cause an error.
186+
*/
187+
static const std::string ERROR_VALUE("#error:");
188+
189+
static bool isErrorValue(const simplecpp::Token *tok)
190+
{
191+
return tok && tok->str().compare(0, ERROR_VALUE.size(), ERROR_VALUE) == 0;
192+
}
193+
194+
static bool isValue(const simplecpp::Token *tok)
195+
{
196+
return tok && (tok->number || isErrorValue(tok));
197+
}
198+
182199
static bool isAlternativeBinaryOp(const simplecpp::Token *tok, const std::string &alt)
183200
{
184201
return (tok->name &&
185202
tok->str() == alt &&
186203
tok->previous &&
187204
tok->next &&
188-
(tok->previous->number || tok->previous->name || tok->previous->op == ')') &&
189-
(tok->next->number || tok->next->name || tok->next->op == '('));
205+
(isValue(tok->previous) || tok->previous->name || tok->previous->op == ')') &&
206+
(isValue(tok->next) || tok->next->name || tok->next->op == '('));
190207
}
191208

192209
static bool isAlternativeUnaryOp(const simplecpp::Token *tok, const std::string &alt)
@@ -1072,23 +1089,6 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
10721089
combineOperators();
10731090
}
10741091

1075-
/*
1076-
* When an operation can't be evaluated (division by zero), its result is an
1077-
* error value. The error is only reported if the error value is used, so an
1078-
* operand that is not evaluated, as in "0 && (1/0)", does not cause an error.
1079-
*/
1080-
static const std::string ERROR_VALUE("#error:");
1081-
1082-
static bool isErrorValue(const simplecpp::Token *tok)
1083-
{
1084-
return tok && tok->str().compare(0, ERROR_VALUE.size(), ERROR_VALUE) == 0;
1085-
}
1086-
1087-
static bool isValue(const simplecpp::Token *tok)
1088-
{
1089-
return tok && (tok->number || isErrorValue(tok));
1090-
}
1091-
10921092
/**
10931093
* If one operand of the binary operator tok is an error value then
10941094
* "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
@@ -2862,7 +2862,13 @@ static void ifShortCircuit()
28622862
"0 && 1 % 0 && 1",
28632863
"(1 || 1/0) && !(0 && 1/0) && 0",
28642864
"0 && (2 + (3 * (1/0)))",
2865-
"0 && -(1/0)"
2865+
"0 && -(1/0)",
2866+
"0 and (1/0)",
2867+
"!(1 or 1/0)",
2868+
"defined(X) and X/0",
2869+
"0 && 1 bitor (1/0)",
2870+
"!(1 || (1/0) bitand 1)",
2871+
"0 and 1 xor (1/0)"
28662872
};
28672873
for (const char * const expr : exprs) {
28682874
const std::string code = std::string("#if ") + expr + "\n"

0 commit comments

Comments
 (0)