Unions: C lets you read any member at any time, even the wrong one.
Now you can tell clang which field is the tag. A member can only be read after checking it. This is similar to Rust enums.
1
5
295
Null: C lets you use a pointer without checking if it's null.
Now a pointer that may be null, like the result of malloc, must be checked before use. Pointers are assumed non-null unless annotated otherwise with the existing `_Nullable`.
1
6
328
Leaks inside structs: a struct can own memory too. Freeing the struct while a field still owns something is a compile error.
_Take(x) moves the value out and leaves x null, like Rust's Option::take.
1
7
384
Ownership: A new `_Owned` says who owns/frees a pointer, similar to Box. Forgetting to free it, or freeing it twice, is a compile error.
Unsafe code must be annotated
__unsafe { }
On other compilers, these annotations disappear, so it's still C with a C ABI.
1
8
423
Locks: you can express which lock guards which data.
You can only read or write the data while holding its lock; otherwise clang warns.
Upstream clang has had this for years, as -Wthread-safety.
1
13
538
Strings: you can tell clang a char * ends in a 0, similar to Rust's &CStr.
You can only walk it one char at a time. Jumping ahead (s[5], s += 2) is a compile error.
This is also part of `-fbounds-safety` from Apple's clang fork.
1
11
678
Buffer sizes: buffers in C are just pointers with conventions for their size. Now you can express the conventions in code that the compiler can check.
Checks happen at compile time where possible, at run time otherwise (like a Rust panic).
Enable with `-fbounds-safety`
First up, Lifetimes: clang can tell when a pointer outlives what it points to.
C support is new, experimental (clang 23), and inspired by Rust's borrow checker.
discourse.llvm.org/t/rfc-int…
`-Xclang -fexperimental-lifetime-safety-c -Wlifetime-safety`
Last week I attended the LLVM Developers' Meeting in Santa Clara for three days. It was filled with interesting people and talks, a good mix of industry and academia. I also gave a talk on my RFC to add null pointer dereference warnings to clang.
Formal verification was a hot topic - there were talks on the formal semantics of LLVM-IR, and VeIR, the formalization of MLIR transformations using Lean. I also appreciated the talk on LLVM in safety critical industries, having worked in aerospace in the past.
Regarding my talk, the idea is pointer nullability is unspecified in C and C++. But Clang supports nullability annotations (_Nullable and _Nonnull) and has sufficient information to warn on use where _Nullable pointers are used, but it does not warn. I am proposing it exploits this information and warns, making code safer. github.com/cs01/llvm-project…
I attended last year for the first time and loved the vibe (before the first talk I was surprised to see Chris Lattner happen to sit next to me and start chatting with me). If you are interested, definitely check it out next year. It's a great way to support the @llvmorg, learn new things, and network.
2
149
















