From 2b2f12bcd531adafad8184f9bbe048a3cd6194d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 28 May 2016 11:27:45 +0200 Subject: [PATCH] Fixed #6758 (Preprocessor: handle #__VA_ARGS__) --- lib/preprocessor.cpp | 28 ++++++++++++++++++---------- test/testpreprocessor.cpp | 11 +++++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 2efa76e80..737c717c6 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -2739,6 +2739,13 @@ public: pos = 0; while ((pos = macrocode.find("__VA_ARGS__", pos)) != std::string::npos) { macrocode.erase(pos, 11); + if (pos > 0U && + macrocode[pos-1U] == '#' && + (pos == 1U || macrocode[pos-2U]!='#')) { + --pos; + macrocode.erase(pos,1U); + s = '\"' + s + '\"'; + } macrocode.insert(pos, s); pos += s.length(); } @@ -2792,16 +2799,6 @@ public: // Macro had more parameters than caller used. macrocode = ""; return false; - } else if (stringify) { - const std::string &s(givenparams[i]); - std::ostringstream ostr; - ostr << "\""; - for (std::string::size_type j = 0; j < s.size(); ++j) { - if (s[j] == '\\' || s[j] == '\"') - ostr << '\\'; - ostr << s[j]; - } - str = ostr.str() + "\""; } else str = givenparams[i]; @@ -2809,6 +2806,17 @@ public: } } + if (stringify) { + std::ostringstream ostr; + ostr << "\""; + for (std::string::size_type j = 0; j < str.size(); ++j) { + if (str[j] == '\\' || str[j] == '\"') + ostr << '\\'; + ostr << str[j]; + } + str = ostr.str() + "\""; + } + // expand nopar macro if (tok->strAt(-1) != "##") { const std::map::const_iterator it = macros.find(str); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index b6ae9a3a2..ee062dc51 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -198,6 +198,7 @@ private: TEST_CASE(va_args_2); TEST_CASE(va_args_3); TEST_CASE(va_args_4); + TEST_CASE(va_args_5); TEST_CASE(multi_character_character); TEST_CASE(stringify); @@ -2139,6 +2140,16 @@ private: ASSERT_EQUALS("\n$abc($123)\n", OurPreprocessor::expandMacros(filedata)); } + void va_args_5() { + const char filedata1[] = "#define A(...) #__VA_ARGS__\n" + "A(123)\n"; + ASSERT_EQUALS("\n$\"123\"\n", OurPreprocessor::expandMacros(filedata1)); + + const char filedata2[] = "#define A(X,...) X(#__VA_ARGS__)\n" + "A(f,123)\n"; + ASSERT_EQUALS("\n$f(\"123\")\n", OurPreprocessor::expandMacros(filedata2)); + } + void multi_character_character() {