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 all commits
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
59 changes: 50 additions & 9 deletions ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,58 @@ class Annotation {
Vector<Expression>,
IndexedVector<NamedExpression>> body;

inline auto &getUnparsed() { return std::get<UnparsedAnnotation>(body); }
inline const auto &getUnparsed() const { return std::get<UnparsedAnnotation>(body); }
inline auto &getExpr() { return std::get<ExpressionAnnotation>(body); }
inline const auto &getExpr() const { return std::get<ExpressionAnnotation>(body); }
inline auto &getUnparsed() {
try {
return std::get<UnparsedAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation has been parsed already.");
}
}
inline const auto &getUnparsed() const {
try {
return std::get<UnparsedAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation has been parsed already.");
}
}
inline auto &getExpr() {
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 {
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 {
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() {
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 {
try {
return std::get<KVAnnotation>(body);
} catch (const std::bad_variant_access &) {
BUG("Annotation does not contain a key-value list.");
}
}
inline auto &getKV() { return std::get<KVAnnotation>(body); }
inline const auto &getKV() const { return std::get<KVAnnotation>(body); }

/// If this is true this is a structured annotation, and there are some
/// constraints on its contents.
Expand Down
Loading