Skip to content

Commit 53f2e25

Browse files
committed
[bindings] Use new non-null annotation feature in cbindgen
This adds a new annotation for objects we take by reference in the C header indicating the pointers must not be null. We have to disable some warning clang now dumps that we haven't annotated all pointers, as cbindgen is not yet able to add a nullable annotation.
1 parent ac078c1 commit 53f2e25

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

c-bindings-gen/src/main.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,10 +1556,18 @@ fn main() {
15561556
let mut cpp_header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
15571557
.open(&args[6]).expect("Unable to open new header file");
15581558

1559-
writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap();
1560-
writeln!(header_file, "#else\n#define MUST_USE_STRUCT\n#endif").unwrap();
1561-
writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap();
1562-
writeln!(header_file, "#else\n#define MUST_USE_RES\n#endif").unwrap();
1559+
writeln!(header_file, "#if defined(__GNUC__)").unwrap();
1560+
writeln!(header_file, "#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap();
1561+
writeln!(header_file, "#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap();
1562+
writeln!(header_file, "#else").unwrap();
1563+
writeln!(header_file, "#define MUST_USE_STRUCT").unwrap();
1564+
writeln!(header_file, "#define MUST_USE_RES").unwrap();
1565+
writeln!(header_file, "#endif").unwrap();
1566+
writeln!(header_file, "#if defined(__clang__)").unwrap();
1567+
writeln!(header_file, "#define NONNULL_PTR _Nonnull").unwrap();
1568+
writeln!(header_file, "#else").unwrap();
1569+
writeln!(header_file, "#define NONNULL_PTR").unwrap();
1570+
writeln!(header_file, "#endif").unwrap();
15631571
writeln!(cpp_header_file, "#include <string.h>\nnamespace LDK {{").unwrap();
15641572

15651573
// First parse the full crate's ASTs, caching them so that we can hold references to the AST

genbindings.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ else
6060
echo "WARNING: Please install valgrind for more testing"
6161
fi
6262

63+
CLANGOPTS="-Wall -Wno-nullability-completeness -pthread"
64+
6365
# Test a statically-linked C++ version, tracking the resulting binary size and runtime
6466
# across debug, LTO, and cross-language LTO builds (using the same compiler each time).
65-
clang++ -std=c++11 -Wall -pthread demo.cpp target/debug/libldk.a -ldl
67+
clang++ $CLANGOPTS demo.cpp target/debug/libldk.a -ldl
6668
strip ./a.out
6769
echo " C++ Bin size and runtime w/o optimization:"
6870
ls -lha a.out
@@ -83,11 +85,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
8385
set +e
8486

8587
# First the C demo app...
86-
clang-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.c target/debug/libldk.a -ldl
88+
clang-$LLVM_V $CLANGOPTS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
8789
./a.out
8890

8991
# ...then the C++ demo app
90-
clang++-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.cpp target/debug/libldk.a -ldl
92+
clang++-$LLVM_V -std=c++11 $CLANGOPTS -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
9193
./a.out >/dev/null
9294

9395
# restore exit-on-failure
@@ -153,11 +155,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "
153155
mv Cargo.toml.bk Cargo.toml
154156

155157
# First the C demo app...
156-
$CLANG -fsanitize=address -Wall -g -pthread demo.c target/debug/libldk.a -ldl
158+
$CLANG $CLANGOPTS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
157159
ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
158160

159161
# ...then the C++ demo app
160-
$CLANGPP -std=c++11 -fsanitize=address -Wall -g -pthread demo.cpp target/debug/libldk.a -ldl
162+
$CLANGPP $CLANGOPTS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
161163
ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
162164
else
163165
echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
@@ -168,7 +170,7 @@ fi
168170

169171
# Now build with LTO on on both C++ and rust, but without cross-language LTO:
170172
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
171-
clang++ -std=c++11 -Wall -flto -O2 -pthread demo.cpp target/release/libldk.a -ldl
173+
clang++ $CLANGOPTS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl
172174
strip ./a.out
173175
echo "C++ Bin size and runtime with only RL (LTO) optimized:"
174176
ls -lha a.out
@@ -181,7 +183,7 @@ if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
181183
# packaging than simply shipping the rustup binaries (eg Debian should Just Work
182184
# here).
183185
CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld
184-
$CLANGPP -Wall -std=c++11 -flto -fuse-ld=lld -O2 -pthread demo.cpp target/release/libldk.a -ldl
186+
$CLANGPP $CLANGOPTS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl
185187
strip ./a.out
186188
echo "C++ Bin size and runtime with cross-language LTO:"
187189
ls -lha a.out

lightning-c-bindings/cbindgen.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,6 @@ default_features = true
550550
#
551551
# default: []
552552
features = ["cbindgen"]
553+
554+
[ptr]
555+
non_null_attribute = "NONNULL_PTR"

0 commit comments

Comments
 (0)