Skip to content

Commit e50c48c

Browse files
committed
Fix #581 (Failure when header is included twice with different macros defined)
1 parent 6ddb8c6 commit e50c48c

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

integration_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,38 @@ def test_incpath_dir(record_property, tmpdir):
446446

447447
assert '' == stderr
448448
assert "error: could not open include 'inc'\n" == stdout
449+
450+
451+
def test_include_header_twice(tmpdir):
452+
""" Issue #581 - Failure when header is included twice with different
453+
macros defined"""
454+
455+
header_file = tmpdir / 'test.h'
456+
with open(header_file, 'wt') as f:
457+
f.write(f"""
458+
#if defined AAA
459+
#elif defined BBB
460+
# undef BBB
461+
#endif
462+
463+
#ifdef BBB
464+
# error BBB is defined
465+
#endif
466+
""")
467+
468+
test_file = os.path.join(tmpdir, 'test.c')
469+
with open(test_file, 'wt') as f:
470+
f.write(f"""
471+
# define Y
472+
# include "test.h"
473+
474+
# define BBB
475+
# include "test.h"
476+
""")
477+
478+
args = [test_file]
479+
480+
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
481+
482+
assert stderr == ''
483+

simplecpp.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3701,12 +3701,11 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37013701
else
37023702
ifstates.push(conditionIsTrue ? True : ElseIsTrue);
37033703
iftokens.push(rawtok);
3704-
} else if (ifstates.top() == True) {
3705-
ifstates.top() = AlwaysFalse;
3706-
iftokens.top()->nextcond = rawtok;
3707-
iftokens.top() = rawtok;
3708-
} else if (ifstates.top() == ElseIsTrue && conditionIsTrue) {
3709-
ifstates.top() = True;
3704+
} else {
3705+
if (ifstates.top() == True)
3706+
ifstates.top() = AlwaysFalse;
3707+
else if (ifstates.top() == ElseIsTrue && conditionIsTrue)
3708+
ifstates.top() = True;
37103709
iftokens.top()->nextcond = rawtok;
37113710
iftokens.top() = rawtok;
37123711
}

0 commit comments

Comments
 (0)