diff --git a/externals/simplecpp/simplecpp.cpp b/externals/simplecpp/simplecpp.cpp index a75bb57e2..9c6299dda 100644 --- a/externals/simplecpp/simplecpp.cpp +++ b/externals/simplecpp/simplecpp.cpp @@ -1058,16 +1058,26 @@ private: unsigned int par = 0; const Token *tok = lpar; while (sameline(lpar, tok)) { - if (!expandArg(tokens, tok, tok->location, macros, expandedmacros1, expandedmacros, parametertokens)) - tokens->push_back(new Token(*tok)); - if (tok->op == '(') - ++par; - else if (tok->op == ')') { - --par; - if (par == 0U) - break; + if (tok->op == '#' && sameline(tok,tok->next) && tok->next->op == '#' && sameline(tok,tok->next->next)) { + // A##B => AB + const std::string strB(expandArgStr(tok->next->next, parametertokens)); + if (variadic && strB.empty() && tok->previous->op == ',') + tokens->deleteToken(tokens->back()); + else + tokens->back()->setstr(tokens->back()->str + strB); + tok = tok->next->next->next; + } else { + if (!expandArg(tokens, tok, tok->location, macros, expandedmacros1, expandedmacros, parametertokens)) + tokens->push_back(new Token(*tok)); + if (tok->op == '(') + ++par; + else if (tok->op == ')') { + --par; + if (par == 0U) + break; + } + tok = tok->next; } - tok = tok->next; } return sameline(lpar,tok) ? tok : NULL; } @@ -1196,10 +1206,16 @@ private: // #123 => "123" TokenList tokenListHash(files); tok = expandToken(&tokenListHash, loc, tok, macros, expandedmacros1, expandedmacros, parametertokens2); - std::string s; - for (const Token *hashtok = tokenListHash.cfront(); hashtok; hashtok = hashtok->next) - s += hashtok->str; - output->push_back(newMacroToken('\"' + s + '\"', loc, expandedmacros1.empty())); + std::ostringstream ostr; + for (const Token *hashtok = tokenListHash.cfront(); hashtok; hashtok = hashtok->next) { + for (unsigned int i = 0; i < hashtok->str.size(); i++) { + unsigned char c = hashtok->str[i]; + if (c == '\"' || c == '\\' || c == '\'') + ostr << '\\'; + ostr << c; + } + } + output->push_back(newMacroToken('\"' + ostr.str() + '\"', loc, expandedmacros1.empty())); } } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 4533a93eb..e298c79ed 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -1446,7 +1446,7 @@ private: void stringify5() const { const char filedata[] = "#define A(x) a(#x,x)\n" "A(foo(\"\\\"\"))\n"; - ASSERT_EQUALS("\na ( \"foo(\"\\\"\")\" , foo ( \"\\\"\" ) )", OurPreprocessor::expandMacros(filedata)); + ASSERT_EQUALS("\na ( \"foo(\\\"\\\\\\\"\\\")\" , foo ( \"\\\"\" ) )", OurPreprocessor::expandMacros(filedata)); } void pragma() {