Skip to content

Commit 6c99773

Browse files
committed
refs #1059 - extract configurations for #if x < y and #if x > y
1 parent 41fa8a9 commit 6c99773

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/preprocessor.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,17 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
415415
return cond->str() + '=' + cond->next->next->str();
416416
}
417417

418+
if (len == 3 && cond->name && (next1->op == '<' || next1->op == '>') && next2->number) {
419+
if (defined.find(cond->str()) == defined.end()) {
420+
int v = strToInt<int>(cond->next->next->str());
421+
if (next1->op == '<')
422+
v -= 1;
423+
else
424+
v += 1;
425+
return cond->str() + '=' + std::to_string(v);
426+
}
427+
}
428+
418429
std::set<std::string> configset;
419430
for (; sameline(iftok,cond); cond = cond->next) {
420431
if (cond->op == '!') {

test/testpreprocessor.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ class TestPreprocessor : public TestFixture {
319319
TEST_CASE(getConfigs13); // #14222
320320
TEST_CASE(getConfigs14); // #1059
321321
TEST_CASE(getConfigs15); // #1059
322+
TEST_CASE(getConfigs16); // #1059
323+
TEST_CASE(getConfigs17); // #1059
322324
TEST_CASE(getConfigsError);
323325

324326
TEST_CASE(getConfigsD1);
@@ -653,7 +655,7 @@ class TestPreprocessor : public TestFixture {
653655
"#else\n"
654656
" B\n"
655657
"#endif\n";
656-
TODO_ASSERT_EQUALS("\nLIBVER=101\n", "\n", getConfigsStr(filedata));
658+
ASSERT_EQUALS("\nLIBVER=101\n", getConfigsStr(filedata));
657659
}
658660

659661
void if_cond2() {
@@ -2304,6 +2306,20 @@ class TestPreprocessor : public TestFixture {
23042306
ASSERT_EQUALS("\nA=1\n", getConfigsStr(filedata));
23052307
}
23062308

2309+
void getConfigs16() { // #1059
2310+
const char filedata[] = "#if A > 1\n"
2311+
"1\n"
2312+
"#endif\n";
2313+
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
2314+
}
2315+
2316+
void getConfigs17() { // #1059
2317+
const char filedata[] = "#if A < 1\n"
2318+
"1\n"
2319+
"#endif\n";
2320+
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
2321+
}
2322+
23072323
void getConfigsError() {
23082324
const char filedata1[] = "#ifndef X\n"
23092325
"#error \"!X\"\n"

0 commit comments

Comments
 (0)