Skip to content

Commit 1392ebd

Browse files
authored
Fix #700: macro not set for tokens expanded from __LINE__ inside function-like macros (#701)
1 parent 439ac67 commit 1392ebd

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

simplecpp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,14 +1994,17 @@ namespace simplecpp {
19941994

19951995
if (nameTokInst->str() == "__FILE__") {
19961996
output.push_back(new Token('\"'+output.file(loc)+'\"', loc));
1997+
output.back()->macro = "__FILE__";
19971998
return nameTokInst->next;
19981999
}
19992000
if (nameTokInst->str() == "__LINE__") {
20002001
output.push_back(new Token(toString(loc.line), loc));
2002+
output.back()->macro = "__LINE__";
20012003
return nameTokInst->next;
20022004
}
20032005
if (nameTokInst->str() == "__COUNTER__") {
20042006
output.push_back(new Token(toString(usageList.size()-1U), loc));
2007+
output.back()->macro = "__COUNTER__";
20052008
return nameTokInst->next;
20062009
}
20072010

test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3782,6 +3782,31 @@ static void tokenMacro5()
37823782
ASSERT_EQUALS("SET_BPF_JUMP", tok->macro);
37833783
}
37843784

3785+
static void tokenMacro6()
3786+
{
3787+
const char code[] = "#define FILE() __FILE__\n"
3788+
"#define LINE() __LINE__\n"
3789+
"#define COUNTER() __COUNTER__\n"
3790+
"FILE()\n"
3791+
"LINE()\n"
3792+
"COUNTER()\n";
3793+
std::vector<std::string> files;
3794+
simplecpp::FileDataCache cache;
3795+
simplecpp::TokenList tokenList(files);
3796+
const simplecpp::TokenList rawtokens = makeTokenList(code,files);
3797+
simplecpp::preprocess(tokenList, rawtokens, files, cache, simplecpp::DUI());
3798+
const simplecpp::Token *tok;
3799+
3800+
tok = tokenList.cfront();
3801+
ASSERT_EQUALS("__FILE__", tok->macro);
3802+
3803+
tok = tok->next;
3804+
ASSERT_EQUALS("__LINE__", tok->macro);
3805+
3806+
tok = tok->next;
3807+
ASSERT_EQUALS("__COUNTER__", tok->macro);
3808+
}
3809+
37853810
static void undef()
37863811
{
37873812
const char code[] = "#define A\n"
@@ -4781,6 +4806,7 @@ static void runTests(int argc, char **argv, Input input)
47814806
TEST_CASE(tokenMacro3);
47824807
TEST_CASE(tokenMacro4);
47834808
TEST_CASE(tokenMacro5);
4809+
TEST_CASE(tokenMacro6);
47844810

47854811
TEST_CASE(undef);
47864812

0 commit comments

Comments
 (0)