[clang] [clang] Don't omit null pointer checks with -fms-compatibility (PR #204658)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 26 02:52:16 PDT 2026
https://github.com/eleviant updated https://github.com/llvm/llvm-project/pull/204658
>From 85001c413ee73bf59e319fa9a947c4aa0ced5661 Mon Sep 17 00:00:00 2001
From: Evgeny Leviant <eleviant at accesssoftek.com>
Date: Thu, 25 Jun 2026 20:36:36 +0200
Subject: [PATCH 1/2] [clang] Don't omit null pointer checks with -fms-kernel
MSVC preserves null check after dereference, e.g
```
struct Obj { int value, extra; };
int null_check_is_kept(Obj* p) {
int v = p->value;
if (p == nullptr)
return -1;
return v + p->extra;
}
```
Also MSVC keeps standard library calls (e.g memcpy), where target is
null pointer. Make null pointer defined in kernel mode, as some low
level code depends on it.
---
clang/include/clang/Options/Options.td | 4 +--
clang/test/CodeGen/null-checks.c | 35 ++++++++++++++++++++++++++
2 files changed, 37 insertions(+), 2 deletions(-)
create mode 100644 clang/test/CodeGen/null-checks.c
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 22e730ac58fb1..6b7e314c4b4c8 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -3071,10 +3071,10 @@ defm rewrite_includes : BoolFOption<"rewrite-includes",
defm directives_only : OptInCC1FFlag<"directives-only", "">;
defm delete_null_pointer_checks : BoolFOption<"delete-null-pointer-checks",
- CodeGenOpts<"NullPointerIsValid">, DefaultFalse,
+ CodeGenOpts<"NullPointerIsValid">, Default<"LangOpts->Kernel">,
NegFlag<SetTrue, [], [ClangOption, CC1Option],
"Do not treat usage of null pointers as undefined behavior">,
- PosFlag<SetFalse, [], [ClangOption], "Treat usage of null pointers as undefined behavior (default)">,
+ PosFlag<SetFalse, [], [ClangOption, CC1Option], "Treat usage of null pointers as undefined behavior (default)">,
BothFlags<[], [ClangOption, CLOption]>>,
DocBrief<[{When enabled, treat null pointer dereference, creation of a reference to null,
or passing a null pointer to a function parameter annotated with the "nonnull"
diff --git a/clang/test/CodeGen/null-checks.c b/clang/test/CodeGen/null-checks.c
new file mode 100644
index 0000000000000..403a08e19de89
--- /dev/null
+++ b/clang/test/CodeGen/null-checks.c
@@ -0,0 +1,35 @@
+// Check that null pointer checks are not optimized out if -fms-compatibility is set
+// RUN: %clang_cc1 -fms-kernel -O2 -triple x86_64-pc-windows-msvc %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -O2 -triple x86_64-pc-windows-msvc %s -emit-llvm -o - | FileCheck %s --check-prefix=NOCHECK
+// RUN: %clang_cc1 -fms-kernel -O2 -triple x86_64-pc-windows-msvc -fdelete-null-pointer-checks %s -emit-llvm -o - | FileCheck %s --check-prefix=NOCHECK
+
+// CHECK-LABEL: i32 @process
+// CHECK-NEXT: entry:
+// CHECK-NEXT: %tobool.not = icmp eq ptr %p, null
+// CHECK-NEXT: br i1 %tobool.not, label %cleanup, label %if.end
+
+// CHECK-LABEL: ptr @call_memcpy
+// CHECK-NEXT: entry:
+// CHECK-NEXT: tail call void @llvm.memcpy.p0.p0.i64
+// CHECK-NEXT: ret ptr null
+
+
+// NOCHECK-LABEL: i32 @process
+// NOCHECK-NOT: icmp eq ptr %p, null
+
+// NOCHECK-LABEL: ptr @call_memcpy
+// NOCHECK-NEXT: entry:
+// NOCHECK-NEXT: ret ptr null
+
+struct Obj { int value; int extra; };
+
+int process(struct Obj* p) {
+ int v = p->value;
+ if (!p)
+ return -1;
+ return v + p->extra;
+}
+
+void* call_memcpy(void* p, long long size) {
+ return __builtin_memcpy(0, p, size);
+}
>From 649a09f23e569d62f027380cd59f17f04c898f5a Mon Sep 17 00:00:00 2001
From: Evgeny Leviant <eleviant at accesssoftek.com>
Date: Fri, 26 Jun 2026 11:51:38 +0200
Subject: [PATCH 2/2] Added release notes
---
clang/docs/ReleaseNotes.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f1e9b2d52ec97..187f7d7404a9e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -415,6 +415,8 @@ Modified Compiler Flags
- The `-fzero-call-used-regs` compiler flag is now allowed on RISC-V, only the
"skip", "used-gpr", "used-gpr-arg", "all-gpr" and "all-gpr-arg" options are
supported for the moment.
+- The `-fms-kernel` flag will now implicitly add -fno-delete-null-pointer-checks.
+ Still -fdelete-null-pointer-checks can be used to override this behavior.
- Slightly changed hash id generation to get the unique linkage symbols names
by ``-unique-internal-linkage-names`` option. Now it uses a path that
More information about the cfe-commits
mailing list