Skip to content

Commit df6ec4f

Browse files
committed
rustc: Pretty-print outer attributes of items
Issue #487
1 parent 6c6d7c3 commit df6ec4f

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

Diff for: src/comp/pretty/pprust.rs

+47-15
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ fn print_item(&ps s, &@ast::item item) {
257257
hardbreak(s.s);
258258
maybe_print_comment(s, item.span.lo);
259259
alt (item.node) {
260-
case (ast::item_const(?id, ?ty, ?expr, _, _, _)) {
260+
case (ast::item_const(?id, ?ty, ?expr, ?attrs, _, _)) {
261+
print_outer_attributes(s, attrs);
261262
head(s, "const");
262263
print_type(s, *ty);
263264
space(s.s);
@@ -268,19 +269,22 @@ fn print_item(&ps s, &@ast::item item) {
268269
word(s.s, ";");
269270
end(s); // end the outer cbox
270271
}
271-
case (ast::item_fn(?name,?_fn,?typarams,_,_,_)) {
272+
case (ast::item_fn(?name,?_fn,?typarams,?attrs,_,_)) {
273+
print_outer_attributes(s, attrs);
272274
print_fn(s, _fn.decl, _fn.proto, name, typarams);
273275
word(s.s, " ");
274276
print_block(s, _fn.body);
275277
}
276-
case (ast::item_mod(?id,?_mod,_,_)) {
278+
case (ast::item_mod(?id,?_mod,?attrs,_)) {
279+
print_outer_attributes(s, attrs);
277280
head(s, "mod");
278281
word_nbsp(s, id);
279282
bopen(s);
280283
for (@ast::item itm in _mod.items) {print_item(s, itm);}
281284
bclose(s, item.span);
282285
}
283-
case (ast::item_native_mod(?id,?nmod,_,_)) {
286+
case (ast::item_native_mod(?id,?nmod,?attrs,_)) {
287+
print_outer_attributes(s, attrs);
284288
head(s, "native");
285289
alt (nmod.abi) {
286290
case (ast::native_abi_rust) {word_nbsp(s, "\"rust\"");}
@@ -317,7 +321,8 @@ fn print_item(&ps s, &@ast::item item) {
317321
}
318322
bclose(s, item.span);
319323
}
320-
case (ast::item_ty(?id,?ty,?params,_,_,_)) {
324+
case (ast::item_ty(?id,?ty,?params,?attrs,_,_)) {
325+
print_outer_attributes(s, attrs);
321326
ibox(s, indent_unit);
322327
ibox(s, 0u);
323328
word_nbsp(s, "type");
@@ -331,7 +336,8 @@ fn print_item(&ps s, &@ast::item item) {
331336
end(s); // end the outer ibox
332337
break_offset(s.s, 0u, 0);
333338
}
334-
case (ast::item_tag(?id,?variants,?params,_,_,_)) {
339+
case (ast::item_tag(?id,?variants,?params,?attrs,_,_)) {
340+
print_outer_attributes(s, attrs);
335341
head(s, "tag");
336342
word(s.s, id);
337343
print_type_params(s, params);
@@ -354,7 +360,8 @@ fn print_item(&ps s, &@ast::item item) {
354360
}
355361
bclose(s, item.span);
356362
}
357-
case (ast::item_obj(?id,?_obj,?params,_,_,_)) {
363+
case (ast::item_obj(?id,?_obj,?params,?attrs,_,_)) {
364+
print_outer_attributes(s, attrs);
358365
head(s, "obj");
359366
word(s.s, id);
360367
print_type_params(s, params);
@@ -401,6 +408,30 @@ fn print_item(&ps s, &@ast::item item) {
401408
}
402409
}
403410

411+
fn print_outer_attributes(&ps s, vec[ast::attribute] attrs) {
412+
auto count = 0;
413+
for (ast::attribute attr in attrs) {
414+
alt (attr.node.style) {
415+
case (ast::attr_outer) {
416+
print_attribute(s, attr);
417+
count += 1;
418+
}
419+
case (_) { /* fallthrough */ }
420+
}
421+
}
422+
if (count > 0) {
423+
hardbreak(s.s);
424+
}
425+
}
426+
427+
fn print_attribute(&ps s, &ast::attribute attr) {
428+
hardbreak(s.s);
429+
maybe_print_comment(s, attr.span.lo);
430+
word(s.s, "#[");
431+
print_meta_item(s, @attr.node.value);
432+
word(s.s, "]");
433+
}
434+
404435
fn print_stmt(&ps s, &ast::stmt st) {
405436
maybe_print_comment(s, st.span.lo);
406437
alt (st.node) {
@@ -987,6 +1018,14 @@ fn print_type_params(&ps s, &vec[ast::ty_param] params) {
9871018
}
9881019
}
9891020

1021+
fn print_meta_item(&ps s, &@ast::meta_item item) {
1022+
ibox(s, indent_unit);
1023+
word_space(s, item.node.key);
1024+
word_space(s, "=");
1025+
print_string(s, item.node.value);
1026+
end(s);
1027+
}
1028+
9901029
fn print_view_item(&ps s, &@ast::view_item item) {
9911030
hardbreak(s.s);
9921031
maybe_print_comment(s, item.span.lo);
@@ -996,14 +1035,7 @@ fn print_view_item(&ps s, &@ast::view_item item) {
9961035
word(s.s, id);
9971036
if (vec::len(mta) > 0u) {
9981037
popen(s);
999-
fn print_meta(&ps s, &@ast::meta_item item) {
1000-
ibox(s, indent_unit);
1001-
word_space(s, item.node.key);
1002-
word_space(s, "=");
1003-
print_string(s, item.node.value);
1004-
end(s);
1005-
}
1006-
commasep(s, consistent, mta, print_meta);
1038+
commasep(s, consistent, mta, print_meta_item);
10071039
pclose(s);
10081040
}
10091041
}

0 commit comments

Comments
 (0)