[bindings] Use new non-null annotation feature in cbindgen
authorMatt Corallo <git@bluematt.me>
Thu, 31 Dec 2020 03:09:26 +0000 (22:09 -0500)
committerMatt Corallo <git@bluematt.me>
Wed, 3 Feb 2021 15:11:28 +0000 (10:11 -0500)
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.

c-bindings-gen/src/main.rs
genbindings.sh
lightning-c-bindings/cbindgen.toml

index 4cd3ff557cd02f03061c7fc951f26685076efdc6..90937bfa0e5a7a99cd954553229785a36086d80b 100644 (file)
@@ -1556,10 +1556,18 @@ fn main() {
        let mut cpp_header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
                .open(&args[6]).expect("Unable to open new header file");
 
-       writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap();
-       writeln!(header_file, "#else\n#define MUST_USE_STRUCT\n#endif").unwrap();
-       writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap();
-       writeln!(header_file, "#else\n#define MUST_USE_RES\n#endif").unwrap();
+       writeln!(header_file, "#if defined(__GNUC__)").unwrap();
+       writeln!(header_file, "#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap();
+       writeln!(header_file, "#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap();
+       writeln!(header_file, "#else").unwrap();
+       writeln!(header_file, "#define MUST_USE_STRUCT").unwrap();
+       writeln!(header_file, "#define MUST_USE_RES").unwrap();
+       writeln!(header_file, "#endif").unwrap();
+       writeln!(header_file, "#if defined(__clang__)").unwrap();
+       writeln!(header_file, "#define NONNULL_PTR _Nonnull").unwrap();
+       writeln!(header_file, "#else").unwrap();
+       writeln!(header_file, "#define NONNULL_PTR").unwrap();
+       writeln!(header_file, "#endif").unwrap();
        writeln!(cpp_header_file, "#include <string.h>\nnamespace LDK {{").unwrap();
 
        // First parse the full crate's ASTs, caching them so that we can hold references to the AST
index 007f69e99da17f46d7533c14d05ad717e9f1e041..c63de7dbc055f5d56c0a5d1085eb7ad4ea38b7e0 100755 (executable)
@@ -60,9 +60,11 @@ else
        echo "WARNING: Please install valgrind for more testing"
 fi
 
+CLANGOPTS="-Wall -Wno-nullability-completeness -pthread"
+
 # Test a statically-linked C++ version, tracking the resulting binary size and runtime
 # across debug, LTO, and cross-language LTO builds (using the same compiler each time).
-clang++ -std=c++11 -Wall -pthread demo.cpp target/debug/libldk.a -ldl
+clang++ $CLANGOPTS demo.cpp target/debug/libldk.a -ldl
 strip ./a.out
 echo " C++ Bin size and runtime w/o optimization:"
 ls -lha a.out
@@ -83,11 +85,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
                        set +e
 
                        # First the C demo app...
-                       clang-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.c target/debug/libldk.a -ldl
+                       clang-$LLVM_V $CLANGOPTS -fsanitize=memory -fsanitize-memory-track-origins -g demo.c target/debug/libldk.a -ldl
                        ./a.out
 
                        # ...then the C++ demo app
-                       clang++-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.cpp target/debug/libldk.a -ldl
+                       clang++-$LLVM_V -std=c++11 $CLANGOPTS -fsanitize=memory -fsanitize-memory-track-origins -g demo.cpp target/debug/libldk.a -ldl
                        ./a.out >/dev/null
 
                        # restore exit-on-failure
@@ -153,11 +155,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "
                mv Cargo.toml.bk Cargo.toml
 
                # First the C demo app...
-               $CLANG -fsanitize=address -Wall -g -pthread demo.c target/debug/libldk.a -ldl
+               $CLANG $CLANGOPTS -fsanitize=address -g demo.c target/debug/libldk.a -ldl
                ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
 
                # ...then the C++ demo app
-               $CLANGPP -std=c++11 -fsanitize=address -Wall -g -pthread demo.cpp target/debug/libldk.a -ldl
+               $CLANGPP $CLANGOPTS -std=c++11 -fsanitize=address -g demo.cpp target/debug/libldk.a -ldl
                ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
        else
                echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
@@ -168,7 +170,7 @@ fi
 
 # Now build with LTO on on both C++ and rust, but without cross-language LTO:
 CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C lto
-clang++ -std=c++11 -Wall -flto -O2 -pthread demo.cpp target/release/libldk.a -ldl
+clang++ $CLANGOPTS -std=c++11 -flto -O2 demo.cpp target/release/libldk.a -ldl
 strip ./a.out
 echo "C++ Bin size and runtime with only RL (LTO) optimized:"
 ls -lha a.out
@@ -181,7 +183,7 @@ if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
        # packaging than simply shipping the rustup binaries (eg Debian should Just Work
        # here).
        CARGO_PROFILE_RELEASE_LTO=true cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld
-       $CLANGPP -Wall -std=c++11 -flto -fuse-ld=lld -O2 -pthread demo.cpp target/release/libldk.a -ldl
+       $CLANGPP $CLANGOPTS -flto -fuse-ld=lld -O2 demo.cpp target/release/libldk.a -ldl
        strip ./a.out
        echo "C++ Bin size and runtime with cross-language LTO:"
        ls -lha a.out
index 48267780f3190b9eeee6d1e49a3c686e8d288c20..9480b8688c71f6b6fba21c3cfbbeec6cef3df39e 100644 (file)
@@ -550,3 +550,6 @@ default_features = true
 #
 # default: []
 features = ["cbindgen"]
+
+[ptr]
+non_null_attribute = "NONNULL_PTR"