Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc: add --read-attr <name> #1133

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions man/rustc.1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Pretty-print the input. Valid \fItype\fRs are:
\fB--ls\fR:
Lists symbols defined by the specified \fBcompiled\fR library.
.TP
\fB--read-attr\fR \fIname\fR:
Print the named attribute of the specified \fBcompiled\fR library, or nothing
if no such attribute exists.
.TP
\fB-L\fR \fIpath\fR:
Adds \fIpath\fR to the library search path.
.TP
Expand Down
10 changes: 9 additions & 1 deletion src/comp/driver/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ options:
--static use or produce static libraries
--pretty [type] pretty-print the input instead of compiling
--ls list the symbols defined by a crate file
--read-attr <name> read a specific crate attribute
-L <path> add a directory to the library search path
--noverify suppress LLVM verification step (slight speedup)
--parse-only parse only; do not compile, assemble, or link
Expand Down Expand Up @@ -448,7 +449,8 @@ fn opts() -> [getopts::opt] {
optflag("no-typestate"), optflag("noverify"),
optmulti("cfg"), optflag("test"),
optflag("lib"), optflag("static"), optflag("gc"),
optflag("stack-growth"), optflag("check-unsafe")];
optflag("stack-growth"), optflag("check-unsafe"),
optflagopt("read-attr")];
}

fn build_output_filenames(ifile: str, ofile: option::t<str>,
Expand Down Expand Up @@ -546,6 +548,12 @@ fn main(args: [str]) {
ret;
}

if opt_present(match, "read-attr") {
let arg = getopts::opt_str(match, "read-attr");
metadata::creader::read_attr(sess, ifile, io::stdout(), arg);
ret;
}

let stop_after_codegen =
sopts.output_type != link::output_type_exe ||
sopts.static && sopts.library;
Expand Down
10 changes: 10 additions & 0 deletions src/comp/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import common::*;

export read_crates;
export list_file_metadata;
export read_attr;

// Traverses an AST, reading all the information about use'd crates and native
// libraries necessary for later resolving, typechecking, linking, etc.
Expand Down Expand Up @@ -81,6 +82,15 @@ fn list_file_metadata(sess: session::session, path: str, out: io::writer) {
}
}

fn read_attr(sess: session::session, path: str, out: io::writer, name: str) {
alt get_metadata_section(sess, path) {
option::none. { }
option::some(bytes) {
decoder::print_crate_attr(bytes, out, name);
}
}
}

fn metadata_matches(crate_data: @[u8], metas: [@ast::meta_item]) -> bool {
let attrs = decoder::get_crate_attributes(crate_data);
let linkage_metas = attr::find_linkage_metas(attrs);
Expand Down
13 changes: 13 additions & 0 deletions src/comp/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export list_crate_metadata;
export crate_dep;
export get_crate_deps;
export external_resolver;
export print_crate_attr;

// A function that takes a def_id relative to the crate being searched and
// returns a def_id relative to the compilation environment, i.e. if we hit a
Expand Down Expand Up @@ -415,6 +416,18 @@ fn list_crate_metadata(bytes: @[u8], out: io::writer) {
list_crate_items(bytes, md, out);
}

fn print_crate_attr(bytes: @[u8], out: io::writer, name: str) {
let md = ebml::new_doc(bytes);
for attr: ast::attribute in get_attributes(md) {
alt attr.node.value.node {
ast::meta_name_value(mname, value) when mname == name {
out.write_str(#fmt["%s\n", pprust::attribute_to_str(attr)]);
}
_ { }
}
}
}

// Local Variables:
// mode: rust
// fill-column: 78;
Expand Down