xref: /llvm-project/clang/test/SemaCXX/warn-range-loop-analysis-trivially-copyable.cpp (revision efcf192a0a5993165f837ce71250fb6df689634b)
1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wloop-analysis -verify %s
2 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -Wrange-loop-analysis -verify %s
3 
test_POD_64_bytes()4 void test_POD_64_bytes() {
5   struct Record {
6     char a[64];
7   };
8 
9   Record records[8];
10   for (const auto r : records)
11     (void)r;
12 }
13 
test_POD_65_bytes()14 void test_POD_65_bytes() {
15   struct Record {
16     char a[65];
17   };
18 
19   // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
20   // expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
21   Record records[8];
22   for (const auto r : records)
23     (void)r;
24 }
25 
test_TriviallyCopyable_64_bytes()26 void test_TriviallyCopyable_64_bytes() {
27   struct Record {
28     Record() {}
29     char a[64];
30   };
31 
32   Record records[8];
33   for (const auto r : records)
34     (void)r;
35 }
test_TriviallyCopyConstructible_64_bytes()36 void test_TriviallyCopyConstructible_64_bytes() {
37   struct Record {
38     char a[64];
39     Record& operator=(Record const& other){return *this;};
40 
41   };
42 
43   Record records[8];
44   for (const auto r : records)
45     (void)r;
46 }
47 
test_TriviallyCopyable_65_bytes()48 void test_TriviallyCopyable_65_bytes() {
49   struct Record {
50     Record() {}
51     char a[65];
52   };
53 
54   // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
55   // expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
56   Record records[8];
57   for (const auto r : records)
58     (void)r;
59 }
60 
test_TriviallyCopyConstructible_65_bytes()61 void test_TriviallyCopyConstructible_65_bytes() {
62   struct Record {
63     char a[65];
64     Record& operator=(Record const& other){return *this;};
65 
66   };
67   // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
68   // expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
69   Record records[8];
70   for (const auto r : records)
71     (void)r;
72 }
73 
test_NonTriviallyCopyable()74 void test_NonTriviallyCopyable() {
75   struct Record {
76     Record() {}
77     ~Record() {}
78     volatile int a;
79     int b;
80   };
81 
82   // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
83   // expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
84   Record records[8];
85   for (const auto r : records)
86     (void)r;
87 }
88 
test_TrivialABI_64_bytes()89 void test_TrivialABI_64_bytes() {
90   struct [[clang::trivial_abi]] Record {
91     Record() {}
92     ~Record() {}
93     char a[64];
94   };
95 
96   Record records[8];
97   for (const auto r : records)
98     (void)r;
99 }
100 
test_TrivialABI_65_bytes()101 void test_TrivialABI_65_bytes() {
102   struct [[clang::trivial_abi]] Record {
103     Record() {}
104     ~Record() {}
105     char a[65];
106   };
107 
108   // expected-warning@+3 {{loop variable 'r' creates a copy from type 'const Record'}}
109   // expected-note@+2 {{use reference type 'const Record &' to prevent copying}}
110   Record records[8];
111   for (const auto r : records)
112     (void)r;
113 }
114 
115