Skip to content

Commit 723ca4b

Browse files
committed
Validate export_name attribute
1 parent b9a11f0 commit 723ca4b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,9 +1692,9 @@ pub fn build_return_block<'blk, 'tcx>(fcx: &FunctionContext<'blk, 'tcx>,
16921692
}
16931693
}
16941694

1695-
// trans_closure: Builds an LLVM function out of a source function.
1696-
// If the function closes over its environment a closure will be
1697-
// returned.
1695+
/// Builds an LLVM function out of a source function.
1696+
///
1697+
/// If the function closes over its environment a closure will be returned.
16981698
pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
16991699
decl: &ast::FnDecl,
17001700
body: &ast::Block,
@@ -1827,8 +1827,7 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
18271827
finish_fn(&fcx, bcx, output_type, ret_debug_loc);
18281828
}
18291829

1830-
// trans_fn: creates an LLVM function corresponding to a source language
1831-
// function.
1830+
/// Creates an LLVM function corresponding to a source language function.
18321831
pub fn trans_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
18331832
decl: &ast::FnDecl,
18341833
body: &ast::Block,
@@ -2645,10 +2644,9 @@ fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, id: ast::NodeId,
26452644
None => {}
26462645
}
26472646

2648-
match attr::first_attr_value_str_by_name(attrs, "export_name") {
2647+
match attr::find_export_name_attr(ccx.sess().diagnostic(), attrs) {
26492648
// Use provided name
26502649
Some(name) => name.to_string(),
2651-
26522650
_ => ccx.tcx().map.with_path(id, |path| {
26532651
if attr::contains_name(attrs, "no_mangle") {
26542652
// Don't mangle

src/libsyntax/attr.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,23 @@ pub fn find_crate_name(attrs: &[Attribute]) -> Option<InternedString> {
282282
first_attr_value_str_by_name(attrs, "crate_name")
283283
}
284284

285+
/// Find the value of #[export_name=*] attribute and check its validity.
286+
pub fn find_export_name_attr(diag: &SpanHandler, attrs: &[Attribute]) -> Option<InternedString> {
287+
attrs.iter().fold(None, |ia,attr| {
288+
if attr.check_name("export_name") {
289+
if let s@Some(_) = attr.value_str() {
290+
s
291+
} else {
292+
diag.span_err(attr.span, "export_name attribute has invalid format");
293+
diag.handler.help("use #[export_name=\"*\"]");
294+
None
295+
}
296+
} else {
297+
ia
298+
}
299+
})
300+
}
301+
285302
#[derive(Copy, Clone, PartialEq)]
286303
pub enum InlineAttr {
287304
None,

0 commit comments

Comments
 (0)