@@ -697,22 +697,35 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
697697
698698 if (oldLastToken != cback ()) {
699699 oldLastToken = cback ();
700- if (!isLastLinePreprocessor ())
700+ const Token * const llTok = isLastLinePreprocessor ();
701+ if (!llTok)
701702 continue ;
702- const std::string lastline (lastLine ());
703- if (lastline == " # file %str%" ) {
703+ const Token * const llNextToken = llTok->next ;
704+ if (!llTok->next )
705+ continue ;
706+ // #file "file.c"
707+ if (llNextToken->str () == " file" &&
708+ llNextToken->next &&
709+ llNextToken->next ->str ()[0 ] == ' \" ' )
710+ {
704711 const Token *strtok = cback ();
705712 while (strtok->comment )
706713 strtok = strtok->previous ;
707714 loc.push (location);
708715 location.fileIndex = fileIndex (strtok->str ().substr (1U , strtok->str ().size () - 2U ));
709716 location.line = 1U ;
710- } else if (lastline == " # line %num%" ) {
711- const Token *numtok = cback ();
712- while (numtok->comment )
713- numtok = numtok->previous ;
714- lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), &location);
715- } else if (lastline == " # %num% %str%" || lastline == " # line %num% %str%" ) {
717+ }
718+ // #3 "file.c"
719+ // #line 3 "file.c"
720+ else if ((llNextToken->number &&
721+ llNextToken->next &&
722+ llNextToken->next ->str ()[0 ] == ' \" ' ) ||
723+ (llNextToken->str () == " line" &&
724+ llNextToken->next &&
725+ llNextToken->next ->number &&
726+ llNextToken->next ->next &&
727+ llNextToken->next ->next ->str ()[0 ] == ' \" ' ))
728+ {
716729 const Token *strtok = cback ();
717730 while (strtok->comment )
718731 strtok = strtok->previous ;
@@ -722,8 +735,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
722735 lineDirective (fileIndex (replaceAll (strtok->str ().substr (1U , strtok->str ().size () - 2U )," \\\\ " ," \\ " )),
723736 std::atol (numtok->str ().c_str ()), &location);
724737 }
738+ // #line 3
739+ else if (llNextToken->str () == " line" &&
740+ llNextToken->next &&
741+ llNextToken->next ->number )
742+ {
743+ const Token *numtok = cback ();
744+ while (numtok->comment )
745+ numtok = numtok->previous ;
746+ lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), &location);
747+ }
725748 // #endfile
726- else if (lastline == " # endfile" && !loc.empty ()) {
749+ else if (llNextToken->str () == " endfile" && !loc.empty ())
750+ {
727751 location = loc.top ();
728752 loc.pop ();
729753 }
@@ -1418,34 +1442,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
14181442 return ret;
14191443}
14201444
1421- std::string simplecpp::TokenList::lastLine (int maxsize) const
1422- {
1423- std::string ret;
1424- int count = 0 ;
1425- for (const Token *tok = cback (); ; tok = tok->previous ) {
1426- if (!sameline (tok, cback ())) {
1427- break ;
1428- }
1429- if (tok->comment )
1430- continue ;
1431- if (++count > maxsize)
1432- return " " ;
1433- if (!ret.empty ())
1434- ret += ' ' ;
1435- // add tokens in reverse for performance reasons
1436- if (tok->str ()[0 ] == ' \" ' )
1437- ret += " %rts%" ; // %str%
1438- else if (tok->number )
1439- ret += " %mun%" ; // %num%
1440- else {
1441- ret += tok->str ();
1442- std::reverse (ret.end () - tok->str ().length (), ret.end ());
1443- }
1444- }
1445- std::reverse (ret.begin (), ret.end ());
1446- return ret;
1447- }
1448-
14491445const simplecpp::Token* simplecpp::TokenList::lastLineTok (int maxsize) const
14501446{
14511447 const Token* prevTok = nullptr ;
@@ -1462,10 +1458,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14621458 return prevTok;
14631459}
14641460
1465- bool simplecpp::TokenList::isLastLinePreprocessor (int maxsize) const
1461+ const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor (int maxsize) const
14661462{
14671463 const Token * const prevTok = lastLineTok (maxsize);
1468- return prevTok && prevTok->op == ' #' ;
1464+ if (prevTok && prevTok->op == ' #' )
1465+ return prevTok;
1466+ return nullptr ;
14691467}
14701468
14711469unsigned int simplecpp::TokenList::fileIndex (const std::string &filename)
0 commit comments