aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/expand/rust-macro-expand.cc
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2023-03-20 03:10:26 +0100
committerCohenArthur <arthur.cohen@embecosm.com>2023-03-25 08:47:56 +0000
commite10e882ddf3d0f85f36e6645c4637f9725ee2562 (patch)
treead265ba4af46d53e1fbdeccbbbcde5cfeab3e0c3 /gcc/rust/expand/rust-macro-expand.cc
parent2b52571e8aa28a100b8989ece12e929a9fb6bcf4 (diff)
downloadgcc-e10e882ddf3d0f85f36e6645c4637f9725ee2562.zip
gcc-e10e882ddf3d0f85f36e6645c4637f9725ee2562.tar.gz
gcc-e10e882ddf3d0f85f36e6645c4637f9725ee2562.tar.bz2
expand: Move cfg-attrs related functions out of MacroExpander
The current situation where the MacroExpander and AttrVisitor recursively call into each other is not great, and causes pains for macro imports. We need to split this pass in two, with one pass being responsible for cfg-attr checking and stripping, and one being responsible for macro expansion. gcc/rust/ChangeLog: * expand/rust-macro-expand.cc (MacroExpander::expand_crate): Do not cfg-attr strip in MacroExpander (MacroExpander::fails_cfg): Function moved... (MacroExpander::fails_cfg_with_expand): Function moved... (MacroExpander::expand_cfg_attrs): Function moved... * expand/rust-attribute-visitor.cc (fails_cfg): ...here. (fails_cfg_with_expand): ...here. (expand_cfg_attrs): ...here. (AttrVisitor::expand_struct_fields): Use new functions. (AttrVisitor::expand_tuple_fields): Likewise. (AttrVisitor::expand_function_params): Likewise. (AttrVisitor::visit): Likewise. (AttrVisitor::go): New function. * expand/rust-attribute-visitor.h: Declare AttrVisitor::go. * expand/rust-macro-expand.h (struct MacroExpander): Remove cfg-attr related functions.
Diffstat (limited to 'gcc/rust/expand/rust-macro-expand.cc')
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc107
1 files changed, 0 insertions, 107 deletions
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 71aafef..28ba563 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -285,102 +285,6 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon)
set_expanded_fragment (std::move (fragment));
}
-/* Determines whether any cfg predicate is false and hence item with attributes
- * should be stripped. Note that attributes must be expanded before calling. */
-bool
-MacroExpander::fails_cfg (const AST::AttrVec &attrs) const
-{
- for (const auto &attr : attrs)
- {
- if (attr.get_path () == "cfg" && !attr.check_cfg_predicate (session))
- return true;
- }
- return false;
-}
-
-/* Determines whether any cfg predicate is false and hence item with attributes
- * should be stripped. Will expand attributes as well. */
-bool
-MacroExpander::fails_cfg_with_expand (AST::AttrVec &attrs) const
-{
- // TODO: maybe have something that strips cfg attributes that evaluate true?
- for (auto &attr : attrs)
- {
- if (attr.get_path () == "cfg")
- {
- if (!attr.is_parsed_to_meta_item ())
- attr.parse_attr_to_meta_item ();
-
- // DEBUG
- if (!attr.is_parsed_to_meta_item ())
- rust_debug ("failed to parse attr to meta item, right before "
- "cfg predicate check");
- else
- rust_debug ("attr has been successfully parsed to meta item, "
- "right before cfg predicate check");
-
- if (!attr.check_cfg_predicate (session))
- {
- // DEBUG
- rust_debug (
- "cfg predicate failed for attribute: \033[0;31m'%s'\033[0m",
- attr.as_string ().c_str ());
-
- return true;
- }
- else
- {
- // DEBUG
- rust_debug ("cfg predicate succeeded for attribute: "
- "\033[0;31m'%s'\033[0m",
- attr.as_string ().c_str ());
- }
- }
- }
- return false;
-}
-
-// Expands cfg_attr attributes.
-void
-MacroExpander::expand_cfg_attrs (AST::AttrVec &attrs)
-{
- for (std::size_t i = 0; i < attrs.size (); i++)
- {
- auto &attr = attrs[i];
- if (attr.get_path () == "cfg_attr")
- {
- if (!attr.is_parsed_to_meta_item ())
- attr.parse_attr_to_meta_item ();
-
- if (attr.check_cfg_predicate (session))
- {
- // split off cfg_attr
- AST::AttrVec new_attrs = attr.separate_cfg_attrs ();
-
- // remove attr from vector
- attrs.erase (attrs.begin () + i);
-
- // add new attrs to vector
- attrs.insert (attrs.begin () + i,
- std::make_move_iterator (new_attrs.begin ()),
- std::make_move_iterator (new_attrs.end ()));
- }
-
- /* do something - if feature (first token in tree) is in fact enabled,
- * make tokens listed afterwards into attributes. i.e.: for
- * [cfg_attr(feature = "wow", wow1, wow2)], if "wow" is true, then add
- * attributes [wow1] and [wow2] to attribute list. This can also be
- * recursive, so check for expanded attributes being recursive and
- * possibly recursively call the expand_attrs? */
- }
- else
- {
- i++;
- }
- }
- attrs.shrink_to_fit ();
-}
-
void
MacroExpander::expand_crate ()
{
@@ -393,17 +297,6 @@ MacroExpander::expand_crate ()
// TODO: does cfg apply for inner attributes? research.
// the apparent answer (from playground test) is yes
- // expand crate cfg_attr attributes
- expand_cfg_attrs (crate.inner_attrs);
-
- if (fails_cfg_with_expand (crate.inner_attrs))
- {
- // basically, delete whole crate
- crate.strip_crate ();
- // TODO: maybe create warning here? probably not desired behaviour
- }
- // expand module attributes?
-
push_context (ITEM);
// expand attributes recursively and strip items if required