[clang] [clang][analyzer] Add StoreToImmutable checker (PR #150417)
Endre Fülöp via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 28 05:20:04 PDT 2025
================
@@ -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}}
----------------
gamesh411 wrote:
My experience, is that not all expected-note lines are dead code even *without* specifying the `analyzer-output=text`.
Just to summarize my findings:
With *no* `analyzer-output=text`, and just the `expected-warning` comments in file, the test runner complains about the notes "Memory region is in immutable space" being seen but not expected. (so it does not duplicate warnings as notes, but still emits other notes belonging to warnings).
With `analyzer-output=text`, the only difference is that the warning lines are duplicated as notes as well.
In light of that, not providing the `analyzer-output=text` and using the `expected-warning` lines without the duplicate note lines *and* using the `expected-note` on the declaration lines seems to be the least redundant way of expressing what we expect.
@steakhal What do you think?
https://github.com/llvm/llvm-project/pull/150417
More information about the cfe-commits
mailing list