Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "errorlogger.h"
#include "errortypes.h"
#include "library.h"
#include "mathlib.h"
#include "path.h"
#include "platform.h"
#include "settings.h"
Expand Down Expand Up @@ -413,17 +414,21 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s

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

const auto lessGreaterThanConfig = [](const simplecpp::Token* dtok, const simplecpp::Token* ctok) {
int v = MathLib::toBigNumber(ctok->next->str());
if (ctok->op == '<')
v -= 1;
else
v += 1;
return dtok->str() + '=' + std::to_string(v);
};

if (len == 3 && cond->name && (next1->op == '<' || next1->op == '>') && next2->number) {
if (defined.find(cond->str()) == defined.end()) {
int v = strToInt<int>(cond->next->next->str());
if (next1->op == '<')
v -= 1;
else
v += 1;
return cond->str() + '=' + std::to_string(v);
return lessGreaterThanConfig(cond, next1);
}
}

Expand All @@ -437,15 +442,33 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
configset.insert(cond->next->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()) {
configset.insert(lessGreaterThanConfig(dtok, cond));
}
}
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) {
Expand Down
256 changes: 232 additions & 24 deletions test/testpreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,15 @@ class TestPreprocessor : public TestFixture {
TEST_CASE(getConfigs11); // #9832 - include guards
TEST_CASE(getConfigs12); // #14222
TEST_CASE(getConfigs13); // #14222
TEST_CASE(getConfigs14); // #1059
TEST_CASE(getConfigs15); // #1059
TEST_CASE(getConfigs16); // #1059
TEST_CASE(getConfigs17); // #1059
TEST_CASE(getConfigs_gte); // #1059
TEST_CASE(getConfigs_lte); // #1059
TEST_CASE(getConfigs_gt); // #1059
TEST_CASE(getConfigs_lt); // #1059
TEST_CASE(getConfigs_eq_compound); // #1059
TEST_CASE(getConfigs_gte_compound); // #1059
TEST_CASE(getConfigs_lte_compound); // #1059
TEST_CASE(getConfigs_gt_compound); // #1059
TEST_CASE(getConfigs_lt_compound); // #1059
TEST_CASE(getConfigsError);

TEST_CASE(getConfigsD1);
Expand Down Expand Up @@ -2293,32 +2298,235 @@ class TestPreprocessor : public TestFixture {
ASSERT_EQUALS("\n", getConfigsStr(filedata, nullptr, "gnu.cfg"));
}

void getConfigs14() { // #1059
const char filedata[] = "#if A >= 1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=1\n", getConfigsStr(filedata));
void getConfigs_gte() { // #1059
{
const char filedata[] = "#if A >= 1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=1\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A >= 201112L\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=201112L\n", getConfigsStr(filedata));
}
}

void getConfigs15() { // #1059
const char filedata[] = "#if A <= 1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=1\n", getConfigsStr(filedata));
void getConfigs_lte() { // #1059
{
const char filedata[] = "#if A <= 1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=1\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A <= 201112L\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=201112L\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 getConfigs_gt() { // #1059
{
const char filedata[] = "#if A > 1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 1L\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 1U\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 1UL\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 1Z\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 0x1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 01\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 0b1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 1t\n"
"1\n"
"#endif\n";
ASSERT_THROW_INTERNAL_EQUALS(getConfigsStr(filedata), INTERNAL, "Internal Error. MathLib::toBigNumber: input was not completely consumed: 1t");
}
{
const char filedata[] = "#if A > 1.0\n"
"1\n"
"#endif\n";
TODO_ASSERT_THROW(getConfigsStr(filedata), InternalError); // floating point literals are not allowed
}
}

void getConfigs17() { // #1059
const char filedata[] = "#if A < 1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
void getConfigs_lt() { // #1059
{
const char filedata[] = "#if A < 1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 1L\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 1U\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 1UL\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 1Z\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 0x1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 01\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 0b1\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 1t\n"
"1\n"
"#endif\n";
ASSERT_THROW_INTERNAL_EQUALS(getConfigsStr(filedata), INTERNAL, "Internal Error. MathLib::toBigNumber: input was not completely consumed: 1t");
}
{
const char filedata[] = "#if A < 1.0\n"
"1\n"
"#endif\n";
TODO_ASSERT_THROW(getConfigsStr(filedata), InternalError); // floating point literals are not allowed
}
}

void getConfigs_eq_compound() { // #1059
{
const char filedata[] = "#if A == 1 && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A == 201112L && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=201112L;B\n", getConfigsStr(filedata));
}
}

void getConfigs_gte_compound() { // #1059
{
const char filedata[] = "#if A >= 1 && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A >= 201112L && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=201112L;B\n", getConfigsStr(filedata));
}
}

void getConfigs_lte_compound() { // #1059
{
const char filedata[] = "#if A <= 1 && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=1;B\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A <= 201112L && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=201112L;B\n", getConfigsStr(filedata));
}
}

void getConfigs_gt_compound() { // #1059
{
const char filedata[] = "#if A > 1 && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=2;B\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A > 201112L && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=201113;B\n", getConfigsStr(filedata));
}
}

void getConfigs_lt_compound() { // #1059
{
const char filedata[] = "#if A < 1 && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=0;B\n", getConfigsStr(filedata));
}
{
const char filedata[] = "#if A < 201112L && defined(B)\n"
"1\n"
"#endif\n";
ASSERT_EQUALS("\nA=201111;B\n", getConfigsStr(filedata));
}
}

void getConfigsError() {
Expand Down