Fixed #772 (Preprocessor: #if(A) is not seen equal to #if A)

This commit is contained in:
Daniel Marjamäki 2009-10-04 07:25:30 +02:00
parent 4387071eb1
commit 1a48f869c8
3 changed files with 45 additions and 4 deletions

View File

@ -324,7 +324,7 @@ std::string Preprocessor::read(std::istream &istr)
} }
} }
return removeComments(code.str()); return removeParantheses(removeComments(code.str()));
} }
@ -442,6 +442,38 @@ std::string Preprocessor::removeComments(const std::string &str)
} }
std::string Preprocessor::removeParantheses(const std::string &str)
{
if (str.find("\n#if") == std::string::npos)
return str;
std::istringstream istr(str.c_str());
std::ostringstream ret;
std::string line;
while (std::getline(istr, line))
{
if (line.substr(0, 3) == "#if")
{
while (line.find(" (") != std::string::npos)
line.erase(line.find(" ("), 1);
while (line.find("( ") != std::string::npos)
line.erase(line.find("( ") + 1, 1);
while (line.find(" )") != std::string::npos)
line.erase(line.find(" )"), 1);
while (line.find(") ") != std::string::npos)
line.erase(line.find(") ") + 1, 1);
if (line.substr(0, 4) == "#if(" && line.find(")") == line.length() - 1)
{
line[3] = ' ';
line.erase(line.length() - 1);
}
}
ret << line << "\n";
}
return ret.str();
}
static void _removeAsm(std::string &str, const std::string::size_type pos) static void _removeAsm(std::string &str, const std::string::size_type pos)
{ {

View File

@ -99,6 +99,14 @@ protected:
*/ */
static std::string removeComments(const std::string &str); static std::string removeComments(const std::string &str);
/**
* Remove redundant parantheses from preprocessor commands. This should only be called from read().
* @param str Code processed by read().
* @return code with reduced parantheses
* @throws std::runtime_error when code contains unhandled characters
*/
static std::string removeParantheses(const std::string &str);
/** /**
* Returns the string between double quote characters or \< \> characters. * Returns the string between double quote characters or \< \> characters.
* @param str e.g. \code#include "menu.h"\endcode or \code#include <menu.h>\endcode * @param str e.g. \code#include "menu.h"\endcode or \code#include <menu.h>\endcode

View File

@ -342,14 +342,15 @@ private:
void test6() void test6()
{ {
const char filedata[] = "#if(AAA)\n" const char filedata[] = "#if(A)\n"
"#if(AAA)\n"; "#if ( A ) \n"
"#if A\n";
std::istringstream istr(filedata); std::istringstream istr(filedata);
const std::string actual(Preprocessor::read(istr)); const std::string actual(Preprocessor::read(istr));
// Compare results.. // Compare results..
ASSERT_EQUALS("#if (AAA)\n#if (AAA)\n", actual); ASSERT_EQUALS("#if A\n#if A\n#if A\n", actual);
} }
void test7() void test7()