Fixed #2048 (Preprocessor: ifndef incorrectly simplified)

This commit is contained in:
Daniel Marjamäki 2010-09-14 17:45:37 +02:00
parent f2a3267ac7
commit 42d0ad8262
2 changed files with 32 additions and 9 deletions

View File

@ -1317,13 +1317,20 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
if (line.compare(0, 8, "#define ") == 0) if (line.compare(0, 8, "#define ") == 0)
{ {
std::string::size_type pos = line.find_first_of(" (", 8); match = true;
if (pos == std::string::npos) for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it)
cfgmap[line.substr(8)] = ""; match &= bool(*it);
else if (line[pos] == ' ')
cfgmap[line.substr(8, pos - 8)] = line.substr(pos + 1); if (match)
else {
cfgmap[line.substr(8, pos - 8)] = ""; std::string::size_type pos = line.find_first_of(" (", 8);
if (pos == std::string::npos)
cfgmap[line.substr(8)] = "";
else if (line[pos] == ' ')
cfgmap[line.substr(8, pos - 8)] = line.substr(pos + 1);
else
cfgmap[line.substr(8, pos - 8)] = "";
}
} }
else if (line.find("#elif ") == 0) else if (line.find("#elif ") == 0)

View File

@ -186,7 +186,8 @@ private:
// define and then ifdef // define and then ifdef
TEST_CASE(define_ifdef); TEST_CASE(define_ifdef);
TEST_CASE(define_ifndef); TEST_CASE(define_ifndef1);
TEST_CASE(define_ifndef2);
TEST_CASE(endfile); TEST_CASE(endfile);
TEST_CASE(redundant_config); TEST_CASE(redundant_config);
@ -2217,7 +2218,7 @@ private:
} }
} }
void define_ifndef() void define_ifndef1()
{ {
const char filedata[] = "#define A(x) (x)\n" const char filedata[] = "#define A(x) (x)\n"
"#ifndef A\n" "#ifndef A\n"
@ -2236,6 +2237,21 @@ private:
ASSERT_EQUALS(2, actual.size()); ASSERT_EQUALS(2, actual.size());
} }
void define_ifndef2()
{
const char filedata[] = "#ifdef A\n"
"#define B char\n"
"#endif\n"
"#ifndef B\n"
"#define B int\n"
"#endif\n"
"B me;\n";
// Preprocess => actual result..
ASSERT_EQUALS("\n\n\n\n\n\nint me;\n", Preprocessor::getcode(filedata, "", "a.cpp", NULL, NULL));
ASSERT_EQUALS("\n\n\n\n\n\nchar me;\n", Preprocessor::getcode(filedata, "A", "a.cpp", NULL, NULL));
}
void redundant_config() void redundant_config()
{ {
const char filedata[] = "int main() {\n" const char filedata[] = "int main() {\n"