Skip to content

Commit febf2f0

Browse files
committed
add dui option for keeping comments in preprocessed source
1 parent 36e6e12 commit febf2f0

3 files changed

Lines changed: 13 additions & 11 deletions

File tree

main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
4141

4242
// Settings..
4343
simplecpp::DUI dui;
44-
dui.removeComments = true;
44+
dui.removeIncludeComments = true;
4545
bool quiet = false;
4646
bool error_only = false;
4747
for (int i = 1; i < argc; i++) {
@@ -242,7 +242,8 @@ int main(int argc, char **argv)
242242
f.close();
243243
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
244244
}
245-
rawtokens->removeComments();
245+
if (dui.removeSourceComments)
246+
rawtokens->removeComments();
246247
simplecpp::FileDataCache filedata;
247248
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
248249
simplecpp::cleanup(filedata);

simplecpp.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,7 +3152,7 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
31523152

31533153
auto *const data = new FileData {path, TokenList(path, filenames, outputList)};
31543154

3155-
if (dui.removeComments)
3155+
if (dui.removeIncludeComments)
31563156
data->tokens.removeComments();
31573157

31583158
name_it->second = data;
@@ -3293,7 +3293,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
32933293
if (!filedata->tokens.front())
32943294
continue;
32953295

3296-
if (dui.removeComments)
3296+
if (dui.removeIncludeComments)
32973297
filedata->tokens.removeComments();
32983298

32993299
filelist.emplace_back(filedata->tokens.front());
@@ -3332,7 +3332,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
33323332
if (!filedata->tokens.front())
33333333
continue;
33343334

3335-
if (dui.removeComments)
3335+
if (dui.removeIncludeComments)
33363336
filedata->tokens.removeComments();
33373337

33383338
filelist.emplace_back(filedata->tokens.front());
@@ -3341,7 +3341,7 @@ simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens,
33413341
return cache;
33423342
}
33433343

3344-
static bool preprocessToken(simplecpp::TokenList &output, const simplecpp::Token *&tok1, simplecpp::MacroMap &macros, std::vector<std::string> &files, simplecpp::OutputList *outputList)
3344+
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)
33453345
{
33463346
const simplecpp::Token * const tok = tok1;
33473347
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
33723372
}
33733373
output.takeTokens(value);
33743374
} else {
3375-
if (!tok->comment)
3375+
if (!tok->comment || !dui.removeSourceComments)
33763376
output.push_back(new simplecpp::Token(*tok));
33773377
tok1 = tok->next;
33783378
}
@@ -3647,7 +3647,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36473647
TokenList inc2(files);
36483648
if (!inc1.empty() && inc1.cfront()->name) {
36493649
const Token *inctok = inc1.cfront();
3650-
if (!preprocessToken(inc2, inctok, macros, files, outputList)) {
3650+
if (!preprocessToken(inc2, inctok, macros, files, outputList, dui)) {
36513651
output.clear();
36523652
return;
36533653
}
@@ -3817,7 +3817,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
38173817
maybeUsedMacros[rawtok->next->str()].emplace_back(rawtok->next->location);
38183818

38193819
const Token *tmp = tok;
3820-
if (!preprocessToken(expr, tmp, macros, files, outputList)) {
3820+
if (!preprocessToken(expr, tmp, macros, files, outputList, dui)) {
38213821
output.clear();
38223822
return;
38233823
}
@@ -3915,7 +3915,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
39153915
const Location loc(rawtok->location);
39163916
TokenList tokens(files);
39173917

3918-
if (!preprocessToken(tokens, rawtok, macros, files, outputList)) {
3918+
if (!preprocessToken(tokens, rawtok, macros, files, outputList, dui)) {
39193919
output.clear();
39203920
return;
39213921
}

simplecpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ namespace simplecpp {
438438
std::list<std::string> includes;
439439
std::string std;
440440
bool clearIncludeCache{};
441-
bool removeComments{}; /** remove comment tokens from included files */
441+
bool removeIncludeComments{}; /** remove comment tokens from included files */
442+
bool removeSourceComments{true}; /** remove comment tokens from preprocessed files */
442443
};
443444

444445
struct SIMPLECPP_LIB FileData {

0 commit comments

Comments
 (0)