@@ -699,42 +699,44 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
699699
700700 if (oldLastToken != cback ()) {
701701 oldLastToken = cback ();
702- const Token * const llTok = isLastLinePreprocessor ();
703- if (!llTok)
702+
703+ // #line 3
704+ // #line 3 "file.c"
705+ // #3
706+ // #3 "file.c"
707+ const Token * ppTok = isLastLinePreprocessor ();
708+ if (!ppTok)
704709 continue ;
705- const Token * const llNextToken = llTok->next ;
706- if (!llTok->next )
710+
711+ const auto advanceAndSkipComments = [](const Token* tok) {
712+ do {
713+ tok = tok->next ;
714+ } while (tok && tok->comment );
715+ return tok;
716+ };
717+
718+ // skip #
719+ ppTok = advanceAndSkipComments (ppTok);
720+ if (!ppTok)
707721 continue ;
708- if (llNextToken->next ) {
709- // TODO: add support for "# 3"
710- // #3 "file.c"
711- // #line 3 "file.c"
712- if ((llNextToken->number &&
713- llNextToken->next ->str ()[0 ] == ' \" ' ) ||
714- (llNextToken->str () == " line" &&
715- llNextToken->next ->number &&
716- llNextToken->next ->next &&
717- llNextToken->next ->next ->str ()[0 ] == ' \" ' ))
718- {
719- const Token *strtok = cback ();
720- while (strtok->comment )
721- strtok = strtok->previous ;
722- const Token *numtok = strtok->previous ;
723- while (numtok->comment )
724- numtok = numtok->previous ;
725- lineDirective (fileIndex (replaceAll (strtok->str ().substr (1U , strtok->str ().size () - 2U )," \\\\ " ," \\ " )),
726- std::atol (numtok->str ().c_str ()), location);
727- }
728- // #line 3
729- else if (llNextToken->str () == " line" &&
730- llNextToken->next ->number )
731- {
732- const Token *numtok = cback ();
733- while (numtok->comment )
734- numtok = numtok->previous ;
735- lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), location);
736- }
737- }
722+
723+ if (ppTok->str () == " line" )
724+ ppTok = advanceAndSkipComments (ppTok);
725+
726+ if (!ppTok || !ppTok->number )
727+ continue ;
728+
729+ const unsigned int line = std::atol (ppTok->str ().c_str ());
730+ ppTok = advanceAndSkipComments (ppTok);
731+
732+ unsigned int fileindex;
733+
734+ if (ppTok && ppTok->str ()[0 ] == ' \" ' )
735+ fileindex = fileIndex (replaceAll (ppTok->str ().substr (1U , ppTok->str ().size () - 2U )," \\\\ " ," \\ " ));
736+ else
737+ fileindex = location.fileIndex ;
738+
739+ lineDirective (fileindex, line, location);
738740 }
739741
740742 continue ;
0 commit comments