This is an archive of the discontinued LLVM Phabricator instance.

[MLIR Parser] Improve QoI for "expected token" errors
ClosedPublic

Authored by lattner on May 10 2022, 2:23 AM.

Details

Summary

A typical problem with missing a token is that the missing
token is at the end of a line. The problem with this is that
the error message gets reported on the start of the following
line (which is where the next / invalid token is) which can
be confusing.

Handle this by noticing this case and backing up to the end of
the previous line.

Diff Detail

Event Timeline

lattner created this revision.May 10 2022, 2:23 AM
Herald added a reviewer: rriddle. · View Herald Transcript
Herald added a reviewer: ftynse. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
lattner requested review of this revision.May 10 2022, 2:23 AM

Just a couple questions, thanks for knocking this out!

mlir/lib/Parser/Parser.cpp
180

Would something like this work and simplify the loop here?

const char *bufferStart = state.lex.getBufferBegin();
const char *curPtr = loc.getPointer();

// Take the current buffer and back up over empty lines and the last newline if one exists.
auto trimmed = StringRef(bufferStart, curPtr).rtrim(" \t\n");
return emitError(SMLoc::getFromPointer(trimmed.end() - 1), message);
187

OOC why not just curPtr - 1 for these and below?

rriddle accepted this revision.May 10 2022, 7:18 AM

LGTM with @bzcheesemans comments addressed. Would love a TODO to look through comments though, otherwise we are going to be emitting diagnostics that point into comments with expected tokens.

This revision is now accepted and ready to land.May 10 2022, 7:18 AM
lattner updated this revision to Diff 428376.May 10 2022, 7:43 AM

Use fancy stringref trick; ignore comments in lines.

This revision was landed with ongoing or failed builds.May 10 2022, 7:44 AM
This revision was automatically updated to reflect the committed changes.