From: Owen Avery Date: Tue, 12 Aug 2025 02:06:02 +0000 (-0400) Subject: gccrs: CfgStrip AST nodes marked with #[test] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c38cc8c51cf6445105bb10df165dcd9379062429;p=thirdparty%2Fgcc.git gccrs: CfgStrip AST nodes marked with #[test] gcc/rust/ChangeLog: * expand/rust-cfg-strip.cc: Include "rust-macro-expand.h". (fails_cfg): Rename to... (CfgStrip::fails_cfg): ...here and handle test attributes. (fails_cfg_with_expand): Rename to... (CfgStrip::fails_cfg_with_expand): ...here and handle test attributes. * expand/rust-cfg-strip.h (struct ExpansionCfg): Forward declare. (CfgStrip::fails_cfg): New member function. (CfgStrip::fails_cfg_with_expand): Likewise. (CfgStrip::CfgStrip): Accept reference to ExpansionCfg. (CfgStrip::expansion_cfg): New member variable. * rust-session-manager.cc (Session::expansion): Pass ExpansionCfg instance to CfgStrip constructor. gcc/testsuite/ChangeLog: * rust/compile/cfg-test.rs: New test. Signed-off-by: Owen Avery --- diff --git a/gcc/rust/expand/rust-cfg-strip.cc b/gcc/rust/expand/rust-cfg-strip.cc index 58d80716009..8a1cf787a0a 100644 --- a/gcc/rust/expand/rust-cfg-strip.cc +++ b/gcc/rust/expand/rust-cfg-strip.cc @@ -22,6 +22,7 @@ #include "rust-path.h" #include "rust-session-manager.h" #include "rust-attribute-values.h" +#include "rust-macro-expand.h" namespace Rust { @@ -30,7 +31,7 @@ namespace Rust { * should be stripped. Note that attributes must be expanded before calling. */ bool -fails_cfg (const AST::AttrVec &attrs) +CfgStrip::fails_cfg (const AST::AttrVec &attrs) const { auto &session = Session::get_instance (); @@ -39,6 +40,9 @@ fails_cfg (const AST::AttrVec &attrs) if (attr.get_path () == Values::Attributes::CFG && !attr.check_cfg_predicate (session)) return true; + else if (!expansion_cfg.should_test + && attr.get_path () == Values::Attributes::TEST) + return true; } return false; } @@ -48,7 +52,7 @@ fails_cfg (const AST::AttrVec &attrs) * should be stripped. Will expand attributes as well. */ bool -fails_cfg_with_expand (AST::AttrVec &attrs) +CfgStrip::fails_cfg_with_expand (AST::AttrVec &attrs) const { auto &session = Session::get_instance (); @@ -85,6 +89,9 @@ fails_cfg_with_expand (AST::AttrVec &attrs) attr.as_string ().c_str ()); } } + else if (!expansion_cfg.should_test + && attr.get_path () == Values::Attributes::TEST) + return true; } return false; } diff --git a/gcc/rust/expand/rust-cfg-strip.h b/gcc/rust/expand/rust-cfg-strip.h index 767cf28a712..75c50471924 100644 --- a/gcc/rust/expand/rust-cfg-strip.h +++ b/gcc/rust/expand/rust-cfg-strip.h @@ -23,14 +23,23 @@ #include "rust-item.h" namespace Rust { + +// forward declare +struct ExpansionCfg; + // Visitor used to maybe_strip attributes. class CfgStrip : public AST::DefaultASTVisitor { private: + bool fails_cfg (const AST::AttrVec &attrs) const; + + bool fails_cfg_with_expand (AST::AttrVec &attrs) const; + public: using DefaultASTVisitor::visit; - CfgStrip () {} + CfgStrip (const ExpansionCfg &expansion_cfg) : expansion_cfg (expansion_cfg) + {} /* Run the AttrVisitor on an entire crate */ void go (AST::Crate &crate); @@ -194,6 +203,9 @@ public: { DefaultASTVisitor::visit (item); } + +private: + const ExpansionCfg &expansion_cfg; }; } // namespace Rust diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index c88f4675753..5b8ba5759bf 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc @@ -938,7 +938,7 @@ Session::expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx) while (!fixed_point_reached && iterations < cfg.recursion_limit) { - CfgStrip ().go (crate); + CfgStrip (cfg).go (crate); // Errors might happen during cfg strip pass bool visitor_dirty = false; diff --git a/gcc/testsuite/rust/compile/cfg-test.rs b/gcc/testsuite/rust/compile/cfg-test.rs new file mode 100644 index 00000000000..a2e870c80dc --- /dev/null +++ b/gcc/testsuite/rust/compile/cfg-test.rs @@ -0,0 +1,4 @@ +#[test] +fn foo() { + some_function_which_doesnt_exist(); +}