merge from subversion client

This commit is contained in:
Daniel Marjamäki 2011-02-26 10:04:38 -08:00
commit 7baf508f85
3 changed files with 72 additions and 0 deletions

View File

@ -178,6 +178,10 @@ std::string Preprocessor::read(std::istream &istr, const std::string &filename,
// Remove all comments..
result = removeComments(result, filename, settings);
// Remove '#if 0' blocks
if (result.find("#if 0\n") != std::string::npos)
result = removeIf0(result);
// ------------------------------------------------------------------------------------------
//
// Clean up all preprocessor statements
@ -547,6 +551,37 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
return code.str();
}
std::string Preprocessor::removeIf0(const std::string &code)
{
std::ostringstream ret;
std::istringstream istr(code);
std::string line;
while (std::getline(istr,line))
{
if (line != "#if 0")
ret << line << "\n";
else
{
// replace '#if 0' with empty line
ret << "\n";
// goto the end of the '#if 0' block
unsigned int level = 1;
while (level > 0 && std::getline(istr,line))
{
if (line.compare(0,3,"#if") == 0)
++level;
else if (line == "#endif")
--level;
// replace code within '#if 0' block with empty lines
ret << "\n";
}
}
}
return ret.str();
}
std::string Preprocessor::removeParantheses(const std::string &str)
{

View File

@ -144,6 +144,13 @@ protected:
*/
std::string removeComments(const std::string &str, const std::string &filename, Settings *settings);
/**
* Cleanup 'if 0' from the code
* @param str Code processed by read().
* @return code without 'if 0'
*/
static std::string removeIf0(const std::string &code);
/**
* Remove redundant parentheses from preprocessor commands. This should only be called from read().
* @param str Code processed by read().

View File

@ -92,6 +92,10 @@ private:
TEST_CASE(error3);
// Don't handle include in a #if 0 block
TEST_CASE(if0_include_1);
TEST_CASE(if0_include_2);
// Handling include guards (don't create extra configuration for it)
TEST_CASE(includeguard1);
TEST_CASE(includeguard2);
@ -637,6 +641,32 @@ private:
ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str());
}
void if0_include_1()
{
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code("#if 0\n"
"#include \"a.h\"\n"
"#endif\n"
"AB\n");
ASSERT_EQUALS("\n\n\nAB\n", preprocessor.read(code,"",NULL));
}
void if0_include_2()
{
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code("#if 0\n"
"#include \"a.h\"\n"
"#ifdef WIN32\n"
"#else\n"
"#endif\n"
"#endif\n"
"AB\n");
ASSERT_EQUALS("\n\n\n\n\n\nAB\n", preprocessor.read(code,"",NULL));
}
void includeguard1()
{