Skip to content

Commit 5c581bb

Browse files
committed
fixup! [Sema] do not destruct fields of unions
1 parent ec5ae1b commit 5c581bb

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

clang/test/Analysis/NewDelete-checker-test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ namespace optional_union {
457457
unique_ptr<int> present;
458458
char notpresent;
459459
custom_union_t() : present(unique_ptr<int>()) {}
460-
~custom_union_t() {};
460+
~custom_union_t() {}
461461
};
462462

463463
void testUnionCorrect() {

clang/test/Analysis/dtor-array.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -377,27 +377,3 @@ void directUnknownSymbol() {
377377
}
378378

379379
}
380-
381-
void testUnionDtor() {
382-
static int unionDtorCalled;
383-
InlineDtor::cnt = 0;
384-
InlineDtor::dtorCalled = 0;
385-
unionDtorCalled = 0;
386-
{
387-
union UnionDtor {
388-
InlineDtor kind1;
389-
char kind2;
390-
~UnionDtor() { unionDtorCalled++; }
391-
};
392-
UnionDtor u1{.kind1{}};
393-
UnionDtor u2{.kind2{}};
394-
auto u3 = new UnionDtor{.kind1{}};
395-
auto u4 = new UnionDtor{.kind2{}};
396-
delete u3;
397-
delete u4;
398-
}
399-
400-
clang_analyzer_eval(unionDtorCalled == 4); // expected-warning {{TRUE}}
401-
clang_analyzer_eval(InlineDtor::dtorCalled != 4); // expected-warning {{TRUE}}
402-
clang_analyzer_eval(InlineDtor::dtorCalled == 0); // expected-warning {{TRUE}}
403-
}

clang/test/Analysis/dtor-union.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-config c++-inlining=destructors -verify -std=c++11 %s
2+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-config c++-inlining=destructors -verify -std=c++17 %s
3+
4+
void clang_analyzer_eval(bool);
5+
6+
struct InlineDtor {
7+
static int cnt;
8+
static int dtorCalled;
9+
~InlineDtor() {
10+
++dtorCalled;
11+
}
12+
};
13+
14+
int InlineDtor::cnt = 0;
15+
int InlineDtor::dtorCalled = 0;
16+
17+
void testUnionDtor() {
18+
static int unionDtorCalled;
19+
InlineDtor::cnt = 0;
20+
InlineDtor::dtorCalled = 0;
21+
unionDtorCalled = 0;
22+
{
23+
union UnionDtor {
24+
InlineDtor kind1;
25+
char kind2;
26+
~UnionDtor() { unionDtorCalled++; }
27+
};
28+
UnionDtor u1{.kind1{}};
29+
UnionDtor u2{.kind2{}};
30+
auto u3 = new UnionDtor{.kind1{}};
31+
auto u4 = new UnionDtor{.kind2{}};
32+
delete u3;
33+
delete u4;
34+
}
35+
36+
clang_analyzer_eval(unionDtorCalled == 4); // expected-warning {{TRUE}}
37+
clang_analyzer_eval(InlineDtor::dtorCalled == 0); // expected-warning {{TRUE}}
38+
}

0 commit comments

Comments
 (0)