Skip to content

Commit c1d02bd

Browse files
authored
[mlir] Change end of OperationDefinition. (#77273)
Store the last token parsed in the parser state so that the range parsed can utilize its end rather than the start of the token after parsed. This results in a tighter range (especially true in the case of comments, see ```mlir |%c4 = arith.constant 4 : index // Foo | ``` vs ```mlir |%c4 = arith.constant 4 : index| ``` ). Discovered while working on a little textual post processing tool.
1 parent f1e4142 commit c1d02bd

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

mlir/lib/AsmParser/Parser.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,7 @@ ParseResult OperationParser::parseOperation() {
12091209
resultIt += std::get<1>(record);
12101210
}
12111211
state.asmState->finalizeOperationDefinition(
1212-
op, nameTok.getLocRange(), /*endLoc=*/getToken().getLoc(),
1212+
op, nameTok.getLocRange(), /*endLoc=*/getLastToken().getEndLoc(),
12131213
asmResultGroups);
12141214
}
12151215

@@ -1225,8 +1225,9 @@ ParseResult OperationParser::parseOperation() {
12251225

12261226
// Add this operation to the assembly state if it was provided to populate.
12271227
} else if (state.asmState) {
1228-
state.asmState->finalizeOperationDefinition(op, nameTok.getLocRange(),
1229-
/*endLoc=*/getToken().getLoc());
1228+
state.asmState->finalizeOperationDefinition(
1229+
op, nameTok.getLocRange(),
1230+
/*endLoc=*/getLastToken().getEndLoc());
12301231
}
12311232

12321233
return success();
@@ -1500,8 +1501,9 @@ Operation *OperationParser::parseGenericOperation(Block *insertBlock,
15001501
// If we are populating the parser asm state, finalize this operation
15011502
// definition.
15021503
if (state.asmState)
1503-
state.asmState->finalizeOperationDefinition(op, nameToken.getLocRange(),
1504-
/*endLoc=*/getToken().getLoc());
1504+
state.asmState->finalizeOperationDefinition(
1505+
op, nameToken.getLocRange(),
1506+
/*endLoc=*/getLastToken().getEndLoc());
15051507
return op;
15061508
}
15071509

mlir/lib/AsmParser/Parser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class Parser {
102102
const Token &getToken() const { return state.curToken; }
103103
StringRef getTokenSpelling() const { return state.curToken.getSpelling(); }
104104

105+
/// Return the last parsed token.
106+
const Token &getLastToken() const { return state.lastToken; }
107+
105108
/// If the current token has the specified kind, consume it and return true.
106109
/// If not, return false.
107110
bool consumeIf(Token::Kind kind) {
@@ -115,6 +118,7 @@ class Parser {
115118
void consumeToken() {
116119
assert(state.curToken.isNot(Token::eof, Token::error) &&
117120
"shouldn't advance past EOF or errors");
121+
state.lastToken = state.curToken;
118122
state.curToken = state.lex.lexToken();
119123
}
120124

@@ -129,6 +133,7 @@ class Parser {
129133
/// Reset the parser to the given lexer position.
130134
void resetToken(const char *tokPos) {
131135
state.lex.resetPointer(tokPos);
136+
state.lastToken = state.curToken;
132137
state.curToken = state.lex.lexToken();
133138
}
134139

mlir/lib/AsmParser/ParserState.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ struct ParserState {
5454
AsmParserCodeCompleteContext *codeCompleteContext)
5555
: config(config),
5656
lex(sourceMgr, config.getContext(), codeCompleteContext),
57-
curToken(lex.lexToken()), symbols(symbols), asmState(asmState),
58-
codeCompleteContext(codeCompleteContext) {}
57+
curToken(lex.lexToken()), lastToken(Token::error, ""), symbols(symbols),
58+
asmState(asmState), codeCompleteContext(codeCompleteContext) {}
5959
ParserState(const ParserState &) = delete;
6060
void operator=(const ParserState &) = delete;
6161

@@ -68,6 +68,9 @@ struct ParserState {
6868
/// This is the next token that hasn't been consumed yet.
6969
Token curToken;
7070

71+
/// This is the last token that has been consumed.
72+
Token lastToken;
73+
7174
/// The current state for symbol parsing.
7275
SymbolState &symbols;
7376

0 commit comments

Comments
 (0)