2020#include < cstring>
2121#include < ctime>
2222#include < exception>
23- #include < fstream>
23+ #include < fstream> // IWYU pragma: keep
2424#include < iostream>
2525#include < limits>
2626#include < list>
2727#include < map>
2828#include < set>
29- #include < sstream>
29+ #include < sstream> // IWYU pragma: keep
3030#include < stack>
3131#include < stdexcept>
3232#include < string>
@@ -1984,7 +1984,7 @@ namespace simplecpp {
19841984 if (paren == 0 )
19851985 return tok->next ->next ;
19861986 tok = tok->next ;
1987- if (parametertokens.front ()->next ->str () != " )" && parametertokens. size () > args. size () )
1987+ if (parametertokens.size () > args. size () && parametertokens. front ()->next ->str () != " )" )
19881988 tok = expandToken (output, loc, tok, macros, expandedmacros, parametertokens)->previous ;
19891989 }
19901990 }
@@ -3085,9 +3085,11 @@ static std::string getFileName(const std::map<std::string, simplecpp::TokenList
30853085 return (filedata.find (header) != filedata.end ()) ? simplecpp::simplifyPath (header) : " " ;
30863086 }
30873087
3088- const std::string relativeFilename = getRelativeFileName (sourcefile, header);
3089- if (!systemheader && filedata.find (relativeFilename) != filedata.end ())
3090- return relativeFilename;
3088+ if (!systemheader) {
3089+ const std::string relativeFilename = getRelativeFileName (sourcefile, header);
3090+ if (filedata.find (relativeFilename) != filedata.end ())
3091+ return relativeFilename;
3092+ }
30913093
30923094 for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
30933095 std::string s = simplecpp::simplifyPath (getIncludePathFileName (*it, header));
@@ -3143,6 +3145,8 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
31433145 continue ;
31443146 }
31453147
3148+ if (dui.removeComments )
3149+ tokenlist->removeComments ();
31463150 ret[filename] = tokenlist;
31473151 filelist.push_back (tokenlist->front ());
31483152 }
@@ -3178,6 +3182,8 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
31783182 f.close ();
31793183
31803184 TokenList *tokens = new TokenList (header2, filenames, outputList);
3185+ if (dui.removeComments )
3186+ tokens->removeComments ();
31813187 ret[header2] = tokens;
31823188 if (tokens->front ())
31833189 filelist.push_back (tokens->front ());
@@ -3268,6 +3274,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
32683274 sizeOfType.insert (std::make_pair (" double *" , sizeof (double *)));
32693275 sizeOfType.insert (std::make_pair (" long double *" , sizeof (long double *)));
32703276
3277+ // use a dummy vector for the macros because as this is not part of the file and would add an empty entry - e.g. /usr/include/poll.h
3278+ std::vector<std::string> dummy;
3279+
32713280 const bool hasInclude = (dui.std .size () == 5 && dui.std .compare (0 ,3 ," c++" ) == 0 && dui.std >= " c++17" );
32723281 MacroMap macros;
32733282 for (std::list<std::string>::const_iterator it = dui.defines .begin (); it != dui.defines .end (); ++it) {
@@ -3279,26 +3288,26 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
32793288 continue ;
32803289 const std::string lhs (macrostr.substr (0 ,eq));
32813290 const std::string rhs (eq==std::string::npos ? std::string (" 1" ) : macrostr.substr (eq+1 ));
3282- const Macro macro (lhs, rhs, files );
3291+ const Macro macro (lhs, rhs, dummy );
32833292 macros.insert (std::pair<TokenString,Macro>(macro.name (), macro));
32843293 }
32853294
3286- macros.insert (std::make_pair (" __FILE__" , Macro (" __FILE__" , " __FILE__" , files )));
3287- macros.insert (std::make_pair (" __LINE__" , Macro (" __LINE__" , " __LINE__" , files )));
3288- macros.insert (std::make_pair (" __COUNTER__" , Macro (" __COUNTER__" , " __COUNTER__" , files )));
3295+ macros.insert (std::make_pair (" __FILE__" , Macro (" __FILE__" , " __FILE__" , dummy )));
3296+ macros.insert (std::make_pair (" __LINE__" , Macro (" __LINE__" , " __LINE__" , dummy )));
3297+ macros.insert (std::make_pair (" __COUNTER__" , Macro (" __COUNTER__" , " __COUNTER__" , dummy )));
32893298 struct tm ltime = {};
32903299 getLocaltime (ltime);
3291- macros.insert (std::make_pair (" __DATE__" , Macro (" __DATE__" , getDateDefine (<ime), files )));
3292- macros.insert (std::make_pair (" __TIME__" , Macro (" __TIME__" , getTimeDefine (<ime), files )));
3300+ macros.insert (std::make_pair (" __DATE__" , Macro (" __DATE__" , getDateDefine (<ime), dummy )));
3301+ macros.insert (std::make_pair (" __TIME__" , Macro (" __TIME__" , getTimeDefine (<ime), dummy )));
32933302
32943303 if (!dui.std .empty ()) {
32953304 std::string std_def = simplecpp::getCStdString (dui.std );
32963305 if (!std_def.empty ()) {
3297- macros.insert (std::make_pair (" __STDC_VERSION__" , Macro (" __STDC_VERSION__" , std_def, files )));
3306+ macros.insert (std::make_pair (" __STDC_VERSION__" , Macro (" __STDC_VERSION__" , std_def, dummy )));
32983307 } else {
32993308 std_def = simplecpp::getCppStdString (dui.std );
33003309 if (!std_def.empty ())
3301- macros.insert (std::make_pair (" __cplusplus" , Macro (" __cplusplus" , std_def, files )));
3310+ macros.insert (std::make_pair (" __cplusplus" , Macro (" __cplusplus" , std_def, dummy )));
33023311 }
33033312 }
33043313
@@ -3446,6 +3455,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
34463455 header2 = openHeader (f, dui, rawtok->location .file (), header, systemheader);
34473456 if (f.is_open ()) {
34483457 TokenList * const tokens = new TokenList (f, files, header2, outputList);
3458+ if (dui.removeComments )
3459+ tokens->removeComments ();
34493460 filedata[header2] = tokens;
34503461 }
34513462 }
0 commit comments