diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 0f43a7615..15124b489 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -1529,11 +1529,10 @@ public: continue; if (str[0] == '#' || tok->isName()) { - bool stringify = false; - if (str[0] == '#') + const bool stringify(str[0] == '#'); + if (stringify) { str = str.erase(0, 1); - stringify = true; } for (unsigned int i = 0; i < _params.size(); ++i) { @@ -1557,7 +1556,18 @@ public: return false; } else if (stringify) - str = "\"" + params2[i] + "\""; + { + const std::string &s(params2[i]); + std::ostringstream ostr; + ostr << "\""; + for (std::string::size_type i = 0; i < s.size(); ++i) + { + if (s[i] == '\\' || s[i] == '\"') + ostr << '\\'; + ostr << s[i]; + } + str = ostr.str() + "\""; + } else str = params2[i]; diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 05e8542eb..bb8d567c6 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -136,6 +136,7 @@ private: TEST_CASE(stringify2); TEST_CASE(stringify3); TEST_CASE(stringify4); + TEST_CASE(stringify5); TEST_CASE(ifdefwithfile); TEST_CASE(pragma); TEST_CASE(pragma_asm); @@ -1211,6 +1212,13 @@ private: ASSERT_EQUALS("\n1 \n\n\"abc\" 2", actual); } + void stringify5() + { + const char filedata[] = "#define A(x) a(#x,x)\n" + "A(foo(\"\\\"\"))\n"; + ASSERT_EQUALS("\na(\"foo(\\\"\\\\\\\"\\\")\",foo(\"\\\"\"))\n", OurPreprocessor::expandMacros(filedata)); + } + void pragma() { const char filedata[] = "#pragma once\n"