浏览代码

Make assert more hygienic

Tibor Nagy 6 年之前
父节点
当前提交
614b2f5103
共有 5 个文件被更改,包括 37 次插入14 次删除
  1. 0 14
      include/assert.h
  2. 12 0
      include/bits/assert.h
  3. 7 0
      src/header/assert/cbindgen.toml
  4. 17 0
      src/header/assert/mod.rs
  5. 1 0
      src/header/mod.rs

+ 0 - 14
include/assert.h

@@ -1,14 +0,0 @@
-#ifndef _ASSERT_H
-#define _ASSERT_H
-
-#ifdef NDEBUG
-# define assert(cond)
-#else
-# include <stdio.h>
-# define assert(cond) if (!(cond)) { \
-    fprintf(stderr, "%s: %s:%d: Assertion `%s` failed.\n", __func__, __FILE__, __LINE__, #cond); \
-    abort(); \
-    }
-#endif
-
-#endif

+ 12 - 0
include/bits/assert.h

@@ -0,0 +1,12 @@
+#ifndef _BITS_ASSERT_H
+#define _BITS_ASSERT_H
+
+#ifdef NDEBUG
+# define assert(cond)
+#else
+# define assert(cond) if (!(cond)) { \
+    __assert(__func__, __FILE__, __LINE__, #cond); \
+  }
+#endif
+
+#endif

+ 7 - 0
src/header/assert/cbindgen.toml

@@ -0,0 +1,7 @@
+sys_includes = ["bits/assert.h"]
+include_guard = "_ASSERT_H"
+language = "C"
+style = "Tag"
+
+[enum]
+prefix_with_name = true

+ 17 - 0
src/header/assert/mod.rs

@@ -0,0 +1,17 @@
+//! assert implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/assert.h.html
+
+use c_str::CStr;
+use core::fmt::Write;
+use header::{stdio, stdlib};
+use platform;
+use platform::types::*;
+
+#[no_mangle]
+pub unsafe extern "C" fn __assert(func: *const c_char, file: *const c_char, line: c_int, cond: *const c_char) {
+    let func = CStr::from_ptr(func).to_str().unwrap();
+    let file = CStr::from_ptr(file).to_str().unwrap();
+    let cond = CStr::from_ptr(cond).to_str().unwrap();
+
+    write!(*stdio::stderr, "{}: {}:{}: Assertion `{}` failed.\n", func, file, line, cond).unwrap();
+    stdlib::abort();
+}

+ 1 - 0
src/header/mod.rs

@@ -1,5 +1,6 @@
 pub mod aio;
 pub mod arpa_inet;
+pub mod assert;
 pub mod ctype;
 pub mod dirent;
 pub mod errno;