1139: hir: improve doc attribute handling r=philberty a=liushuyu

- hir: improve doc attribute handling

Addresses #1140

Co-authored-by: liushuyu <liushuyu011@gmail.com>
This commit is contained in:
bors[bot] 2022-04-21 19:25:01 +00:00 committed by GitHub
commit 243ef0dfe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 5 deletions

View File

@ -839,7 +839,11 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
&& attr.get_attr_input ().get_attr_input_type () && attr.get_attr_input ().get_attr_input_type ()
== AST::AttrInput::AttrInputType::LITERAL; == 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); handle_lang_item_attribute (item, attr);
else if (!attribute_handled_in_another_pass (str_path)) 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 void
ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item, ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item,
const AST::Attribute &attr) const AST::Attribute &attr)

View File

@ -268,6 +268,9 @@ protected:
void handle_lang_item_attribute (const HIR::Item &item, void handle_lang_item_attribute (const HIR::Item &item,
const AST::Attribute &attr); 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 is_known_attribute (const std::string &attribute_path) const;
bool bool

View File

@ -21,11 +21,12 @@
namespace Rust { namespace Rust {
namespace Analysis { 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[] static const BuiltinAttrDefinition __definitions[]
= {{"inline", CODE_GENERATION}, {"cfg", EXPANSION}, = {{"inline", CODE_GENERATION}, {"cfg", EXPANSION},
{"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS}, {"cfg_attr", EXPANSION}, {"allow", STATIC_ANALYSIS},
{"lang", HIR_LOWERING}, {"must_use", STATIC_ANALYSIS}}; {"doc", HIR_LOWERING}, {"lang", HIR_LOWERING},
{"must_use", STATIC_ANALYSIS}};
BuiltinAttributeMappings * BuiltinAttributeMappings *
BuiltinAttributeMappings::get () BuiltinAttributeMappings::get ()

View File

@ -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 {};
}