Skip to content

Commit a5bee88

Browse files
committed
Avoid crash by using dyn_cast instead of cast for FileLineColLoc
1 parent de33390 commit a5bee88

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

mlir/lib/Query/Matcher/MatchFinder.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,23 @@ MatchFinder::collectMatches(Operation *root, DynMatcher matcher) const {
3838

3939
void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
4040
Operation *op) const {
41-
auto fileLoc = cast<FileLineColLoc>(op->getLoc());
42-
SMLoc smloc = qs.getSourceManager().FindLocForLineAndColumn(
43-
qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
44-
llvm::SMDiagnostic diag =
45-
qs.getSourceManager().GetMessage(smloc, llvm::SourceMgr::DK_Note, "");
46-
diag.print("", os, true, false, true);
41+
if (auto fileLoc = dyn_cast<FileLineColLoc>(op->getLoc())) {
42+
SMLoc smloc = qs.getSourceManager().FindLocForLineAndColumn(
43+
qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
44+
llvm::SMDiagnostic diag =
45+
qs.getSourceManager().GetMessage(smloc, llvm::SourceMgr::DK_Note, "");
46+
diag.print("", os, true, false, true);
47+
}
4748
}
4849

4950
void MatchFinder::printMatch(llvm::raw_ostream &os, QuerySession &qs,
5051
Operation *op, const std::string &binding) const {
51-
auto fileLoc = cast<FileLineColLoc>(op->getLoc());
52-
auto smloc = qs.getSourceManager().FindLocForLineAndColumn(
53-
qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
54-
qs.getSourceManager().PrintMessage(os, smloc, llvm::SourceMgr::DK_Note,
55-
"\"" + binding + "\" binds here");
52+
if (auto fileLoc = dyn_cast<FileLineColLoc>(op->getLoc())) {
53+
auto smloc = qs.getSourceManager().FindLocForLineAndColumn(
54+
qs.getBufferId(), fileLoc.getLine(), fileLoc.getColumn());
55+
qs.getSourceManager().PrintMessage(os, smloc, llvm::SourceMgr::DK_Note,
56+
"\"" + binding + "\" binds here");
57+
}
5658
}
5759

5860
std::vector<Operation *>

0 commit comments

Comments
 (0)