From bb2b2e000ba088ce1a3bd6cfcfec1d265c215974 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Wed, 20 May 2009 21:36:59 +0300 Subject: [PATCH] Fix ticket #316 (\n is tokenized into \\ in a string when macro is used) http://apps.sourceforge.net/trac/cppcheck/ticket/316 --- src/preprocessor.cpp | 5 ----- test/testpreprocessor.cpp | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index ba796c66f..4bc50fbe6 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -1130,11 +1130,6 @@ std::string Preprocessor::expandMacros(std::string code, const std::string &file while (pos2 < code.length() && code[pos2] != ch) { par += code[pos2]; - if (code[pos2] == '\\') - { - par += code[pos2]; - ++pos2; - } ++pos2; } if (pos2 == code.length()) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 3e11c9305..73e8ecf58 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -125,6 +125,7 @@ private: TEST_CASE(conditionalDefine); TEST_CASE(multiline_comment); TEST_CASE(macro_parameters); + TEST_CASE(newline_in_macro); } @@ -1046,6 +1047,27 @@ private: ASSERT_EQUALS("[file.c:6]: (error) Syntax error. Not enough parameters for macro 'BC'.\n", errout.str()); } + void newline_in_macro() + { + errout.str(""); + const char filedata[] = "#define ABC(str) printf( str )\n" + "void f()\n" + "{\n" + " ABC(\"\\n\");\n" + "}\n"; + + // Preprocess => actual result.. + std::istringstream istr(filedata); + std::map actual; + Preprocessor preprocessor; + preprocessor.preprocess(istr, actual, "file.c", std::list(), this); + + // Compare results.. + ASSERT_EQUALS(1, static_cast(actual.size())); + ASSERT_EQUALS("\nvoid f()\n{\nprintf(\"\\n\");\n}\n", actual[""]); + ASSERT_EQUALS("", errout.str()); + } + };