aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorliushuyu <liushuyu011@gmail.com>2022-04-19 20:49:35 -0600
committerliushuyu <liushuyu011@gmail.com>2022-04-20 02:56:36 -0600
commita1c34b478fee1c1415b65624ad647cf3b93c9090 (patch)
treeae3b08c1f2b3d5205062da96adcb00c14f703275
parentdc57f4ced0d168be911ca22db81fe9526e2d70a8 (diff)
downloadgcc-a1c34b478fee1c1415b65624ad647cf3b93c9090.zip
gcc-a1c34b478fee1c1415b65624ad647cf3b93c9090.tar.gz
gcc-a1c34b478fee1c1415b65624ad647cf3b93c9090.tar.bz2
hir: improve doc attribute handling ...
... should fix #1125 Signed-off-by: Zixing Liu <liushuyu011@gmail.com>
-rw-r--r--gcc/rust/hir/rust-ast-lower-base.cc27
-rw-r--r--gcc/rust/hir/rust-ast-lower-base.h3
-rw-r--r--gcc/rust/util/rust-attributes.cc9
-rw-r--r--gcc/testsuite/rust/compile/torture/doc_comment.rs16
4 files changed, 50 insertions, 5 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc
index fa7c700..85960ef 100644
--- a/gcc/rust/hir/rust-ast-lower-base.cc
+++ b/gcc/rust/hir/rust-ast-lower-base.cc
@@ -838,7 +838,11 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
&& attr.get_attr_input ().get_attr_input_type ()
== AST::AttrInput::AttrInputType::LITERAL;
- if (is_lang_item)
+ bool is_doc_item = str_path.compare ("doc") == 0;
+
+ if (is_doc_item)
+ handle_doc_item_attribute (item, attr);
+ else if (is_lang_item)
handle_lang_item_attribute (item, attr);
else if (!attribute_handled_in_another_pass (str_path))
{
@@ -849,6 +853,27 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
}
void
+ASTLoweringBase::handle_doc_item_attribute (const HIR::Item &item,
+ const AST::Attribute &attr)
+{
+ auto simple_doc_comment = attr.has_attr_input ()
+ && attr.get_attr_input ().get_attr_input_type ()
+ == AST::AttrInput::AttrInputType::LITERAL;
+ if (simple_doc_comment)
+ return;
+
+ const AST::AttrInput &input = attr.get_attr_input ();
+ bool is_token_tree
+ = input.get_attr_input_type () == AST::AttrInput::AttrInputType::TOKEN_TREE;
+ rust_assert (is_token_tree);
+ const auto &option = static_cast<const AST::DelimTokenTree &> (input);
+ AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
+
+ // TODO: add actual and complete checks for the doc attributes
+ rust_assert (meta_item);
+}
+
+void
ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item,
const AST::Attribute &attr)
{
diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h
index 185f467..c61d700 100644
--- a/gcc/rust/hir/rust-ast-lower-base.h
+++ b/gcc/rust/hir/rust-ast-lower-base.h
@@ -268,6 +268,9 @@ protected:
void handle_lang_item_attribute (const HIR::Item &item,
const AST::Attribute &attr);
+ void handle_doc_item_attribute (const HIR::Item &item,
+ const AST::Attribute &attr);
+
bool is_known_attribute (const std::string &attribute_path) const;
bool
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 4f3bd0b..b1211fe 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -21,11 +21,12 @@
namespace Rust {
namespace Analysis {
-// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#256
+// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248
static const BuiltinAttrDefinition __definitions[]
- = {{"inline", CODE_GENERATION}, {"cfg", EXPANSION},
- {"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS},
- {"lang", HIR_LOWERING}, {"must_use", STATIC_ANALYSIS}};
+ = {{"inline", CODE_GENERATION}, {"cfg", EXPANSION},
+ {"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS},
+ {"doc", HIR_LOWERING}, {"lang", HIR_LOWERING},
+ {"must_use", STATIC_ANALYSIS}};
BuiltinAttributeMappings *
BuiltinAttributeMappings::get ()
diff --git a/gcc/testsuite/rust/compile/torture/doc_comment.rs b/gcc/testsuite/rust/compile/torture/doc_comment.rs
new file mode 100644
index 0000000..f99e415
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/doc_comment.rs
@@ -0,0 +1,16 @@
+/// doc comment 1
+/// doc comment 2
+/// `blah blah` markdown
+pub struct TestStruct {}
+
+#[doc(hidden)]
+pub struct DocAttribute {}
+
+#[doc(a,b)]
+pub struct UnkAttribute {}
+
+fn main() {
+ let _ = TestStruct {};
+ let _ = DocAttribute {};
+ let _ = UnkAttribute {};
+}