Rename the current parseSymbolName to parseOptionalSymbolName
authorRiver Riddle <riverriddle@google.com>
Wed, 13 Nov 2019 17:31:45 +0000 (09:31 -0800)
committerA. Unique TensorFlower <gardener@tensorflow.org>
Wed, 13 Nov 2019 17:32:20 +0000 (09:32 -0800)
The current implementation silently fails if the '@' identifier isn't present, making it similar to the 'optional' parse methods. This change renames the current implementation to 'Optional' and adds a new 'parseSymbolName' that emits an error.

PiperOrigin-RevId: 280214610

mlir/include/mlir/IR/OpImplementation.h
mlir/lib/IR/FunctionSupport.cpp
mlir/lib/IR/Module.cpp
mlir/lib/Parser/Parser.cpp
mlir/test/IR/invalid-func-op.mlir

index a167a7d..46c6701 100644 (file)
@@ -357,9 +357,21 @@ public:
   // Identifier Parsing
   //===--------------------------------------------------------------------===//
 
+  /// Parse an @-identifier and store it (without the '@' symbol) in a string
+  /// attribute named 'attrName'.
+  ParseResult parseSymbolName(StringAttr &result, StringRef attrName,
+                              SmallVectorImpl<NamedAttribute> &attrs) {
+    if (failed(parseOptionalSymbolName(result, attrName, attrs)))
+      return emitError(getCurrentLocation())
+             << "expected valid '@'-identifier for symbol name";
+    return success();
+  }
+
+  /// Parse an optional @-identifier and store it (without the '@' symbol) in a
+  /// string attribute named 'attrName'.
   virtual ParseResult
-  parseSymbolName(StringAttr &result, StringRef attrName,
-                  SmallVectorImpl<NamedAttribute> &attrs) = 0;
+  parseOptionalSymbolName(StringAttr &result, StringRef attrName,
+                          SmallVectorImpl<NamedAttribute> &attrs) = 0;
 
   //===--------------------------------------------------------------------===//
   // Operand Parsing
index 29cae17..6b27eb8 100644 (file)
@@ -159,12 +159,10 @@ mlir::impl::parseFunctionLikeOp(OpAsmParser &parser, OperationState &result,
   auto &builder = parser.getBuilder();
 
   // Parse the name as a symbol reference attribute.
-  FlatSymbolRefAttr nameAttr;
-  if (parser.parseAttribute(nameAttr, ::mlir::SymbolTable::getSymbolAttrName(),
-                            result.attributes))
+  StringAttr nameAttr;
+  if (parser.parseSymbolName(nameAttr, ::mlir::SymbolTable::getSymbolAttrName(),
+                             result.attributes))
     return failure();
-  // Convert the parsed function attr into a string attr.
-  result.attributes.back().second = builder.getStringAttr(nameAttr.getValue());
 
   // Parse the function signature.
   auto signatureLocation = parser.getCurrentLocation();
index f5cc98e..79e0452 100644 (file)
@@ -44,8 +44,8 @@ ModuleOp ModuleOp::create(Location loc, Optional<StringRef> name) {
 ParseResult ModuleOp::parse(OpAsmParser &parser, OperationState &result) {
   // If the name is present, parse it.
   StringAttr nameAttr;
-  (void)parser.parseSymbolName(nameAttr, mlir::SymbolTable::getSymbolAttrName(),
-                               result.attributes);
+  (void)parser.parseOptionalSymbolName(
+      nameAttr, mlir::SymbolTable::getSymbolAttrName(), result.attributes);
 
   // If module attributes are present, parse them.
   if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
index 2843aae..9dd6d50 100644 (file)
@@ -847,12 +847,13 @@ static T parseSymbol(llvm::StringRef inputStr, MLIRContext *context,
 //===----------------------------------------------------------------------===//
 
 InFlightDiagnostic Parser::emitError(SMLoc loc, const Twine &message) {
+  auto diag = mlir::emitError(getEncodedSourceLocation(loc), message);
+
   // If we hit a parse error in response to a lexer error, then the lexer
   // already reported the error.
   if (getToken().is(Token::error))
-    return InFlightDiagnostic();
-
-  return mlir::emitError(getEncodedSourceLocation(loc), message);
+    diag.abandon();
+  return diag;
 }
 
 //===----------------------------------------------------------------------===//
@@ -3937,10 +3938,11 @@ public:
     return success();
   }
 
-  /// Parse an @-identifier and store it (without the '@' symbol) in a string
-  /// attribute named 'attrName'.
-  ParseResult parseSymbolName(StringAttr &result, StringRef attrName,
-                              SmallVectorImpl<NamedAttribute> &attrs) override {
+  /// Parse an optional @-identifier and store it (without the '@' symbol) in a
+  /// string attribute named 'attrName'.
+  ParseResult
+  parseOptionalSymbolName(StringAttr &result, StringRef attrName,
+                          SmallVectorImpl<NamedAttribute> &attrs) override {
     Token atToken = parser.getToken();
     if (atToken.isNot(Token::at_identifier))
       return failure();
index 1554736..20af5ec 100644 (file)
@@ -3,7 +3,7 @@
 // -----
 
 func @func_op() {
-  // expected-error@+1 {{expected non-function type}}
+  // expected-error@+1 {{expected valid '@'-identifier for symbol name}}
   func missingsigil() -> (i1, index, f32)
   return
 }