Skip to content

Commit 501d61a

Browse files
committed
librustc_llvm: encapsulate "as" cast from/to a bool
When the trueness is defined as "!= 0" rather than "== 1", it's error prone to write such a comparison for each occurrence. Signed-off-by: NODA, Kai <[email protected]>
1 parent 526f2bf commit 501d61a

File tree

9 files changed

+58
-32
lines changed

9 files changed

+58
-32
lines changed

src/librustc_llvm/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ pub type Bool = c_uint;
7373
pub const True: Bool = 1 as Bool;
7474
pub const False: Bool = 0 as Bool;
7575

76+
#[inline(always)]
77+
pub fn as_bool(b: Bool) -> bool { False != b }
78+
#[inline(always)]
79+
pub fn as_llvm_bool(b: bool) -> Bool { b as Bool }
80+
7681
// Consts for the LLVM CallConv type, pre-cast to usize.
7782

7883
#[derive(Copy, Clone, PartialEq)]
@@ -2178,13 +2183,13 @@ pub fn SetDLLStorageClass(global: ValueRef, class: DLLStorageClassTypes) {
21782183

21792184
pub fn SetUnnamedAddr(global: ValueRef, unnamed: bool) {
21802185
unsafe {
2181-
LLVMSetUnnamedAddr(global, unnamed as Bool);
2186+
LLVMSetUnnamedAddr(global, as_llvm_bool(unnamed));
21822187
}
21832188
}
21842189

21852190
pub fn set_thread_local(global: ValueRef, is_thread_local: bool) {
21862191
unsafe {
2187-
LLVMSetThreadLocal(global, is_thread_local as Bool);
2192+
LLVMSetThreadLocal(global, as_llvm_bool(is_thread_local));
21882193
}
21892194
}
21902195

@@ -2443,3 +2448,18 @@ impl Drop for OperandBundleDef {
24432448
mod llvmdeps {
24442449
include! { env!("CFG_LLVM_LINKAGE_FILE") }
24452450
}
2451+
2452+
#[cfg(test)]
2453+
mod tests {
2454+
use super::{True, False};
2455+
2456+
#[test]
2457+
fn test_bool_cast() {
2458+
assert_eq!( True, super::as_llvm_bool(true));
2459+
assert_eq!(False, super::as_llvm_bool(false));
2460+
2461+
assert!( super::as_bool(True));
2462+
assert!( super::as_bool(12345));
2463+
assert!(!super::as_bool(False));
2464+
}
2465+
}

src/librustc_metadata/loader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ use rustc::session::search_paths::PathKind;
223223
use rustc::util::common;
224224

225225
use rustc_llvm as llvm;
226-
use rustc_llvm::{False, ObjectFile, mk_section_iter};
226+
use rustc_llvm::{ObjectFile, mk_section_iter};
227227
use rustc_llvm::archive_ro::ArchiveRO;
228228
use syntax::codemap::Span;
229229
use syntax::errors::DiagnosticBuilder;
@@ -793,7 +793,7 @@ fn get_metadata_section_imp(target: &Target, filename: &Path)
793793
}
794794
};
795795
let si = mk_section_iter(of.llof);
796-
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
796+
while !llvm::as_bool(llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi)) {
797797
let mut name_buf = ptr::null();
798798
let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
799799
let name = slice::from_raw_parts(name_buf as *const u8,

src/librustc_trans/base.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ pub fn load_ty_builder<'a, 'tcx>(b: &Builder<'a, 'tcx>, ptr: ValueRef, t: Ty<'tc
922922

923923
unsafe {
924924
let global = llvm::LLVMIsAGlobalVariable(ptr);
925-
if !global.is_null() && llvm::LLVMIsGlobalConstant(global) == llvm::True {
925+
if !global.is_null() && llvm::as_bool(llvm::LLVMIsGlobalConstant(global)) {
926926
let val = llvm::LLVMGetInitializer(global);
927927
if !val.is_null() {
928928
if t.is_bool() {
@@ -2530,7 +2530,7 @@ fn internalize_symbols(cx: &SharedCrateContext, reachable: &HashSet<&str>) {
25302530
// We only care about external declarations (not definitions)
25312531
// and available_externally definitions.
25322532
if !(linkage == llvm::ExternalLinkage as c_uint &&
2533-
llvm::LLVMIsDeclaration(val) != 0) &&
2533+
llvm::as_bool(llvm::LLVMIsDeclaration(val))) &&
25342534
!(linkage == llvm::AvailableExternallyLinkage as c_uint) {
25352535
continue;
25362536
}
@@ -2551,7 +2551,7 @@ fn internalize_symbols(cx: &SharedCrateContext, reachable: &HashSet<&str>) {
25512551
// We only care about external definitions.
25522552
if !((linkage == llvm::ExternalLinkage as c_uint ||
25532553
linkage == llvm::WeakODRLinkage as c_uint) &&
2554-
llvm::LLVMIsDeclaration(val) == 0) {
2554+
!llvm::as_bool(llvm::LLVMIsDeclaration(val))) {
25552555
continue;
25562556
}
25572557

@@ -2590,7 +2590,7 @@ fn create_imps(cx: &SharedCrateContext) {
25902590
.filter(|&val| {
25912591
llvm::LLVMGetLinkage(val) ==
25922592
llvm::ExternalLinkage as c_uint &&
2593-
llvm::LLVMIsDeclaration(val) == 0
2593+
!llvm::as_bool(llvm::LLVMIsDeclaration(val))
25942594
})
25952595
.collect();
25962596

@@ -2715,7 +2715,7 @@ pub fn trans_crate<'tcx>(tcx: &TyCtxt<'tcx>,
27152715
static INIT: Once = Once::new();
27162716
static mut POISONED: bool = false;
27172717
INIT.call_once(|| {
2718-
if llvm::LLVMStartMultithreaded() != 1 {
2718+
if !llvm::as_bool(llvm::LLVMStartMultithreaded()) {
27192719
// use an extra bool to make sure that all future usage of LLVM
27202720
// cannot proceed despite the Once not running more than once.
27212721
POISONED = true;

src/librustc_trans/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn Switch(cx: Block, v: ValueRef, else_: BasicBlockRef, num_cases: usize)
116116

117117
pub fn AddCase(s: ValueRef, on_val: ValueRef, dest: BasicBlockRef) {
118118
unsafe {
119-
if llvm::LLVMIsUndef(s) == llvm::True { return; }
119+
if llvm::as_bool(llvm::LLVMIsUndef(s)) { return; }
120120
llvm::LLVMAddCase(s, on_val, dest);
121121
}
122122
}
@@ -933,7 +933,7 @@ pub fn Phi(cx: Block, ty: Type, vals: &[ValueRef],
933933

934934
pub fn AddIncomingToPhi(phi: ValueRef, val: ValueRef, bb: BasicBlockRef) {
935935
unsafe {
936-
if llvm::LLVMIsUndef(phi) == llvm::True { return; }
936+
if llvm::as_bool(llvm::LLVMIsUndef(phi)) { return; }
937937
llvm::LLVMAddIncoming(phi, &val, &bb, 1 as c_uint);
938938
}
939939
}

src/librustc_trans/common.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use session::Session;
1616
use llvm;
1717
use llvm::{ValueRef, BasicBlockRef, BuilderRef, ContextRef, TypeKind};
18-
use llvm::{True, False, Bool, OperandBundleDef};
18+
use llvm::{True, OperandBundleDef};
1919
use rustc::cfg;
2020
use rustc::hir::def::Def;
2121
use rustc::hir::def_id::DefId;
@@ -810,7 +810,7 @@ pub fn C_undef(t: Type) -> ValueRef {
810810

811811
pub fn C_integral(t: Type, u: u64, sign_extend: bool) -> ValueRef {
812812
unsafe {
813-
llvm::LLVMConstInt(t.to_ref(), u, sign_extend as Bool)
813+
llvm::LLVMConstInt(t.to_ref(), u, llvm::as_llvm_bool(sign_extend))
814814
}
815815
}
816816

@@ -902,7 +902,7 @@ pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> Va
902902
let sc = llvm::LLVMConstStringInContext(cx.llcx(),
903903
s.as_ptr() as *const c_char,
904904
s.len() as c_uint,
905-
!null_terminated as Bool);
905+
llvm::as_llvm_bool(!null_terminated));
906906

907907
let gsym = token::gensym("str");
908908
let sym = format!("str{}", gsym.0);
@@ -934,7 +934,7 @@ pub fn C_struct_in_context(llcx: ContextRef, elts: &[ValueRef], packed: bool) ->
934934
unsafe {
935935
llvm::LLVMConstStructInContext(llcx,
936936
elts.as_ptr(), elts.len() as c_uint,
937-
packed as Bool)
937+
llvm::as_llvm_bool(packed))
938938
}
939939
}
940940

@@ -1018,16 +1018,20 @@ pub fn const_to_opt_uint(v: ValueRef) -> Option<u64> {
10181018
}
10191019

10201020
pub fn is_undef(val: ValueRef) -> bool {
1021-
unsafe {
1022-
llvm::LLVMIsUndef(val) != False
1023-
}
1021+
llvm::as_bool(
1022+
unsafe {
1023+
llvm::LLVMIsUndef(val)
1024+
}
1025+
)
10241026
}
10251027

10261028
#[allow(dead_code)] // potentially useful
10271029
pub fn is_null(val: ValueRef) -> bool {
1028-
unsafe {
1029-
llvm::LLVMIsNull(val) != False
1030-
}
1030+
llvm::as_bool(
1031+
unsafe {
1032+
llvm::LLVMIsNull(val)
1033+
}
1034+
)
10311035
}
10321036

10331037
pub fn monomorphize_type<'blk, 'tcx>(bcx: &BlockS<'blk, 'tcx>, t: Ty<'tcx>) -> Ty<'tcx> {

src/librustc_trans/consts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use llvm;
1313
use llvm::{ConstFCmp, ConstICmp, SetLinkage, SetUnnamedAddr};
14-
use llvm::{InternalLinkage, ValueRef, Bool, True};
14+
use llvm::{InternalLinkage, ValueRef, True};
1515
use middle::const_qualif::ConstQualif;
1616
use rustc_const_eval::{ConstEvalErr, lookup_const_fn_by_id, lookup_const_by_id, ErrKind};
1717
use rustc_const_eval::eval_repeat_count;
@@ -756,11 +756,11 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
756756
let repr = adt::represent_type(cx, t_expr);
757757
let discr = adt::const_get_discrim(&repr, v);
758758
let iv = C_integral(cx.int_type(), discr.0, false);
759-
let s = adt::is_discr_signed(&repr) as Bool;
759+
let s = llvm::as_llvm_bool(adt::is_discr_signed(&repr));
760760
llvm::LLVMConstIntCast(iv, llty.to_ref(), s)
761761
},
762762
(CastTy::Int(_), CastTy::Int(_)) => {
763-
let s = t_expr.is_signed() as Bool;
763+
let s = llvm::as_llvm_bool(t_expr.is_signed());
764764
llvm::LLVMConstIntCast(v, llty.to_ref(), s)
765765
},
766766
(CastTy::Int(_), CastTy::Float) => {

src/librustc_trans/declare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub fn get_declared_value(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
168168
pub fn get_defined_value(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
169169
get_declared_value(ccx, name).and_then(|val|{
170170
let declaration = unsafe {
171-
llvm::LLVMIsDeclaration(val) != 0
171+
llvm::as_bool(llvm::LLVMIsDeclaration(val))
172172
};
173173
if !declaration {
174174
Some(val)

src/librustc_trans/type_.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![allow(non_upper_case_globals)]
1212

1313
use llvm;
14-
use llvm::{TypeRef, Bool, False, True, TypeKind};
14+
use llvm::{TypeRef, False, True, TypeKind};
1515
use llvm::{Float, Double, X86_FP80, PPC_FP128, FP128};
1616

1717
use context::CrateContext;
@@ -171,7 +171,7 @@ impl Type {
171171
let els: &[TypeRef] = Type::to_ref_slice(els);
172172
ty!(llvm::LLVMStructTypeInContext(ccx.llcx(), els.as_ptr(),
173173
els.len() as c_uint,
174-
packed as Bool))
174+
llvm::as_llvm_bool(packed)))
175175
}
176176

177177
pub fn named_struct(ccx: &CrateContext, name: &str) -> Type {
@@ -215,7 +215,7 @@ impl Type {
215215
let slice: &[TypeRef] = Type::to_ref_slice(els);
216216
unsafe {
217217
llvm::LLVMStructSetBody(self.to_ref(), slice.as_ptr(),
218-
els.len() as c_uint, packed as Bool)
218+
els.len() as c_uint, llvm::as_llvm_bool(packed))
219219
}
220220
}
221221

@@ -231,9 +231,11 @@ impl Type {
231231
}
232232

233233
pub fn is_packed(&self) -> bool {
234-
unsafe {
235-
llvm::LLVMIsPackedStruct(self.to_ref()) == True
236-
}
234+
llvm::as_bool(
235+
unsafe {
236+
llvm::LLVMIsPackedStruct(self.to_ref())
237+
}
238+
)
237239
}
238240

239241
pub fn element_type(&self) -> Type {

src/test/run-make/execution-engine/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl ExecutionEngine {
172172

173173
let res = unsafe { llvm::LLVMRustLoadDynamicLibrary(cs.as_ptr()) };
174174

175-
if res == 0 {
175+
if !llvm::as_bool(res) {
176176
panic!("Failed to load crate {:?}: {}",
177177
path.display(), llvm_error());
178178
}

0 commit comments

Comments
 (0)