Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks to annotation getters. #5052

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use try-catch instead of BUG_CHECK.
Signed-off-by: fruffy <fruffy@nyu.edu>
  • Loading branch information
fruffy committed Dec 11, 2024
commit 83900800bfb1f23c7d067eb111521c9486eee226
54 changes: 38 additions & 16 deletions ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -333,34 +333,56 @@ class Annotation {
IndexedVector<NamedExpression>> body;

inline auto &getUnparsed() {
BUG_CHECK(annotationKind() == Kind::Unparsed, "Annotation has been parsed already.");
return std::get<UnparsedAnnotation>(body);
try {
return std::get<UnparsedAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation has been parsed already.");
}
}
inline const auto &getUnparsed() const {
BUG_CHECK(annotationKind() == Kind::Unparsed, "Annotation has been parsed already.");
return std::get<UnparsedAnnotation>(body);
try {
return std::get<UnparsedAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation has been parsed already.");
}
}
inline auto &getExpr() {
BUG_CHECK(annotationKind() == Kind::Unstructured || annotationKind() == Kind::StructuredExpressionList, "Annotation does not contain an expression list.");
return std::get<ExpressionAnnotation>(body);
try {
return std::get<ExpressionAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation does not contain an expression list.");
}
}
inline const auto &getExpr() const {
BUG_CHECK(annotationKind() == Kind::Unstructured || annotationKind() == Kind::StructuredExpressionList, "Annotation does not contain an expression list.");
return std::get<ExpressionAnnotation>(body);
try {
return std::get<ExpressionAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation does not contain an expression list.");
}
}
inline Expression getExpr(size_t idx) const {
BUG_CHECK(annotationKind() == Kind::Unstructured || annotationKind() == Kind::StructuredExpressionList, "Annotation does not contain an expression list.");
const auto &expr = getExpr();
BUG_CHECK(idx < expr.size(), "invalid annotation expression index");
return expr[idx];
try {
const auto &expr = getExpr();
return expr[idx];
} catch (const std::out_of_range &) {
BUG("invalid annotation expression index");
} catch (const std::bad_variant_access &) {
BUG("Annotation does not contain an expression list.");
}
}
inline auto &getKV() {
BUG_CHECK(std::holds_alternative<KVAnnotation>(body), "Annotation does not contain a key-value list.");
return std::get<KVAnnotation>(body);
try {
return std::get<KVAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation does not contain a key-value list.");
}
}
inline const auto &getKV() const {
BUG_CHECK(std::holds_alternative<KVAnnotation>(body), "Annotation does not contain a key-value list.");
return std::get<KVAnnotation>(body);
try {
return std::get<KVAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation does not contain a key-value list.");
}
}

/// If this is true this is a structured annotation, and there are some
Expand Down
Loading