diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index b10ff859ad6..e286830a582 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -415,6 +415,17 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::setstr() + '=' + cond->next->next->str(); } + if (len == 3 && cond->name && (next1->op == '<' || next1->op == '>') && next2->number) { + if (defined.find(cond->str()) == defined.end()) { + int v = strToInt(cond->next->next->str()); + if (next1->op == '<') + v -= 1; + else + v += 1; + return cond->str() + '=' + std::to_string(v); + } + } + std::set configset; for (; sameline(iftok,cond); cond = cond->next) { if (cond->op == '!') { @@ -425,15 +436,38 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::setnext->str() + "=0"); continue; } - if (cond->str() != "defined") + if (cond->str() == "==" || cond->str() == "<=" || cond->str() == ">=") { + if (cond->next->number) { + const simplecpp::Token *dtok = cond->previous; + if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end()) + configset.insert(dtok->str() + '=' + cond->next->str()); + } continue; - const simplecpp::Token *dtok = cond->next; - if (!dtok) - break; - if (dtok->op == '(') - dtok = dtok->next; - if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end()) - configset.insert(dtok->str()); + } + if (cond->op == '<' || cond->op == '>') { + if (cond->next->number) { + const simplecpp::Token *dtok = cond->previous; + if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end()) { + int v = strToInt(cond->next->str()); + if (cond->op == '<') + v -= 1; + else + v += 1; + configset.insert(dtok->str() + '=' + std::to_string(v)); + } + } + continue; + } + if (cond->str() == "defined") { + const simplecpp::Token *dtok = cond->next; + if (!dtok) + break; + if (dtok->op == '(') + dtok = dtok->next; + if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end()) + configset.insert(dtok->str()); + continue; + } } std::string cfgStr; for (const std::string &s : configset) { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 781f6b6cf35..04fb57e420a 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -319,6 +319,13 @@ class TestPreprocessor : public TestFixture { TEST_CASE(getConfigs13); // #14222 TEST_CASE(getConfigs14); // #1059 TEST_CASE(getConfigs15); // #1059 + TEST_CASE(getConfigs16); // #1059 + TEST_CASE(getConfigs17); // #1059 + TEST_CASE(getConfigs18); // #1059 + TEST_CASE(getConfigs19); // #1059 + TEST_CASE(getConfigs20); // #1059 + TEST_CASE(getConfigs21); // #1059 + TEST_CASE(getConfigs22); // #1059 TEST_CASE(getConfigsError); TEST_CASE(getConfigsD1); @@ -653,7 +660,7 @@ class TestPreprocessor : public TestFixture { "#else\n" " B\n" "#endif\n"; - TODO_ASSERT_EQUALS("\nLIBVER=101\n", "\n", getConfigsStr(filedata)); + ASSERT_EQUALS("\nLIBVER=101\n", getConfigsStr(filedata)); } void if_cond2() { @@ -2304,6 +2311,55 @@ class TestPreprocessor : public TestFixture { ASSERT_EQUALS("\nA=1\n", getConfigsStr(filedata)); } + void getConfigs16() { // #1059 + const char filedata[] = "#if A > 1\n" + "1\n" + "#endif\n"; + ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata)); + } + + void getConfigs17() { // #1059 + const char filedata[] = "#if A < 1\n" + "1\n" + "#endif\n"; + ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata)); + } + + void getConfigs18() { // #1059 + const char filedata[] = "#if A == 1 && defined(B)\n" + "1\n" + "#endif\n"; + ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata)); + } + + void getConfigs19() { // #1059 + const char filedata[] = "#if A >= 1 && defined(B)\n" + "1\n" + "#endif\n"; + ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata)); + } + + void getConfigs20() { // #1059 + const char filedata[] = "#if A <= 1 && defined(B)\n" + "1\n" + "#endif\n"; + ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata)); + } + + void getConfigs21() { // #1059 + const char filedata[] = "#if A > 1 && defined(B)\n" + "1\n" + "#endif\n"; + ASSERT_EQUALS("\nA=2;B\n", getConfigsStr(filedata)); + } + + void getConfigs22() { // #1059 + const char filedata[] = "#if A < 1 && defined(B)\n" + "1\n" + "#endif\n"; + ASSERT_EQUALS("\nA=0;B\n", getConfigsStr(filedata)); + } + void getConfigsError() { const char filedata1[] = "#ifndef X\n" "#error \"!X\"\n"