Skip to content

Commit 1537e9e

Browse files
authored
Fix #680: Add option to keep comments in preprocessed code (#681)
1 parent df97c30 commit 1537e9e

3 files changed

Lines changed: 40 additions & 10 deletions

File tree

main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ int main(int argc, char **argv)
242242
f.close();
243243
rawtokens = new simplecpp::TokenList(filename,files,dui,&outputList);
244244
}
245-
rawtokens->removeComments();
245+
if (dui.removeComments)
246+
rawtokens->removeComments();
246247
simplecpp::FileDataCache filedata;
247248
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
248249
simplecpp::cleanup(filedata);

simplecpp.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3411,7 +3411,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
34113411
return cache;
34123412
}
34133413

3414-
static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token *&tok1, simplecpp::MacroMap &macros, std::vector<std::string> &files, simplecpp::OutputList *outputList)
3414+
static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token *&tok1, simplecpp::MacroMap &macros, std::vector<std::string> &files, simplecpp::OutputList *outputList, const simplecpp::DUI &dui)
34153415
{
34163416
const simplecpp::Token * const tok = tok1;
34173417
const simplecpp::MacroMap::const_iterator it = tok->name ? macros.find(tok->str()) : macros.end();
@@ -3442,7 +3442,7 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token
34423442
}
34433443
output.takeTokens(value);
34443444
} else {
3445-
if (!tok->comment)
3445+
if (!tok->comment || !dui.removeComments)
34463446
output.push_back(new simplecpp::Token(*tok));
34473447
tok1 = tok->next;
34483448
}
@@ -3717,7 +3717,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37173717
TokenList inc2(files);
37183718
if (!inc1.empty() && inc1.cfront()->name) {
37193719
const Token *inctok = inc1.cfront();
3720-
if (!preprocessToken(inc2, inctok, macros, files, outputList)) {
3720+
if (!preprocessToken(inc2, inctok, macros, files, outputList, dui)) {
37213721
output.clear();
37223722
return;
37233723
}
@@ -3899,7 +3899,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
38993899
maybeUsedMacros[rawtok->next->str()].emplace_back(rawtok->next->location);
39003900

39013901
const Token *tmp = tok;
3902-
if (!preprocessToken(expr, tmp, macros, files, outputList)) {
3902+
if (!preprocessToken(expr, tmp, macros, files, outputList, dui)) {
39033903
output.clear();
39043904
return;
39053905
}
@@ -3997,7 +3997,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
39973997
const Location loc(rawtok->location);
39983998
TokenList tokens(files);
39993999

4000-
if (!preprocessToken(tokens, rawtok, macros, files, outputList)) {
4000+
if (!preprocessToken(tokens, rawtok, macros, files, outputList, dui)) {
40014001
output.clear();
40024002
return;
40034003
}

test.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,15 @@ static void combineOperators_ellipsis()
533533

534534
static void comment()
535535
{
536+
simplecpp::DUI dui;
537+
dui.removeComments = true;
538+
536539
ASSERT_EQUALS("// abc", readfile("// abc"));
537-
ASSERT_EQUALS("", preprocess("// abc"));
540+
ASSERT_EQUALS("", preprocess("// abc", dui));
538541
ASSERT_EQUALS("/*\n\n*/abc", readfile("/*\n\n*/abc"));
539-
ASSERT_EQUALS("\n\nabc", preprocess("/*\n\n*/abc"));
542+
ASSERT_EQUALS("\n\nabc", preprocess("/*\n\n*/abc", dui));
540543
ASSERT_EQUALS("* p = a / * b / * c ;", readfile("*p=a/ *b/ *c;"));
541-
ASSERT_EQUALS("* p = a / * b / * c ;", preprocess("*p=a/ *b/ *c;"));
544+
ASSERT_EQUALS("* p = a / * b / * c ;", preprocess("*p=a/ *b/ *c;", dui));
542545
}
543546

544547
static void comment_multiline()
@@ -578,6 +581,27 @@ static void comment_multiline()
578581
ASSERT_EQUALS("// abc\ndef", readfile("// abc\\\ndef"));
579582
}
580583

584+
static void keep_comments()
585+
{
586+
simplecpp::DUI dui;
587+
dui.removeComments = false;
588+
589+
{
590+
const char code[] = "/* comment */\n";
591+
ASSERT_EQUALS("/* comment */", preprocess(code,dui));
592+
}
593+
594+
{
595+
const char code[] = "// comment\n";
596+
ASSERT_EQUALS("// comment", preprocess(code,dui));
597+
}
598+
599+
{
600+
const char code[] = "#define MACRO /* comment */\nMACRO\n";
601+
ASSERT_EQUALS("\n/* comment */", preprocess(code,dui));
602+
}
603+
}
604+
581605

582606
static void constFold()
583607
{
@@ -2584,13 +2608,16 @@ static void location11()
25842608

25852609
static void location12()
25862610
{
2611+
simplecpp::DUI dui;
2612+
dui.removeComments = true;
2613+
25872614
const char code[] =
25882615
"/**//**/#/**//**/line/**//**/3/**//**/\"file.c\"/**/\n"
25892616
"__LINE__ __FILE__\n";
25902617
ASSERT_EQUALS("\n"
25912618
"#line 3 \"file.c\"\n"
25922619
"3 \"file.c\"",
2593-
preprocess(code));
2620+
preprocess(code, dui));
25942621
}
25952622

25962623

@@ -3244,6 +3271,7 @@ static void include9()
32443271
simplecpp::TokenList out(files);
32453272
simplecpp::DUI dui;
32463273
dui.includePaths.emplace_back(".");
3274+
dui.removeComments = true;
32473275
simplecpp::preprocess(out, rawtokens_c, files, cache, dui);
32483276

32493277
ASSERT_EQUALS("\n#line 2 \"1.h\"\nx = 1 ;", out.stringify());
@@ -4383,6 +4411,7 @@ static void runTests(int argc, char **argv, Input input)
43834411

43844412
TEST_CASE(comment);
43854413
TEST_CASE(comment_multiline);
4414+
TEST_CASE(keep_comments);
43864415

43874416
TEST_CASE(constFold);
43884417

0 commit comments

Comments
 (0)