Fixed #7842 (Preprocessor::getConfigs: #error in #ifndef not handled well)

This commit is contained in:
Daniel Marjamäki 2017-04-22 12:44:57 +02:00
parent 1039c710e4
commit e72ec4282d
2 changed files with 39 additions and 12 deletions

View File

@ -184,6 +184,11 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::set<s
return cond->str;
}
if (len == 2 && cond->op == '!' && next1->name) {
if (defined.find(next1->str) == defined.end())
return next1->str + "=0";
}
if (len == 3 && cond->op == '(' && next1->name && next2->op == ')') {
if (defined.find(next1->str) == defined.end() && undefined.find(next1->str) == undefined.end())
return next1->str;
@ -375,7 +380,10 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
ret.erase("");
std::vector<std::string> configs(configs_if);
configs.push_back(configs_ifndef.back());
ret.insert(cfg(configs, userDefines));
ret.erase(cfg(configs, userDefines));
if (!elseError.empty())
elseError += ';';
elseError += cfg(configs_ifndef, userDefines);
}
if (!configs_if.empty() && !configs_if.back().empty()) {
const std::string &last = configs_if.back();
@ -383,9 +391,11 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
std::vector<std::string> configs(configs_if);
ret.erase(cfg(configs, userDefines));
configs[configs.size() - 1U] = last.substr(0,last.size()-2U);
ret.insert(cfg(configs, userDefines));
if (configs.size() == 1U)
ret.erase("");
if (!elseError.empty())
elseError += ';';
elseError += cfg(configs, userDefines);
}
}
} else if (cmdtok->str == "define" && sameline(tok, cmdtok->next) && cmdtok->next->name) {

View File

@ -379,15 +379,32 @@ private:
}
void error6() {
const char filedata[] = "#ifdef A\n"
"#else\n"
"#error 1\n"
"#endif\n"
"#ifdef B\n"
"#else\n"
"#error 2\n"
"#endif\n";
ASSERT_EQUALS("\nA\nA;B\nB\n", getConfigsStr(filedata));
const char filedata1[] = "#ifdef A\n"
"#else\n"
"#error 1\n"
"#endif\n"
"#ifdef B\n"
"#else\n"
"#error 2\n"
"#endif\n";
ASSERT_EQUALS("\nA\nA;B\nB\n", getConfigsStr(filedata1));
const char filedata2[] = "#ifndef A\n"
"#error 1\n"
"#endif\n"
"#ifndef B\n"
"#error 2\n"
"#endif\n";
ASSERT_EQUALS("A;B\n", getConfigsStr(filedata2));
const char filedata3[] = "#if !A\n"
"#error 1\n"
"#endif\n"
"#if !B\n"
"#error 2\n"
"#endif\n";
ASSERT_EQUALS("A;B\n", getConfigsStr(filedata3));
}
void setPlatformInfo() {
@ -2105,7 +2122,7 @@ private:
"#error \"!Y\"\n"
"#endif\n"
"#endif\n";
ASSERT_EQUALS("\nX\nX;Y\n", getConfigsStr(filedata2));
ASSERT_EQUALS("\nX\nY\n", getConfigsStr(filedata2));
}
void getConfigsD1() {