Skip to content

Commit ad9b49d

Browse files
authored
refs #342 - do not load included files twice in CLI application / added DUI::removeComment (#340)
1 parent c5c02ff commit ad9b49d

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

main.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <iostream>
1212
#include <map>
1313
#include <string>
14-
#include <utility>
1514
#include <vector>
1615

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

112+
dui.removeComments = true;
113+
113114
// Perform preprocessing
114115
simplecpp::OutputList outputList;
115116
std::vector<std::string> files;
@@ -126,11 +127,10 @@ int main(int argc, char **argv)
126127
rawtokens = new simplecpp::TokenList(filename,files,&outputList);
127128
}
128129
rawtokens->removeComments();
129-
std::map<std::string, simplecpp::TokenList*> included = simplecpp::load(*rawtokens, files, dui, &outputList);
130-
for (std::pair<std::string, simplecpp::TokenList *> i : included)
131-
i.second->removeComments();
132130
simplecpp::TokenList outputTokens(files);
133-
simplecpp::preprocess(outputTokens, *rawtokens, files, included, dui, &outputList);
131+
std::map<std::string, simplecpp::TokenList*> filedata;
132+
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
133+
simplecpp::cleanup(filedata);
134134
delete rawtokens;
135135
rawtokens = nullptr;
136136

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

177-
// cleanup included tokenlists
178-
simplecpp::cleanup(included);
179-
180177
return 0;
181178
}

simplecpp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3145,6 +3145,8 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
31453145
continue;
31463146
}
31473147

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

31823184
TokenList *tokens = new TokenList(header2, filenames, outputList);
3185+
if (dui.removeComments)
3186+
tokens->removeComments();
31833187
ret[header2] = tokens;
31843188
if (tokens->front())
31853189
filelist.push_back(tokens->front());
@@ -3448,6 +3452,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34483452
header2 = openHeader(f, dui, rawtok->location.file(), header, systemheader);
34493453
if (f.is_open()) {
34503454
TokenList * const tokens = new TokenList(f, files, header2, outputList);
3455+
if (dui.removeComments)
3456+
tokens->removeComments();
34513457
filedata[header2] = tokens;
34523458
}
34533459
}

simplecpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,14 @@ namespace simplecpp {
320320
* On the command line these are configured by -D, -U, -I, --include, -std
321321
*/
322322
struct SIMPLECPP_LIB DUI {
323-
DUI() : clearIncludeCache(false) {}
323+
DUI() : clearIncludeCache(false), removeComments(false) {}
324324
std::list<std::string> defines;
325325
std::set<std::string> undefined;
326326
std::list<std::string> includePaths;
327327
std::list<std::string> includes;
328328
std::string std;
329329
bool clearIncludeCache;
330+
bool removeComments; /** remove comment tokens from included files */
330331
};
331332

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

0 commit comments

Comments
 (0)