From 939c3b80ffd4a52450ab28a5b4b946289d866fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 13 Jul 2026 14:11:53 +0200 Subject: [PATCH 1/3] fix --- main.cpp | 3 ++- simplecpp.cpp | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 06afcfbb..fa3417dd 100644 --- a/main.cpp +++ b/main.cpp @@ -242,7 +242,8 @@ int main(int argc, char **argv) f.close(); rawtokens = new simplecpp::TokenList(filename,files,&outputList); } - rawtokens->removeComments(); + if (dui.removeComments) + rawtokens->removeComments(); simplecpp::FileDataCache filedata; simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList); simplecpp::cleanup(filedata); diff --git a/simplecpp.cpp b/simplecpp.cpp index 748ea6a9..3a779e58 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -3341,7 +3341,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens, return cache; } -static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token *&tok1, simplecpp::MacroMap ¯os, std::vector &files, simplecpp::OutputList *outputList) +static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token *&tok1, simplecpp::MacroMap ¯os, std::vector &files, simplecpp::OutputList *outputList, const simplecpp::DUI &dui) { const simplecpp::Token * const tok = tok1; const simplecpp::MacroMap::const_iterator it = tok->name ? macros.find(tok->str()) : macros.end(); @@ -3372,7 +3372,7 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token } output.takeTokens(value); } else { - if (!tok->comment) + if (!tok->comment || !dui.removeComments) output.push_back(new simplecpp::Token(*tok)); tok1 = tok->next; } @@ -3647,7 +3647,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL TokenList inc2(files); if (!inc1.empty() && inc1.cfront()->name) { const Token *inctok = inc1.cfront(); - if (!preprocessToken(inc2, inctok, macros, files, outputList)) { + if (!preprocessToken(inc2, inctok, macros, files, outputList, dui)) { output.clear(); return; } @@ -3817,7 +3817,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL maybeUsedMacros[rawtok->next->str()].emplace_back(rawtok->next->location); const Token *tmp = tok; - if (!preprocessToken(expr, tmp, macros, files, outputList)) { + if (!preprocessToken(expr, tmp, macros, files, outputList, dui)) { output.clear(); return; } @@ -3915,7 +3915,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL const Location loc(rawtok->location); TokenList tokens(files); - if (!preprocessToken(tokens, rawtok, macros, files, outputList)) { + if (!preprocessToken(tokens, rawtok, macros, files, outputList, dui)) { output.clear(); return; } From 85582557449f57a7c3f45d3b91ca8ce237f3cffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 13 Jul 2026 14:12:05 +0200 Subject: [PATCH 2/3] update tests --- test.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test.cpp b/test.cpp index 990a6daf..4bb15a99 100644 --- a/test.cpp +++ b/test.cpp @@ -517,12 +517,15 @@ static void combineOperators_ellipsis() static void comment() { + simplecpp::DUI dui; + dui.removeComments = true; + ASSERT_EQUALS("// abc", readfile("// abc")); - ASSERT_EQUALS("", preprocess("// abc")); + ASSERT_EQUALS("", preprocess("// abc", dui)); ASSERT_EQUALS("/*\n\n*/abc", readfile("/*\n\n*/abc")); - ASSERT_EQUALS("\n\nabc", preprocess("/*\n\n*/abc")); + ASSERT_EQUALS("\n\nabc", preprocess("/*\n\n*/abc", dui)); ASSERT_EQUALS("* p = a / * b / * c ;", readfile("*p=a/ *b/ *c;")); - ASSERT_EQUALS("* p = a / * b / * c ;", preprocess("*p=a/ *b/ *c;")); + ASSERT_EQUALS("* p = a / * b / * c ;", preprocess("*p=a/ *b/ *c;", dui)); } static void comment_multiline() @@ -2568,13 +2571,16 @@ static void location11() static void location12() { + simplecpp::DUI dui; + dui.removeComments = true; + const char code[] = "/**//**/#/**//**/line/**//**/3/**//**/\"file.c\"/**/\n" "__LINE__ __FILE__\n"; ASSERT_EQUALS("\n" "#line 3 \"file.c\"\n" "3 \"file.c\"", - preprocess(code)); + preprocess(code, dui)); } static void missingHeader1() @@ -3012,6 +3018,7 @@ static void include9() simplecpp::TokenList out(files); simplecpp::DUI dui; dui.includePaths.emplace_back("."); + dui.removeComments = true; simplecpp::preprocess(out, rawtokens_c, files, cache, dui); ASSERT_EQUALS("\n#line 2 \"1.h\"\nx = 1 ;", out.stringify()); From 04b4e4514ffef5433d82b3c53833945d893b1b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Mon, 13 Jul 2026 14:12:11 +0200 Subject: [PATCH 3/3] add test --- test.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test.cpp b/test.cpp index 4bb15a99..d2b1cac6 100644 --- a/test.cpp +++ b/test.cpp @@ -565,6 +565,27 @@ static void comment_multiline() ASSERT_EQUALS("// abc\ndef", readfile("// abc\\\ndef")); } +static void keep_comments() +{ + simplecpp::DUI dui; + dui.removeComments = false; + + { + const char code[] = "/* comment */\n"; + ASSERT_EQUALS("/* comment */", preprocess(code,dui)); + } + + { + const char code[] = "// comment\n"; + ASSERT_EQUALS("// comment", preprocess(code,dui)); + } + + { + const char code[] = "#define MACRO /* comment */\nMACRO\n"; + ASSERT_EQUALS("\n/* comment */", preprocess(code,dui)); + } +} + static void constFold() { @@ -4073,6 +4094,7 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(comment); TEST_CASE(comment_multiline); + TEST_CASE(keep_comments); TEST_CASE(constFold);