aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-10-23 15:00:54 +0200
committerPhilip Herron <philip.herron@embecosm.com>2023-10-31 11:39:08 +0000
commit5d622b6a6d61743e054b226e441c755e1062c067 (patch)
tree4f48f75fa3ad2e2705f3d8cf2bf67a3e7333d838 /gcc
parent4ac19a050193e0091a9556997780a0affb8a9621 (diff)
downloadgcc-5d622b6a6d61743e054b226e441c755e1062c067.zip
gcc-5d622b6a6d61743e054b226e441c755e1062c067.tar.gz
gcc-5d622b6a6d61743e054b226e441c755e1062c067.tar.bz2
Document proc macro token tree indices
Multiple references to procedural macro token trees were left as magic number in the code. This commit introduces some constexpr for those along some explanation for the selected value. gcc/rust/ChangeLog: * backend/rust-compile-base.cc (get_attributes): Add documentation for indices 3 and 4. (get_trait_name): Add documentation for index 1. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/backend/rust-compile-base.cc27
1 files changed, 23 insertions, 4 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index 9a7533a..81ce0e9 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -165,12 +165,24 @@ get_attributes (const AST::Attribute &attr)
// TODO: Should we rely on fixed index ? Should we search for the
// attribute tokentree instead ?
- if (tt.get_token_trees ().size () > 3)
+
+ // Derive proc macros have the following format:
+ // #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))]
+ // -~~~~~~~~ - ~~~~~~~~~~---------------------
+ // ^0 ^1 ^2 ^3 ^4
+ // - "attributes" is stored at position 3 in the token tree
+ // - attribute are stored in the delimited token tree in position 4
+ constexpr size_t attr_kw_pos = 3;
+ constexpr size_t attribute_list_pos = 4;
+
+ if (tt.get_token_trees ().size () > attr_kw_pos)
{
- rust_assert (tt.get_token_trees ()[3]->as_string () == "attributes");
+ rust_assert (tt.get_token_trees ()[attr_kw_pos]->as_string ()
+ == "attributes");
auto attributes = static_cast<const AST::DelimTokenTree *> (
- tt.get_token_trees ()[4].get ());
+ tt.get_token_trees ()[attribute_list_pos].get ());
+
auto &token_trees = attributes->get_token_trees ();
for (auto i = token_trees.cbegin () + 1; // Skip opening parenthesis
@@ -186,11 +198,18 @@ get_attributes (const AST::Attribute &attr)
static std::string
get_trait_name (const AST::Attribute &attr)
{
+ // Derive proc macros have the following format:
+ // #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))]
+ // -~~~~~~~~ - ~~~~~~~~~~---------------------
+ // ^0 ^1 ^2 ^3 ^4
+ // - The trait name is stored at position 1
+ constexpr size_t trait_name_pos = 1;
+
rust_assert (attr.get_attr_input ().get_attr_input_type ()
== Rust::AST::AttrInput::TOKEN_TREE);
const auto &tt
= static_cast<const AST::DelimTokenTree &> (attr.get_attr_input ());
- return tt.get_token_trees ()[1]->as_string ();
+ return tt.get_token_trees ()[trait_name_pos]->as_string ();
}
void