Skip to content

Commit ef596d9

Browse files
committed
Fixes #3196
gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): * ast/rust-ast.h: * lex/rust-token.cc (Token::as_string): * lex/rust-token.h (enum PrimitiveCoreType): * parse/rust-parse-impl.h (Parser::parse_inner_attribute): (Parser::parse_outer_attribute): Signed-off-by: lucas.plantrose <[email protected]>
1 parent 6708d35 commit ef596d9

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

gcc/rust/ast/rust-ast-collector.cc

+15
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ TokenCollector::visit (VariadicParam &param)
141141
void
142142
TokenCollector::visit (Attribute &attrib)
143143
{
144+
if (attrib.is_from_comment() && attrib.has_attr_input())
145+
{
146+
if (attrib.is_inner_attribute())
147+
push(Rust::Token::make (INNER_DOC_COMMENT, attrib.get_locus ()));
148+
else
149+
push(Rust::Token::make (OUTER_DOC_COMMENT, attrib.get_locus ()));
150+
auto lit = (static_cast<AttrInputLiteral &> (attrib.get_attr_input ())).get_literal().get_literal();
151+
push(Rust::Token::make_doc_string_literal(attrib.get_locus (), lit.as_string ()));
152+
push(Rust::Token::make (DOC_COMMENT_END, attrib.get_locus ()));
153+
return;
154+
}
155+
else
156+
{
157+
144158
push (Rust::Token::make (HASH, attrib.get_locus ()));
145159
if (attrib.is_inner_attribute ())
146160
push (Rust::Token::make (EXCLAM, UNDEF_LOCATION));
@@ -173,6 +187,7 @@ TokenCollector::visit (Attribute &attrib)
173187
}
174188
}
175189
push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
190+
}
176191
}
177192

178193
void

gcc/rust/ast/rust-ast.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ struct Attribute
636636

637637
bool inner_attribute;
638638

639+
// Only relevant in case of a doc attribute
640+
bool from_comment;
641+
639642
// TODO: maybe a variable storing whether attr input is parsed or not
640643

641644
public:
@@ -644,9 +647,10 @@ struct Attribute
644647

645648
// Constructor has pointer AttrInput for polymorphism reasons
646649
Attribute (SimplePath path, std::unique_ptr<AttrInput> input,
647-
location_t locus = UNDEF_LOCATION, bool inner_attribute = false)
650+
location_t locus = UNDEF_LOCATION, bool inner_attribute = false,
651+
bool from_comment = false)
648652
: path (std::move (path)), attr_input (std::move (input)), locus (locus),
649-
inner_attribute (inner_attribute)
653+
inner_attribute (inner_attribute), from_comment (from_comment)
650654
{}
651655

652656
bool is_derive () const;
@@ -681,6 +685,8 @@ struct Attribute
681685
// Returns whether the attribute is considered an "empty" attribute.
682686
bool is_empty () const { return attr_input == nullptr && path.is_empty (); }
683687

688+
bool is_from_comment () { return from_comment; }
689+
684690
location_t get_locus () const { return locus; }
685691

686692
AttrInput &get_attr_input () const { return *attr_input; }

gcc/rust/lex/rust-token.cc

+2
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ Token::as_string () const
268268
return get_str ();
269269
else
270270
return get_str () + get_type_hint_str ();
271+
case DOC_STRING_LITERAL:
272+
return escape_special_chars(*std::move (str), Context::String);
271273
default:
272274
return get_str ();
273275
}

gcc/rust/lex/rust-token.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,10 @@ enum PrimitiveCoreType
144144
/* Macros */ \
145145
RS_TOKEN (DOLLAR_SIGN, "$") \
146146
/* Doc Comments */ \
147-
RS_TOKEN (INNER_DOC_COMMENT, "#![doc]") \
148-
RS_TOKEN (OUTER_DOC_COMMENT, "#[doc]") \
147+
RS_TOKEN (INNER_DOC_COMMENT, "/**!") \
148+
RS_TOKEN (OUTER_DOC_COMMENT, "/**") \
149+
RS_TOKEN (DOC_COMMENT_END, "*/") \
150+
RS_TOKEN (DOC_STRING_LITERAL, "string") \
149151
RS_TOKEN_KEYWORD_2015 (ABSTRACT, "abstract") /* unused */ \
150152
RS_TOKEN_KEYWORD_2015 (AS, "as") \
151153
RS_TOKEN_KEYWORD_2018 (ASYNC, "async") /* unused */ \
@@ -396,6 +398,11 @@ class Token
396398
return TokenPtr (new Token (OUTER_DOC_COMMENT, locus, std::move (str)));
397399
}
398400

401+
static TokenPtr make_doc_string_literal (location_t locus, std::string &&str)
402+
{
403+
return TokenPtr (new Token (DOC_STRING_LITERAL, locus, std::move (str)));
404+
}
405+
399406
// Makes and returns a new TokenPtr of type LIFETIME.
400407
static TokenPtr make_lifetime (location_t locus, std::string &&str)
401408
{
@@ -458,6 +465,7 @@ return *str;
458465
case BYTE_CHAR_LITERAL:
459466
case BYTE_STRING_LITERAL:
460467
case RAW_STRING_LITERAL:
468+
case DOC_STRING_LITERAL:
461469
return true;
462470
default:
463471
return false;

gcc/rust/parse/rust-parse-impl.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ Parser<ManagedTokenSource>::parse_inner_attribute ()
523523
auto path = std::move (std::get<0> (values));
524524
auto input = std::move (std::get<1> (values));
525525
auto loc = std::get<2> (values);
526-
return AST::Attribute (std::move (path), std::move (input), loc, true);
526+
return AST::Attribute (std::move (path), std::move (input), loc, true,
527+
true);
527528
}
528529

529530
if (lexer.peek_token ()->get_id () != HASH)
@@ -1242,7 +1243,8 @@ Parser<ManagedTokenSource>::parse_outer_attribute ()
12421243
auto path = std::move (std::get<0> (values));
12431244
auto input = std::move (std::get<1> (values));
12441245
auto loc = std::get<2> (values);
1245-
return AST::Attribute (std::move (path), std::move (input), loc, false);
1246+
return AST::Attribute (std::move (path), std::move (input), loc, false,
1247+
true);
12461248
}
12471249

12481250
if (lexer.peek_token ()->get_id () == INNER_DOC_COMMENT)

0 commit comments

Comments
 (0)