Skip to content

Commit 4176343

Browse files
committed
auto merge of #11846 : michaelwoerister/rust/cu_name, r=pcwalton
Fixes #11600
2 parents 760ddb3 + 0a03bc0 commit 4176343

File tree

13 files changed

+115
-57
lines changed

13 files changed

+115
-57
lines changed

src/librustc/driver/driver.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -897,42 +897,56 @@ pub fn build_session_options(binary: ~str,
897897
return sopts;
898898
}
899899

900-
pub fn build_session(sopts: @session::Options, demitter: @diagnostic::Emitter)
900+
pub fn build_session(sopts: @session::Options,
901+
local_crate_source_file: Option<Path>,
902+
demitter: @diagnostic::Emitter)
901903
-> Session {
902904
let codemap = @codemap::CodeMap::new();
903905
let diagnostic_handler =
904906
diagnostic::mk_handler(Some(demitter));
905907
let span_diagnostic_handler =
906908
diagnostic::mk_span_handler(diagnostic_handler, codemap);
907-
build_session_(sopts, codemap, demitter, span_diagnostic_handler)
909+
910+
build_session_(sopts, local_crate_source_file, codemap, demitter, span_diagnostic_handler)
908911
}
909912

910913
pub fn build_session_(sopts: @session::Options,
911-
cm: @codemap::CodeMap,
914+
local_crate_source_file: Option<Path>,
915+
codemap: @codemap::CodeMap,
912916
demitter: @diagnostic::Emitter,
913917
span_diagnostic_handler: @diagnostic::SpanHandler)
914918
-> Session {
915919
let target_cfg = build_target_config(sopts, demitter);
916-
let p_s = parse::new_parse_sess_special_handler(span_diagnostic_handler,
917-
cm);
920+
let p_s = parse::new_parse_sess_special_handler(span_diagnostic_handler, codemap);
918921
let cstore = @CStore::new(token::get_ident_interner());
919922
let filesearch = @filesearch::FileSearch::new(
920923
&sopts.maybe_sysroot,
921924
sopts.target_triple,
922925
sopts.addl_lib_search_paths);
926+
927+
// Make the path absolute, if necessary
928+
let local_crate_source_file = local_crate_source_file.map(|path|
929+
if path.is_absolute() {
930+
path.clone()
931+
} else {
932+
os::getcwd().join(path.clone())
933+
}
934+
);
935+
923936
@Session_ {
924937
targ_cfg: target_cfg,
925938
opts: sopts,
926939
cstore: cstore,
927940
parse_sess: p_s,
928-
codemap: cm,
941+
codemap: codemap,
929942
// For a library crate, this is always none
930943
entry_fn: RefCell::new(None),
931944
entry_type: Cell::new(None),
932945
macro_registrar_fn: RefCell::new(None),
933946
span_diagnostic: span_diagnostic_handler,
934947
filesearch: filesearch,
935948
building_library: Cell::new(false),
949+
local_crate_source_file: local_crate_source_file,
936950
working_dir: os::getcwd(),
937951
lints: RefCell::new(HashMap::new()),
938952
node_id: Cell::new(1),
@@ -1164,13 +1178,8 @@ mod test {
11641178
Ok(m) => m,
11651179
Err(f) => fail!("test_switch_implies_cfg_test: {}", f.to_err_msg())
11661180
};
1167-
let sessopts = build_session_options(
1168-
~"rustc",
1169-
matches,
1170-
@diagnostic::DefaultEmitter as @diagnostic::Emitter);
1171-
let sess = build_session(sessopts,
1172-
@diagnostic::DefaultEmitter as
1173-
@diagnostic::Emitter);
1181+
let sessopts = build_session_options(~"rustc", matches, @diagnostic::DefaultEmitter);
1182+
let sess = build_session(sessopts, None, @diagnostic::DefaultEmitter);
11741183
let cfg = build_configuration(sess);
11751184
assert!((attr::contains_name(cfg, "test")));
11761185
}
@@ -1187,13 +1196,8 @@ mod test {
11871196
f.to_err_msg());
11881197
}
11891198
};
1190-
let sessopts = build_session_options(
1191-
~"rustc",
1192-
matches,
1193-
@diagnostic::DefaultEmitter as @diagnostic::Emitter);
1194-
let sess = build_session(sessopts,
1195-
@diagnostic::DefaultEmitter as
1196-
@diagnostic::Emitter);
1199+
let sessopts = build_session_options(~"rustc", matches, @diagnostic::DefaultEmitter);
1200+
let sess = build_session(sessopts, None, @diagnostic::DefaultEmitter);
11971201
let cfg = build_configuration(sess);
11981202
let mut test_items = cfg.iter().filter(|m| "test" == m.name());
11991203
assert!(test_items.next().is_some());

