Skip to content

Commit d18952a

Browse files
powerboat9P-E-P
authored andcommitted
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 <[email protected]>
1 parent b14e16c commit d18952a

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

gcc/rust/expand/rust-cfg-strip.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "rust-path.h"
2323
#include "rust-session-manager.h"
2424
#include "rust-attribute-values.h"
25+
#include "rust-macro-expand.h"
2526

2627
namespace Rust {
2728

@@ -30,7 +31,7 @@ namespace Rust {
3031
* should be stripped. Note that attributes must be expanded before calling.
3132
*/
3233
bool
33-
fails_cfg (const AST::AttrVec &attrs)
34+
CfgStrip::fails_cfg (const AST::AttrVec &attrs) const
3435
{
3536
auto &session = Session::get_instance ();
3637

@@ -39,6 +40,9 @@ fails_cfg (const AST::AttrVec &attrs)
3940
if (attr.get_path () == Values::Attributes::CFG
4041
&& !attr.check_cfg_predicate (session))
4142
return true;
43+
else if (!expansion_cfg.should_test
44+
&& attr.get_path () == Values::Attributes::TEST)
45+
return true;
4246
}
4347
return false;
4448
}
@@ -48,7 +52,7 @@ fails_cfg (const AST::AttrVec &attrs)
4852
* should be stripped. Will expand attributes as well.
4953
*/
5054
bool
51-
fails_cfg_with_expand (AST::AttrVec &attrs)
55+
CfgStrip::fails_cfg_with_expand (AST::AttrVec &attrs) const
5256
{
5357
auto &session = Session::get_instance ();
5458

@@ -85,6 +89,9 @@ fails_cfg_with_expand (AST::AttrVec &attrs)
8589
attr.as_string ().c_str ());
8690
}
8791
}
92+
else if (!expansion_cfg.should_test
93+
&& attr.get_path () == Values::Attributes::TEST)
94+
return true;
8895
}
8996
return false;
9097
}

gcc/rust/expand/rust-cfg-strip.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,23 @@
2323
#include "rust-item.h"
2424

2525
namespace Rust {
26+
27+
// forward declare
28+
struct ExpansionCfg;
29+
2630
// Visitor used to maybe_strip attributes.
2731
class CfgStrip : public AST::DefaultASTVisitor
2832
{
2933
private:
34+
bool fails_cfg (const AST::AttrVec &attrs) const;
35+
36+
bool fails_cfg_with_expand (AST::AttrVec &attrs) const;
37+
3038
public:
3139
using DefaultASTVisitor::visit;
3240

33-
CfgStrip () {}
41+
CfgStrip (const ExpansionCfg &expansion_cfg) : expansion_cfg (expansion_cfg)
42+
{}
3443

3544
/* Run the AttrVisitor on an entire crate */
3645
void go (AST::Crate &crate);
@@ -194,6 +203,9 @@ class CfgStrip : public AST::DefaultASTVisitor
194203
{
195204
DefaultASTVisitor::visit (item);
196205
}
206+
207+
private:
208+
const ExpansionCfg &expansion_cfg;
197209
};
198210
} // namespace Rust
199211

gcc/rust/rust-session-manager.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ Session::expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx)
938938

939939
while (!fixed_point_reached && iterations < cfg.recursion_limit)
940940
{
941-
CfgStrip ().go (crate);
941+
CfgStrip (cfg).go (crate);
942942
// Errors might happen during cfg strip pass
943943
bool visitor_dirty = false;
944944

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn foo() {
3+
some_function_which_doesnt_exist();
4+
}

0 commit comments

Comments
 (0)