aboutsummaryrefslogtreecommitdiff
path: root/clang/test/Analysis/zero-size-non-pod-array.cpp
blob: 628be0d7896e00454fdca34624c00bad51701adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -std=c++11 -verify %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -std=c++17 -verify %s

void clang_analyzer_eval(bool);

struct S{
    static int CtorInvocationCount;
    static int DtorInvocationCount;

    S(){CtorInvocationCount++;}
    ~S(){DtorInvocationCount++;}
};

int S::CtorInvocationCount = 0;
int S::DtorInvocationCount = 0;

void zeroSizeArrayStack() {
    S::CtorInvocationCount = 0;

    S arr[0];

    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
}

void zeroSizeMultidimensionalArrayStack() {
    S::CtorInvocationCount = 0;
    S::DtorInvocationCount = 0;

    {
        S arr[2][0];
        S arr2[0][2];

        S arr3[0][2][2];
        S arr4[2][2][0];
        S arr5[2][0][2];
    }

    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
    clang_analyzer_eval(S::DtorInvocationCount == 0); //expected-warning{{TRUE}}
}

void zeroSizeArrayStackInLambda() {
    S::CtorInvocationCount = 0;
    S::DtorInvocationCount = 0;

    []{
        S arr[0];
    }();    
    
    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
    clang_analyzer_eval(S::DtorInvocationCount == 0); //expected-warning{{TRUE}}
}

void zeroSizeArrayHeap() {
    S::CtorInvocationCount = 0;
    S::DtorInvocationCount = 0;

    auto *arr = new S[0];
    delete[] arr;
    
    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
    clang_analyzer_eval(S::DtorInvocationCount == 0); //expected-warning{{TRUE}}
}

void zeroSizeMultidimensionalArrayHeap() {
    S::CtorInvocationCount = 0;
    S::DtorInvocationCount = 0;

    auto *arr = new S[2][0];
    delete[] arr;
        
    auto *arr2 = new S[0][2];
    delete[] arr2;

    auto *arr3 = new S[0][2][2];
    delete[] arr3;

    auto *arr4 = new S[2][2][0];
    delete[] arr4;

    auto *arr5 = new S[2][0][2];
    delete[] arr5;

    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
    clang_analyzer_eval(S::DtorInvocationCount == 0); //expected-warning{{TRUE}}
}

#if __cplusplus >= 201703L

void zeroSizeArrayBinding() {
    S::CtorInvocationCount = 0;

    S arr[0];

    // Note: This is an error in gcc but a warning in clang.
    // In MSVC the declaration of 'S arr[0]' is already an error
    // and it doesn't recognize this syntax as a structured binding.
    auto [] = arr; //expected-warning{{ISO C++17 does not allow a decomposition group to be empty}}

    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
}

#endif

void zeroSizeArrayLambdaCapture() {
    S::CtorInvocationCount = 0;
    S::DtorInvocationCount = 0;
        
    S arr[0];

    auto l = [arr]{};
    [arr]{}();    
    
    //FIXME: These should be TRUE. We should avoid calling the destructor 
    // of the temporary that is materialized as the lambda.
    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}} expected-warning{{FALSE}}
    clang_analyzer_eval(S::DtorInvocationCount == 0); //expected-warning{{TRUE}} expected-warning{{FALSE}}
}

// FIXME: Report a warning if the standard is at least C++17.
#if __cplusplus < 201703L
void zeroSizeArrayLambdaCaptureUndefined1() {
    S arr[0];
    int n;

    auto l = [arr, n]{
        int x = n; //expected-warning{{Assigned value is uninitialized}}
        (void) x;
    };

    l();
}
#endif

void zeroSizeArrayLambdaCaptureUndefined2() {
    S arr[0];
    int n;

    [arr, n]{
        int x = n; //expected-warning{{Assigned value is uninitialized}}
        (void) x;
    }();
}

struct Wrapper{
    S arr[0];
};

void zeroSizeArrayMember() {
    S::CtorInvocationCount = 0;
    S::DtorInvocationCount = 0;

    {
        Wrapper W;
    }

    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
    clang_analyzer_eval(S::DtorInvocationCount == 0); //expected-warning{{TRUE}}
}

void zeroSizeArrayMemberCopyMove() {
    S::CtorInvocationCount = 0;
    S::DtorInvocationCount = 0;
    
    {
        Wrapper W;
        Wrapper W2 = W;
        Wrapper W3 = (Wrapper&&) W2;
    }

    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
    clang_analyzer_eval(S::DtorInvocationCount == 0); //expected-warning{{TRUE}}
}

struct MultiWrapper{
    S arr[2][0];
};

void zeroSizeMultidimensionalArrayMember() {
    S::CtorInvocationCount = 0;
    S::DtorInvocationCount = 0;

    {
        MultiWrapper MW;
    }

    clang_analyzer_eval(S::CtorInvocationCount == 0); //expected-warning{{TRUE}}
    clang_analyzer_eval(S::DtorInvocationCount == 0); //expected-warning{{TRUE}}
}