src/librustc/driver/session.rs

+3
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ pub struct Session_ {
211211
macro_registrar_fn: RefCell<Option<ast::DefId>>,
212212
filesearch: @filesearch::FileSearch,
213213
building_library: Cell<bool>,
214+
// The name of the root source file of the crate, in the local file system. The path is always
215+
// expected to be absolute. `None` means that there is no source file.
216+
local_crate_source_file: Option<Path>,
214217
working_dir: Path,
215218
lints: RefCell<HashMap<ast::NodeId,
216219
~[(lint::Lint, codemap::Span, ~str)]>>,

src/librustc/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -230,22 +230,22 @@ pub fn run_compiler(args: &[~str], demitter: @diagnostic::Emitter) {
230230
version(binary);
231231
return;
232232
}
233-
let input = match matches.free.len() {
233+
let (input, input_file_path) = match matches.free.len() {
234234
0u => d::early_error(demitter, "no input filename given"),
235235
1u => {
236236
let ifile = matches.free[0].as_slice();
237237
if "-" == ifile {
238238
let src = str::from_utf8_owned(io::stdin().read_to_end()).unwrap();
239-
d::StrInput(src.to_managed())
239+
(d::StrInput(src.to_managed()), None)
240240
} else {
241-
d::FileInput(Path::new(ifile))
241+
(d::FileInput(Path::new(ifile)), Some(Path::new(ifile)))
242242
}
243243
}
244244
_ => d::early_error(demitter, "multiple input filenames provided")
245245
};
246246

247247
let sopts = d::build_session_options(binary, matches, demitter);
248-
let sess = d::build_session(sopts, demitter);
248+
let sess = d::build_session(sopts, input_file_path, demitter);
249249
let odir = matches.opt_str("out-dir").map(|o| Path::new(o));
250250
let ofile = matches.opt_str("o").map(|o| Path::new(o));
251251
let cfg = d::build_configuration(sess);

src/librustc/middle/trans/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2670,15 +2670,15 @@ pub fn trans_crate(sess: session::Session,
26702670
let link_meta = link::build_link_meta(sess, crate.attrs, output,
26712671
&mut symbol_hasher);
26722672

2673-
// Append ".rc" to crate name as LLVM module identifier.
2673+
// Append ".rs" to crate name as LLVM module identifier.
26742674
//
26752675
// LLVM code generator emits a ".file filename" directive
26762676
// for ELF backends. Value of the "filename" is set as the
26772677
// LLVM module identifier. Due to a LLVM MC bug[1], LLVM
26782678
// crashes if the module identifer is same as other symbols
26792679
// such as a function name in the module.
26802680
// 1. http://llvm.org/bugs/show_bug.cgi?id=11479
2681-
let llmod_id = link_meta.crateid.name.clone() + ".rc";
2681+
let llmod_id = link_meta.crateid.name.clone() + ".rs";
26822682

26832683
let ccx = @CrateContext::new(sess,
26842684
llmod_id,

src/librustc/middle/trans/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl CrateContext {
168168

169169
let (crate_map_name, crate_map) = decl_crate_map(sess, link_meta.clone(), llmod);
170170
let dbg_cx = if sess.opts.debuginfo {
171-
Some(debuginfo::CrateDebugContext::new(llmod, name.to_owned()))
171+
Some(debuginfo::CrateDebugContext::new(llmod))
172172
} else {
173173
None
174174
};

src/librustc/middle/trans/debuginfo.rs

+40-15
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ use middle::ty;
140140
use middle::pat_util;
141141
use util::ppaux;
142142

143-
use std::c_str::ToCStr;
143+
use std::c_str::{CString, ToCStr};
144144
use std::cell::{Cell, RefCell};
145145
use std::hashmap::HashMap;
146146
use std::hashmap::HashSet;
@@ -171,7 +171,6 @@ static DW_ATE_unsigned_char: c_uint = 0x08;
171171

172172
/// A context object for maintaining all state needed by the debuginfo module.
173173
pub struct CrateDebugContext {
174-
priv crate_file: ~str,
175174
priv llcontext: ContextRef,
176175
priv builder: DIBuilderRef,
177176
priv current_debug_location: Cell<DebugLocation>,
@@ -184,13 +183,12 @@ pub struct CrateDebugContext {
184183
}
185184

186185
impl CrateDebugContext {
187-
pub fn new(llmod: ModuleRef, crate: ~str) -> CrateDebugContext {
186+
pub fn new(llmod: ModuleRef) -> CrateDebugContext {
188187
debug!("CrateDebugContext::new");
189188
let builder = unsafe { llvm::LLVMDIBuilderCreate(llmod) };
190189
// DIBuilder inherits context from the module, so we'd better use the same one
191190
let llcontext = unsafe { llvm::LLVMGetModuleContext(llmod) };
192191
return CrateDebugContext {
193-
crate_file: crate,
194192
llcontext: llcontext,
195193
builder: builder,
196194
current_debug_location: Cell::new(UnknownLocation),
@@ -849,26 +847,49 @@ fn create_DIArray(builder: DIBuilderRef, arr: &[DIDescriptor]) -> DIArray {
849847
};
850848
}
851849

852-
fn compile_unit_metadata(cx: @CrateContext) {
853-
let dcx = debug_context(cx);
854-
let crate_name: &str = dcx.crate_file;
855-
856-
debug!("compile_unit_metadata: {:?}", crate_name);
850+
fn compile_unit_metadata(cx: &CrateContext) {
851+
let work_dir = &cx.sess.working_dir;
852+
let compile_unit_name = match cx.sess.local_crate_source_file {
853+
None => fallback_path(cx),
854+
Some(ref abs_path) => {
855+
if abs_path.is_relative() {
856+
cx.sess.warn("debuginfo: Invalid path to crate's local root source file!");
857+
fallback_path(cx)
858+
} else {
859+
match abs_path.path_relative_from(work_dir) {
860+
Some(ref p) if p.is_relative() => {
861+
// prepend "./" if necessary
862+
let dotdot = bytes!("..");
863+
let prefix = &[dotdot[0], ::std::path::SEP_BYTE];
864+
let mut path_bytes = p.as_vec().to_owned();
865+
866+
if path_bytes.slice_to(2) != prefix &&
867+
path_bytes.slice_to(2) != dotdot {
868+
path_bytes.insert(0, prefix[0]);
869+
path_bytes.insert(1, prefix[1]);
870+
}
871+
872+
path_bytes.to_c_str()
873+
}
874+
_ => fallback_path(cx)
875+
}
876+
}
877+
}
878+
};
857879

858-
// FIXME (#9639): This needs to handle non-utf8 paths
859-
let work_dir = cx.sess.working_dir.as_str().unwrap();
880+
debug!("compile_unit_metadata: {:?}", compile_unit_name);
860881
let producer = format!("rustc version {}", env!("CFG_VERSION"));
861882

862-
crate_name.with_c_str(|crate_name| {
863-
work_dir.with_c_str(|work_dir| {
883+
compile_unit_name.with_ref(|compile_unit_name| {
884+
work_dir.as_vec().with_c_str(|work_dir| {
864885
producer.with_c_str(|producer| {
865886
"".with_c_str(|flags| {
866887
"".with_c_str(|split_name| {
867888
unsafe {
868889
llvm::LLVMDIBuilderCreateCompileUnit(
869-
dcx.builder,
890+
debug_context(cx).builder,
870891
DW_LANG_RUST,
871-
crate_name,
892+
compile_unit_name,
872893
work_dir,
873894
producer,
874895
cx.sess.opts.optimize != session::No,
@@ -881,6 +902,10 @@ fn compile_unit_metadata(cx: @CrateContext) {
881902
})
882903
})
883904
});
905+
906+
fn fallback_path(cx: &CrateContext) -> CString {
907+
cx.link_meta.crateid.name.to_c_str()
908+
}
884909
}
885910

886911
fn declare_local(bcx: &Block,

src/librustc/middle/typeck/infer/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn setup_env(test_name: &str, source_string: &str) -> Env {
5252
let matches = getopts(~[~"-Z", ~"verbose"], optgroups()).get();
5353
let diag = diagnostic::collect(messages);
5454
let sessopts = build_session_options(~"rustc", &matches, diag);
55-
let sess = build_session(sessopts, diag);
55+
let sess = build_session(sessopts, None, diag);
5656
let cfg = build_configuration(sess, ~"whatever", str_input(~""));
5757
let dm = HashMap();
5858
let amap = HashMap();

src/librustdoc/core.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ fn get_ast_and_resolve(cpath: &Path,
6464
syntax::diagnostic::mk_span_handler(diagnostic_handler, parsesess.cm);
6565

6666
let sess = driver::driver::build_session_(sessopts,
67+
Some(cpath.clone()),
6768
parsesess.cm,
68-
@diagnostic::DefaultEmitter as
69-
@diagnostic::Emitter,
69+
@diagnostic::DefaultEmitter,
7070
span_diagnostic_handler);
7171

7272
let mut cfg = build_configuration(sess);

src/librustdoc/test.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ use visit_ast::RustdocVisitor;
3434

3535
pub fn run(input: &str, matches: &getopts::Matches) -> int {
3636
let parsesess = parse::new_parse_sess(None);
37-
let input = driver::FileInput(Path::new(input));
37+
let input_path = Path::new(input);
38+
let input = driver::FileInput(input_path.clone());
3839
let libs = matches.opt_strs("L").map(|s| Path::new(s.as_slice()));
3940
let libs = @RefCell::new(libs.move_iter().collect());
4041

@@ -52,9 +53,9 @@ pub fn run(input: &str, matches: &getopts::Matches) -> int {
5253
diagnostic::mk_span_handler(diagnostic_handler, parsesess.cm);
5354

5455
let sess = driver::build_session_(sessopts,
56+
Some(input_path),
5557
parsesess.cm,
56-
@diagnostic::DefaultEmitter as
57-
@diagnostic::Emitter,
58+
@diagnostic::DefaultEmitter,
5859
span_diagnostic_handler);
5960

6061
let cfg = driver::build_configuration(sess);
@@ -113,9 +114,9 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>) {
113114
diagnostic::mk_span_handler(diagnostic_handler, parsesess.cm);
114115

115116
let sess = driver::build_session_(sessopts,
117+
None,
116118
parsesess.cm,
117-
@diagnostic::DefaultEmitter as
118-
@diagnostic::Emitter,
119+
@diagnostic::DefaultEmitter,
119120
span_diagnostic_handler);
120121

121122
let outdir = TempDir::new("rustdoctest").expect("rustdoc needs a tempdir");

src/librustpkg/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ impl<'a> PkgScript<'a> {
114114
};
115115
let input = driver::FileInput(script.clone());
116116
let sess = driver::build_session(options,
117-
@diagnostic::DefaultEmitter as
118-
@diagnostic::Emitter);
117+
Some(script.clone()),
118+
@diagnostic::DefaultEmitter);
119119
let cfg = driver::build_configuration(sess);
120120
let crate = driver::phase_1_parse_input(sess, cfg.clone(), &input);
121121
let loader = &mut Loader::new(sess);

src/librustpkg/tests.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1770,11 +1770,8 @@ fn test_linker_build() {
17701770
let matches = getopts([], optgroups());
17711771
let options = build_session_options(~"rustpkg",
17721772
matches.as_ref().unwrap(),
1773-
@diagnostic::DefaultEmitter as
1774-
@diagnostic::Emitter);
1775-
let sess = build_session(options,
1776-
@diagnostic::DefaultEmitter as
1777-
@diagnostic::Emitter);
1773+
@diagnostic::DefaultEmitter);
1774+
let sess = build_session(options, None, @diagnostic::DefaultEmitter);
17781775
let test_sys = test_sysroot();
17791776
// FIXME (#9639): This needs to handle non-utf8 paths
17801777
let cc = get_cc_prog(sess);

src/librustpkg/util.rs

+1
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ pub fn compile_input(context: &BuildContext,
264264
debug!("About to build session...");
265265

266266
let sess = driver::build_session(options,
267+
Some(in_file.clone()),
267268
@diagnostic::DefaultEmitter as
268269
@diagnostic::Emitter);
269270

src/test/debug-info/issue11600.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
fn main() {
2+
let args : ~[~str] = ::std::os::args();
3+
::std::io::println(args[0]);
4+
}
5+
6+
7+
// xfail-android: FIXME(#10381)
8+
9+
// This test case checks whether compile unit names are set correctly, so that the correct default
10+
// source file can be found.
11+
12+
// compile-flags:-Z extra-debug-info
13+
// debugger:list
14+
// check:1[...]fn main() {
15+
// check:2[...]let args : ~[~str] = ::std::os::args();
16+
// check:3[...]::std::io::println(args[0]);
17+
// check:4[...]}
18+
19+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
20+
// file at the top-level directory of this distribution and at
21+
// http://rust-lang.org/COPYRIGHT.
22+
//
23+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
24+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
25+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
26+
// option. This file may not be copied, modified, or distributed
27+
// except according to those terms.

0 commit comments

Comments
 (0)