From cf9aba2b2b1d9a13b6768fb8e4e974a67d346bde Mon Sep 17 00:00:00 2001 From: Jacques Pienaar Date: Sun, 9 Sep 2018 17:59:22 -0700 Subject: [PATCH] Check for absence of delimiters when delimiters is None and fixed number of operands expected. Ensure delimiters are absent where not expected. This is only checked in the case where operand count is known. This allows for the currently accepted case where there is a operand list with no delimiter and variable number of operands (which could be empty), followed by a delimited operand list. PiperOrigin-RevId: 212202064 --- mlir/lib/Parser/Parser.cpp | 15 ++++++++++++++- mlir/test/IR/invalid-ops.mlir | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp index 3a817e9231a3..b9b903cd2b5e 100644 --- a/mlir/lib/Parser/Parser.cpp +++ b/mlir/lib/Parser/Parser.cpp @@ -1785,7 +1785,20 @@ public: // Handle delimiters. switch (delimiter) { case Delimiter::None: - break; + // Don't check for the absence of a delimiter if the number of operands + // is unknown (and hence the operand list could be empty). + if (requiredOperandCount == -1) + break; + // Token already matches an identifier and so can't be a delimiter. + if (parser.getToken().is(Token::percent_identifier)) + break; + // Test against known delimiters. + if (parser.getToken().is(Token::l_paren) || + parser.getToken().is(Token::l_square)) + return emitError(startLoc, "unexpected delimiter"); + return emitError(startLoc, "unable to parse '" + + parser.getTokenSpelling() + + "' as operand"); case Delimiter::OptionalParen: if (parser.getToken().isNot(Token::l_paren)) return false; diff --git a/mlir/test/IR/invalid-ops.mlir b/mlir/test/IR/invalid-ops.mlir index 62944e4ac6f6..1e4e6965c439 100644 --- a/mlir/test/IR/invalid-ops.mlir +++ b/mlir/test/IR/invalid-ops.mlir @@ -126,5 +126,20 @@ mlfunc @calls(%arg0 : i32) { cfgfunc @cfgfunc_with_ops(f32) { bb0(%a : f32): - %sf = addf(%a, %a) : f32 // expected-error {{custom op 'addf' expected 2 operands}} + %sf = addf %a, %a, %a : f32 // expected-error {{custom op 'addf' expected 2 operands}} } + +// ----- + +cfgfunc @cfgfunc_with_ops(f32) { +bb0(%a : f32): + %sf = addf(%a, %a) : f32 // expected-error {{unexpected delimiter}} +} + +// ----- + +cfgfunc @cfgfunc_with_ops(f32) { +bb0(%a : f32): + %sf = addf{%a, %a} : f32 // expected-error {{unable to parse '{' as operand}} +} +