refactor #if 0 handling to leave preprocessor statements alone

This commit is contained in:
Greg Hewgill 2011-03-01 20:04:11 +13:00
parent a331516735
commit 2efb2efaca
2 changed files with 37 additions and 8 deletions

View File

@ -563,19 +563,33 @@ std::string Preprocessor::removeIf0(const std::string &code)
else
{
// replace '#if 0' with empty line
ret << "\n";
ret << line << "\n";
// goto the end of the '#if 0' block
unsigned int level = 1;
bool in = false;
while (level > 0 && std::getline(istr,line))
{
if (line.compare(0,3,"#if") == 0)
++level;
else if (line == "#endif")
--level;
else if (line == "#else")
{
if (level == 1)
in = true;
}
else
{
if (in)
ret << line << "\n";
else
// replace code within '#if 0' block with empty lines
ret << "\n";
continue;
}
// replace code within '#if 0' block with empty lines
ret << "\n";
ret << line << "\n";
}
}
}

View File

@ -94,6 +94,7 @@ private:
TEST_CASE(if0_exclude);
TEST_CASE(if0_whitespace);
TEST_CASE(if0_else);
// Don't handle include in a #if 0 block
TEST_CASE(if0_include_1);
@ -653,13 +654,13 @@ private:
"A\n"
"#endif\n"
"B\n");
ASSERT_EQUALS("\n\n\nB\n", preprocessor.read(code,"",NULL));
ASSERT_EQUALS("#if 0\n\n#endif\nB\n", preprocessor.read(code,"",NULL));
std::istringstream code2("#if (0)\n"
"A\n"
"#endif\n"
"B\n");
ASSERT_EQUALS("\n\n\nB\n", preprocessor.read(code2,"",NULL));
ASSERT_EQUALS("#if 0\n\n#endif\nB\n", preprocessor.read(code2,"",NULL));
}
void if0_whitespace()
@ -671,7 +672,21 @@ private:
"A\n"
" # endif \n"
"B\n");
ASSERT_EQUALS("\n\n\nB\n", preprocessor.read(code,"",NULL));
ASSERT_EQUALS("#if 0\n\n#endif\nB\n", preprocessor.read(code,"",NULL));
}
void if0_else()
{
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code("#if 0\n"
"A\n"
"#else\n"
"B\n"
"#endif\n"
"C\n");
ASSERT_EQUALS("#if 0\n\n#else\nB\n#endif\nC\n", preprocessor.read(code,"",NULL));
}
void if0_include_1()
@ -683,7 +698,7 @@ private:
"#include \"a.h\"\n"
"#endif\n"
"AB\n");
ASSERT_EQUALS("\n\n\nAB\n", preprocessor.read(code,"",NULL));
ASSERT_EQUALS("#if 0\n\n#endif\nAB\n", preprocessor.read(code,"",NULL));
}
void if0_include_2()
@ -698,7 +713,7 @@ private:
"#endif\n"
"#endif\n"
"AB\n");
ASSERT_EQUALS("\n\n\n\n\n\nAB\n", preprocessor.read(code,"",NULL));
ASSERT_EQUALS("#if 0\n\n#ifdef WIN32\n#else\n#endif\n#endif\nAB\n", preprocessor.read(code,"",NULL));
}
void includeguard1()