diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-08-09 09:37:41 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-08-09 09:37:41 +0200 |
commit | 4ffd884a69396d828049d4a14d17e6d3f6c8d61f (patch) | |
tree | 9f498600f878ccd86cca5872dccdde1f0dd44b1b /gcc | |
parent | 084f959076c10d54bdb5c80cad10a0aac5756ea4 (diff) | |
download | gcc-4ffd884a69396d828049d4a14d17e6d3f6c8d61f.zip gcc-4ffd884a69396d828049d4a14d17e6d3f6c8d61f.tar.gz gcc-4ffd884a69396d828049d4a14d17e6d3f6c8d61f.tar.bz2 |
const-checker: Add `is_const_extern_fn` helper function
Co-authored-by: philberty <philip.herron@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/checks/errors/rust-const-checker.cc | 27 | ||||
-rw-r--r-- | gcc/rust/checks/errors/rust-const-checker.h | 7 |
2 files changed, 23 insertions, 11 deletions
diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index bd4c7f1..ad0a2cf 100644 --- a/gcc/rust/checks/errors/rust-const-checker.cc +++ b/gcc/rust/checks/errors/rust-const-checker.cc @@ -37,6 +37,21 @@ ConstChecker::go (HIR::Crate &crate) item->accept_vis (*this); } +bool +ConstChecker::is_const_extern_fn (HIR::ExternalFunctionItem &fn) +{ + // FIXME: Is it really how we want to handle `rustc_const_stable` + // and `rustc_const_unstable`? + // TODO: Add these attributes to the attribute check and handle + // `stable` and `unstable` as well + return std::any_of ( + fn.get_outer_attrs ().begin (), fn.get_outer_attrs ().end (), + [] (const AST::Attribute &attr) { + // `starts_with` in C++11... + return attr.get_path ().as_string ().rfind ("rustc_const_", 0) == 0; + }); +} + void ConstChecker::visit (IdentifierExpr &ident_expr) {} @@ -261,17 +276,7 @@ ConstChecker::check_function_call (HirId fn_id, Location locus) { { auto fn = static_cast<ExternalFunctionItem *> (maybe_extern_item); - auto is_const_extern = std::any_of ( - fn->get_outer_attrs ().begin (), fn->get_outer_attrs ().end (), - [] (const AST::Attribute &attr) { - // `starts_with` in C++11... - // FIXME: Is it really how we want to handle `rustc_const_stable` - // and `rustc_const_unstable`? - // TODO: Add these attributes to the attribute check and handle - // `stable` and `unstable` as well - return attr.get_path ().as_string ().rfind ("rustc_const_", 0) == 0; - }); - if (!is_const_extern) + if (!is_const_extern_fn (*fn)) is_error = true; } } diff --git a/gcc/rust/checks/errors/rust-const-checker.h b/gcc/rust/checks/errors/rust-const-checker.h index a474fc8..608ea3e 100644 --- a/gcc/rust/checks/errors/rust-const-checker.h +++ b/gcc/rust/checks/errors/rust-const-checker.h @@ -33,6 +33,13 @@ public: void go (HIR::Crate &crate); + /** + * Check if an item is a const extern item or not + * TODO: Move this to a const compilation context class or an attribute + * checking class + */ + static bool is_const_extern_fn (HIR::ExternalFunctionItem &fn); + private: /** * Check that only const functions are called in const contexts |