Preprocessor: Handle C++0x rawstrings by replacing them with normal strings. Ticket: #2022

This commit is contained in:
Daniel Marjamäki 2010-09-13 19:36:40 +02:00
parent b1d74ed6b8
commit b0bb71ee20
2 changed files with 73 additions and 2 deletions

View File

@ -334,6 +334,67 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri
while (i < str.length() && chNext != ch && chNext != '\n');
}
// Rawstring..
else if (str.compare(i,2,"R\"")==0)
{
std::string delim;
for (std::string::size_type i2 = i+2; i2 < str.length(); ++i2)
{
if (i2 > 16 ||
std::isspace(str[i2]) ||
std::iscntrl(str[i2]) ||
str[i2] == ')' ||
str[i2] == '\\')
{
delim = " ";
break;
}
else if (str[i2] == '(')
break;
delim += str[i2];
}
const std::string::size_type endpos = str.find(")" + delim + "\"", i);
if (delim != " " && endpos != std::string::npos)
{
unsigned int rawstringnewlines = 0;
code << '\"';
for (std::string::size_type p = i + 3 + delim.size(); p < endpos; ++p)
{
if (str[p] == '\n')
{
rawstringnewlines++;
code << "\\n";
}
else if (std::iscntrl((unsigned char)str[p]) ||
std::isspace((unsigned char)str[p]))
{
code << " ";
}
else if (str[p] == '\\')
{
code << "\\";
}
else if (str[p] == '\"' || str[p] == '\'')
{
code << "\\" << (char)str[p];
}
else
{
code << (char)str[p];
}
}
code << "\"";
if (rawstringnewlines > 0)
code << std::string(rawstringnewlines, '\n');
i = endpos + delim.size() + 2;
}
else
{
code << "R";
previous = 'R';
}
}
// Just some code..
else

View File

@ -65,7 +65,8 @@ private:
void run()
{
// Just read the code into a string. Perform simple cleanup of the code
TEST_CASE(readCode);
TEST_CASE(readCode1);
TEST_CASE(readCode2);
// The bug that started the whole work with the new preprocessor
TEST_CASE(Bug2190219);
@ -192,7 +193,7 @@ private:
}
void readCode()
void readCode1()
{
const char code[] = " \t a //\n"
" #aa\t /* remove this */\tb \r\n";
@ -202,6 +203,15 @@ private:
ASSERT_EQUALS("a \n#aa b \n", codestr);
}
void readCode2()
{
const char code[] = "R\"( \" /* abc */ \n)\"";
Preprocessor preprocessor(0,this);
std::istringstream istr(code);
std::string codestr(preprocessor.read(istr,"test.c",0));
ASSERT_EQUALS("\" \\\" /* abc */ \\n\"\n", codestr);
}
void Bug2190219()
{