Fix #1455 (Preprocessor: Wrong line number in "No pair for character" error)

http://sourceforge.net/apps/trac/cppcheck/ticket/1455
This commit is contained in:
Reijo Tomperi 2010-02-26 23:11:23 +02:00
parent 54bf67e766
commit 9b6336013b
2 changed files with 21 additions and 3 deletions

View File

@ -1856,10 +1856,11 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
std::string::size_type pos = 0; std::string::size_type pos = 0;
// scan line to see if there are any macros to expand.. // scan line to see if there are any macros to expand..
unsigned int tmpLinenr = 0;
while (pos < line.size()) while (pos < line.size())
{ {
if (line[pos] == '\n') if (line[pos] == '\n')
++linenr; ++tmpLinenr;
// skip strings.. // skip strings..
if (line[pos] == '\"' || line[pos] == '\'') if (line[pos] == '\"' || line[pos] == '\'')
@ -1872,7 +1873,7 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
if (pos >= line.size()) if (pos >= line.size())
{ {
writeError(filename, writeError(filename,
linenr, linenr + tmpLinenr,
errorLogger, errorLogger,
"noQuoteCharPair", "noQuoteCharPair",
std::string("No pair for character (") + ch + "). Can't process file. File is either invalid or unicode, which is currently not supported."); std::string("No pair for character (") + ch + "). Can't process file. File is either invalid or unicode, which is currently not supported.");
@ -2031,7 +2032,7 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file
{ {
// Syntax error in code // Syntax error in code
writeError(filename, writeError(filename,
linenr, linenr + tmpLinenr,
errorLogger, errorLogger,
"syntaxError", "syntaxError",
std::string("Syntax error. Not enough parameters for macro '") + macro->name() + "'."); std::string("Syntax error. Not enough parameters for macro '") + macro->name() + "'.");

View File

@ -1667,6 +1667,23 @@ private:
ASSERT_EQUALS("\n\nint a = 1;\n", actual); ASSERT_EQUALS("\n\nint a = 1;\n", actual);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{
const char filedata[] = "void foo()\n"
"{\n"
"\n"
"\n"
"\n"
"int a = 0;\n"
"printf(Text\");\n"
"}\n";
// expand macros..
errout.str("");
const std::string actual(OurPreprocessor::expandMacros(filedata, this));
ASSERT_EQUALS("[file.cpp:7]: (error) No pair for character (\"). Can't process file. File is either invalid or unicode, which is currently not supported.\n", errout.str());
}
} }
void unicodeInCode() void unicodeInCode()