@@ -660,8 +660,6 @@ static const std::string COMMENT_END("*/");
660660
661661void simplecpp::TokenList::readfile (Stream &stream, const std::string &filename, OutputList *outputList)
662662{
663- std::stack<simplecpp::Location> loc;
664-
665663 unsigned int multiline = 0U ;
666664
667665 const Token *oldLastToken = nullptr ;
@@ -700,42 +698,44 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
700698
701699 if (oldLastToken != cback ()) {
702700 oldLastToken = cback ();
703- const Token * const llTok = isLastLinePreprocessor ();
704- if (!llTok)
701+
702+ // #line 3
703+ // #line 3 "file.c"
704+ // #3
705+ // #3 "file.c"
706+ const Token * ppTok = isLastLinePreprocessor ();
707+ if (!ppTok)
705708 continue ;
706- const Token * const llNextToken = llTok->next ;
707- if (!llTok->next )
709+
710+ const auto advanceAndSkipComments = [](const Token* tok) {
711+ do {
712+ tok = tok->next ;
713+ } while (tok && tok->comment );
714+ return tok;
715+ };
716+
717+ // skip #
718+ ppTok = advanceAndSkipComments (ppTok);
719+ if (!ppTok)
708720 continue ;
709- if (llNextToken->next ) {
710- // TODO: add support for "# 3"
711- // #3 "file.c"
712- // #line 3 "file.c"
713- if ((llNextToken->number &&
714- llNextToken->next ->str ()[0 ] == ' \" ' ) ||
715- (llNextToken->str () == " line" &&
716- llNextToken->next ->number &&
717- llNextToken->next ->next &&
718- llNextToken->next ->next ->str ()[0 ] == ' \" ' ))
719- {
720- const Token *strtok = cback ();
721- while (strtok->comment )
722- strtok = strtok->previous ;
723- const Token *numtok = strtok->previous ;
724- while (numtok->comment )
725- numtok = numtok->previous ;
726- lineDirective (fileIndex (replaceAll (strtok->str ().substr (1U , strtok->str ().size () - 2U )," \\\\ " ," \\ " )),
727- std::atol (numtok->str ().c_str ()), location);
728- }
729- // #line 3
730- else if (llNextToken->str () == " line" &&
731- llNextToken->next ->number )
732- {
733- const Token *numtok = cback ();
734- while (numtok->comment )
735- numtok = numtok->previous ;
736- lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), location);
737- }
738- }
721+
722+ if (ppTok->str () == " line" )
723+ ppTok = advanceAndSkipComments (ppTok);
724+
725+ if (!ppTok || !ppTok->number )
726+ continue ;
727+
728+ const unsigned int line = std::atol (ppTok->str ().c_str ());
729+ ppTok = advanceAndSkipComments (ppTok);
730+
731+ unsigned int fileindex;
732+
733+ if (ppTok && ppTok->str ()[0 ] == ' \" ' )
734+ fileindex = fileIndex (replaceAll (ppTok->str ().substr (1U , ppTok->str ().size () - 2U )," \\\\ " ," \\ " ));
735+ else
736+ fileindex = location.fileIndex ;
737+
738+ lineDirective (fileindex, line, &location);
739739 }
740740
741741 continue ;
0 commit comments