Skip to content

[mlir] Make parser not rely on terminating null. #151007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mlir/lib/AsmParser/DialectSymbolParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ParseResult Parser::parseDialectSymbolBody(StringRef &body,
nestedPunctuation.pop_back();
return success();
};
const char *curBufferEnd = state.lex.getBufferEnd();
do {
// Handle code completions, which may appear in the middle of the symbol
// body.
Expand All @@ -98,6 +99,12 @@ ParseResult Parser::parseDialectSymbolBody(StringRef &body,
break;
}

if (curBufferEnd == curPtr) {
if (!nestedPunctuation.empty())
return emitPunctError();
return emitError("unexpected nul or EOF in pretty dialect name");
}

char c = *curPtr++;
switch (c) {
case '\0':
Expand Down
27 changes: 24 additions & 3 deletions mlir/lib/AsmParser/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ Lexer::Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context,
AsmParserCodeCompleteContext *codeCompleteContext)
: sourceMgr(sourceMgr), context(context), codeCompleteLoc(nullptr) {
auto bufferID = sourceMgr.getMainFileID();

// Check to see if the main buffer contains the last buffer, and if so the
// last buffer should be used as main file for parsing.
if (sourceMgr.getNumBuffers() > 1) {
unsigned lastFileID = sourceMgr.getNumBuffers();
const llvm::MemoryBuffer *main = sourceMgr.getMemoryBuffer(bufferID);
const llvm::MemoryBuffer *last = sourceMgr.getMemoryBuffer(lastFileID);
if (main->getBufferStart() <= last->getBufferStart() &&
main->getBufferEnd() >= last->getBufferEnd()) {
bufferID = lastFileID;
}
}
curBuffer = sourceMgr.getMemoryBuffer(bufferID)->getBuffer();
curPtr = curBuffer.begin();

Expand Down Expand Up @@ -71,13 +83,17 @@ Token Lexer::emitError(const char *loc, const Twine &message) {
}

Token Lexer::lexToken() {
const char *curBufferEnd = curBuffer.end();
while (true) {
const char *tokStart = curPtr;

// Check to see if the current token is at the code completion ___location.
if (tokStart == codeCompleteLoc)
return formToken(Token::code_complete, tokStart);

if (tokStart == curBufferEnd)
return formToken(Token::eof, tokStart);

// Lex the next token.
switch (*curPtr++) {
default:
Expand All @@ -102,7 +118,7 @@ Token Lexer::lexToken() {
case 0:
// This may either be a nul character in the source file or may be the EOF
// marker that llvm::MemoryBuffer guarantees will be there.
if (curPtr - 1 == curBuffer.end())
if (curPtr - 1 == curBufferEnd)
return formToken(Token::eof, tokStart);
continue;

Expand Down Expand Up @@ -259,15 +275,19 @@ void Lexer::skipComment() {
assert(*curPtr == '/');
++curPtr;

const char *curBufferEnd = curBuffer.end();
while (true) {
if (curPtr == curBufferEnd)
return;

switch (*curPtr++) {
case '\n':
case '\r':
// Newline is end of comment.
return;
case 0:
// If this is the end of the buffer, end the comment.
if (curPtr - 1 == curBuffer.end()) {
if (curPtr - 1 == curBufferEnd) {
--curPtr;
return;
}
Expand Down Expand Up @@ -405,6 +425,7 @@ Token Lexer::lexPrefixedIdentifier(const char *tokStart) {
Token Lexer::lexString(const char *tokStart) {
assert(curPtr[-1] == '"');

const char *curBufferEnd = curBuffer.end();
while (true) {
// Check to see if there is a code completion ___location within the string. In
// these cases we generate a completion ___location and place the currently
Expand All @@ -419,7 +440,7 @@ Token Lexer::lexString(const char *tokStart) {
case 0:
// If this is a random nul character in the middle of a string, just
// include it. If it is the end of file, then it is an error.
if (curPtr - 1 != curBuffer.end())
if (curPtr - 1 != curBufferEnd)
continue;
[[fallthrough]];
case '\n':
Expand Down
3 changes: 3 additions & 0 deletions mlir/lib/AsmParser/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Lexer {
/// Returns the start of the buffer.
const char *getBufferBegin() { return curBuffer.data(); }

/// Returns the end of the buffer.
const char *getBufferEnd() { return curBuffer.end(); }

/// Return the code completion ___location of the lexer, or nullptr if there is
/// none.
const char *getCodeCompleteLoc() const { return codeCompleteLoc; }
Expand Down