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

@ -1316,6 +1316,12 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
std::string ndef = getdef(line, false); std::string ndef = getdef(line, false);
if (line.compare(0, 8, "#define ") == 0) if (line.compare(0, 8, "#define ") == 0)
{
match = true;
for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it)
match &= bool(*it);
if (match)
{ {
std::string::size_type pos = line.find_first_of(" (", 8); std::string::size_type pos = line.find_first_of(" (", 8);
if (pos == std::string::npos) if (pos == std::string::npos)
@ -1325,6 +1331,7 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg,
else else
cfgmap[line.substr(8, pos - 8)] = ""; 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"