Skip to content

Commit 243ef0d

Browse files
bors[bot]liushuyu
andauthored
Merge #1139
1139: hir: improve doc attribute handling r=philberty a=liushuyu - hir: improve doc attribute handling Addresses #1140 Co-authored-by: liushuyu <liushuyu011@gmail.com>
2 parents cb42c36 + a1c34b4 commit 243ef0d

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
@@ -839,7 +839,11 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
839839
&& attr.get_attr_input ().get_attr_input_type ()
840840
== AST::AttrInput::AttrInputType::LITERAL;
841841

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

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