blob: 034007813e4e4c095248c880ef0885379d658be5 [file] [log] [blame]
George Karpenkova393e682018-08-29 20:29:17 +00001// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core -analyzer-output=plist-multi-file -o %t.plist -verify -analyzer-config eagerly-assume=false %s
Hubert Tong61c848d2019-06-11 14:21:32 +00002// RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/cxx-for-range.cpp.plist -
Jordan Rosecf10ea82013-06-06 21:53:45 +00003
4extern void work();
5
6void testLoop() {
7 int z[] = {1,2};
8 for (int y : z) {
9 work();
10 work();
11 if (y == 2)
Nico Weber97c675d2021-07-09 11:39:40 -040012 *(volatile int *)0 = 1; // expected-warning {{Dereference of null pointer}}
Jordan Rosecf10ea82013-06-06 21:53:45 +000013 work();
14 work();
15 (void)y;
16 }
17
Nico Weber97c675d2021-07-09 11:39:40 -040018 *(volatile int *)0 = 1; // no-warning
Jordan Rosecf10ea82013-06-06 21:53:45 +000019}
20
21class MagicVector {
22public:
23 MagicVector();
24
25 using iterator = int *;
26
27 iterator begin() const;
28 iterator end() const;
29};
30
31MagicVector get(bool fail = false) {
32 if (fail)
Nico Weber97c675d2021-07-09 11:39:40 -040033 *(volatile int *)0 = 1; // expected-warning {{Dereference of null pointer}}
Jordan Rosecf10ea82013-06-06 21:53:45 +000034 return MagicVector{};
35}
36
37void testLoopOpaqueCollection() {
38 for (int y : get()) {
39 work();
40 work();
41 if (y == 2)
Nico Weber97c675d2021-07-09 11:39:40 -040042 *(volatile int *)0 = 1; // expected-warning {{Dereference of null pointer}}
Jordan Rosecf10ea82013-06-06 21:53:45 +000043 work();
44 work();
45 (void)y;
46 }
47
Nico Weber97c675d2021-07-09 11:39:40 -040048 *(volatile int *)0 = 1; // expected-warning {{Dereference of null pointer}}
Jordan Rosecf10ea82013-06-06 21:53:45 +000049}
50
51
52class MagicVector2 {
53public:
54 MagicVector2();
55
56 class iterator {
57 public:
58 int operator*() const;
59 iterator &operator++();
60 bool operator==(const iterator &);
61 bool operator!=(const iterator &);
62 };
63
64 iterator begin() const;
65 iterator end() const;
66};
67
68MagicVector2 get2() {
69 return MagicVector2{};
70}
71
72void testLoopOpaqueIterator() {
73 for (int y : get2()) {
74 work();
75 work();
76 if (y == 2)
Nico Weber97c675d2021-07-09 11:39:40 -040077 *(volatile int *)0 = 1; // expected-warning {{Dereference of null pointer}}
Jordan Rosecf10ea82013-06-06 21:53:45 +000078 work();
79 work();
80 (void)y;
81 }
82
Nico Weber97c675d2021-07-09 11:39:40 -040083 *(volatile int *)0 = 1; // expected-warning {{Dereference of null pointer}}
Jordan Rosecf10ea82013-06-06 21:53:45 +000084}
85
86
87void testLoopErrorInRange() {
88 for (int y : get(true)) { // error inside get()
89 work();
90 work();
91 if (y == 2)
Nico Weber97c675d2021-07-09 11:39:40 -040092 *(volatile int *)0 = 1; // no-warning
Jordan Rosecf10ea82013-06-06 21:53:45 +000093 work();
94 work();
95 (void)y;
96 }
97
Nico Weber97c675d2021-07-09 11:39:40 -040098 *(volatile int *)0 = 1; // no-warning
Jordan Rosecf10ea82013-06-06 21:53:45 +000099}
Richard Smith8baa5002018-09-28 18:44:09 +0000100
101void testForRangeInit() {
102 for (int *arr[3] = {nullptr, nullptr, nullptr}; int *p : arr) // expected-warning {{extension}}
103 *p = 1; // expected-warning {{Dereference of null pointer}}
104}