Fix crash in ODS backend for Type/Attr when an incorrect construct is used for Type...
authorMehdi Amini <joker.eph@gmail.com>
Sat, 22 Jul 2023 04:47:33 +0000 (21:47 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Sat, 22 Jul 2023 05:06:02 +0000 (22:06 -0700)
Instead of crashing, try to print a useful error message.

mlir/lib/TableGen/AttrOrTypeDef.cpp

index e9fc927..56c96ff 100644 (file)
@@ -11,6 +11,7 @@
 #include "llvm/ADT/FunctionExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/ErrorHandling.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 
@@ -257,7 +258,18 @@ StringRef AttrOrTypeParameter::getComparator() const {
 StringRef AttrOrTypeParameter::getCppType() const {
   if (auto *stringType = dyn_cast<llvm::StringInit>(getDef()))
     return stringType->getValue();
-  return *getDefValue<llvm::StringInit>("cppType");
+  auto cppType = getDefValue<llvm::StringInit>("cppType");
+  if (cppType)
+    return *cppType;
+  if (auto *init = dyn_cast<llvm::DefInit>(getDef()))
+    llvm::PrintFatalError(
+        init->getDef()->getLoc(),
+        Twine("Missing `cppType` field in Attribute/Type parameter: ") +
+            init->getAsString());
+  llvm::report_fatal_error(
+      Twine("Missing `cppType` field in Attribute/Type parameter: ") +
+          getDef()->getAsString(),
+      /*gen_crash_diag=*/false);
 }
 
 StringRef AttrOrTypeParameter::getCppAccessorType() const {