Skip to content

Commit a1c34b4

Browse files
committed
hir: improve doc attribute handling ...
... should fix #1125 Signed-off-by: Zixing Liu <liushuyu011@gmail.com>
1 parent dc57f4c commit a1c34b4

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

gcc/rust/hir/rust-ast-lower-base.cc

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,11 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
838838
&& attr.get_attr_input ().get_attr_input_type ()
839839
== AST::AttrInput::AttrInputType::LITERAL;
840840

841-
if (is_lang_item)
841+
bool is_doc_item = str_path.compare ("doc") == 0;
842+
843+
if (is_doc_item)
844+
handle_doc_item_attribute (item, attr);
845+
else if (is_lang_item)
842846
handle_lang_item_attribute (item, attr);
843847
else if (!attribute_handled_in_another_pass (str_path))
844848
{
@@ -848,6 +852,27 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
848852
}
849853
}
850854

855+
void
856+
ASTLoweringBase::handle_doc_item_attribute (const HIR::Item &item,
857+
const AST::Attribute &attr)
858+
{
859+
auto simple_doc_comment = attr.has_attr_input ()
860+
&& attr.get_attr_input ().get_attr_input_type ()
861+
== AST::AttrInput::AttrInputType::LITERAL;
862+
if (simple_doc_comment)
863+
return;
864+
865+
const AST::AttrInput &input = attr.get_attr_input ();
866+
bool is_token_tree
867+
= input.get_attr_input_type () == AST::AttrInput::AttrInputType::TOKEN_TREE;
868+
rust_assert (is_token_tree);
869+
const auto &option = static_cast<const AST::DelimTokenTree &> (input);
870+
AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
871+
872+
// TODO: add actual and complete checks for the doc attributes
873+
rust_assert (meta_item);
874+
}
875+
851876
void
852877
ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item,
853878
const AST::Attribute &attr)

gcc/rust/hir/rust-ast-lower-base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ class ASTLoweringBase : public AST::ASTVisitor
268268
void handle_lang_item_attribute (const HIR::Item &item,
269269
const AST::Attribute &attr);
270270

271+
void handle_doc_item_attribute (const HIR::Item &item,
272+
const AST::Attribute &attr);
273+
271274
bool is_known_attribute (const std::string &attribute_path) const;
272275

273276
bool

gcc/rust/util/rust-attributes.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
namespace Rust {
2222
namespace Analysis {
2323

24-
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#256
24+
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248
2525
static const BuiltinAttrDefinition __definitions[]
26-
= {{"inline", CODE_GENERATION}, {"cfg", EXPANSION},
27-
{"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS},
28-
{"lang", HIR_LOWERING}, {"must_use", STATIC_ANALYSIS}};
26+
= {{"inline", CODE_GENERATION}, {"cfg", EXPANSION},
27+
{"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS},
28+
{"doc", HIR_LOWERING}, {"lang", HIR_LOWERING},
29+
{"must_use", STATIC_ANALYSIS}};
2930

3031
BuiltinAttributeMappings *
3132
BuiltinAttributeMappings::get ()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// doc comment 1
2+
/// doc comment 2
3+
/// `blah blah` markdown
4+
pub struct TestStruct {}
5+
6+
#[doc(hidden)]
7+
pub struct DocAttribute {}
8+
9+
#[doc(a,b)]
10+
pub struct UnkAttribute {}
11+
12+
fn main() {
13+
let _ = TestStruct {};
14+
let _ = DocAttribute {};
15+
let _ = UnkAttribute {};
16+
}

0 commit comments

Comments
 (0)