Skip to content

Commit

Permalink
refs #342 - do not load included files twice in CLI application / add…
Browse files Browse the repository at this point in the history
…ed `DUI::removeComment` (#340)
firewave authored Mar 8, 2024
1 parent c5c02ff commit ad9b49d
Showing 3 changed files with 13 additions and 9 deletions.
13 changes: 5 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

int main(int argc, char **argv)
@@ -110,6 +109,8 @@ int main(int argc, char **argv)
std::exit(0);
}

dui.removeComments = true;

// Perform preprocessing
simplecpp::OutputList outputList;
std::vector<std::string> files;
@@ -126,11 +127,10 @@ int main(int argc, char **argv)
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
}
rawtokens->removeComments();
std::map<std::string, simplecpp::TokenList*> included = simplecpp::load(*rawtokens, files, dui, &outputList);
for (std::pair<std::string, simplecpp::TokenList *> i : included)
i.second->removeComments();
simplecpp::TokenList outputTokens(files);
simplecpp::preprocess(outputTokens, *rawtokens, files, included, dui, &outputList);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
simplecpp::cleanup(filedata);
delete rawtokens;
rawtokens = nullptr;

@@ -174,8 +174,5 @@ int main(int argc, char **argv)
}
}

// cleanup included tokenlists
simplecpp::cleanup(included);

return 0;
}
6 changes: 6 additions & 0 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
@@ -3145,6 +3145,8 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
continue;
}

if (dui.removeComments)
tokenlist->removeComments();
ret[filename] = tokenlist;
filelist.push_back(tokenlist->front());
}
@@ -3180,6 +3182,8 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
f.close();

TokenList *tokens = new TokenList(header2, filenames, outputList);
if (dui.removeComments)
tokens->removeComments();
ret[header2] = tokens;
if (tokens->front())
filelist.push_back(tokens->front());
@@ -3448,6 +3452,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
header2 = openHeader(f, dui, rawtok->location.file(), header, systemheader);
if (f.is_open()) {
TokenList * const tokens = new TokenList(f, files, header2, outputList);
if (dui.removeComments)
tokens->removeComments();
filedata[header2] = tokens;
}
}
3 changes: 2 additions & 1 deletion simplecpp.h
Original file line number Diff line number Diff line change
@@ -320,13 +320,14 @@ namespace simplecpp {
* On the command line these are configured by -D, -U, -I, --include, -std
*/
struct SIMPLECPP_LIB DUI {
DUI() : clearIncludeCache(false) {}
DUI() : clearIncludeCache(false), removeComments(false) {}
std::list<std::string> defines;
std::set<std::string> undefined;
std::list<std::string> includePaths;
std::list<std::string> includes;
std::string std;
bool clearIncludeCache;
bool removeComments; /** remove comment tokens from included files */
};

SIMPLECPP_LIB long long characterLiteralToLL(const std::string& str);

0 comments on commit ad9b49d

Please sign in to comment.