[Clang/AMDGPU] Zero sized arrays not allowed in HIP device code. (#113470)
[llvm-project.git] / clang / test / Analysis / undefined-new-element.cpp
blob87d815501a6af6b85f68f85fc486468ca4a8921c
1 // RUN: %clang_analyze_cc1 %s -analyzer-checker=core.uninitialized.NewArraySize -analyzer-output=text -verify
3 #include "Inputs/system-header-simulator-cxx.h"
5 void checkUndefinedElmenetCountValue() {
6 int n;
7 // expected-note@-1{{'n' declared without an initial value}}
9 int *arr = new int[n]; // expected-warning{{Element count in new[] is a garbage value}}
10 // expected-note@-1{{Element count in new[] is a garbage value}}
13 void checkUndefinedElmenetCountMultiDimensionalValue() {
14 int n;
15 // expected-note@-1{{'n' declared without an initial value}}
17 auto *arr = new int[n][5]; // expected-warning{{Element count in new[] is a garbage value}}
18 // expected-note@-1{{Element count in new[] is a garbage value}}
21 void checkUndefinedElmenetCountReference() {
22 int n;
23 // expected-note@-1{{'n' declared without an initial value}}
25 int &ref = n;
26 // expected-note@-1{{'ref' initialized here}}
28 int *arr = new int[ref]; // expected-warning{{Element count in new[] is a garbage value}}
29 // expected-note@-1{{Element count in new[] is a garbage value}}
32 void checkUndefinedElmenetCountMultiDimensionalReference() {
33 int n;
34 // expected-note@-1{{'n' declared without an initial value}}
36 int &ref = n;
37 // expected-note@-1{{'ref' initialized here}}
39 auto *arr = new int[ref][5]; // expected-warning{{Element count in new[] is a garbage value}}
40 // expected-note@-1{{Element count in new[] is a garbage value}}
43 int foo() {
44 int n;
46 return n;
49 void checkUndefinedElmenetCountFunction() {
50 int *arr = new int[foo()]; // expected-warning{{Element count in new[] is a garbage value}}
51 // expected-note@-1{{Element count in new[] is a garbage value}}
54 void checkUndefinedElmenetCountMultiDimensionalFunction() {
55 auto *arr = new int[foo()][5]; // expected-warning{{Element count in new[] is a garbage value}}
56 // expected-note@-1{{Element count in new[] is a garbage value}}
59 void *malloc(size_t);
61 void checkUndefinedPlacementElementCount() {
62 int n;
63 // expected-note@-1{{'n' declared without an initial value}}
65 void *buffer = malloc(sizeof(std::string) * 10);
66 std::string *p =
67 ::new (buffer) std::string[n]; // expected-warning{{Element count in new[] is a garbage value}}
68 // expected-note@-1{{Element count in new[] is a garbage value}}