Fixed #6758 (Preprocessor: handle #__VA_ARGS__)

This commit is contained in:
Daniel Marjamäki 2016-05-28 11:27:45 +02:00
parent cee67730b1
commit 2b2f12bcd5
2 changed files with 29 additions and 10 deletions

View File

@ -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<std::string, PreprocessorMacro *>::const_iterator it = macros.find(str);

View File

@ -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() {