@@ -685,22 +685,35 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
685
685
686
686
if (oldLastToken != cback ()) {
687
687
oldLastToken = cback ();
688
- if (!isLastLinePreprocessor ())
688
+ const Token * const llTok = isLastLinePreprocessor ();
689
+ if (!llTok)
689
690
continue ;
690
- const std::string lastline (lastLine ());
691
- if (lastline == " # file %str%" ) {
691
+ const Token * const llNextToken = llTok->next ;
692
+ if (!llTok->next )
693
+ continue ;
694
+ // #file "file.c"
695
+ if (llNextToken->str () == " file" &&
696
+ llNextToken->next &&
697
+ llNextToken->next ->str ()[0 ] == ' \" ' )
698
+ {
692
699
const Token *strtok = cback ();
693
700
while (strtok->comment )
694
701
strtok = strtok->previous ;
695
702
loc.push (location);
696
703
location.fileIndex = fileIndex (strtok->str ().substr (1U , strtok->str ().size () - 2U ));
697
704
location.line = 1U ;
698
- } else if (lastline == " # line %num%" ) {
699
- const Token *numtok = cback ();
700
- while (numtok->comment )
701
- numtok = numtok->previous ;
702
- lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), &location);
703
- } else if (lastline == " # %num% %str%" || lastline == " # line %num% %str%" ) {
705
+ }
706
+ // #3 "file.c"
707
+ // #line 3 "file.c"
708
+ else if ((llNextToken->number &&
709
+ llNextToken->next &&
710
+ llNextToken->next ->str ()[0 ] == ' \" ' ) ||
711
+ (llNextToken->str () == " line" &&
712
+ llNextToken->next &&
713
+ llNextToken->next ->number &&
714
+ llNextToken->next ->next &&
715
+ llNextToken->next ->next ->str ()[0 ] == ' \" ' ))
716
+ {
704
717
const Token *strtok = cback ();
705
718
while (strtok->comment )
706
719
strtok = strtok->previous ;
@@ -710,8 +723,19 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
710
723
lineDirective (fileIndex (replaceAll (strtok->str ().substr (1U , strtok->str ().size () - 2U )," \\\\ " ," \\ " )),
711
724
std::atol (numtok->str ().c_str ()), &location);
712
725
}
726
+ // #line 3
727
+ else if (llNextToken->str () == " line" &&
728
+ llNextToken->next &&
729
+ llNextToken->next ->number )
730
+ {
731
+ const Token *numtok = cback ();
732
+ while (numtok->comment )
733
+ numtok = numtok->previous ;
734
+ lineDirective (location.fileIndex , std::atol (numtok->str ().c_str ()), &location);
735
+ }
713
736
// #endfile
714
- else if (lastline == " # endfile" && !loc.empty ()) {
737
+ else if (llNextToken->str () == " endfile" && !loc.empty ())
738
+ {
715
739
location = loc.top ();
716
740
loc.pop ();
717
741
}
@@ -1405,34 +1429,6 @@ std::string simplecpp::TokenList::readUntil(Stream &stream, const Location &loca
1405
1429
return ret;
1406
1430
}
1407
1431
1408
- std::string simplecpp::TokenList::lastLine (int maxsize) const
1409
- {
1410
- std::string ret;
1411
- int count = 0 ;
1412
- for (const Token *tok = cback (); ; tok = tok->previous ) {
1413
- if (!sameline (tok, cback ())) {
1414
- break ;
1415
- }
1416
- if (tok->comment )
1417
- continue ;
1418
- if (++count > maxsize)
1419
- return " " ;
1420
- if (!ret.empty ())
1421
- ret += ' ' ;
1422
- // add tokens in reverse for performance reasons
1423
- if (tok->str ()[0 ] == ' \" ' )
1424
- ret += " %rts%" ; // %str%
1425
- else if (tok->number )
1426
- ret += " %mun%" ; // %num%
1427
- else {
1428
- ret += tok->str ();
1429
- std::reverse (ret.end () - tok->str ().length (), ret.end ());
1430
- }
1431
- }
1432
- std::reverse (ret.begin (), ret.end ());
1433
- return ret;
1434
- }
1435
-
1436
1432
const simplecpp::Token* simplecpp::TokenList::lastLineTok (int maxsize) const
1437
1433
{
1438
1434
const Token* prevTok = nullptr ;
@@ -1449,10 +1445,12 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
1449
1445
return prevTok;
1450
1446
}
1451
1447
1452
- bool simplecpp::TokenList::isLastLinePreprocessor (int maxsize) const
1448
+ const simplecpp::Token* simplecpp::TokenList::isLastLinePreprocessor (int maxsize) const
1453
1449
{
1454
1450
const Token * const prevTok = lastLineTok (maxsize);
1455
- return prevTok && prevTok->op == ' #' ;
1451
+ if (prevTok && prevTok->op == ' #' )
1452
+ return prevTok;
1453
+ return nullptr ;
1456
1454
}
1457
1455
1458
1456
unsigned int simplecpp::TokenList::fileIndex (const std::string &filename)
0 commit comments