Fixed #9832 (extract configurations; include guards not handled correctly '#if !defined(FILE1_H)')

This commit is contained in:
Daniel Marjamäki 2021-03-12 18:16:09 +01:00
parent 103e52f394
commit 7d4582d8ef
2 changed files with 42 additions and 0 deletions

View File

@ -411,6 +411,37 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set<std::string>
if (isUndefined(config, undefined))
config.clear();
bool ifndef = false;
if (cmdtok->str() == "ifndef")
ifndef = true;
else {
const std::vector<std::string> match{"if", "!", "defined", "(", config, ")"};
int i = 0;
ifndef = true;
for (const simplecpp::Token *t = cmdtok; i < match.size(); t = t->next) {
if (!t || t->str() != match[i++]) {
ifndef = false;
break;
}
}
}
// include guard..
if (ifndef && tok->location.fileIndex > 0) {
bool includeGuard = true;
for (const simplecpp::Token *t = tok->previous; t; t = t->previous) {
if (t->location.fileIndex == tok->location.fileIndex) {
includeGuard = false;
break;
}
}
if (includeGuard) {
configs_if.push_back(std::string());
configs_ifndef.push_back(std::string());
continue;
}
}
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,userDefines));

View File

@ -221,6 +221,7 @@ private:
TEST_CASE(getConfigs7e);
TEST_CASE(getConfigs8); // #if A==1 => cfg: A=1
TEST_CASE(getConfigs10); // #5139
TEST_CASE(getConfigs11); // #9832 - include guards
TEST_CASE(getConfigsError);
TEST_CASE(getConfigsD1);
@ -2134,6 +2135,16 @@ private:
ASSERT_EQUALS("\n", getConfigsStr(filedata));
}
void getConfigs11() { // #9832 - include guards
const char filedata[] = "#file \"test.h\"\n"
"#if !defined(test_h)\n"
"#define test_h\n"
"123\n"
"#endif\n"
"#endfile\n";
ASSERT_EQUALS("\n", getConfigsStr(filedata));
}
void getConfigsError() {
const char filedata1[] = "#ifndef X\n"
"#error \"!X\"\n"