@@ -733,8 +733,6 @@ static const std::string COMMENT_END("*/");
733733
734734void simplecpp::TokenList::readfile (Stream &stream, const std::string &filename, OutputList *outputList)
735735{
736- std::stack<simplecpp::Location> loc;
737-
738736 unsigned int multiline = 0U ;
739737
740738 const Token *oldLastToken = nullptr ;
@@ -776,42 +774,44 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
776774
777775 if (oldLastToken != cback ()) {
778776 oldLastToken = cback ();
779- const Token * const llTok = isLastLinePreprocessor ();
780- if (!llTok)
777+
778+ // #line 3
779+ // #line 3 "file.c"
780+ // #3
781+ // #3 "file.c"
782+ const Token * ppTok = isLastLinePreprocessor ();
783+ if (!ppTok)
781784 continue ;
782- const Token * const llNextToken = llTok->next ;
783- if (!llTok->next )
785+
786+ const auto advanceAndSkipComments = [](const Token* tok) {
787+ do {
788+ tok = tok->next ;
789+ } while (tok && tok->comment );
790+ return tok;
791+ };
792+
793+ // skip #
794+ ppTok = advanceAndSkipComments (ppTok);
795+ if (!ppTok)
784796 continue ;
785- if (llNextToken->next ) {
786- // TODO: add support for "# 3"
787- // #3 "file.c"
788- // #line 3 "file.c"
789- if ((llNextToken->number &&
790- llNextToken->next ->str ()[0 ] == ' \" ' ) ||
791- (llNextToken->str () == " line" &&
792- llNextToken->next ->number &&
793- llNextToken->next ->next &&
794- llNextToken->next ->next ->str ()[0 ] == ' \" ' ))
795- {
796- const Token *strtok = cback ();
797- while (strtok->comment )
798- strtok = strtok->previous ;
799- const Token *numtok = strtok->previous ;
800- while (numtok->comment )
801- numtok = numtok->previous ;
802- lineDirective (fileIndex (replaceAll (strtok->str ().substr (1U , strtok->str ().size () - 2U )," \\\\ " ," \\ " )),
803- std::atol (numtok->str ().c_str ()), &location);
804- }
805- // #line 3
806- else if (llNextToken->str () == " line" &&
807- llNextToken->next ->number )
808- {
809- const Token *numtok = cback ();
810- while (numtok->comment )
811- numtok = numtok->previous ;
812- lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), &location);
813- }
814- }
797+
798+ if (ppTok->str () == " line" )
799+ ppTok = advanceAndSkipComments (ppTok);
800+
801+ if (!ppTok || !ppTok->number )
802+ continue ;
803+
804+ const unsigned int line = std::atol (ppTok->str ().c_str ());
805+ ppTok = advanceAndSkipComments (ppTok);
806+
807+ unsigned int fileindex;
808+
809+ if (ppTok && ppTok->str ()[0 ] == ' \" ' )
810+ fileindex = fileIndex (replaceAll (ppTok->str ().substr (1U , ppTok->str ().size () - 2U )," \\\\ " ," \\ " ));
811+ else
812+ fileindex = location.fileIndex ;
813+
814+ lineDirective (fileindex, line, &location);
815815 }
816816
817817 continue ;
0 commit comments