Fix segfaults when printing unlinked statements, instructions and blocks. Fancy printing requires a pointer to the function since SSA values get function-specific names. This CL adds checks to ensure that we don't dereference null pointers in unliked objects. Unlinked statements, instructions and blocks are printed as <<UNLINKED STATEMENT>> etc.
PiperOrigin-RevId: 207293992
diff --git a/lib/IR/StmtBlock.cpp b/lib/IR/StmtBlock.cpp
index 21b870f..2769bb9 100644
--- a/lib/IR/StmtBlock.cpp
+++ b/lib/IR/StmtBlock.cpp
@@ -38,7 +38,10 @@
MLFunction *StmtBlock::findFunction() const {
StmtBlock *block = const_cast<StmtBlock *>(this);
- while (block->getParentStmt() != nullptr)
+ while (block->getParentStmt()) {
block = block->getParentStmt()->getBlock();
- return static_cast<MLFunction *>(block);
+ if (!block)
+ return nullptr;
+ }
+ return dyn_cast<MLFunction>(block);
}