@@ -135,6 +135,39 @@ void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens)
135135 }
136136}
137137
138+ void Preprocessor::setDirectives (const simplecpp::TokenList &tokens1)
139+ {
140+ // directive list..
141+ directives.clear ();
142+
143+ std::list<const simplecpp::TokenList *> list;
144+ list.push_back (&tokens1);
145+ for (std::map<std::string, simplecpp::TokenList *>::const_iterator it = tokenlists.begin (); it != tokenlists.end (); ++it) {
146+ list.push_back (it->second );
147+ }
148+
149+ for (std::list<const simplecpp::TokenList *>::const_iterator it = list.begin (); it != list.end (); ++it) {
150+ for (const simplecpp::Token *tok = (*it)->cbegin (); tok; tok = tok ? tok->next : nullptr ) {
151+ if ((tok->op != ' #' ) || (tok->previous && tok->previous ->location .line == tok->location .line ))
152+ continue ;
153+ if (tok->next && tok->next ->str == " endfile" )
154+ continue ;
155+ Directive directive (tok->location .file (), tok->location .line , " " );
156+ for (const simplecpp::Token *tok2 = tok; tok2 && tok2->location .line == directive.linenr ; tok2 = tok2->next ) {
157+ if (tok2->comment )
158+ continue ;
159+ if (!directive.str .empty () && (tok2->location .col > tok2->previous ->location .col + tok2->previous ->str .size ()))
160+ directive.str += ' ' ;
161+ if (directive.str == " #" && tok2->str == " file" )
162+ directive.str += " include" ;
163+ else
164+ directive.str += tok2->str ;
165+ }
166+ directives.push_back (directive);
167+ }
168+ }
169+ }
170+
138171static bool sameline (const simplecpp::Token *tok1, const simplecpp::Token *tok2)
139172{
140173 return tok1 && tok2 && tok1->location .sameline (tok2->location );
@@ -465,27 +498,6 @@ std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std
465498 };
466499 }
467500
468- // directive list..
469- directives.clear ();
470- for (const simplecpp::Token *tok = tokens1.cbegin (); tok; tok = tok ? tok->next : nullptr ) {
471- if ((tok->op != ' #' ) || (tok->previous && tok->previous ->location .line == tok->location .line ))
472- continue ;
473- if (tok->next && tok->next ->str == " endfile" )
474- continue ;
475- Directive directive (tok->location .file (), tok->location .line , " " );
476- for (const simplecpp::Token *tok2 = tok; tok2 && tok2->location .line == directive.linenr ; tok2 = tok2->next ) {
477- if (tok2->comment )
478- continue ;
479- if (!directive.str .empty () && (tok2->location .col > tok2->previous ->location .col + tok2->previous ->str .size ()))
480- directive.str += ' ' ;
481- if (directive.str == " #" && tok2->str == " file" )
482- directive.str += " include" ;
483- else
484- directive.str += tok2->str ;
485- }
486- directives.push_back (directive);
487- }
488-
489501 // ensure that guessed define macros without value are not used in the code
490502 for (std::list<std::string>::const_iterator defineIt = dui.defines .begin (); defineIt != dui.defines .end (); ++defineIt) {
491503 if (defineIt->find (" =" ) != std::string::npos)
@@ -591,8 +603,11 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string
591603 std::vector<std::string> files;
592604
593605 std::istringstream istr (filedata);
594- const simplecpp::TokenList & tokens1 = simplecpp::TokenList (istr, files, Path::simplifyPath (filename), &outputList);
606+ simplecpp::TokenList tokens1 (istr, files, Path::simplifyPath (filename), &outputList);
595607 inlineSuppressions (tokens1);
608+ tokens1.removeComments ();
609+ removeComments ();
610+ setDirectives (tokens1);
596611
597612 for (simplecpp::OutputList::const_iterator it = outputList.begin (); it != outputList.end (); ++it) {
598613 switch (it->type ) {
0 commit comments