preprocessor: handle the \newline in strings

This commit is contained in:
Daniel Marjamäki 2009-01-10 07:07:51 +00:00
parent febdc3fe6e
commit cd9323ca62
1 changed files with 22 additions and 8 deletions

View File

@ -101,21 +101,29 @@ std::string Preprocessor::read(std::istream &istr)
// String constants.. // String constants..
else if (ch == '\"') else if (ch == '\"')
{ {
int newlines = 0;
code << "\""; code << "\"";
do do
{ {
ch = (char)istr.get(); ch = (char)istr.get();
code << std::string(1, ch); if ( ch == '\\' )
if (ch == '\\')
{ {
ch = (char)istr.get(); char chNext = (char)istr.get();
code << std::string(1, ch); if ( chNext == '\n' )
++newlines;
// Avoid exiting loop if string contains "-characters else
ch = 0; {
code << std::string(1, ch);
code << std::string(1, chNext);
}
} }
else
code << std::string(1, ch);
} }
while (istr.good() && ch != '\"'); while (istr.good() && ch != '\"');
// Insert extra newlines after the string in case the string contained \newline sequences
code << std::string(newlines, '\n');
} }
// char constants.. // char constants..
@ -418,8 +426,14 @@ std::string Preprocessor::expandMacros(std::string code)
{ {
// Search for macros and expand them.. // Search for macros and expand them..
std::string::size_type defpos = 0; std::string::size_type defpos = 0;
while ((defpos = code.find("#define", defpos)) != std::string::npos) while ((defpos = code.find("#define ", defpos)) != std::string::npos)
{ {
if ( defpos > 0 && code[defpos-1] != '\n' )
{
defpos += 6;
continue;
}
// Get macro.. // Get macro..
std::string::size_type endpos = code.find("\n", defpos + 6); std::string::size_type endpos = code.find("\n", defpos + 6);
while (endpos != std::string::npos && code[endpos-1] == '\\') while (endpos != std::string::npos && code[endpos-1] == '\\')