From 9b6336013be990dd159d80398e0bc0790032b4c6 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Fri, 26 Feb 2010 23:11:23 +0200 Subject: [PATCH] Fix #1455 (Preprocessor: Wrong line number in "No pair for character" error) http://sourceforge.net/apps/trac/cppcheck/ticket/1455 --- lib/preprocessor.cpp | 7 ++++--- test/testpreprocessor.cpp | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index a538b41c7..f861f37f5 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1856,10 +1856,11 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file std::string::size_type pos = 0; // scan line to see if there are any macros to expand.. + unsigned int tmpLinenr = 0; while (pos < line.size()) { if (line[pos] == '\n') - ++linenr; + ++tmpLinenr; // skip strings.. if (line[pos] == '\"' || line[pos] == '\'') @@ -1872,7 +1873,7 @@ std::string Preprocessor::expandMacros(const std::string &code, std::string file if (pos >= line.size()) { writeError(filename, - linenr, + linenr + tmpLinenr, errorLogger, "noQuoteCharPair", 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 writeError(filename, - linenr, + linenr + tmpLinenr, errorLogger, "syntaxError", std::string("Syntax error. Not enough parameters for macro '") + macro->name() + "'."); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 3256ca87b..0b58d1ea6 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -1667,6 +1667,23 @@ private: ASSERT_EQUALS("\n\nint a = 1;\n", actual); 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()