Visitar URL original
Fixed #79 (Incorrectly preprocessed cpp file) · versat/simplecpp@f0a9cee · GitHub
Skip to content

Commit f0a9cee

Browse files
committed
Fixed cppcheck-opensource#79 (Incorrectly preprocessed cpp file)
1 parent 2a0c07f commit f0a9cee

2 files changed

Lines changed: 68 additions & 40 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,41 @@ namespace simplecpp {
16521652
return functionLike() ? parametertokens2.back()->next : nameTokInst->next;
16531653
}
16541654

1655+
const Token *recursiveExpandToken(TokenList *output, TokenList &temp, const Location &loc, const Token *tok, const std::map<TokenString,Macro> &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
1656+
if (!(temp.cback() && temp.cback()->name && tok->next && tok->next->op == '(')) {
1657+
output->takeTokens(temp);
1658+
return tok->next;
1659+
}
1660+
1661+
if (!sameline(tok, tok->next)) {
1662+
output->takeTokens(temp);
1663+
return tok->next;
1664+
}
1665+
1666+
const std::map<TokenString, Macro>::const_iterator it = macros.find(temp.cback()->str());
1667+
if (it == macros.end() || expandedmacros.find(temp.cback()->str()) != expandedmacros.end()) {
1668+
output->takeTokens(temp);
1669+
return tok->next;
1670+
}
1671+
1672+
const Macro &calledMacro = it->second;
1673+
if (!calledMacro.functionLike()) {
1674+
output->takeTokens(temp);
1675+
return tok->next;
1676+
}
1677+
1678+
TokenList temp2(files);
1679+
temp2.push_back(new Token(temp.cback()->str(), tok->location));
1680+
1681+
const Token *tok2 = appendTokens(&temp2, loc, tok->next, macros, expandedmacros, parametertokens);
1682+
if (!tok2)
1683+
return tok->next;
1684+
output->takeTokens(temp);
1685+
output->deleteToken(output->back());
1686+
calledMacro.expand(output, loc, temp2.cfront(), macros, expandedmacros);
1687+
return tok2->next;
1688+
}
1689+
16551690
const Token *expandToken(TokenList *output, const Location &loc, const Token *tok, const std::map<TokenString,Macro> &macros, const std::set<TokenString> &expandedmacros, const std::vector<const Token*> &parametertokens) const {
16561691
// Not name..
16571692
if (!tok->name) {
@@ -1662,50 +1697,22 @@ namespace simplecpp {
16621697
// Macro parameter..
16631698
{
16641699
TokenList temp(files);
1665-
if (expandArg(&temp, tok, loc, macros, expandedmacros, parametertokens)) {
1666-
if (!(temp.cback() && temp.cback()->name && tok->next && tok->next->op == '(')) {
1667-
output->takeTokens(temp);
1668-
return tok->next;
1669-
}
1670-
1671-
if (!sameline(tok, tok->next)) {
1672-
output->takeTokens(temp);
1673-
return tok->next;
1674-
}
1675-
1676-
const std::map<TokenString, Macro>::const_iterator it = macros.find(temp.cback()->str());
1677-
if (it == macros.end() || expandedmacros.find(temp.cback()->str()) != expandedmacros.end()) {
1678-
output->takeTokens(temp);
1679-
return tok->next;
1680-
}
1681-
1682-
const Macro &calledMacro = it->second;
1683-
if (!calledMacro.functionLike()) {
1684-
output->takeTokens(temp);
1685-
return tok->next;
1686-
}
1687-
1688-
TokenList temp2(files);
1689-
temp2.push_back(new Token(temp.cback()->str(), tok->location));
1690-
1691-
const Token *tok2 = appendTokens(&temp2, loc, tok->next, macros, expandedmacros, parametertokens);
1692-
if (!tok2)
1693-
return tok->next;
1694-
1695-
output->takeTokens(temp);
1696-
output->deleteToken(output->back());
1697-
calledMacro.expand(output, loc, temp2.cfront(), macros, expandedmacros);
1698-
1699-
return tok2->next;
1700-
}
1700+
if (expandArg(&temp, tok, loc, macros, expandedmacros, parametertokens))
1701+
return recursiveExpandToken(output, temp, loc, tok, macros, expandedmacros, parametertokens);
17011702
}
17021703

17031704
// Macro..
17041705
const std::map<TokenString, Macro>::const_iterator it = macros.find(tok->str());
17051706
if (it != macros.end() && expandedmacros.find(tok->str()) == expandedmacros.end()) {
1707+
std::set<std::string> expandedmacros2(expandedmacros);
1708+
expandedmacros2.insert(tok->str());
1709+
17061710
const Macro &calledMacro = it->second;
1707-
if (!calledMacro.functionLike())
1708-
return calledMacro.expand(output, loc, tok, macros, expandedmacros);
1711+
if (!calledMacro.functionLike()) {
1712+
TokenList temp(files);
1713+
calledMacro.expand(&temp, loc, tok, macros, expandedmacros);
1714+
return recursiveExpandToken(output, temp, loc, tok, macros, expandedmacros2, parametertokens);
1715+
}
17091716
if (!sameline(tok, tok->next) || tok->next->op != '(') {
17101717
output->push_back(newMacroToken(tok->str(), loc, true));
17111718
return tok->next;
@@ -1717,8 +1724,9 @@ namespace simplecpp {
17171724
output->push_back(newMacroToken(tok->str(), loc, true));
17181725
return tok->next;
17191726
}
1720-
calledMacro.expand(output, loc, tokens.cfront(), macros, expandedmacros);
1721-
return tok2->next;
1727+
TokenList temp(files);
1728+
calledMacro.expand(&temp, loc, tokens.cfront(), macros, expandedmacros);
1729+
return recursiveExpandToken(output, temp, loc, tok2, macros, expandedmacros2, parametertokens);
17221730
}
17231731

17241732
else if (tok->str() == DEFINED) {

‎test.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,24 @@ static void define_define_14() // issue #58 - endless recursion
485485
ASSERT_EQUALS("\n\n\nf ( f ( w", preprocess(code)); // Don't crash
486486
}
487487

488+
static void define_define_15() // issue #72 without __VA_ARGS__
489+
{
490+
const char code[] = "#define a f\n"
491+
"#define foo(x,y) a(x,y)\n"
492+
"#define f(x, y) x y\n"
493+
"foo(1,2)";
494+
ASSERT_EQUALS("\n\n\n1 2", preprocess(code));
495+
}
496+
497+
static void define_define_16() // issue #72 with __VA_ARGS__
498+
{
499+
const char code[] = "#define ab(a, b) a##b\n"
500+
"#define foo(...) ab(f, 2) (__VA_ARGS__)\n"
501+
"#define f2(x, y) x y\n"
502+
"foo(1,2)";
503+
ASSERT_EQUALS("\n\n\n1 2", preprocess(code));
504+
}
505+
488506
static void define_va_args_1()
489507
{
490508
const char code[] = "#define A(fmt...) dostuff(fmt)\n"
@@ -1909,6 +1927,8 @@ int main(int argc, char **argv)
19091927
TEST_CASE(define_define_12); // expand result of ##
19101928
TEST_CASE(define_define_13);
19111929
TEST_CASE(define_define_14);
1930+
TEST_CASE(define_define_15);
1931+
TEST_CASE(define_define_16);
19121932
TEST_CASE(define_va_args_1);
19131933
TEST_CASE(define_va_args_2);
19141934
TEST_CASE(define_va_args_3);

0 commit comments

Comments
 (0)