@@ -694,33 +694,55 @@ 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%" ) {
701- const Token *strtok = cback ();
702- while (strtok->comment )
703- strtok = strtok->previous ;
704- loc.push (location);
705- location.fileIndex = fileIndex (strtok->str ().substr (1U , strtok->str ().size () - 2U ));
706- 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%" ) {
713- const Token *strtok = cback ();
714- while (strtok->comment )
715- strtok = strtok->previous ;
716- const Token *numtok = strtok->previous ;
717- while (numtok->comment )
718- numtok = numtok->previous ;
719- lineDirective (fileIndex (replaceAll (strtok->str ().substr (1U , strtok->str ().size () - 2U )," \\\\ " ," \\ " )),
720- std::atol (numtok->str ().c_str ()), &location);
700+ const Token * const llNextToken = llTok->next ;
701+ if (!llTok->next )
702+ continue ;
703+ if (llNextToken->next ) {
704+ // #file "file.c"
705+ if (llNextToken->str () == " file" &&
706+ llNextToken->next ->str ()[0 ] == ' \" ' )
707+ {
708+ const Token *strtok = cback ();
709+ while (strtok->comment )
710+ strtok = strtok->previous ;
711+ loc.push (location);
712+ location.fileIndex = fileIndex (strtok->str ().substr (1U , strtok->str ().size () - 2U ));
713+ location.line = 1U ;
714+ }
715+ // #3 "file.c"
716+ // #line 3 "file.c"
717+ else if ((llNextToken->number &&
718+ llNextToken->next ->str ()[0 ] == ' \" ' ) ||
719+ (llNextToken->str () == " line" &&
720+ llNextToken->next ->number &&
721+ llNextToken->next ->next &&
722+ llNextToken->next ->next ->str ()[0 ] == ' \" ' ))
723+ {
724+ const Token *strtok = cback ();
725+ while (strtok->comment )
726+ strtok = strtok->previous ;
727+ const Token *numtok = strtok->previous ;
728+ while (numtok->comment )
729+ numtok = numtok->previous ;
730+ lineDirective (fileIndex (replaceAll (strtok->str ().substr (1U , strtok->str ().size () - 2U )," \\\\ " ," \\ " )),
731+ std::atol (numtok->str ().c_str ()), &location);
732+ }
733+ // #line 3
734+ else if (llNextToken->str () == " line" &&
735+ llNextToken->next ->number )
736+ {
737+ const Token *numtok = cback ();
738+ while (numtok->comment )
739+ numtok = numtok->previous ;
740+ lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), &location);
741+ }
721742 }
722743 // #endfile
723- else if (lastline == " # endfile" && !loc.empty ()) {
744+ else if (llNextToken->str () == " endfile" && !loc.empty ())
745+ {
724746 location = loc.top ();
725747 loc.pop ();
726748 }
@@ -1414,34 +1436,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
14141436 return ret;
14151437}
14161438
1417- std::string simplecpp::TokenList::lastLine (int maxsize) const
1418- {
1419- std::string ret;
1420- int count = 0 ;
1421- for (const Token *tok = cback (); ; tok = tok->previous ) {
1422- if (!sameline (tok, cback ())) {
1423- break ;
1424- }
1425- if (tok->comment )
1426- continue ;
1427- if (++count > maxsize)
1428- return " " ;
1429- if (!ret.empty ())
1430- ret += ' ' ;
1431- // add tokens in reverse for performance reasons
1432- if (tok->str ()[0 ] == ' \" ' )
1433- ret += " %rts%" ; // %str%
1434- else if (tok->number )
1435- ret += " %mun%" ; // %num%
1436- else {
1437- ret += tok->str ();
1438- std::reverse (ret.end () - tok->str ().length (), ret.end ());
1439- }
1440- }
1441- std::reverse (ret.begin (), ret.end ());
1442- return ret;
1443- }
1444-
14451439const simplecpp::Token* simplecpp::TokenList::lastLineTok (int maxsize) const
14461440{
14471441 const Token* prevTok = nullptr ;
@@ -1458,10 +1452,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14581452 return prevTok;
14591453}
14601454
1461- bool simplecpp::TokenList::isLastLinePreprocessor (int maxsize) const
1455+ const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor (int maxsize) const
14621456{
14631457 const Token * const prevTok = lastLineTok (maxsize);
1464- return prevTok && prevTok->op == ' #' ;
1458+ if (prevTok && prevTok->op == ' #' )
1459+ return prevTok;
1460+ return nullptr ;
14651461}
14661462
14671463unsigned int simplecpp::TokenList::fileIndex (const std::string &filename)
0 commit comments