Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ void simplecpp::TokenList::constFoldUnaryNotPosNeg(simplecpp::Token *tok)
tok->setstr(tok->next->str());
deleteToken(tok->next);
} else if (tok->op == '!' && tok->next && tok->next->number) {
tok->setstr(tok->next->str() == "0" ? "1" : "0");
tok->setstr(stringToLL(tok->next->str()) == 0 ? "1" : "0");
deleteToken(tok->next);
} else if (tok->op == '~' && tok->next && tok->next->number) {
tok->setstr(toString(~stringToLL(tok->next->str())));
Expand Down Expand Up @@ -1571,7 +1571,7 @@ void simplecpp::TokenList::constFoldQuestionOp(Token *&tok1)
deleteToken(trueTok);
deleteToken(falseTok);
} else {
Token * const result = (condTok->str() != "0" ? trueTok : falseTok);
Token * const result = (stringToLL(condTok->str()) != 0 ? trueTok : falseTok);
if (condTok == tok1)
tok1 = result;
deleteToken(condTok->next); // ?
Expand Down
27 changes: 27 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,32 @@ static void ifShortCircuit()
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division/modulo by zero\n", toString(outputList));
}

static void ifZeroCondition()
{
// a zero value that is not written as 0 is false
const char * const exprs[] = {
"00 ? 1 : 0",
"-0 ? 1 : 0",
"-X ? 1 : 0",
"0L ? 1 : 0",
"!00 ? 0 : 1",
"!0L ? 0 : 1"
};
for (const char * const expr : exprs) {
const std::string code = std::string("#if ") + expr + "\n"
"1\n"
"#else\n"
"2\n"
"#endif\n";
ASSERT_EQUALS("\n\n\n2", preprocess(code.c_str()));
}

// the selected operand is evaluated
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess("#if 00 ? 1 : 1/0\n#endif\n", &outputList));
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division/modulo by zero\n", toString(outputList));
}

static void ifTrueFalse()
{
const char code[] = "#if true && !false\n"
Expand Down Expand Up @@ -5123,6 +5149,7 @@ static void runTests(int argc, char **argv, Input input)
TEST_CASE(ifoverflow);
TEST_CASE(ifdiv0);
TEST_CASE(ifShortCircuit);
TEST_CASE(ifZeroCondition);
TEST_CASE(ifTrueFalse);
TEST_CASE(ifalt); // using "and", "or", etc
TEST_CASE(ifexpr);
Expand Down
Loading