Preprocessor: Report correct file and line in message 'No pair for character..' (#261)

This commit is contained in:
Daniel Marjamäki 2009-04-26 11:05:32 +02:00
parent ebd15cec0f
commit 7b58e09a07
2 changed files with 78 additions and 30 deletions

View File

@ -789,23 +789,55 @@ std::string Preprocessor::expandMacros(std::string code, const std::string &file
// String or char..
if (code[pos1] == '\"' || code[pos1] == '\'')
{
//char ch = code[pos1];
// Find the end of the string/char..
++pos1;
while (code[pos1] != ch)
while (pos1 < code.size() && code[pos1] != ch && code[pos1] != '\n')
{
if (code[pos1] == '\\')
++pos1;
++pos1;
}
if (!code[pos1])
// End of line/file was reached without finding pair
if (pos1 >= code.size() || code[pos1] == '\n')
{
// End of file was reached without finding pair
if (errorLogger)
{
std::string fname(filename);
int lineno = 0;
for (std::string::size_type p = pos1; p > 0; --p)
{
// newline..
if (code[p-1] == '\n')
lineno++;
// #file..
else if (code[p-1] == '#')
{
// Previous char should be a newline..
if (p == 1 || code[p-2] == '\n')
{
// #file..
if (code.substr(p - 1, 6) == "#file ")
{
fname = code.substr(p + 5, code.find("\n", p) - p - 5);
break;
}
else
++lineno;
}
}
// start of file..
else if (p == 1)
++lineno;
}
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = 0;
loc.file = filename;
loc.line = lineno;
loc.file = fname;
locationList.push_back(loc);
errorLogger->reportErr(
ErrorLogger::ErrorMessage(locationList,
@ -815,7 +847,7 @@ std::string Preprocessor::expandMacros(std::string code, const std::string &file
}
return "";
}
}
continue;
}

View File

@ -821,6 +821,7 @@ private:
}
void missing_doublequote()
{
{
const char filedata[] = "#define a\n"
"#ifdef 1\n"
@ -829,10 +830,25 @@ private:
// expand macros..
errout.str("");
std::string actual = OurPreprocessor::expandMacros(filedata, this);
const std::string actual(OurPreprocessor::expandMacros(filedata, this));
ASSERT_EQUALS("", actual);
ASSERT_EQUALS("[file.cpp:0]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout.str());
ASSERT_EQUALS("[file.cpp:3]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout.str());
}
{
const char filedata[] = "#file abc.h\n"
"#define a\n"
"\"\n"
"#endfile\n";
// expand macros..
errout.str("");
const std::string actual(OurPreprocessor::expandMacros(filedata, this));
ASSERT_EQUALS("", actual);
ASSERT_EQUALS("[abc.h:2]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout.str());
}
}
};