[clang] [clang][analyzer] Add StoreToImmutable checker (PR #150417)

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 25 05:11:05 PDT 2025


Endre =?utf-8?q?Fülöp?= <endre.fulop at sigmatechnology.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/150417 at github.com>


================
@@ -0,0 +1,166 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.StoreToImmutable -verify %s
+
+// Test basic functionality of StoreToImmutable checker
+// This tests direct writes to immutable regions without function modeling
+
+// Direct write to a const global variable
+const int global_const = 42; // expected-note {{Memory region is in immutable space}}
+
+void test_direct_write_to_const_global() {
+  // This should trigger a warning about writing to immutable memory
+  *(int*)&global_const = 100; // expected-warning {{Writing to immutable memory is undefined behavior}}
+  // expected-note at -1 {{Writing to immutable memory is undefined behavior. This memory region is marked as immutable and should not be modified}}
----------------
steakhal wrote:

I think there is a miscommunication.
None of the `expected-note` comments in any of your tests are effective. All of those lines are dead code.
It is because the `-analyzer-output=text` is not present in the RUN line in any of your tests.

Since the checker emits a custom note, there should be one test file testing it, or just have it enabled in all of your RUN lines in all files.

https://github.com/llvm/llvm-project/pull/150417


More information about the cfe-commits mailing list