[clang] Don't omit null pointer checks with -fms-kernel (PR #193800)

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 07:14:01 PDT 2026


https://github.com/eleviant updated https://github.com/llvm/llvm-project/pull/193800

>From e08c1bfe7868fe6655a187218fafe975e5959b4e Mon Sep 17 00:00:00 2001
From: Evgeny Leviant <eleviant at accesssoftek.com>
Date: Thu, 9 Apr 2026 11:58:30 +0200
Subject: [PATCH] [clang] Don't omit null pointer checks with -fms-kernel

---
 clang/lib/Frontend/CompilerInvocation.cpp |  5 +++++
 clang/test/CodeGen/MSKernel/null-deref.c  | 16 ++++++++++++++++
 2 files changed, 21 insertions(+)
 create mode 100644 clang/test/CodeGen/MSKernel/null-deref.c

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index c6e8644905964..a8a5d57155228 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1924,6 +1924,11 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
     Opts.DIBugsReportFilePath = "";
   }
 
+  if (LangOpts->Kernel &&
+      !Args.hasFlag(OPT_fdelete_null_pointer_checks,
+                    OPT_fno_delete_null_pointer_checks, false))
+    Opts.NullPointerIsValid = true;
+
   Opts.NewStructPathTBAA = !Args.hasArg(OPT_no_struct_path_tbaa) &&
                            Args.hasArg(OPT_new_struct_path_tbaa);
   Opts.OptimizeSize = getOptimizationLevelSize(Args);
diff --git a/clang/test/CodeGen/MSKernel/null-deref.c b/clang/test/CodeGen/MSKernel/null-deref.c
new file mode 100644
index 0000000000000..e981f35369bd7
--- /dev/null
+++ b/clang/test/CodeGen/MSKernel/null-deref.c
@@ -0,0 +1,16 @@
+// Check that null pointer checks are not omited in kernel mode compilations
+// RUN: %clang_cc1 -fms-kernel -fms-extensions -triple x86_64-pc-windows-msvc -O2 %s -emit-llvm -o - | FileCheck %s
+
+// CHECK:      define dso_local i32 @process(ptr noundef readonly captures(address_is_null) %p) local_unnamed_addr #0
+// CHECK-NEXT: entry:
+// CHECK-NEXT:   %{{.*}} = icmp eq ptr %p, null
+// CHECK:      attributes #0 = {{.*}} null_pointer_is_valid
+
+struct Obj { int value; int extra; };
+
+int process(struct Obj* p) {
+    int v = p->value;
+    if (!p)
+        return -1;
+    return v + p->extra;
+}



More information about the cfe-commits mailing list