@@ -37,6 +37,18 @@ Lexer::Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context,
37
37
AsmParserCodeCompleteContext *codeCompleteContext)
38
38
: sourceMgr(sourceMgr), context(context), codeCompleteLoc(nullptr ) {
39
39
auto bufferID = sourceMgr.getMainFileID ();
40
+
41
+ // Check to see if the main buffer contains the last buffer, and if so the
42
+ // last buffer should be used as main file for parsing.
43
+ if (sourceMgr.getNumBuffers () > 1 ) {
44
+ unsigned lastFileID = sourceMgr.getNumBuffers ();
45
+ const llvm::MemoryBuffer *main = sourceMgr.getMemoryBuffer (bufferID);
46
+ const llvm::MemoryBuffer *last = sourceMgr.getMemoryBuffer (lastFileID);
47
+ if (main->getBufferStart () <= last->getBufferStart () &&
48
+ main->getBufferEnd () >= last->getBufferEnd ()) {
49
+ bufferID = lastFileID;
50
+ }
51
+ }
40
52
curBuffer = sourceMgr.getMemoryBuffer (bufferID)->getBuffer ();
41
53
curPtr = curBuffer.begin ();
42
54
@@ -71,13 +83,17 @@ Token Lexer::emitError(const char *loc, const Twine &message) {
71
83
}
72
84
73
85
Token Lexer::lexToken () {
86
+ const char *curBufferEnd = curBuffer.end ();
74
87
while (true ) {
75
88
const char *tokStart = curPtr;
76
89
77
90
// Check to see if the current token is at the code completion location.
78
91
if (tokStart == codeCompleteLoc)
79
92
return formToken (Token::code_complete, tokStart);
80
93
94
+ if (tokStart == curBufferEnd)
95
+ return formToken (Token::eof, tokStart);
96
+
81
97
// Lex the next token.
82
98
switch (*curPtr++) {
83
99
default :
@@ -102,7 +118,7 @@ Token Lexer::lexToken() {
102
118
case 0 :
103
119
// This may either be a nul character in the source file or may be the EOF
104
120
// marker that llvm::MemoryBuffer guarantees will be there.
105
- if (curPtr - 1 == curBuffer. end () )
121
+ if (curPtr - 1 == curBufferEnd )
106
122
return formToken (Token::eof, tokStart);
107
123
continue ;
108
124
@@ -259,15 +275,19 @@ void Lexer::skipComment() {
259
275
assert (*curPtr == ' /' );
260
276
++curPtr;
261
277
278
+ const char *curBufferEnd = curBuffer.end ();
262
279
while (true ) {
280
+ if (curPtr == curBufferEnd)
281
+ return ;
282
+
263
283
switch (*curPtr++) {
264
284
case ' \n ' :
265
285
case ' \r ' :
266
286
// Newline is end of comment.
267
287
return ;
268
288
case 0 :
269
289
// If this is the end of the buffer, end the comment.
270
- if (curPtr - 1 == curBuffer. end () ) {
290
+ if (curPtr - 1 == curBufferEnd ) {
271
291
--curPtr;
272
292
return ;
273
293
}
@@ -405,6 +425,7 @@ Token Lexer::lexPrefixedIdentifier(const char *tokStart) {
405
425
Token Lexer::lexString (const char *tokStart) {
406
426
assert (curPtr[-1 ] == ' "' );
407
427
428
+ const char *curBufferEnd = curBuffer.end ();
408
429
while (true ) {
409
430
// Check to see if there is a code completion location within the string. In
410
431
// these cases we generate a completion location and place the currently
@@ -419,7 +440,7 @@ Token Lexer::lexString(const char *tokStart) {
419
440
case 0 :
420
441
// If this is a random nul character in the middle of a string, just
421
442
// include it. If it is the end of file, then it is an error.
422
- if (curPtr - 1 != curBuffer. end () )
443
+ if (curPtr - 1 != curBufferEnd )
423
444
continue ;
424
445
[[fallthrough]];
425
446
case ' \n ' :
0 commit comments