Skip to content

Commit b17b484

Browse files
committed
Fix #711 (#if true evaluates to false in C++ and C23)
1 parent 54b71fe commit b17b484

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,10 +2824,18 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI
28242824
/** Evaluate name
28252825
* @throws std::runtime_error thrown on undefined function-like macro
28262826
*/
2827-
static void simplifyName(simplecpp::TokenList &expr)
2827+
static void simplifyName(simplecpp::TokenList &expr, const simplecpp::DUI &dui)
28282828
{
2829+
// In C++ and C23 the keywords true/false are 1/0 in #if expressions
2830+
const simplecpp::cstd_t cstd = simplecpp::getCStd(dui.std);
2831+
const bool hasTrueFalse = (cstd == simplecpp::CUnknown || cstd >= simplecpp::C23);
2832+
28292833
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
28302834
if (tok->name) {
2835+
if (hasTrueFalse && (tok->str() == "true" || tok->str() == "false")) {
2836+
tok->setstr(tok->str() == "true" ? "1" : "0");
2837+
continue;
2838+
}
28312839
static const std::set<std::string> altop{"and","or","bitand","bitor","compl","not","not_eq","xor"};
28322840
if (altop.find(tok->str()) != altop.end()) {
28332841
bool alt;
@@ -3101,7 +3109,7 @@ static long long evaluate(simplecpp::TokenList &expr, const simplecpp::DUI &dui,
31013109
simplifyComments(expr);
31023110
simplifySizeof(expr, sizeOfType);
31033111
simplifyHasInclude(expr, dui);
3104-
simplifyName(expr);
3112+
simplifyName(expr, dui);
31053113
simplifyNumbers(expr);
31063114
expr.constFold();
31073115
// TODO: handle invalid expressions

‎test.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,6 +2609,24 @@ static void ifdiv0()
26092609
ASSERT_EQUALS("", preprocess(code));
26102610
}
26112611

2612+
static void ifTrueFalse()
2613+
{
2614+
const char code[] = "#if true && !false\n"
2615+
"1\n"
2616+
"#else\n"
2617+
"2\n"
2618+
"#endif\n";
2619+
simplecpp::DUI dui;
2620+
ASSERT_EQUALS("\n1", preprocess(code, dui, nullptr));
2621+
dui.std = "c++11";
2622+
ASSERT_EQUALS("\n1", preprocess(code, dui, nullptr));
2623+
dui.std = "c23";
2624+
ASSERT_EQUALS("\n1", preprocess(code, dui, nullptr));
2625+
// before C23, true and false are ordinary identifiers that evaluate to 0
2626+
dui.std = "c17";
2627+
ASSERT_EQUALS("\n\n\n2", preprocess(code, dui, nullptr));
2628+
}
2629+
26122630
static void ifalt() // using "and", "or", etc
26132631
{
26142632
const char *code;
@@ -4794,6 +4812,7 @@ static void runTests(int argc, char **argv, Input input)
47944812
TEST_CASE(ifif);
47954813
TEST_CASE(ifoverflow);
47964814
TEST_CASE(ifdiv0);
4815+
TEST_CASE(ifTrueFalse);
47974816
TEST_CASE(ifalt); // using "and", "or", etc
47984817
TEST_CASE(ifexpr);
47994818
TEST_CASE(ifUndefFuncStyleMacro);

0 commit comments

Comments
 (0)