@@ -694,22 +694,35 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
694694
695695 if (oldLastToken != cback ()) {
696696 oldLastToken = cback ();
697- if (!isLastLinePreprocessor ())
697+ const Token * const llTok = isLastLinePreprocessor ();
698+ if (!llTok)
698699 continue ;
699- const std::string lastline (lastLine ());
700- if (lastline == " # file %str%" ) {
700+ const Token * const llNextToken = llTok->next ;
701+ if (!llTok->next )
702+ continue ;
703+ // #file "file.c"
704+ if (llNextToken->str () == " file" &&
705+ llNextToken->next &&
706+ llNextToken->next ->str ()[0 ] == ' \" ' )
707+ {
701708 const Token *strtok = cback ();
702709 while (strtok->comment )
703710 strtok = strtok->previous ;
704711 loc.push (location);
705712 location.fileIndex = fileIndex (strtok->str ().substr (1U , strtok->str ().size () - 2U ));
706713 location.line = 1U ;
707- } else if (lastline == " # line %num%" ) {
708- const Token *numtok = cback ();
709- while (numtok->comment )
710- numtok = numtok->previous ;
711- lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), &location);
712- } else if (lastline == " # %num% %str%" || lastline == " # line %num% %str%" ) {
714+ }
715+ // #3 "file.c"
716+ // #line 3 "file.c"
717+ else if ((llNextToken->number &&
718+ llNextToken->next &&
719+ llNextToken->next ->str ()[0 ] == ' \" ' ) ||
720+ (llNextToken->str () == " line" &&
721+ llNextToken->next &&
722+ llNextToken->next ->number &&
723+ llNextToken->next ->next &&
724+ llNextToken->next ->next ->str ()[0 ] == ' \" ' ))
725+ {
713726 const Token *strtok = cback ();
714727 while (strtok->comment )
715728 strtok = strtok->previous ;
@@ -719,8 +732,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
719732 lineDirective (fileIndex (replaceAll (strtok->str ().substr (1U , strtok->str ().size () - 2U )," \\\\ " ," \\ " )),
720733 std::atol (numtok->str ().c_str ()), &location);
721734 }
735+ // #line 3
736+ else if (llNextToken->str () == " line" &&
737+ llNextToken->next &&
738+ llNextToken->next ->number )
739+ {
740+ const Token *numtok = cback ();
741+ while (numtok->comment )
742+ numtok = numtok->previous ;
743+ lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), &location);
744+ }
722745 // #endfile
723- else if (lastline == " # endfile" && !loc.empty ()) {
746+ else if (llNextToken->str () == " endfile" && !loc.empty ())
747+ {
724748 location = loc.top ();
725749 loc.pop ();
726750 }
@@ -1415,34 +1439,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
14151439 return ret;
14161440}
14171441
1418- std::string simplecpp::TokenList::lastLine (int maxsize) const
1419- {
1420- std::string ret;
1421- int count = 0 ;
1422- for (const Token *tok = cback (); ; tok = tok->previous ) {
1423- if (!sameline (tok, cback ())) {
1424- break ;
1425- }
1426- if (tok->comment )
1427- continue ;
1428- if (++count > maxsize)
1429- return " " ;
1430- if (!ret.empty ())
1431- ret += ' ' ;
1432- // add tokens in reverse for performance reasons
1433- if (tok->str ()[0 ] == ' \" ' )
1434- ret += " %rts%" ; // %str%
1435- else if (tok->number )
1436- ret += " %mun%" ; // %num%
1437- else {
1438- ret += tok->str ();
1439- std::reverse (ret.end () - tok->str ().length (), ret.end ());
1440- }
1441- }
1442- std::reverse (ret.begin (), ret.end ());
1443- return ret;
1444- }
1445-
14461442const simplecpp::Token* simplecpp::TokenList::lastLineTok (int maxsize) const
14471443{
14481444 const Token* prevTok = nullptr ;
@@ -1459,10 +1455,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14591455 return prevTok;
14601456}
14611457
1462- bool simplecpp::TokenList::isLastLinePreprocessor (int maxsize) const
1458+ const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor (int maxsize) const
14631459{
14641460 const Token * const prevTok = lastLineTok (maxsize);
1465- return prevTok && prevTok->op == ' #' ;
1461+ if (prevTok && prevTok->op == ' #' )
1462+ return prevTok;
1463+ return nullptr ;
14661464}
14671465
14681466unsigned int simplecpp::TokenList::fileIndex (const std::string &filename)
0 commit comments