Skip to content

Commit a3e956c

Browse files
committed
Fix #709 Do not evaluate #elif after a taken group
1 parent 370f18d commit a3e956c

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3839,9 +3839,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
38393839
return;
38403840
}
38413841

3842+
const bool isElif = (rawtok->str() == ELIF || rawtok->str() == ELIFDEF || rawtok->str() == ELIFNDEF);
3843+
38423844
bool conditionIsTrue;
3843-
if (ifstates.top() == AlwaysFalse || (ifstates.top() == ElseIsTrue && rawtok->str() != ELIF &&
3844-
rawtok->str() != ELIFDEF && rawtok->str() != ELIFNDEF)) {
3845+
// An #if is only evaluated in an active group. An #elif is only evaluated
3846+
// if no previous group was taken; otherwise it is skipped (C 6.10.1p6).
3847+
if (isElif ? (ifstates.top() != ElseIsTrue) : (ifstates.top() != True)) {
38453848
conditionIsTrue = false;
38463849
}
38473850
else if (rawtok->str() == IFDEF || rawtok->str() == ELIFDEF) {

‎test.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,16 @@ static void elif()
24902490
"3\n"
24912491
"#endif";
24922492
ASSERT_EQUALS("\n\n\n\n\n3", preprocess(code3));
2493+
2494+
// #elif after a taken group must not be evaluated
2495+
const char code4[] = "#if 1\n"
2496+
"1\n"
2497+
"#elif 1/0\n"
2498+
"2\n"
2499+
"#elif garbage(\n"
2500+
"3\n"
2501+
"#endif";
2502+
ASSERT_EQUALS("\n1", preprocess(code4));
24932503
}
24942504

24952505
static void elifdef()

0 commit comments

Comments
 (0)