Skip to content

Commit cad477c

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 <lucas.plantrose@epita.fr>
1 parent 6708d35 commit cad477c

5 files changed

Lines changed: 72 additions & 34 deletions

File tree

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

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -141,38 +141,58 @@ TokenCollector::visit (VariadicParam &param)
141141
void
142142
TokenCollector::visit (Attribute &attrib)
143143
{
144-
push (Rust::Token::make (HASH, attrib.get_locus ()));
145-
if (attrib.is_inner_attribute ())
146-
push (Rust::Token::make (EXCLAM, UNDEF_LOCATION));
147-
push (Rust::Token::make (LEFT_SQUARE, UNDEF_LOCATION));
148-
visit (attrib.get_path ());
149-
150-
if (attrib.has_attr_input ())
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 ()))
151+
.get_literal ()
152+
.get_literal ();
153+
push (Rust::Token::make_doc_string_literal (attrib.get_locus (),
154+
lit.as_string ()));
155+
push (Rust::Token::make (DOC_COMMENT_END, attrib.get_locus ()));
156+
return;
157+
}
158+
else
151159
{
152-
switch (attrib.get_attr_input ().get_attr_input_type ())
160+
push (Rust::Token::make (HASH, attrib.get_locus ()));
161+
if (attrib.is_inner_attribute ())
162+
push (Rust::Token::make (EXCLAM, UNDEF_LOCATION));
163+
push (Rust::Token::make (LEFT_SQUARE, UNDEF_LOCATION));
164+
visit (attrib.get_path ());
165+
166+
if (attrib.has_attr_input ())
153167
{
154-
case AST::AttrInput::AttrInputType::LITERAL: {
155-
visit (static_cast<AttrInputLiteral &> (attrib.get_attr_input ()));
156-
break;
157-
}
158-
case AST::AttrInput::AttrInputType::MACRO: {
159-
visit (static_cast<AttrInputMacro &> (attrib.get_attr_input ()));
160-
break;
161-
}
162-
case AST::AttrInput::AttrInputType::META_ITEM: {
163-
visit (static_cast<AttrInputMetaItemContainer &> (
164-
attrib.get_attr_input ()));
165-
break;
166-
}
167-
case AST::AttrInput::AttrInputType::TOKEN_TREE: {
168-
visit (static_cast<DelimTokenTree &> (attrib.get_attr_input ()));
169-
break;
170-
}
171-
default:
172-
rust_unreachable ();
168+
switch (attrib.get_attr_input ().get_attr_input_type ())
169+
{
170+
case AST::AttrInput::AttrInputType::LITERAL: {
171+
visit (
172+
static_cast<AttrInputLiteral &> (attrib.get_attr_input ()));
173+
break;
174+
}
175+
case AST::AttrInput::AttrInputType::MACRO: {
176+
visit (
177+
static_cast<AttrInputMacro &> (attrib.get_attr_input ()));
178+
break;
179+
}
180+
case AST::AttrInput::AttrInputType::META_ITEM: {
181+
visit (static_cast<AttrInputMetaItemContainer &> (
182+
attrib.get_attr_input ()));
183+
break;
184+
}
185+
case AST::AttrInput::AttrInputType::TOKEN_TREE: {
186+
visit (
187+
static_cast<DelimTokenTree &> (attrib.get_attr_input ()));
188+
break;
189+
}
190+
default:
191+
rust_unreachable ();
192+
}
173193
}
194+
push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
174195
}
175-
push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
176196
}
177197

178198
void

‎gcc/rust/ast/rust-ast.h‎

Lines changed: 8 additions & 2 deletions
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‎

Lines changed: 2 additions & 0 deletions
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‎

Lines changed: 10 additions & 2 deletions
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‎

Lines changed: 4 additions & 2 deletions
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)