Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
Expand Down
12 changes: 6 additions & 6 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -3398,7 +3398,7 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
return ret;
}

static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token **tok1, simplecpp::MacroMap &macros, std::vector<std::string> &files, simplecpp::OutputList *outputList)
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)
{
const simplecpp::Token * const tok = *tok1;
const simplecpp::MacroMap::const_iterator it = macros.find(tok->str());
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down