Skip to content

Commit de3329e

Browse files
committed
Fixup after rebasing: s/garbage/uninitialized/g changes, new expr list init
1 parent 6db1434 commit de3329e

File tree

1 file changed

+165
-128
lines changed

1 file changed

+165
-128
lines changed

clang/test/Analysis/list-initialization.cpp

+165-128
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// RUN: %clang_analyze_cc1 -verify %s\
1+
// RUN: %clang_analyze_cc1 -w -verify %s\
22
// RUN: -analyzer-checker=core,unix.Malloc\
33
// RUN: -analyzer-checker=debug.ExprInspection -std=c++11
4-
// RUN: %clang_analyze_cc1 -verify %s\
4+
// RUN: %clang_analyze_cc1 -w -verify %s\
55
// RUN: -analyzer-checker=core,unix.Malloc\
66
// RUN: -analyzer-checker=debug.ExprInspection -std=c++17
77

@@ -397,6 +397,151 @@ void u32_string() {
397397
clang_analyzer_eval(U'\0' == arr[2]); // expected-warning{{TRUE}}
398398
}
399399
} // namespace CXX14_char_array_single_string_clause
400+
401+
namespace CXX17_designated_clauses {
402+
struct S {
403+
int foo;
404+
int bar;
405+
};
406+
void one_designated_one_not() {
407+
S direct{1, .bar = 13};
408+
clang_analyzer_eval(1 == direct.foo); // expected-warning{{TRUE}}
409+
clang_analyzer_eval(13 == direct.bar); // expected-warning{{TRUE}}
410+
411+
S copy = {1, .bar = 13};
412+
clang_analyzer_eval(1 == copy.foo); // expected-warning{{TRUE}}
413+
clang_analyzer_eval(13 == copy.bar); // expected-warning{{TRUE}}
414+
415+
S *ptr = new S{1, .bar = 13};
416+
clang_analyzer_eval(1 == ptr->foo); // expected-warning{{TRUE}}
417+
clang_analyzer_eval(13 == ptr->bar); // expected-warning{{TRUE}}
418+
delete ptr;
419+
420+
S slot;
421+
S *place = new (&slot) S{1, .bar = 13};
422+
clang_analyzer_eval(1 == place->foo); // expected-warning{{TRUE}}
423+
clang_analyzer_eval(13 == place->bar); // expected-warning{{TRUE}}
424+
}
425+
void all_designated() {
426+
S direct{ .foo = 13, .bar = 1 };
427+
clang_analyzer_eval(13 == direct.foo); // expected-warning{{TRUE}}
428+
clang_analyzer_eval(1 == direct.bar); // expected-warning{{TRUE}}
429+
430+
S copy = { .foo = 13, .bar = 1 };
431+
clang_analyzer_eval(13 == copy.foo); // expected-warning{{TRUE}}
432+
clang_analyzer_eval(1 == copy.bar); // expected-warning{{TRUE}}
433+
434+
S *ptr = new S{ .foo = 13, .bar = 1 };
435+
clang_analyzer_eval(13 == ptr->foo); // expected-warning{{TRUE}}
436+
clang_analyzer_eval(1 == ptr->bar); // expected-warning{{TRUE}}
437+
delete ptr;
438+
439+
S slot;
440+
S *place = new (&slot) S{ .foo = 13, .bar = 1 };
441+
clang_analyzer_eval(13 == place->foo); // expected-warning{{TRUE}}
442+
clang_analyzer_eval(1 == place->bar); // expected-warning{{TRUE}}
443+
}
444+
445+
class PubClass {
446+
public:
447+
int foo;
448+
int bar;
449+
};
450+
void public_class_designated_initializers() {
451+
PubClass direct{
452+
.foo = 13,
453+
.bar = 1,
454+
};
455+
clang_analyzer_eval(13 == direct.foo); // expected-warning{{TRUE}}
456+
clang_analyzer_eval(1 == direct.bar); // expected-warning{{TRUE}}
457+
458+
PubClass copy = {
459+
.foo = 13,
460+
.bar = 1,
461+
};
462+
clang_analyzer_eval(13 == copy.foo); // expected-warning{{TRUE}}
463+
clang_analyzer_eval(1 == copy.bar); // expected-warning{{TRUE}}
464+
465+
PubClass *ptr = new PubClass{
466+
.foo = 13,
467+
.bar = 1,
468+
};
469+
clang_analyzer_eval(13 == ptr->foo); // expected-warning{{TRUE}}
470+
clang_analyzer_eval(1 == ptr->bar); // expected-warning{{TRUE}}
471+
delete ptr;
472+
473+
PubClass slot;
474+
PubClass *place = new (&slot) PubClass{
475+
.foo = 13,
476+
.bar = 1,
477+
};
478+
clang_analyzer_eval(13 == place->foo); // expected-warning{{TRUE}}
479+
clang_analyzer_eval(1 == place->bar); // expected-warning{{TRUE}}
480+
}
481+
482+
struct Three {
483+
int x;
484+
int y;
485+
int z;
486+
};
487+
void designated_initializers_with_gaps() {
488+
Three direct{
489+
.x = 13,
490+
.z = 1,
491+
};
492+
clang_analyzer_eval(13 == direct.x); // expected-warning{{TRUE}}
493+
clang_analyzer_eval(0 == direct.y); // expected-warning{{TRUE}}
494+
clang_analyzer_eval(1 == direct.z); // expected-warning{{TRUE}}
495+
496+
Three copy = {
497+
.x = 13,
498+
.z = 1,
499+
};
500+
clang_analyzer_eval(13 == copy.x); // expected-warning{{TRUE}}
501+
clang_analyzer_eval(0 == copy.y); // expected-warning{{TRUE}}
502+
clang_analyzer_eval(1 == copy.z); // expected-warning{{TRUE}}
503+
504+
Three *ptr = new Three{
505+
.x = 13,
506+
.z = 1,
507+
};
508+
clang_analyzer_eval(13 == ptr->x); // expected-warning{{TRUE}}
509+
clang_analyzer_eval(0 == ptr->y); // expected-warning{{TRUE}}
510+
clang_analyzer_eval(1 == ptr->z); // expected-warning{{TRUE}}
511+
delete ptr;
512+
513+
Three slot;
514+
Three *place = new (&slot) Three{
515+
.x = 13,
516+
.z = 1,
517+
};
518+
clang_analyzer_eval(13 == place->x); // expected-warning{{TRUE}}
519+
clang_analyzer_eval(0 == place->y); // expected-warning{{TRUE}}
520+
clang_analyzer_eval(1 == place->z); // expected-warning{{TRUE}}
521+
}
522+
523+
struct Inner {
524+
int bar;
525+
};
526+
struct Nested {
527+
int foo;
528+
Inner inner;
529+
int baz;
530+
};
531+
void nested_aggregates() {
532+
auto N1 = new Nested{.baz = 14};
533+
clang_analyzer_eval(0 == N1->foo); // expected-warning{{TRUE}}
534+
clang_analyzer_eval(0 == N1->inner.bar); // expected-warning{{TRUE}}
535+
clang_analyzer_eval(14 == N1->baz); // expected-warning{{TRUE}}
536+
537+
auto N2 = new Nested{1, {.bar = 2}, 3};
538+
clang_analyzer_eval(1 == N2->foo); // expected-warning{{TRUE}}
539+
clang_analyzer_eval(2 == N2->inner.bar); // expected-warning{{TRUE}}
540+
clang_analyzer_eval(3 == N2->baz); // expected-warning{{TRUE}}
541+
542+
escape(N1,N2);
543+
}
544+
} // namespace CXX17_designated_clauses
400545
#endif // __cplusplus >= 201703L
401546

