Skip to content

refs #342 - do not load included files twice in CLI application / added DUI::removeComment #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2024
Merged
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
13 changes: 5 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

int main(int argc, char **argv)
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Up @@ -3143,6 +3143,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());
}
Expand Down Expand Up @@ -3178,6 +3180,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());
Expand Down Expand Up @@ -3446,6 +3450,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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down