[clang] [WIP][NFC][analyzer] Add a test that demonstrates a bug (PR #188805)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 26 10:49:52 PDT 2026
https://github.com/NagyDonat created https://github.com/llvm/llvm-project/pull/188805
This test demonstrates a shortcoming of the analyzer.
I don't intend to merge this patch, but merging it wouldn't cause any problem.
>From c505a83259a6b47e2e5c7ece37c8012b8f60fa91 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Don=C3=A1t=20Nagy?= <donat.nagy at ericsson.com>
Date: Thu, 26 Mar 2026 18:48:03 +0100
Subject: [PATCH] [WIP][NFC][analyzer] Add a test that demonstrates a bug
This test demonstrates a shortcoming of the analyzer.
I don't intend to merge this patch, but merging it wouldn't cause any
problem.
---
...-structs-initializer-not-visible-in-main.c | 64 +++++++++++++++++++
1 file changed, 64 insertions(+)
create mode 100644 clang/test/Analysis/array-of-structs-initializer-not-visible-in-main.c
diff --git a/clang/test/Analysis/array-of-structs-initializer-not-visible-in-main.c b/clang/test/Analysis/array-of-structs-initializer-not-visible-in-main.c
new file mode 100644
index 0000000000000..9aa9ea8294c26
--- /dev/null
+++ b/clang/test/Analysis/array-of-structs-initializer-not-visible-in-main.c
@@ -0,0 +1,64 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
+
+// This test demonstrates that the analyzer cannot 'see' the initializer of a
+// non-const array of structs during the analysis of the main() function.
+
+void clang_analyzer_warnIfReached(void);
+
+struct S {
+ int a;
+};
+
+struct S struct_array[1] = {
+ {11},
+};
+
+int num_array[1] = {22};
+
+struct S single_struct = {33};
+
+int main(int argc, char **argv) {
+ if (argc == 1) {
+ if (struct_array->a == 11) {
+ clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+ } else {
+ // FIXME: This should be unreachable, because the analyzer should realize
+ // that when 'main' starts 'struct_array' still contains its initializer.
+ clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+ }
+ } else if (argc == 2) {
+ if (*num_array == 22) {
+ clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+ } else {
+ clang_analyzer_warnIfReached(); // unreachable
+ }
+ } else {
+ if (single_struct.a == 33) {
+ clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+ } else {
+ clang_analyzer_warnIfReached(); // unreachable
+ }
+ }
+}
+
+const struct S struct_array_const[1] = { {44} };
+
+void use_struct_array(void) {
+ if (struct_array->a == 44) {
+ clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+ } else {
+ clang_analyzer_warnIfReached(); // unreachable
+ }
+}
+
+struct S struct_array_nonconst[1] = { {55} };
+
+void use_struct_array_nonconst(void) {
+ if (struct_array_nonconst->a == 55) {
+ clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+ } else {
+ // This is intentionally reachable, because this is a non-const array which
+ // may have been changed before the call to this function.
+ clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+ }
+}
More information about the cfe-commits
mailing list