402547
// Common across different C++ versions
@@ -477,44 +622,6 @@ void none_designated_swapped() {
477622
clang_analyzer_eval(1 == place->foo); // expected-warning{{TRUE}}
478623
clang_analyzer_eval(13 == place->bar); // expected-warning{{TRUE}}
479624
}
480-
void one_designated_one_not() {
481-
S direct{1, .bar = 13};
482-
clang_analyzer_eval(1 == direct.foo); // expected-warning{{TRUE}}
483-
clang_analyzer_eval(13 == direct.bar); // expected-warning{{TRUE}}
484-
485-
S copy = {1, .bar = 13};
486-
clang_analyzer_eval(1 == copy.foo); // expected-warning{{TRUE}}
487-
clang_analyzer_eval(13 == copy.bar); // expected-warning{{TRUE}}
488-
489-
S *ptr = new S{1, .bar = 13};
490-
clang_analyzer_eval(1 == ptr->foo); // expected-warning{{TRUE}}
491-
clang_analyzer_eval(13 == ptr->bar); // expected-warning{{TRUE}}
492-
delete ptr;
493-
494-
S slot;
495-
S *place = new (&slot) S{1, .bar = 13};
496-
clang_analyzer_eval(1 == place->foo); // expected-warning{{TRUE}}
497-
clang_analyzer_eval(13 == place->bar); // expected-warning{{TRUE}}
498-
}
499-
void all_designated() {
500-
S direct{ .foo = 13, .bar = 1 };
501-
clang_analyzer_eval(13 == direct.foo); // expected-warning{{TRUE}}
502-
clang_analyzer_eval(1 == direct.bar); // expected-warning{{TRUE}}
503-
504-
S copy = { .foo = 13, .bar = 1 };
505-
clang_analyzer_eval(13 == copy.foo); // expected-warning{{TRUE}}
506-
clang_analyzer_eval(1 == copy.bar); // expected-warning{{TRUE}}
507-
508-
S *ptr = new S{ .foo = 13, .bar = 1 };
509-
clang_analyzer_eval(13 == ptr->foo); // expected-warning{{TRUE}}
510-
clang_analyzer_eval(1 == ptr->bar); // expected-warning{{TRUE}}
511-
delete ptr;
512-
513-
S slot;
514-
S *place = new (&slot) S{ .foo = 13, .bar = 1 };
515-
clang_analyzer_eval(13 == place->foo); // expected-warning{{TRUE}}
516-
clang_analyzer_eval(1 == place->bar); // expected-warning{{TRUE}}
517-
}
518625

