Skip to content

Commit 059cbaa

Browse files
committed
auto merge of #9005 : alexcrichton/rust/rusty-log, r=brson
Also redefine all of the standard logging macros to use more rust code instead of custom LLVM translation code. This makes them a bit easier to understand, but also more flexibile for future types of logging. Additionally, this commit removes the LogType language item in preparation for changing how logging is performed.
2 parents fd2488b + 8a96618 commit 059cbaa

23 files changed

+115
-195
lines changed

src/librustc/middle/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ impl CFGBuilder {
389389
self.straightline(expr, pred, [r, l])
390390
}
391391

392-
ast::ExprLog(l, r) |
393392
ast::ExprIndex(_, l, r) |
394393
ast::ExprBinary(_, _, l, r) => { // NB: && and || handled earlier
395394
self.straightline(expr, pred, [l, r])
@@ -405,6 +404,7 @@ impl CFGBuilder {
405404
self.straightline(expr, pred, [e])
406405
}
407406

407+
ast::ExprLogLevel |
408408
ast::ExprMac(*) |
409409
ast::ExprInlineAsm(*) |
410410
ast::ExprSelf |

src/librustc/middle/dataflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,12 @@ impl<'self, O:DataFlowOperator> PropagationContext<'self, O> {
702702
join_bits(&self.dfcx.oper, temp, in_out);
703703
}
704704

705-
ast::ExprLog(l, r) |
706705
ast::ExprIndex(_, l, r) |
707706
ast::ExprBinary(_, _, l, r) => {
708707
self.walk_exprs([l, r], in_out, loop_scopes);
709708
}
710709

710+
ast::ExprLogLevel |
711711
ast::ExprLit(*) |
712712
ast::ExprPath(*) |
713713
ast::ExprSelf => {

src/librustc/middle/lang_items.rs

-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ pub enum LangItem {
5959

6060
StrEqFnLangItem, // 19
6161
UniqStrEqFnLangItem, // 20
62-
LogTypeFnLangItem, // 21
6362
FailFnLangItem, // 22
6463
FailBoundsCheckFnLangItem, // 23
6564
ExchangeMallocFnLangItem, // 24
@@ -238,9 +237,6 @@ impl LanguageItems {
238237
pub fn uniq_str_eq_fn(&self) -> Option<DefId> {
239238
self.items[UniqStrEqFnLangItem as uint]
240239
}
241-
pub fn log_type_fn(&self) -> Option<DefId> {
242-
self.items[LogTypeFnLangItem as uint]
243-
}
244240
pub fn fail_fn(&self) -> Option<DefId> {
245241
self.items[FailFnLangItem as uint]
246242
}
@@ -357,7 +353,6 @@ impl<'self> LanguageItemCollector<'self> {
357353

358354
item_refs.insert(@"str_eq", StrEqFnLangItem as uint);
359355
item_refs.insert(@"uniq_str_eq", UniqStrEqFnLangItem as uint);
360-
item_refs.insert(@"log_type", LogTypeFnLangItem as uint);
361356
item_refs.insert(@"fail_", FailFnLangItem as uint);
362357
item_refs.insert(@"fail_bounds_check",
363358
FailBoundsCheckFnLangItem as uint);

src/librustc/middle/liveness.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ fn visit_expr(v: &mut LivenessVisitor, expr: @Expr, this: @mut IrMaps) {
526526

527527
// otherwise, live nodes are not required:
528528
ExprIndex(*) | ExprField(*) | ExprVstore(*) | ExprVec(*) |
529-
ExprCall(*) | ExprMethodCall(*) | ExprTup(*) | ExprLog(*) |
529+
ExprCall(*) | ExprMethodCall(*) | ExprTup(*) | ExprLogLevel |
530530
ExprBinary(*) | ExprAddrOf(*) |
531531
ExprDoBody(*) | ExprCast(*) | ExprUnary(*) | ExprBreak(_) |
532532
ExprAgain(_) | ExprLit(_) | ExprRet(*) | ExprBlock(*) |
@@ -1217,7 +1217,6 @@ impl Liveness {
12171217
self.propagate_through_expr(l, ln)
12181218
}
12191219

1220-
ExprLog(l, r) |
12211220
ExprIndex(_, l, r) |
12221221
ExprBinary(_, _, l, r) => {
12231222
self.propagate_through_exprs([l, r], succ)
@@ -1240,6 +1239,7 @@ impl Liveness {
12401239
}
12411240
}
12421241

1242+
ExprLogLevel |
12431243
ExprLit(*) => {
12441244
succ
12451245
}
@@ -1496,7 +1496,7 @@ fn check_expr(vt: &mut ErrorCheckVisitor, expr: @Expr, this: @Liveness) {
14961496
// no correctness conditions related to liveness
14971497
ExprCall(*) | ExprMethodCall(*) | ExprIf(*) | ExprMatch(*) |
14981498
ExprWhile(*) | ExprLoop(*) | ExprIndex(*) | ExprField(*) |
1499-
ExprVstore(*) | ExprVec(*) | ExprTup(*) | ExprLog(*) |
1499+
ExprVstore(*) | ExprVec(*) | ExprTup(*) | ExprLogLevel |
15001500
ExprBinary(*) | ExprDoBody(*) |
15011501
ExprCast(*) | ExprUnary(*) | ExprRet(*) | ExprBreak(*) |
15021502
ExprAgain(*) | ExprLit(_) | ExprBlock(*) |

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl mem_categorization_ctxt {
429429
ast::ExprDoBody(*) | ast::ExprUnary(*) |
430430
ast::ExprMethodCall(*) | ast::ExprCast(*) | ast::ExprVstore(*) |
431431
ast::ExprVec(*) | ast::ExprTup(*) | ast::ExprIf(*) |
432-
ast::ExprLog(*) | ast::ExprBinary(*) | ast::ExprWhile(*) |
432+
ast::ExprLogLevel | ast::ExprBinary(*) | ast::ExprWhile(*) |
433433
ast::ExprBlock(*) | ast::ExprLoop(*) | ast::ExprMatch(*) |
434434
ast::ExprLit(*) | ast::ExprBreak(*) | ast::ExprMac(*) |
435435
ast::ExprAgain(*) | ast::ExprStruct(*) | ast::ExprRepeat(*) |

src/librustc/middle/moves.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ impl VisitContext {
480480
self.use_expr(base, Read, visitor);
481481
}
482482

483+
ExprLogLevel |
483484
ExprInlineAsm(*) |
484485
ExprBreak(*) |
485486
ExprAgain(*) |
@@ -489,11 +490,6 @@ impl VisitContext {
489490
self.consume_block(blk, visitor);
490491
}
491492

492-
ExprLog(a_expr, b_expr) => {
493-
self.consume_expr(a_expr, visitor);
494-
self.use_expr(b_expr, Read, visitor);
495-
}
496-
497493
ExprWhile(cond_expr, ref blk) => {
498494
self.consume_expr(cond_expr, visitor);
499495
self.consume_block(blk, visitor);

src/librustc/middle/trans/controlflow.rs

-72
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::c_str::ToCStr;
12-
13-
use back::link;
14-
use lib;
1511
use lib::llvm::*;
1612
use middle::lang_items::{FailFnLangItem, FailBoundsCheckFnLangItem};
17-
use middle::lang_items::LogTypeFnLangItem;
1813
use middle::trans::base::*;
1914
use middle::trans::build::*;
2015
use middle::trans::callee;
@@ -28,7 +23,6 @@ use middle::trans::type_::Type;
2823

2924
use syntax::ast;
3025
use syntax::ast::Ident;
31-
use syntax::ast_map::path_mod;
3226
use syntax::ast_util;
3327
use syntax::codemap::Span;
3428

@@ -206,72 +200,6 @@ pub fn trans_loop(bcx:@mut Block,
206200
return next_bcx;
207201
}
208202

209-
pub fn trans_log(log_ex: &ast::Expr,
210-
lvl: @ast::Expr,
211-
bcx: @mut Block,
212-
e: @ast::Expr) -> @mut Block {
213-
let _icx = push_ctxt("trans_log");
214-
let ccx = bcx.ccx();
215-
let mut bcx = bcx;
216-
if ty::type_is_bot(expr_ty(bcx, lvl)) {
217-
return expr::trans_into(bcx, lvl, expr::Ignore);
218-
}
219-
220-
let (modpath, modname) = {
221-
let path = &mut bcx.fcx.path;
222-
let mut modpath = ~[path_mod(ccx.sess.ident_of(ccx.link_meta.name))];
223-
for e in path.iter() {
224-
match *e {
225-
path_mod(_) => { modpath.push(*e) }
226-
_ => {}
227-
}
228-
}
229-
let modname = path_str(ccx.sess, modpath);
230-
(modpath, modname)
231-
};
232-
233-
let global = if ccx.module_data.contains_key(&modname) {
234-
ccx.module_data.get_copy(&modname)
235-
} else {
236-
let s = link::mangle_internal_name_by_path_and_seq(
237-
ccx, modpath, "loglevel");
238-
let global;
239-
unsafe {
240-
global = do s.with_c_str |buf| {
241-
llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf)
242-
};
243-
llvm::LLVMSetGlobalConstant(global, False);
244-
llvm::LLVMSetInitializer(global, C_null(Type::i32()));
245-
lib::llvm::SetLinkage(global, lib::llvm::InternalLinkage);
246-
}
247-
ccx.module_data.insert(modname, global);
248-
global
249-
};
250-
let current_level = Load(bcx, global);
251-
let level = unpack_result!(bcx, {
252-
do with_scope_result(bcx, lvl.info(), "level") |bcx| {
253-
expr::trans_to_datum(bcx, lvl).to_result()
254-
}
255-
});
256-
257-
let llenabled = ICmp(bcx, lib::llvm::IntUGE, current_level, level);
258-
do with_cond(bcx, llenabled) |bcx| {
259-
do with_scope(bcx, log_ex.info(), "log") |bcx| {
260-
let mut bcx = bcx;
261-
262-
// Translate the value to be logged
263-
let val_datum = unpack_datum!(bcx, expr::trans_to_datum(bcx, e));
264-
265-
// Call the polymorphic log function
266-
let val = val_datum.to_ref_llval(bcx);
267-
let did = langcall(bcx, Some(e.span), "", LogTypeFnLangItem);
268-
let bcx = callee::trans_lang_call_with_type_params(
269-
bcx, did, [level, val], [val_datum.ty], expr::Ignore);
270-
bcx
271-
}
272-
}
273-
}
274-
275203
pub fn trans_break_cont(bcx: @mut Block,
276204
opt_label: Option<Ident>,
277205
to_end: bool)

src/librustc/middle/trans/debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,7 @@ fn populate_scope_map(cx: &mut CrateContext,
19951995
scope_map.insert(exp.id, scope_stack.last().scope_metadata);
19961996

19971997
match exp.node {
1998+
ast::ExprLogLevel |
19981999
ast::ExprSelf |
19992000
ast::ExprLit(_) |
20002001
ast::ExprBreak(_) |
@@ -2033,7 +2034,6 @@ fn populate_scope_map(cx: &mut CrateContext,
20332034
}
20342035

20352036
ast::ExprAssign(@ref sub_exp1, @ref sub_exp2) |
2036-
ast::ExprLog(@ref sub_exp1, @ref sub_exp2) |
20372037
ast::ExprRepeat(@ref sub_exp1, @ref sub_exp2, _) => {
20382038
walk_expr(cx, sub_exp1, scope_stack, scope_map);
20392039
walk_expr(cx, sub_exp2, scope_stack, scope_map);

src/librustc/middle/trans/expr.rs

+44-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ return type, such as `while` loops or assignments (`a = b`).
115115

116116

117117
use back::abi;
118-
use lib::llvm::{ValueRef, llvm, SetLinkage, ExternalLinkage};
118+
use back::link;
119+
use lib::llvm::{ValueRef, llvm, SetLinkage, ExternalLinkage, False};
119120
use lib;
120121
use metadata::csearch;
121122
use middle::trans::_match;
@@ -150,6 +151,7 @@ use std::hashmap::HashMap;
150151
use std::vec;
151152
use syntax::print::pprust::{expr_to_str};
152153
use syntax::ast;
154+
use syntax::ast_map::path_mod;
153155
use syntax::codemap;
154156

155157
// Destinations
@@ -578,6 +580,9 @@ fn trans_rvalue_datum_unadjusted(bcx: @mut Block, expr: @ast::Expr) -> DatumBloc
578580
ast::ExprParen(e) => {
579581
return trans_rvalue_datum_unadjusted(bcx, e);
580582
}
583+
ast::ExprLogLevel => {
584+
return trans_log_level(bcx);
585+
}
581586
_ => {
582587
bcx.tcx().sess.span_bug(
583588
expr.span,
@@ -608,9 +613,6 @@ fn trans_rvalue_stmt_unadjusted(bcx: @mut Block, expr: @ast::Expr) -> @mut Block
608613
ast::ExprRet(ex) => {
609614
return controlflow::trans_ret(bcx, ex);
610615
}
611-
ast::ExprLog(lvl, a) => {
612-
return controlflow::trans_log(expr, lvl, bcx, a);
613-
}
614616
ast::ExprWhile(cond, ref body) => {
615617
return controlflow::trans_while(bcx, cond, body);
616618
}
@@ -1757,3 +1759,41 @@ fn trans_assign_op(bcx: @mut Block,
17571759
fn shorten(x: &str) -> @str {
17581760
(if x.char_len() > 60 {x.slice_chars(0, 60)} else {x}).to_managed()
17591761
}
1762+
1763+
pub fn trans_log_level(bcx: @mut Block) -> DatumBlock {
1764+
let _icx = push_ctxt("trans_log_level");
1765+
let ccx = bcx.ccx();
1766+
1767+
let (modpath, modname) = {
1768+
let path = &mut bcx.fcx.path;
1769+
let mut modpath = ~[path_mod(ccx.sess.ident_of(ccx.link_meta.name))];
1770+
for e in path.iter() {
1771+
match *e {
1772+
path_mod(_) => { modpath.push(*e) }
1773+
_ => {}
1774+
}
1775+
}
1776+
let modname = path_str(ccx.sess, modpath);
1777+
(modpath, modname)
1778+
};
1779+
1780+
let global = if ccx.module_data.contains_key(&modname) {
1781+
ccx.module_data.get_copy(&modname)
1782+
} else {
1783+
let s = link::mangle_internal_name_by_path_and_seq(
1784+
ccx, modpath, "loglevel");
1785+
let global;
1786+
unsafe {
1787+
global = do s.with_c_str |buf| {
1788+
llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf)
1789+
};
1790+
llvm::LLVMSetGlobalConstant(global, False);
1791+
llvm::LLVMSetInitializer(global, C_null(Type::i32()));
1792+
lib::llvm::SetLinkage(global, lib::llvm::InternalLinkage);
1793+
}
1794+
ccx.module_data.insert(modname, global);
1795+
global
1796+
};
1797+
1798+
return immediate_rvalue_bcx(bcx, Load(bcx, global), ty::mk_u32());
1799+
}

src/librustc/middle/trans/type_use.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,6 @@ pub fn mark_for_expr(cx: &Context, e: &Expr) {
377377
let base_ty = ty::node_id_to_type(cx.ccx.tcx, base.id);
378378
type_needs(cx, use_repr, ty::type_autoderef(cx.ccx.tcx, base_ty));
379379
}
380-
ExprLog(_, val) => {
381-
node_type_needs(cx, use_tydesc, val.id);
382-
}
383380
ExprCall(f, _, _) => {
384381
let r = ty::ty_fn_args(ty::node_id_to_type(cx.ccx.tcx, f.id));
385382
for a in r.iter() {
@@ -411,7 +408,7 @@ pub fn mark_for_expr(cx: &Context, e: &Expr) {
411408
ExprMatch(*) | ExprBlock(_) | ExprIf(*) | ExprWhile(*) |
412409
ExprBreak(_) | ExprAgain(_) | ExprUnary(*) | ExprLit(_) |
413410
ExprMac(_) | ExprAddrOf(*) | ExprRet(_) | ExprLoop(*) |
414-
ExprDoBody(_) => (),
411+
ExprDoBody(_) | ExprLogLevel => (),
415412

416413
ExprForLoop(*) => fail!("non-desugared expr_for_loop")
417414
}

src/librustc/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3320,7 +3320,6 @@ pub fn expr_kind(tcx: ctxt,
33203320
ast::ExprBreak(*) |
33213321
ast::ExprAgain(*) |
33223322
ast::ExprRet(*) |
3323-
ast::ExprLog(*) |
33243323
ast::ExprWhile(*) |
33253324
ast::ExprLoop(*) |
33263325
ast::ExprAssign(*) |
@@ -3331,6 +3330,7 @@ pub fn expr_kind(tcx: ctxt,
33313330

33323331
ast::ExprForLoop(*) => fail!("non-desugared expr_for_loop"),
33333332

3333+
ast::ExprLogLevel |
33343334
ast::ExprLit(_) | // Note: lit_str is carved out above
33353335
ast::ExprUnary(*) |
33363336
ast::ExprAddrOf(*) |

src/librustc/middle/typeck/check/mod.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -2526,18 +2526,8 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
25262526
}
25272527
fcx.write_bot(id);
25282528
}
2529-
ast::ExprLog(lv, e) => {
2530-
check_expr_has_type(fcx, lv,
2531-
ty::mk_mach_uint(ast::ty_u32));
2532-
2533-
// Note: this does not always execute, so do not propagate bot:
2534-
check_expr(fcx, e);
2535-
if ty::type_is_error(fcx.expr_ty(e)) {
2536-
fcx.write_error(id);
2537-
}
2538-
else {
2539-
fcx.write_nil(id);
2540-
}
2529+
ast::ExprLogLevel => {
2530+
fcx.write_ty(id, ty::mk_u32())
25412531
}
25422532
ast::ExprParen(a) => {
25432533
check_expr_with_opt_hint(fcx, a, expected);

src/librustc/middle/typeck/check/regionck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ pub mod guarantor {
10201020
ast::ExprBreak(*) |
10211021
ast::ExprAgain(*) |
10221022
ast::ExprRet(*) |
1023-
ast::ExprLog(*) |
1023+
ast::ExprLogLevel |
10241024
ast::ExprWhile(*) |
10251025
ast::ExprLoop(*) |
10261026
ast::ExprAssign(*) |

src/libstd/logging.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn console_off() {
3737
rt::logging::console_off();
3838
}
3939

40-
#[cfg(not(test))]
40+
#[cfg(not(test), stage0)]
4141
#[lang="log_type"]
4242
#[allow(missing_doc)]
4343
pub fn log_type<T>(_level: u32, object: &T) {
@@ -67,3 +67,10 @@ fn newsched_log_str(msg: ~str) {
6767
}
6868
}
6969
}
70+
71+
// XXX: This will change soon to not require an allocation. This is an unstable
72+
// api which should not be used outside of the macros in ext/expand.
73+
#[doc(hidden)]
74+
pub fn log(_level: u32, msg: ~str) {
75+
newsched_log_str(msg);
76+
}

0 commit comments

Comments
 (0)