diff --git a/main.cpp b/main.cpp index 424ef6fa..7faefa9e 100644 --- a/main.cpp +++ b/main.cpp @@ -19,6 +19,7 @@ int main(int argc, char **argv) const char *filename = nullptr; bool use_istream = false; bool fail_on_error = false; + bool keep_comments = false; // Settings.. simplecpp::DUI dui; @@ -75,6 +76,10 @@ int main(int argc, char **argv) fail_on_error = true; found = true; break; + case 'C': + keep_comments = true; + found = true; + break; } if (!found) { std::cout << "error: option '" << arg << "' is unknown." << std::endl; @@ -107,10 +112,11 @@ int main(int argc, char **argv) std::cout << " -q Quiet mode (no output)." << std::endl; std::cout << " -is Use std::istream interface." << std::endl; std::cout << " -e Output errors only." << std::endl; + std::cout << " -C Keep comments." << std::endl; std::exit(0); } - dui.removeComments = true; + dui.removeComments = !keep_comments; // Perform preprocessing simplecpp::OutputList outputList; @@ -126,7 +132,8 @@ int main(int argc, char **argv) } else { rawtokens = new simplecpp::TokenList(filename,files,&outputList); } - rawtokens->removeComments(); + if (!keep_comments) + rawtokens->removeComments(); simplecpp::TokenList outputTokens(files); std::map filedata; simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList); diff --git a/simplecpp.cpp b/simplecpp.cpp index e4f98bb6..fa04345d 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -153,7 +153,7 @@ static unsigned long long stringToULL(const std::string &s) return ret; } -// TODO: added an undercore since this conflicts with a function of the same name in utils.h from Cppcheck source when building Cppcheck with MSBuild +// TODO: added an undercore since this conflicts with a function of the same name in utils.h from Cppcheck source when building Cppcheck with MSBuild static bool startsWith_(const std::string &s, const std::string &p) { return (s.size() >= p.size()) && std::equal(p.begin(), p.end(), s.begin()); @@ -3398,7 +3398,7 @@ std::map simplecpp::load(const simplecpp::To return ret; } -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 = macros.find(tok->str()); @@ -3418,7 +3418,7 @@ static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token } output.takeTokens(value); } else { - if (!tok->comment) + if (!dui.removeComments || !tok->comment) output.push_back(new simplecpp::Token(*tok)); *tok1 = tok->next; } @@ -3631,7 +3631,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; } @@ -3807,7 +3807,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL maybeUsedMacros[rawtok->next->str()].push_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; } @@ -3892,7 +3892,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; } diff --git a/test.cpp b/test.cpp index cec253b8..25f69502 100644 --- a/test.cpp +++ b/test.cpp @@ -2263,6 +2263,7 @@ static void include9() simplecpp::TokenList out(files); simplecpp::DUI dui; dui.includePaths.push_back("."); + dui.removeComments = true; simplecpp::preprocess(out, rawtokens_c, files, filedata, dui); ASSERT_EQUALS("\n#line 2 \"1.h\"\nx = 1 ;", out.stringify());