[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Analysis / stream-errno.c
blob42369677eaa486357686cc865ab8a5841d14d96f
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream,alpha.unix.Errno,apiModeling.StdCLibraryFunctions,debug.ExprInspection \
2 // RUN: -analyzer-config apiModeling.StdCLibraryFunctions:ModelPOSIX=true -verify %s
4 #include "Inputs/system-header-simulator.h"
5 #include "Inputs/errno_func.h"
7 extern void clang_analyzer_eval(int);
8 extern void clang_analyzer_dump(int);
9 extern void clang_analyzer_printState();
11 void check_fopen(void) {
12 FILE *F = fopen("xxx", "r");
13 if (!F) {
14 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
15 if (errno) {} // no-warning
16 return;
18 if (errno) {} // expected-warning{{An undefined value may be read from 'errno' [alpha.unix.Errno]}}
21 void check_tmpfile(void) {
22 FILE *F = tmpfile();
23 if (!F) {
24 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
25 if (errno) {} // no-warning
26 return;
28 if (errno) {} // expected-warning{{An undefined value may be read from 'errno' [alpha.unix.Errno]}}
31 void check_freopen(void) {
32 FILE *F = tmpfile();
33 if (!F)
34 return;
35 F = freopen("xxx", "w", F);
36 if (!F) {
37 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
38 if (errno) {} // no-warning
39 return;
41 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
44 void check_fclose(void) {
45 FILE *F = tmpfile();
46 if (!F)
47 return;
48 int Ret = fclose(F);
49 if (Ret == EOF) {
50 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
51 if (errno) {} // no-warning
52 return;
54 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
57 void check_fread_size0(void) {
58 char Buf[10];
59 FILE *F = tmpfile();
60 if (!F)
61 return;
62 fread(Buf, 0, 1, F);
63 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
66 void check_fread_nmemb0(void) {
67 char Buf[10];
68 FILE *F = tmpfile();
69 if (!F)
70 return;
71 fread(Buf, 1, 0, F);
72 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
75 void check_fread(void) {
76 char Buf[10];
77 FILE *F = tmpfile();
78 if (!F)
79 return;
81 int R = fread(Buf, 1, 10, F);
82 if (R < 10) {
83 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
84 if (errno) {} // no-warning
85 fclose(F);
86 return;
88 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
91 void check_fwrite_size0(void) {
92 char Buf[] = "0123456789";
93 FILE *F = tmpfile();
94 if (!F)
95 return;
96 fwrite(Buf, 0, 1, F);
97 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
100 void check_fwrite_nmemb0(void) {
101 char Buf[] = "0123456789";
102 FILE *F = tmpfile();
103 if (!F)
104 return;
105 fwrite(Buf, 1, 0, F);
106 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
109 void check_fwrite(void) {
110 char Buf[] = "0123456789";
111 FILE *F = tmpfile();
112 if (!F)
113 return;
115 int R = fwrite(Buf, 1, 10, F);
116 if (R < 10) {
117 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
118 if (errno) {} // no-warning
119 fclose(F);
120 return;
122 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
125 void check_fseek(void) {
126 FILE *F = tmpfile();
127 if (!F)
128 return;
129 int S = fseek(F, 11, SEEK_SET);
130 if (S != 0) {
131 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
132 if (errno) {} // no-warning
133 fclose(F);
134 return;
136 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}
139 void check_no_errno_change(void) {
140 FILE *F = tmpfile();
141 if (!F)
142 return;
143 errno = 1;
144 clearerr(F);
145 if (errno) {} // no-warning
146 feof(F);
147 if (errno) {} // no-warning
148 ferror(F);
149 if (errno) {} // no-warning
150 clang_analyzer_eval(errno == 1); // expected-warning{{TRUE}}
151 fclose(F);
154 void check_fgetpos(void) {
155 FILE *F = tmpfile();
156 if (!F)
157 return;
158 errno = 0;
159 fpos_t Pos;
160 int Ret = fgetpos(F, &Pos);
161 if (Ret)
162 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
163 else
164 clang_analyzer_eval(errno == 0); // expected-warning{{TRUE}}
165 if (errno) {} // no-warning
166 fclose(F);
169 void check_fsetpos(void) {
170 FILE *F = tmpfile();
171 if (!F)
172 return;
173 errno = 0;
174 fpos_t Pos;
175 int Ret = fsetpos(F, &Pos);
176 if (Ret)
177 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
178 else
179 clang_analyzer_eval(errno == 0); // expected-warning{{TRUE}}
180 if (errno) {} // no-warning
181 fclose(F);
184 void check_ftell(void) {
185 FILE *F = tmpfile();
186 if (!F)
187 return;
188 errno = 0;
189 long Ret = ftell(F);
190 if (Ret == -1) {
191 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
192 } else {
193 clang_analyzer_eval(errno == 0); // expected-warning{{TRUE}}
194 clang_analyzer_eval(Ret >= 0); // expected-warning{{TRUE}}
196 if (errno) {} // no-warning
197 fclose(F);
200 void check_rewind(void) {
201 FILE *F = tmpfile();
202 if (!F)
203 return;
204 errno = 0;
205 rewind(F);
206 clang_analyzer_eval(errno == 0);
207 // expected-warning@-1{{FALSE}}
208 // expected-warning@-2{{TRUE}}
209 fclose(F);
212 void check_fileno(void) {
213 FILE *F = tmpfile();
214 if (!F)
215 return;
216 int N = fileno(F);
217 if (N == -1) {
218 clang_analyzer_eval(errno != 0); // expected-warning{{TRUE}}
219 if (errno) {} // no-warning
220 fclose(F);
221 return;
223 if (errno) {} // expected-warning{{An undefined value may be read from 'errno'}}