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
7 changes: 5 additions & 2 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3839,9 +3839,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
return;
}

const bool isElif = (rawtok->str() == ELIF || rawtok->str() == ELIFDEF || rawtok->str() == ELIFNDEF);

bool conditionIsTrue;
if (ifstates.top() == AlwaysFalse || (ifstates.top() == ElseIsTrue && rawtok->str() != ELIF &&
rawtok->str() != ELIFDEF && rawtok->str() != ELIFNDEF)) {
// An #if is only evaluated in an active group. An #elif is only evaluated
// if no previous group was taken; otherwise it is skipped (C 6.10.1p6).
if (isElif ? (ifstates.top() != ElseIsTrue) : (ifstates.top() != True)) {
Comment thread
danmar marked this conversation as resolved.
Resolved
conditionIsTrue = false;
}
else if (rawtok->str() == IFDEF || rawtok->str() == ELIFDEF) {
Expand Down
10 changes: 10 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,16 @@ static void elif()
"3\n"
"#endif";
ASSERT_EQUALS("\n\n\n\n\n3", preprocess(code3));

// #elif after a taken group must not be evaluated
const char code4[] = "#if 1\n"
"1\n"
"#elif 1/0\n"
"2\n"
"#elif garbage(\n"
"3\n"
"#endif";
ASSERT_EQUALS("\n1", preprocess(code4));
}

static void elifdef()
Expand Down
Loading