Skip to content

Commit 883cf48

Browse files
committed
fixed #1059 - extract configurations for compound preprocessor checks with operators
1 parent 46c8934 commit 883cf48

2 files changed

Lines changed: 78 additions & 15 deletions

File tree

lib/preprocessor.cpp

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "errorlogger.h"
2323
#include "errortypes.h"
2424
#include "library.h"
25+
#include "mathlib.h"
2526
#include "path.h"
2627
#include "platform.h"
2728
#include "settings.h"
@@ -413,17 +414,21 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
413414

414415
if (len == 3 && cond->name && (next1->str() == "==" || next1->str() == "<=" || next1->str() == ">=") && next2->number) {
415416
if (defined.find(cond->str()) == defined.end())
416-
return cond->str() + '=' + cond->next->next->str();
417+
return cond->str() + '=' + next1->next->str();
417418
}
418419

420+
const auto lessGreaterThanConfig = [](const simplecpp::Token* dtok, const simplecpp::Token* ctok) {
421+
int v = MathLib::toBigNumber(ctok->next->str());
422+
if (ctok->op == '<')
423+
v -= 1;
424+
else
425+
v += 1;
426+
return dtok->str() + '=' + std::to_string(v);
427+
};
428+
419429
if (len == 3 && cond->name && (next1->op == '<' || next1->op == '>') && next2->number) {
420430
if (defined.find(cond->str()) == defined.end()) {
421-
int v = strToInt<int>(cond->next->next->str());
422-
if (next1->op == '<')
423-
v -= 1;
424-
else
425-
v += 1;
426-
return cond->str() + '=' + std::to_string(v);
431+
return lessGreaterThanConfig(cond, next1);
427432
}
428433
}
429434

@@ -437,15 +442,33 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
437442
configset.insert(cond->next->str() + "=0");
438443
continue;
439444
}
440-
if (cond->str() != "defined")
445+
if (cond->str() == "==" || cond->str() == "<=" || cond->str() == ">=") {
446+
if (cond->next->number) {
447+
const simplecpp::Token *dtok = cond->previous;
448+
if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end())
449+
configset.insert(dtok->str() + '=' + cond->next->str());
450+
}
441451
continue;
442-
const simplecpp::Token *dtok = cond->next;
443-
if (!dtok)
444-
break;
445-
if (dtok->op == '(')
446-
dtok = dtok->next;
447-
if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end())
448-
configset.insert(dtok->str());
452+
}
453+
if (cond->op == '<' || cond->op == '>') {
454+
if (cond->next->number) {
455+
const simplecpp::Token *dtok = cond->previous;
456+
if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end()) {
457+
configset.insert(lessGreaterThanConfig(dtok, cond));
458+
}
459+
}
460+
continue;
461+
}
462+
if (cond->str() == "defined") {
463+
const simplecpp::Token *dtok = cond->next;
464+
if (!dtok)
465+
break;
466+
if (dtok->op == '(')
467+
dtok = dtok->next;
468+
if (sameline(iftok,dtok) && dtok->name && defined.find(dtok->str()) == defined.end() && undefined.find(dtok->str()) == undefined.end())
469+
configset.insert(dtok->str());
470+
continue;
471+
}
449472
}
450473
std::string cfgStr;
451474
for (const std::string &s : configset) {

test/testpreprocessor.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ class TestPreprocessor : public TestFixture {
322322
TEST_CASE(getConfigs15); // #1059
323323
TEST_CASE(getConfigs16); // #1059
324324
TEST_CASE(getConfigs17); // #1059
325+
TEST_CASE(getConfigs18); // #1059
326+
TEST_CASE(getConfigs19); // #1059
327+
TEST_CASE(getConfigs20); // #1059
328+
TEST_CASE(getConfigs21); // #1059
329+
TEST_CASE(getConfigs22); // #1059
325330
TEST_CASE(getConfigsError);
326331

327332
TEST_CASE(getConfigsD1);
@@ -2321,6 +2326,41 @@ class TestPreprocessor : public TestFixture {
23212326
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
23222327
}
23232328

2329+
void getConfigs18() { // #1059
2330+
const char filedata[] = "#if A == 1 && defined(B)\n"
2331+
"1\n"
2332+
"#endif\n";
2333+
ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata));
2334+
}
2335+
2336+
void getConfigs19() { // #1059
2337+
const char filedata[] = "#if A >= 1 && defined(B)\n"
2338+
"1\n"
2339+
"#endif\n";
2340+
ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata));
2341+
}
2342+
2343+
void getConfigs20() { // #1059
2344+
const char filedata[] = "#if A <= 1 && defined(B)\n"
2345+
"1\n"
2346+
"#endif\n";
2347+
ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata));
2348+
}
2349+
2350+
void getConfigs21() { // #1059
2351+
const char filedata[] = "#if A > 1 && defined(B)\n"
2352+
"1\n"
2353+
"#endif\n";
2354+
ASSERT_EQUALS("\nA=2;B\n", getConfigsStr(filedata));
2355+
}
2356+
2357+
void getConfigs22() { // #1059
2358+
const char filedata[] = "#if A < 1 && defined(B)\n"
2359+
"1\n"
2360+
"#endif\n";
2361+
ASSERT_EQUALS("\nA=0;B\n", getConfigsStr(filedata));
2362+
}
2363+
23242364
void getConfigsError() {
23252365
const char filedata1[] = "#ifndef X\n"
23262366
"#error \"!X\"\n"

0 commit comments

Comments
 (0)