Skip to content

Commit cab2758

Browse files
danmarclaude
andauthored
Fix #729 (#if: not and compl are only recognized at the start of an expression or after () (#733)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 70d7646 commit cab2758

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,23 @@ static bool isAlternativeBinaryOp(const simplecpp::Token *tok, const std::string
206206
(isValue(tok->next) || tok->next->name || tok->next->op == '('));
207207
}
208208

209+
static bool isAlternativeOpName(const simplecpp::Token *tok)
210+
{
211+
static const std::set<std::string> altop{"and","or","bitand","bitor","compl","not","not_eq","xor"};
212+
return tok->name && altop.find(tok->str()) != altop.end();
213+
}
214+
209215
static bool isAlternativeUnaryOp(const simplecpp::Token *tok, const std::string &alt)
210216
{
211-
return ((tok->name && tok->str() == alt) &&
212-
(!tok->previous || tok->previous->op == '(') &&
213-
(tok->next && (tok->next->name || tok->next->number)));
217+
if (!tok->name || tok->str() != alt)
218+
return false;
219+
// a unary operator comes first or after another operator, but not after an operand
220+
const simplecpp::Token * const prev = tok->previous;
221+
if (prev && (isValue(prev) || prev->op == ')' || (prev->name && !isAlternativeOpName(prev))))
222+
return false;
223+
// it is followed by the start of an operand
224+
const simplecpp::Token * const next = tok->next;
225+
return next && (next->name || isValue(next) || next->isOneOf("(!~+-"));
214226
}
215227

216228
static std::string replaceAll(std::string s, const std::string& from, const std::string& to)
@@ -1318,7 +1330,7 @@ void simplecpp::TokenList::constFoldUnaryNotPosNeg(simplecpp::Token *tok1)
13181330
tok->setstr(toString(~stringToLL(tok->next->str())));
13191331
deleteToken(tok->next);
13201332
} else {
1321-
if (tok->previous && (isValue(tok->previous) || tok->previous->name))
1333+
if (tok->previous && (isValue(tok->previous) || (tok->previous->name && !isAlternativeOpName(tok->previous))))
13221334
continue;
13231335
if (!isValue(tok->next))
13241336
continue;
@@ -2992,8 +3004,7 @@ static void simplifyName(simplecpp::TokenList &expr, const simplecpp::DUI &dui)
29923004
tok->setstr(tok->str() == "true" ? "1" : "0");
29933005
continue;
29943006
}
2995-
static const std::set<std::string> altop{"and","or","bitand","bitor","compl","not","not_eq","xor"};
2996-
if (altop.find(tok->str()) != altop.end()) {
3007+
if (isAlternativeOpName(tok)) {
29973008
bool alt;
29983009
if (tok->str() == "not" || tok->str() == "compl") {
29993010
alt = isAlternativeUnaryOp(tok,tok->str());

‎test.cpp‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2869,7 +2869,10 @@ static void ifShortCircuit()
28692869
"defined(X) and X/0",
28702870
"0 && 1 bitor (1/0)",
28712871
"!(1 || (1/0) bitand 1)",
2872-
"0 and 1 xor (1/0)"
2872+
"0 and 1 xor (1/0)",
2873+
"0 && not (1/0)",
2874+
"0 && compl (1/0)",
2875+
"!(1 || not not (1/0))"
28732876
};
28742877
for (const char * const expr : exprs) {
28752878
const std::string code = std::string("#if ") + expr + "\n"
@@ -2886,6 +2889,9 @@ static void ifShortCircuit()
28862889
simplecpp::OutputList outputList;
28872890
ASSERT_EQUALS("", preprocess("#if 1 && (1/0)\n#endif\n", &outputList));
28882891
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division/modulo by zero\n", toString(outputList));
2892+
outputList.clear();
2893+
ASSERT_EQUALS("", preprocess("#if not (1/0)\n#endif\n", &outputList));
2894+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division/modulo by zero\n", toString(outputList));
28892895
}
28902896

28912897
static void ifZeroCondition()
@@ -3022,6 +3028,45 @@ static void ifalt() // using "and", "or", etc
30223028
ASSERT_EQUALS("\n1", preprocess(code));
30233029
}
30243030

3031+
static void ifAltUnaryOperators() // "not" and "compl" anywhere a unary operator is allowed
3032+
{
3033+
const char * const exprs[] = {
3034+
// at the start of the expression
3035+
"not 0",
3036+
"(not 0)",
3037+
"not 0 && 1",
3038+
"compl 0",
3039+
// after a binary operator, "?" or ":"
3040+
"1 && not 0",
3041+
"1 and not 0",
3042+
"0 || not 0",
3043+
"1 == not 0",
3044+
"0 ? 0 : not 0",
3045+
"1 ? not 0 : 0",
3046+
// next to another unary operator
3047+
"not not 1",
3048+
"not -0",
3049+
"not ~-1",
3050+
"not !0 == 0",
3051+
"compl compl 1 == 1",
3052+
"compl -1 == 0",
3053+
"!(compl -1 + 1 == 0)",
3054+
"-compl 0 == 1",
3055+
// followed by "("
3056+
"not (0)",
3057+
"not(0)",
3058+
"compl (0) == -1"
3059+
};
3060+
for (const char * const expr : exprs) {
3061+
const std::string code = std::string("#if ") + expr + "\n"
3062+
"1\n"
3063+
"#else\n"
3064+
"2\n"
3065+
"#endif\n";
3066+
ASSERT_EQUALS("\n1", preprocess(code.c_str()));
3067+
}
3068+
}
3069+
30253070
static void ifexpr()
30263071
{
30273072
const char code[] = "#define MACRO() (1)\n"
@@ -5226,6 +5271,7 @@ static void runTests(int argc, char **argv, Input input)
52265271
TEST_CASE(ifInvalidExpression);
52275272
TEST_CASE(ifTrueFalse);
52285273
TEST_CASE(ifalt); // using "and", "or", etc
5274+
TEST_CASE(ifAltUnaryOperators);
52295275
TEST_CASE(ifexpr);
52305276
TEST_CASE(ifUndefFuncStyleMacro);
52315277

0 commit comments

Comments
 (0)