Skip to content

Commit b0e67d4

Browse files
dmcppcheckclaude
andcommitted
Fix #729 (#if: not and compl are only recognized at the start of an expression or after ()
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 70d7646 commit b0e67d4

2 files changed

Lines changed: 57 additions & 6 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 *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 *next = tok->next;
225+
return next && (next->name || next->number || 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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3022,6 +3022,45 @@ static void ifalt() // using "and", "or", etc
30223022
ASSERT_EQUALS("\n1", preprocess(code));
30233023
}
30243024

3025+
static void ifAltUnaryOperators() // "not" and "compl" anywhere a unary operator is allowed
3026+
{
3027+
const char * const exprs[] = {
3028+
// at the start of the expression
3029+
"not 0",
3030+
"(not 0)",
3031+
"not 0 && 1",
3032+
"compl 0",
3033+
// after a binary operator, "?" or ":"
3034+
"1 && not 0",
3035+
"1 and not 0",
3036+
"0 || not 0",
3037+
"1 == not 0",
3038+
"0 ? 0 : not 0",
3039+
"1 ? not 0 : 0",
3040+
// next to another unary operator
3041+
"not not 1",
3042+
"not -0",
3043+
"not ~-1",
3044+
"not !0 == 0",
3045+
"compl compl 1 == 1",
3046+
"compl -1 == 0",
3047+
"!(compl -1 + 1 == 0)",
3048+
"-compl 0 == 1",
3049+
// followed by "("
3050+
"not (0)",
3051+
"not(0)",
3052+
"compl (0) == -1"
3053+
};
3054+
for (const char * const expr : exprs) {
3055+
const std::string code = std::string("#if ") + expr + "\n"
3056+
"1\n"
3057+
"#else\n"
3058+
"2\n"
3059+
"#endif\n";
3060+
ASSERT_EQUALS("\n1", preprocess(code.c_str()));
3061+
}
3062+
}
3063+
30253064
static void ifexpr()
30263065
{
30273066
const char code[] = "#define MACRO() (1)\n"
@@ -5226,6 +5265,7 @@ static void runTests(int argc, char **argv, Input input)
52265265
TEST_CASE(ifInvalidExpression);
52275266
TEST_CASE(ifTrueFalse);
52285267
TEST_CASE(ifalt); // using "and", "or", etc
5268+
TEST_CASE(ifAltUnaryOperators);
52295269
TEST_CASE(ifexpr);
52305270
TEST_CASE(ifUndefFuncStyleMacro);
52315271

0 commit comments

Comments
 (0)