Skip to content
Open
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
18 changes: 12 additions & 6 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ static const simplecpp::TokenString IFNDEF("ifndef");
static const simplecpp::TokenString DEFINED("defined");
static const simplecpp::TokenString ELSE("else");
static const simplecpp::TokenString ELIF("elif");
static const simplecpp::TokenString ELIFDEF("elifdef");
static const simplecpp::TokenString ELIFNDEF("elifndef");
static const simplecpp::TokenString ENDIF("endif");

static const simplecpp::TokenString PRAGMA("pragma");
Expand Down Expand Up @@ -3580,7 +3582,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
continue;
}

if (ifstates.size() <= 1U && (rawtok->str() == ELIF || rawtok->str() == ELSE || rawtok->str() == ENDIF)) {
if (ifstates.size() <= 1U && (rawtok->str() == ELIF || rawtok->str() == ELIFDEF ||
rawtok->str() == ELIFNDEF || rawtok->str() == ELSE ||
rawtok->str() == ENDIF)) {
if (outputList) {
simplecpp::Output err{
Output::SYNTAX_ERROR,
Expand Down Expand Up @@ -3721,7 +3725,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
rawtok = filedata->tokens.cfront();
continue;
}
} else if (rawtok->str() == IF || rawtok->str() == IFDEF || rawtok->str() == IFNDEF || rawtok->str() == ELIF) {
} else if (rawtok->str() == IF || rawtok->str() == IFDEF || rawtok->str() == IFNDEF || rawtok->str() == ELIF ||
((getCStd(dui.std) >= C23 || getCppStd(dui.std) >= CPP23) && (rawtok->str() == ELIFDEF || rawtok->str() == ELIFNDEF))) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that we don't need to check the standard. If we see #elifdef we can use the same handling used in C23/C++23.. Let's check the standard if some different behaviors are wanted.

if (!sameline(rawtok,rawtok->next)) {
if (outputList) {
simplecpp::Output out{
Expand All @@ -3736,10 +3741,11 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
}

bool conditionIsTrue;
if (ifstates.top() == AlwaysFalse || (ifstates.top() == ElseIsTrue && rawtok->str() != ELIF)) {
if (ifstates.top() == AlwaysFalse || (ifstates.top() == ElseIsTrue && rawtok->str() != ELIF &&
rawtok->str() != ELIFDEF && rawtok->str() != ELIFNDEF)) {
conditionIsTrue = false;
}
else if (rawtok->str() == IFDEF) {
else if (rawtok->str() == IFDEF || rawtok->str() == ELIFDEF) {
const std::string &name = rawtok->next->str();
conditionIsTrue = (macros.find(name) != macros.end() || (hasInclude && name == HAS_INCLUDE));
maybeUsedMacros[name].emplace_back(rawtok->next->location);
Expand All @@ -3748,7 +3754,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
const long long result = conditionIsTrue ? 1 : 0;
ifCond->emplace_back(rawtok->location, E, result);
}
} else if (rawtok->str() == IFNDEF) {
} else if (rawtok->str() == IFNDEF || rawtok->str() == ELIFNDEF) {
const std::string &name = rawtok->next->str();
conditionIsTrue = (macros.find(name) == macros.end() && !(hasInclude && name == HAS_INCLUDE));
maybeUsedMacros[name].emplace_back(rawtok->next->location);
Expand Down Expand Up @@ -3879,7 +3885,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
}
}

if (rawtok->str() != ELIF) {
if (rawtok->str() != ELIF && rawtok->str() != ELIFDEF && rawtok->str() != ELIFNDEF) {
// push a new ifstate..
if (ifstates.top() != True)
ifstates.push(AlwaysFalse);
Expand Down
78 changes: 78 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,82 @@ static void elif()
ASSERT_EQUALS("\n\n\n\n\n3", preprocess(code3));
}

static void elifdef()
{
simplecpp::DUI dui;
dui.std = "c++23";

{
const char code[] = "#if 1\n"
"1\n"
"#elifdef X\n"
"2\n"
"#else\n"
"3\n"
"#endif";
ASSERT_EQUALS("\n1", preprocess(code, dui));
}
{
const char code[] = "#define X\n"
"#if 0\n"
"1\n"
"#elifdef X\n"
"2\n"
"#else\n"
"3\n"
"#endif";
ASSERT_EQUALS("\n\n\n\n2", preprocess(code, dui));
}
{
const char code[] = "#if 0\n"
"1\n"
"#elifdef X\n"
"2\n"
"#else\n"
"3\n"
"#endif";
ASSERT_EQUALS("\n\n\n\n\n3", preprocess(code, dui));
}
}

static void elifndef()
{
simplecpp::DUI dui;
dui.std = "c++23";

{
const char code[] = "#if 1\n"
"1\n"
"#elifndef X\n"
"2\n"
"#else\n"
"3\n"
"#endif";
ASSERT_EQUALS("\n1", preprocess(code, dui));
}
{
const char code[] = "#if 0\n"
"1\n"
"#elifndef X\n"
"2\n"
"#else\n"
"3\n"
"#endif";
ASSERT_EQUALS("\n\n\n2", preprocess(code, dui));
}
{
const char code[] = "#define X\n"
"#if 0\n"
"1\n"
"#elifndef X\n"
"2\n"
"#else\n"
"3\n"
"#endif";
ASSERT_EQUALS("\n\n\n\n\n\n3", preprocess(code, dui));
}
}

static void ifif()
{
// source code from LLVM
Expand Down Expand Up @@ -4329,6 +4405,8 @@ static void runTests(int argc, char **argv, Input input)
TEST_CASE(ifLogical);
TEST_CASE(ifSizeof);
TEST_CASE(elif);
TEST_CASE(elifdef);
TEST_CASE(elifndef);
TEST_CASE(ifif);
TEST_CASE(ifoverflow);
TEST_CASE(ifdiv0);
Expand Down
Loading