519626
class DefaultCtor {
520627
public:
@@ -542,43 +649,6 @@ void const_lvalue_ref_list_init() {
542649
clang_analyzer_eval(1 == copy.x); // expected-warning{{TRUE}}
543650
}
544651

545-
class PubClass {
546-
public:
547-
int foo;
548-
int bar;
549-
};
550-
void public_class_designated_initializers() {
551-
PubClass direct{
552-
.foo = 13,
553-
.bar = 1,
554-
};
555-
clang_analyzer_eval(13 == direct.foo); // expected-warning{{TRUE}}
556-
clang_analyzer_eval(1 == direct.bar); // expected-warning{{TRUE}}
557-
558-
PubClass copy = {
559-
.foo = 13,
560-
.bar = 1,
561-
};
562-
clang_analyzer_eval(13 == copy.foo); // expected-warning{{TRUE}}
563-
clang_analyzer_eval(1 == copy.bar); // expected-warning{{TRUE}}
564-
565-
PubClass *ptr = new PubClass{
566-
.foo = 13,
567-
.bar = 1,
568-
};
569-
clang_analyzer_eval(13 == ptr->foo); // expected-warning{{TRUE}}
570-
clang_analyzer_eval(1 == ptr->bar); // expected-warning{{TRUE}}
571-
delete ptr;
572-
573-
PubClass slot;
574-
PubClass *place = new (&slot) PubClass{
575-
.foo = 13,
576-
.bar = 1,
577-
};
578-
clang_analyzer_eval(13 == place->foo); // expected-warning{{TRUE}}
579-
clang_analyzer_eval(1 == place->bar); // expected-warning{{TRUE}}
580-
}
581-
582652
class NonAggregateImplicitDefaultCtor {
583653
int NeverUsed = 14;
584654
public:
@@ -726,41 +796,6 @@ void initializer_clauses_sequenced() {
726796

727797
delete ptr;
728798
}
729-
void designated_initializers_with_gaps() {
730-
Three direct{
731-
.x = 13,
732-
.z = 1,
733-
};
734-
clang_analyzer_eval(13 == direct.x); // expected-warning{{TRUE}}
735-
clang_analyzer_eval(0 == direct.y); // expected-warning{{TRUE}}
736-
clang_analyzer_eval(1 == direct.z); // expected-warning{{TRUE}}
737-
738-
Three copy = {
739-
.x = 13,
740-
.z = 1,
741-
};
742-
clang_analyzer_eval(13 == copy.x); // expected-warning{{TRUE}}
743-
clang_analyzer_eval(0 == copy.y); // expected-warning{{TRUE}}
744-
clang_analyzer_eval(1 == copy.z); // expected-warning{{TRUE}}
745-
746-
Three *ptr = new Three{
747-
.x = 13,
748-
.z = 1,
749-
};
750-
clang_analyzer_eval(13 == ptr->x); // expected-warning{{TRUE}}
751-
clang_analyzer_eval(0 == ptr->y); // expected-warning{{TRUE}}
752-
clang_analyzer_eval(1 == ptr->z); // expected-warning{{TRUE}}
753-
delete ptr;
754-
755-
Three slot;
756-
Three *place = new (&slot) Three{
757-
.x = 13,
758-
.z = 1,
759-
};
760-
clang_analyzer_eval(13 == place->x); // expected-warning{{TRUE}}
761-
clang_analyzer_eval(0 == place->y); // expected-warning{{TRUE}}
762-
clang_analyzer_eval(1 == place->z); // expected-warning{{TRUE}}
763-
}
764799

765800
// https://eel.is/c++draft/dcl.init.aggr#note-6:
766801
// Static data members, non-static data members of anonymous
@@ -860,7 +895,7 @@ void array_empty_list_init_values() {
860895
void array_empty_list_init_invalid_index_undef() {
861896
const int *ptr = glob_arr1;
862897
int idx = -42;
863-
auto x = ptr[idx]; // expected-warning{{garbage or undefined}}
898+
auto x = ptr[idx]; // expected-warning{{uninitialized}}
864899
}
865900
void array_empty_list_init_symbolic_index_unknown(int idx) {
866901
clang_analyzer_dump(glob_arr1[idx]); // expected-warning{{Unknown}}
@@ -879,12 +914,12 @@ void array_nested_empty_list_init_values() {
879914
}
880915
void array_nested_empty_list_init_invalid_idx_undef() {
881916
int idx = -42;
882-
auto x = glob_arr4[1][idx]; // expected-warning{{garbage or undefined}}
917+
auto x = glob_arr4[1][idx]; // expected-warning{{uninitialized}}
883918
}
884919
void array_nested_empty_list_init_invalid_idx_undef2() {
885920
const int *ptr = glob_arr4[1];
886921
int idx = -42;
887-
auto x = ptr[idx]; // expected-warning{{garbage or undefined}}
922+
auto x = ptr[idx]; // expected-warning{{uninitialized}}
888923
}
889924

890925
int const glob_arr2[4] = {1, 2};
@@ -898,7 +933,7 @@ void array_fewer_init_clauses_values() {
898933
void array_fewer_init_clauses_values_invalid_index() {
899934
const int *ptr = glob_arr2;
900935
int idx = 42;
901-
auto x = ptr[idx]; // expected-warning{{garbage or undefined}}
936+
auto x = ptr[idx]; // expected-warning{{uninitialized}}
902937
}
903938

904939
int const glob_arr5[4][2] = {{1}, 3, 4, 5};
@@ -920,12 +955,12 @@ void array_nested_init_list_oob_read() {
920955
}
921956
void array_nested_init_list_invalid_index() {
922957
int idx = -42;
923-
auto x = glob_arr5[1][idx]; // expected-warning{{garbage or undefined}}
958+
auto x = glob_arr5[1][idx]; // expected-warning{{uninitialized}}
924959
}
925960
void array_nested_init_list_invalid_index2() {
926961
int const *ptr = &glob_arr5[1][0];
927962
int idx = 42;
928-
auto x = ptr[idx]; // expected-warning{{garbage or undefined}}
963+
auto x = ptr[idx]; // expected-warning{{uninitialized}}
929964
}
930965

931966
char const char_string_init[5] = {"123"};
@@ -938,12 +973,12 @@ void array_char_init_with_char_string() {
938973
}
939974
void array_char_init_with_char_string_invalid_index() {
940975
int idx = -42;
941-
auto x = char_string_init[idx]; // expected-warning{{garbage or undefined}}
976+
auto x = char_string_init[idx]; // expected-warning{{uninitialized}}
942977
}
943978
void array_char_init_with_char_string_invalid_index2() {
944979
const char *ptr = char_string_init;
945980
int idx = 42;
946-
auto x = ptr[idx]; // expected-warning{{garbage or undefined}}
981+
auto x = ptr[idx]; // expected-warning{{uninitialized}}
947982
}
948983

949984
const int glob_arr9[2][4] = {{(1), 2, ((3)), 4}, 5, 6, (((7)))};
@@ -1149,10 +1184,11 @@ void union_single_initializer_clause_first_field() {
11491184
clang_analyzer_eval(1 == ptr->x); // expected-warning{{UNKNOWN}}
11501185
clang_analyzer_eval(1 == place->x); // expected-warning{{UNKNOWN}}
11511186

1152-
clang_analyzer_eval(1 + direct.y); // expected-warning{{UNDEFINED}}
1153-
clang_analyzer_eval(1 + copy.y); // expected-warning{{UNDEFINED}}
1154-
clang_analyzer_eval(1 + ptr->y); // expected-warning{{UNDEFINED}}
1155-
clang_analyzer_eval(1 + place->y); // expected-warning{{UNDEFINED}}
1187+
// FIXME: should be undefined
1188+
clang_analyzer_eval(1 + direct.y); // expected-warning{{UNKNOWN}}
1189+
clang_analyzer_eval(1 + copy.y); // expected-warning{{UNKNOWN}}
1190+
clang_analyzer_eval(1 + ptr->y); // expected-warning{{UNKNOWN}}
1191+
clang_analyzer_eval(1 + place->y); // expected-warning{{UNKNOWN}}
11561192

11571193
delete ptr;
11581194
}
@@ -1169,10 +1205,11 @@ void union_single_initializer_clause_non_first_field() {
11691205
clang_analyzer_eval(1 == ptr->y); // expected-warning{{UNKNOWN}}
11701206
clang_analyzer_eval(1 == place->y); // expected-warning{{UNKNOWN}}
11711207

1172-
clang_analyzer_eval(1 + direct.x); // expected-warning{{UNDEFINED}}
1173-
clang_analyzer_eval(1 + copy.x); // expected-warning{{UNDEFINED}}
1174-
clang_analyzer_eval(1 + ptr->x); // expected-warning{{UNDEFINED}}
1175-
clang_analyzer_eval(1 + place->x); // expected-warning{{UNDEFINED}}
1208+
// FIXME: should be undefined
1209+
clang_analyzer_eval(1 + direct.x); // expected-warning{{UNKNOWN}}
1210+
clang_analyzer_eval(1 + copy.x); // expected-warning{{UNKNOWN}}
1211+
clang_analyzer_eval(1 + ptr->x); // expected-warning{{UNKNOWN}}
1212+
clang_analyzer_eval(1 + place->x); // expected-warning{{UNKNOWN}}
11761213

11771214
delete ptr;
11781215
}

0 commit comments

Comments
 (0)