[clang] [analyzer] Do not destruct fields of unions (PR #122330)

Jameson Nash via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 3 08:32:34 PST 2025


================
@@ -377,3 +377,27 @@ void directUnknownSymbol() {
 }
 
 }
+
+void testUnionDtor() {
+  static int unionDtorCalled;
+  InlineDtor::cnt = 0;
+  InlineDtor::dtorCalled = 0;
+  unionDtorCalled = 0;
+  {
+      union UnionDtor {
+          InlineDtor kind1;
+          char kind2;
+          ~UnionDtor() { unionDtorCalled++; }
+      };
+      UnionDtor u1{.kind1{}};
+      UnionDtor u2{.kind2{}};
+      auto u3 = new UnionDtor{.kind1{}};
+      auto u4 = new UnionDtor{.kind2{}};
+      delete u3;
+      delete u4;
+  }
+
+  clang_analyzer_eval(unionDtorCalled == 4); // expected-warning {{TRUE}}
+  clang_analyzer_eval(InlineDtor::dtorCalled != 4); // expected-warning {{TRUE}}
----------------
vtjnash wrote:

Sure, makes sense. I had left it in as a hint of what the expected bad result would look like in case it breaks, but not necessary

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


More information about the cfe-commits mailing list