Skip to content

Commit 60bfa89

Browse files
committed
[Parse] Make PersistentParserState to hold ParserPosition
instead of PersistentParserState::ParserPos.
1 parent 574d39b commit 60bfa89

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

include/swift/Parse/Lexer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ class Lexer {
261261
return State(getLocForEndOfToken(SourceMgr, Loc));
262262
}
263263

264+
bool isStateForCurrentBuffer(LexerState State) const {
265+
return SourceMgr.findBufferContainingLoc(State.Loc) == getBufferID();
266+
}
267+
264268
/// \brief Restore the lexer state to a given one, that can be located either
265269
/// before or after the current position.
266270
void restoreState(State S, bool enableDiagnostics = false) {

include/swift/Parse/PersistentParserState.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/Basic/SourceLoc.h"
2121
#include "swift/Parse/LocalContext.h"
22+
#include "swift/Parse/ParserPosition.h"
2223
#include "swift/Parse/Scope.h"
2324
#include "llvm/ADT/DenseMap.h"
2425

@@ -114,7 +115,7 @@ class PersistentParserState {
114115
DelayedAccessorBodiesTy DelayedAccessorBodies;
115116

116117
/// \brief Parser sets this if it stopped parsing before the buffer ended.
117-
ParserPos MarkedPos;
118+
ParserPosition MarkedPos;
118119

119120
std::unique_ptr<DelayedDeclState> CodeCompletionDelayedDeclState;
120121

@@ -166,16 +167,16 @@ class PersistentParserState {
166167
return TopLevelCode;
167168
}
168169

169-
void markParserPosition(SourceLoc Loc, SourceLoc PrevLoc,
170+
void markParserPosition(ParserPosition Pos,
170171
bool InPoundLineEnvironment) {
171-
MarkedPos = {Loc, PrevLoc};
172+
MarkedPos = Pos;
172173
this->InPoundLineEnvironment = InPoundLineEnvironment;
173174
}
174175

175176
/// \brief Returns the marked parser position and resets it.
176-
ParserPos takeParserPosition() {
177-
ParserPos Pos = MarkedPos;
178-
MarkedPos = ParserPos();
177+
ParserPosition takeParserPosition() {
178+
ParserPosition Pos = MarkedPos;
179+
MarkedPos = ParserPosition();
179180
return Pos;
180181
}
181182
};

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ bool Parser::parseTopLevel() {
270270

271271
// Next time start relexing from the beginning of the comment so that we can
272272
// attach it to the token.
273-
State->markParserPosition(Tok.getCommentRange().getStart(), PreviousLoc,
273+
State->markParserPosition(getParserPosition(),
274274
InPoundLineEnvironment);
275275

276276
// If we are done parsing the whole file, finalize the token receiver.

lib/Parse/Parser.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,8 @@ Parser::Parser(std::unique_ptr<Lexer> Lex, SourceFile &SF,
460460

461461
auto ParserPos = State->takeParserPosition();
462462
if (ParserPos.isValid() &&
463-
SourceMgr.findBufferContainingLoc(ParserPos.Loc) == L->getBufferID()) {
464-
auto BeginParserPosition = getParserPosition(ParserPos);
465-
restoreParserPosition(BeginParserPosition);
463+
L->isStateForCurrentBuffer(ParserPos.LS)) {
464+
restoreParserPosition(ParserPos);
466465
InPoundLineEnvironment = State->InPoundLineEnvironment;
467466
}
468467
}

unittests/Parse/LexerTests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ TEST_F(LexerTest, RestoreBasic) {
132132
ASSERT_EQ("bbb", Tok.getText());
133133
ASSERT_FALSE(Tok.isAtStartOfLine());
134134

135-
Lexer::State S = L.getStateForBeginningOfToken(Tok);
135+
LexerState S = L.getStateForBeginningOfToken(Tok);
136136

137137
L.lex(Tok);
138138
ASSERT_EQ(tok::identifier, Tok.getKind());
@@ -179,7 +179,7 @@ TEST_F(LexerTest, RestoreNewlineFlag) {
179179
ASSERT_EQ("bbb", Tok.getText());
180180
ASSERT_TRUE(Tok.isAtStartOfLine());
181181

182-
Lexer::State S = L.getStateForBeginningOfToken(Tok);
182+
LexerState S = L.getStateForBeginningOfToken(Tok);
183183

184184
L.lex(Tok);
185185
ASSERT_EQ(tok::identifier, Tok.getKind());
@@ -231,7 +231,7 @@ TEST_F(LexerTest, RestoreStopAtCodeCompletion) {
231231
ASSERT_EQ("bbb", Tok.getText());
232232
ASSERT_FALSE(Tok.isAtStartOfLine());
233233

234-
Lexer::State S = L.getStateForBeginningOfToken(Tok);
234+
LexerState S = L.getStateForBeginningOfToken(Tok);
235235

236236
L.lex(Tok);
237237
ASSERT_EQ(tok::identifier, Tok.getKind());
@@ -290,7 +290,7 @@ TEST_F(LexerTest, RestoreWithTrivia) {
290290
(Trivia{{TriviaPiece::newlines(1), TriviaPiece::spaces(1)}}));
291291
ASSERT_EQ(TrailingTrivia, (Trivia{{TriviaPiece::spaces(1)}}));
292292

293-
Lexer::State S = L.getStateForBeginningOfToken(Tok, LeadingTrivia);
293+
LexerState S = L.getStateForBeginningOfToken(Tok, LeadingTrivia);
294294

295295
L.lex(Tok, LeadingTrivia, TrailingTrivia);
296296
ASSERT_EQ(tok::identifier, Tok.getKind());

0 commit comments

Comments
 (0)