Fixed #7640 (Preprocessor::getConfigs: -U is not honored)

This commit is contained in:
Daniel Marjamäki 2016-07-28 19:22:59 +02:00
parent 57951a2681
commit 0777ecd071
2 changed files with 32 additions and 158 deletions

View File

@ -231,7 +231,7 @@ static std::string cfg(const std::vector<std::string> &configs)
return ret;
}
static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string> &defined, std::set<std::string> &ret)
static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string> &defined, const std::set<std::string> &undefined, std::set<std::string> &ret)
{
std::vector<std::string> configs_if;
std::vector<std::string> configs_ifndef;
@ -253,13 +253,18 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
} else if (cmdtok->str == "if") {
config = readcondition(cmdtok, defined);
}
if (undefined.find(config) != undefined.end())
config.clear();
configs_if.push_back((cmdtok->str == "ifndef") ? std::string() : config);
configs_ifndef.push_back((cmdtok->str == "ifndef") ? config : std::string());
ret.insert(cfg(configs_if));
} else if (cmdtok->str == "elif") {
if (!configs_if.empty())
configs_if.pop_back();
configs_if.push_back(readcondition(cmdtok, defined));
std::string config = readcondition(cmdtok, defined);
if (undefined.find(config) != undefined.end())
config.clear();
configs_if.push_back(config);
ret.insert(cfg(configs_if));
} else if (cmdtok->str == "else") {
if (!configs_if.empty())
@ -290,10 +295,10 @@ std::set<std::string> Preprocessor::getConfigs(const simplecpp::TokenList &token
std::set<std::string> defined;
defined.insert("__cplusplus");
::getConfigs(tokens, defined, ret);
::getConfigs(tokens, defined, _settings.userUndefs, ret);
for (std::map<std::string, simplecpp::TokenList*>::const_iterator it = tokenlists.begin(); it != tokenlists.end(); ++it)
::getConfigs(*(it->second), defined, ret);
::getConfigs(*(it->second), defined, _settings.userUndefs, ret);
return ret;
}

View File

@ -233,11 +233,6 @@ private:
TEST_CASE(undef1);
TEST_CASE(undef2);
TEST_CASE(undef3);
TEST_CASE(undef4);
TEST_CASE(undef5);
TEST_CASE(undef6);
TEST_CASE(undef7);
TEST_CASE(undef9);
TEST_CASE(validateCfg);
@ -2427,172 +2422,46 @@ private:
ASSERT_EQUALS("", actual);
}
void undef1() {
std::string getConfigsStr(const char filedata[], const char *u1=NULL) {
Settings settings;
const char filedata[] = "#ifdef X\n"
"Fred & Wilma\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
settings.userUndefs.insert("X");
if (u1)
settings.userUndefs.insert(u1);
Preprocessor preprocessor(settings, this);
preprocessor.preprocess(istr, actual, "file.c");
std::vector<std::string> files;
std::istringstream istr(filedata);
simplecpp::TokenList tokens(istr,files);
tokens.removeComments();
const std::set<std::string> configs = preprocessor.getConfigs(tokens);
std::string ret;
for (std::set<std::string>::const_iterator it = configs.begin(); it != configs.end(); ++it)
ret += *it + '\n';
return ret;
}
// Compare results..
ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS("", actual[""]);
void undef1() {
const char filedata[] = "#ifdef X\n"
"#endif\n";
ASSERT_EQUALS("\n", getConfigsStr(filedata, "X"));
ASSERT_EQUALS("\nX\n", getConfigsStr(filedata));
}
void undef2() {
Settings settings;
const char filedata[] = "#ifndef X\n"
"Fred & Wilma\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
settings.userUndefs.insert("X");
Preprocessor preprocessor(settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS("\nFred & Wilma", actual[""]);
ASSERT_EQUALS("\n", getConfigsStr(filedata, "X"));
ASSERT_EQUALS("\n", getConfigsStr(filedata)); // no #else
}
void undef3() {
Settings settings;
const char filedata[] = "#define X\n"
"#ifdef X\n"
"Fred & Wilma\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
settings.userUndefs.insert("X"); // User undefs should override internal defines
Preprocessor preprocessor(settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS("", actual[""]);
}
void undef4() {
Settings settings;
const char filedata[] = "#define X Y\n"
"#ifdef X\n"
"Fred & Wilma\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
settings.userUndefs.insert("X"); // User undefs should override internal defines
Preprocessor preprocessor(settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS("", actual[""]);
}
void undef5() {
Settings settings;
const char filedata[] = "#define X() Y\n"
"#ifdef X\n"
"Fred & Wilma\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
settings.userUndefs.insert("X"); // User undefs should override internal defines
Preprocessor preprocessor(settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS("", actual[""]);
}
void undef6() {
Settings settings;
const char filedata[] = "#define X Y\n"
"#ifdef X\n"
const char filedata[] = "#ifndef X\n"
"Fred & Wilma\n"
"#else\n"
"Barney & Betty\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
settings.userUndefs.insert("X"); // User undefs should override internal defines
Preprocessor preprocessor(settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS("\n\n\n\nBarney & Betty", actual[""]);
}
void undef7() {
Settings settings;
const char filedata[] = "#define X XDefined\n"
"X;\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
settings.userUndefs.insert("X"); // User undefs should override internal defines
Preprocessor preprocessor(settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS("\nX ;", actual[""]);
}
void undef9() {
Settings settings;
const char filedata[] = "#define X Y\n"
"#ifndef X\n"
"Fred & Wilma\n"
"#else\n"
"Barney & Betty\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
settings.userUndefs.insert("X"); // User undefs should override internal defines
Preprocessor preprocessor(settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(1U, actual.size());
ASSERT_EQUALS("\n\nFred & Wilma", actual[""]);
ASSERT_EQUALS("\n", getConfigsStr(filedata, "X"));
ASSERT_EQUALS("\nX\n", getConfigsStr(filedata));
}
void validateCfg() {