aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-04-15 08:54:55 +0000
committerGitHub <noreply@github.com>2022-04-15 08:54:55 +0000
commitd17e0aa7695ddb383e0a4fc43185e2ab42c81703 (patch)
treedead9ef51792ed2b75af442b67d983c77da733bf /gcc
parentd36a3c5752cbffab5bc107bb3cf7710442a29f9e (diff)
parenta9c0649503f19bb2399bb8d4fe2f2d45db65727d (diff)
downloadgcc-d17e0aa7695ddb383e0a4fc43185e2ab42c81703.zip
gcc-d17e0aa7695ddb383e0a4fc43185e2ab42c81703.tar.gz
gcc-d17e0aa7695ddb383e0a4fc43185e2ab42c81703.tar.bz2
Merge #1116
1116: Move `cfg!()` macro to builtins. Fixes #1039 r=CohenArthur a=antego Fixes #1039 Hey team, I need help understanding why the test fails. Compilation succeeds, all the existing tests pass. However the test that I've added fails with the error: ``` FAIL: rust/compile/cfg_macro.rs (test for excess errors) Excess errors: /Users/anton/Documents/projects/gcc2/gccrs/gcc/testsuite/rust/compile/cfg_macro.rs:17:8: fatal error: Failed to lower expr: [MacroInvocation: outer attributes: none cfg!((A)) has semicolon: false] compilation terminated. ``` I tried to understand what's happening using a debugger. The only thing that I understood is that the `MacroBuiltin::cfg` function runs. Appreciate any feedback. Thank you. Co-authored-by: antego <antego@users.noreply.github.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/expand/rust-macro-builtins.cc36
-rw-r--r--gcc/rust/expand/rust-macro-builtins.h3
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc44
-rw-r--r--gcc/rust/expand/rust-macro-expand.h5
-rw-r--r--gcc/rust/util/rust-hir-map.cc1
5 files changed, 40 insertions, 49 deletions
diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc
index d0f7302..8c68a7d 100644
--- a/gcc/rust/expand/rust-macro-builtins.cc
+++ b/gcc/rust/expand/rust-macro-builtins.cc
@@ -376,4 +376,40 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc)
return AST::ASTFragment ({node});
}
+AST::ASTFragment
+MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc)
+{
+ // only parse if not already parsed
+ if (!invoc.is_parsed ())
+ {
+ std::unique_ptr<AST::AttrInputMetaItemContainer> converted_input (
+ invoc.get_delim_tok_tree ().parse_to_meta_item ());
+
+ if (converted_input == nullptr)
+ {
+ rust_debug ("DEBUG: failed to parse macro to meta item");
+ // TODO: do something now? is this an actual error?
+ }
+ else
+ {
+ std::vector<std::unique_ptr<AST::MetaItemInner>> meta_items (
+ std::move (converted_input->get_items ()));
+ invoc.set_meta_item_output (std::move (meta_items));
+ }
+ }
+
+ /* TODO: assuming that cfg! macros can only have one meta item inner, like cfg
+ * attributes */
+ if (invoc.get_meta_items ().size () != 1)
+ return AST::ASTFragment::create_error ();
+
+ bool result = invoc.get_meta_items ()[0]->check_cfg_predicate (
+ Session::get_instance ());
+ auto literal_exp = AST::SingleASTNode (std::unique_ptr<AST::Expr> (
+ new AST::LiteralExpr (result ? "true" : "false", AST::Literal::BOOL,
+ PrimitiveCoreType::CORETYPE_BOOL, {}, invoc_locus)));
+
+ return AST::ASTFragment ({literal_exp});
+}
+
} // namespace Rust
diff --git a/gcc/rust/expand/rust-macro-builtins.h b/gcc/rust/expand/rust-macro-builtins.h
index 0471811..e284c03 100644
--- a/gcc/rust/expand/rust-macro-builtins.h
+++ b/gcc/rust/expand/rust-macro-builtins.h
@@ -92,6 +92,9 @@ public:
static AST::ASTFragment env (Location invoc_locus,
AST::MacroInvocData &invoc);
+
+ static AST::ASTFragment cfg (Location invoc_locus,
+ AST::MacroInvocData &invoc);
};
} // namespace Rust
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 20bbbc0..6224b0c 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -25,50 +25,6 @@
#include "rust-attribute-visitor.h"
namespace Rust {
-void
-MacroExpander::parse_macro_to_meta_item (AST::MacroInvocData &invoc)
-{
- // only parse if not already parsed
- if (invoc.is_parsed ())
- return;
-
- std::unique_ptr<AST::AttrInputMetaItemContainer> converted_input (
- invoc.get_delim_tok_tree ().parse_to_meta_item ());
-
- if (converted_input == nullptr)
- {
- rust_debug ("DEBUG: failed to parse macro to meta item");
- // TODO: do something now? is this an actual error?
- }
- else
- {
- std::vector<std::unique_ptr<AST::MetaItemInner>> meta_items (
- std::move (converted_input->get_items ()));
- invoc.set_meta_item_output (std::move (meta_items));
- }
-}
-
-AST::Literal
-MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc)
-{
- // only allow on cfg macros
- if (invoc.get_path () != "cfg")
- return AST::Literal::create_error ();
-
- parse_macro_to_meta_item (invoc);
-
- /* TODO: assuming that cfg! macros can only have one meta item inner, like cfg
- * attributes */
- if (invoc.get_meta_items ().size () != 1)
- return AST::Literal::create_error ();
-
- bool result = invoc.get_meta_items ()[0]->check_cfg_predicate (session);
- if (result)
- return AST::Literal ("true", AST::Literal::BOOL, CORETYPE_BOOL);
- else
- return AST::Literal ("false", AST::Literal::BOOL, CORETYPE_BOOL);
-}
-
AST::ASTFragment
MacroExpander::expand_decl_macro (Location invoc_locus,
AST::MacroInvocData &invoc,
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index f3ca7fc8..3c53d8d 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -226,11 +226,6 @@ struct MacroExpander
bool fails_cfg (const AST::AttrVec &attr) const;
bool fails_cfg_with_expand (AST::AttrVec &attrs) const;
- // Expand the data of a cfg! macro.
- void parse_macro_to_meta_item (AST::MacroInvocData &invoc);
- // Get the literal representation of a cfg! macro.
- AST::Literal expand_cfg_macro (AST::MacroInvocData &invoc);
-
bool depth_exceeds_recursion_limit () const;
bool try_match_rule (AST::MacroRule &match_rule,
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 23b78ef..e9ad87c 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -756,6 +756,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
{"compile_error", MacroBuiltin::compile_error},
{"concat", MacroBuiltin::concat},
{"env", MacroBuiltin::env},
+ {"cfg", MacroBuiltin::cfg},
};
auto builtin = builtin_macros.find (macro->get_rule_name ());