Skip to content

Commit 2448c36

Browse files
committed
Fix gotoNextLine
1 parent eebfbad commit 2448c36

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

simplecpp.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
648648

649649
const Token *oldLastToken = nullptr;
650650

651+
bool locationchange = false;
652+
651653
Location location(fileIndex(filename), 1, 1);
652654
while (stream.good()) {
653655
unsigned char ch = stream.readChar();
@@ -716,6 +718,7 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
716718
if (ppTok && ppTok->str()[0] == '\"')
717719
location.fileIndex = fileIndex(replaceAll(ppTok->str().substr(1U, ppTok->str().size() - 2U),"\\\\","\\"));
718720

721+
locationchange = true;
719722
}
720723

721724
continue;
@@ -934,6 +937,11 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
934937

935938
push_back(new Token(currentToken, location, !!std::isspace(stream.peekChar())));
936939

940+
if (locationchange) {
941+
back()->locationchange = true;
942+
locationchange = false;
943+
}
944+
937945
if (multiline)
938946
location.col += currentToken.size();
939947
else
@@ -1435,7 +1443,7 @@ const simplecpp::Token* simplecpp::TokenList::lastLineTok(int maxsize) const
14351443
const Token* prevTok = nullptr;
14361444
int count = 0;
14371445
for (const Token *tok = cback(); ; tok = tok->previous) {
1438-
if (!sameline(tok, cback()))
1446+
if (!sameline(tok, cback()) || tok->locationchange)
14391447
break;
14401448
if (tok->comment)
14411449
continue;
@@ -2979,7 +2987,9 @@ static const simplecpp::Token *gotoNextLine(const simplecpp::Token *tok)
29792987
{
29802988
const unsigned int line = tok->location.line;
29812989
const unsigned int file = tok->location.fileIndex;
2982-
while (tok && tok->location.line == line && tok->location.fileIndex == file)
2990+
if (tok)
2991+
tok = tok->next;
2992+
while (tok && tok->location.line == line && tok->location.fileIndex == file && !tok->locationchange)
29832993
tok = tok->next;
29842994
return tok;
29852995
}

simplecpp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ namespace simplecpp {
151151
class SIMPLECPP_LIB Token {
152152
public:
153153
Token(const TokenString &s, const Location &loc, bool wsahead = false) :
154-
whitespaceahead(wsahead), location(loc), string(s) {
154+
whitespaceahead(wsahead), locationchange(false), location(loc), string(s) {
155155
flags();
156156
}
157157

158158
Token(const Token &tok) :
159-
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), string(tok.string), mExpandedFrom(tok.mExpandedFrom) {}
159+
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), locationchange(tok.locationchange), location(tok.location), string(tok.string), mExpandedFrom(tok.mExpandedFrom) {}
160160

161161
Token &operator=(const Token &tok) = delete;
162162

@@ -182,6 +182,7 @@ namespace simplecpp {
182182
bool name;
183183
bool number;
184184
bool whitespaceahead;
185+
bool locationchange; /* token location is at a discontinuity from #line directive */
185186
Location location;
186187
Token *previous{};
187188
Token *next{};

0 commit comments

Comments
 (0)