Visitar URL original
Set Token::macro in nested macros · bavison/simplecpp@326e4eb · GitHub
Skip to content

Commit 326e4eb

Browse files
committed
Set Token::macro in nested macros
1 parent da202c3 commit 326e4eb

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎simplecpp.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,8 +1504,11 @@ namespace simplecpp {
15041504
expanded = true;
15051505
}
15061506
}
1507-
if (!expanded)
1507+
if (!expanded) {
15081508
tokens->push_back(new Token(*tok));
1509+
if (tok->macro.empty() && (par > 0 || tok->str() != "("))
1510+
tokens->back()->macro = name();
1511+
}
15091512
}
15101513

15111514
if (tok->op == '(')
@@ -1812,6 +1815,7 @@ namespace simplecpp {
18121815
partok = it->second.expand(output, loc, partok, macros, expandedmacros);
18131816
else {
18141817
output->push_back(newMacroToken(partok->str(), loc, isReplaced(expandedmacros)));
1818+
output->back()->macro = partok->macro;
18151819
partok = partok->next;
18161820
}
18171821
}

‎test.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,22 @@ static void tokenMacro4()
20072007
ASSERT_EQUALS("A", tok->macro);
20082008
}
20092009

2010+
static void tokenMacro5()
2011+
{
2012+
const char code[] = "#define SET_BPF(code) (code)\n"
2013+
"#define SET_BPF_JUMP(code) SET_BPF(D | code)\n"
2014+
"SET_BPF_JUMP(A | B | C);";
2015+
const simplecpp::DUI dui;
2016+
std::vector<std::string> files;
2017+
std::map<std::string, simplecpp::TokenList*> filedata;
2018+
std::istringstream istr(code);
2019+
simplecpp::TokenList tokenList(files);
2020+
simplecpp::preprocess(tokenList, simplecpp::TokenList(istr,files), files, filedata, dui);
2021+
const simplecpp::Token *tok = tokenList.cfront()->next;
2022+
ASSERT_EQUALS("D", tok->str());
2023+
ASSERT_EQUALS("SET_BPF_JUMP", tok->macro);
2024+
}
2025+
20102026
static void undef()
20112027
{
20122028
std::istringstream istr("#define A\n"
@@ -2327,6 +2343,7 @@ int main(int argc, char **argv)
23272343
TEST_CASE(tokenMacro2);
23282344
TEST_CASE(tokenMacro3);
23292345
TEST_CASE(tokenMacro4);
2346+
TEST_CASE(tokenMacro5);
23302347

23312348
TEST_CASE(undef);
23322349

0 commit comments

Comments
 (0)