[clang-cl] Ignore /Wv and /Wv:17 flags
[llvm-project.git] / clang / test / Analysis / std-c-library-functions.c
bloba4032298734d0908e0d9f4bca9ce9a45f0dfecdf
1 // RUN: %clang_analyze_cc1 %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
4 // RUN: -analyzer-checker=debug.ExprInspection \
5 // RUN: -analyzer-config eagerly-assume=false \
6 // RUN: -triple i686-unknown-linux \
7 // RUN: -verify
9 // RUN: %clang_analyze_cc1 %s \
10 // RUN: -analyzer-checker=core \
11 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
12 // RUN: -analyzer-checker=debug.ExprInspection \
13 // RUN: -analyzer-config eagerly-assume=false \
14 // RUN: -triple x86_64-unknown-linux \
15 // RUN: -verify
17 // RUN: %clang_analyze_cc1 %s \
18 // RUN: -analyzer-checker=core \
19 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
20 // RUN: -analyzer-checker=debug.ExprInspection \
21 // RUN: -analyzer-config eagerly-assume=false \
22 // RUN: -triple armv7-a15-linux \
23 // RUN: -verify
25 // RUN: %clang_analyze_cc1 %s \
26 // RUN: -analyzer-checker=core \
27 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
28 // RUN: -analyzer-checker=debug.ExprInspection \
29 // RUN: -analyzer-config eagerly-assume=false \
30 // RUN: -triple thumbv7-a15-linux \
31 // RUN: -verify
33 // RUN: %clang_analyze_cc1 %s \
34 // RUN: -analyzer-checker=core \
35 // RUN: -analyzer-checker=apiModeling.StdCLibraryFunctions \
36 // RUN: -analyzer-config apiModeling.StdCLibraryFunctions:DisplayLoadedSummaries=true \
37 // RUN: -analyzer-checker=debug.ExprInspection \
38 // RUN: -analyzer-config eagerly-assume=false \
39 // RUN: -triple i686-unknown-linux 2>&1 | FileCheck %s
41 // CHECK: Loaded summary for: int isalnum(int)
42 // CHECK-NEXT: Loaded summary for: int isalpha(int)
43 // CHECK-NEXT: Loaded summary for: int isascii(int)
44 // CHECK-NEXT: Loaded summary for: int isblank(int)
45 // CHECK-NEXT: Loaded summary for: int isdigit(int)
46 // CHECK-NEXT: Loaded summary for: int isgraph(int)
47 // CHECK-NEXT: Loaded summary for: int islower(int)
48 // CHECK-NEXT: Loaded summary for: int isprint(int)
49 // CHECK-NEXT: Loaded summary for: int ispunct(int)
50 // CHECK-NEXT: Loaded summary for: int isspace(int)
51 // CHECK-NEXT: Loaded summary for: int isupper(int)
52 // CHECK-NEXT: Loaded summary for: int isxdigit(int)
53 // CHECK-NEXT: Loaded summary for: int getc(FILE *)
54 // CHECK-NEXT: Loaded summary for: int fgetc(FILE *)
55 // CHECK-NEXT: Loaded summary for: int getchar(void)
56 // CHECK-NEXT: Loaded summary for: unsigned int fread(void *restrict, size_t, size_t, FILE *restrict)
57 // CHECK-NEXT: Loaded summary for: unsigned int fwrite(const void *restrict, size_t, size_t, FILE *restrict)
58 // CHECK-NEXT: Loaded summary for: ssize_t read(int, void *, size_t)
59 // CHECK-NEXT: Loaded summary for: ssize_t write(int, const void *, size_t)
60 // CHECK-NEXT: Loaded summary for: ssize_t getline(char **restrict, size_t *restrict, FILE *restrict)
61 // CHECK-NEXT: Loaded summary for: ssize_t getdelim(char **restrict, size_t *restrict, int, FILE *restrict)
64 void clang_analyzer_eval(int);
66 int glob;
68 typedef struct FILE FILE;
69 #define EOF -1
71 int getc(FILE *);
72 void test_getc(FILE *fp) {
73 int x;
74 while ((x = getc(fp)) != EOF) {
75 clang_analyzer_eval(x > 255); // expected-warning{{FALSE}}
76 clang_analyzer_eval(x >= 0); // expected-warning{{TRUE}}
80 int fgetc(FILE *);
81 void test_fgets(FILE *fp) {
82 clang_analyzer_eval(fgetc(fp) < 256); // expected-warning{{TRUE}}
83 clang_analyzer_eval(fgetc(fp) >= 0); // expected-warning{{UNKNOWN}}
87 typedef typeof(sizeof(int)) size_t;
88 typedef signed long ssize_t;
89 ssize_t read(int, void *, size_t);
90 ssize_t write(int, const void *, size_t);
91 void test_read_write(int fd, char *buf) {
92 glob = 1;
93 ssize_t x = write(fd, buf, 10);
94 clang_analyzer_eval(glob); // expected-warning{{UNKNOWN}}
95 if (x >= 0) {
96 clang_analyzer_eval(x <= 10); // expected-warning{{TRUE}}
97 ssize_t y = read(fd, &glob, sizeof(glob));
98 if (y >= 0) {
99 clang_analyzer_eval(y <= sizeof(glob)); // expected-warning{{TRUE}}
100 } else {
101 // -1 overflows on promotion!
102 clang_analyzer_eval(y <= sizeof(glob)); // expected-warning{{FALSE}}
104 } else {
105 clang_analyzer_eval(x == -1); // expected-warning{{TRUE}}
109 size_t fread(void *restrict, size_t, size_t, FILE *restrict);
110 size_t fwrite(const void *restrict, size_t, size_t, FILE *restrict);
111 void test_fread_fwrite(FILE *fp, int *buf) {
113 size_t x = fwrite(buf, sizeof(int), 10, fp);
114 clang_analyzer_eval(x <= 10); // expected-warning{{TRUE}}
116 size_t y = fread(buf, sizeof(int), 10, fp);
117 clang_analyzer_eval(y <= 10); // expected-warning{{TRUE}}
119 size_t z = fwrite(buf, sizeof(int), y, fp);
120 clang_analyzer_eval(z <= y); // expected-warning{{TRUE}}
123 void test_fread_uninitialized(void) {
124 void *ptr;
125 size_t sz;
126 size_t nmem;
127 FILE *fp;
128 (void)fread(ptr, sz, nmem, fp); // expected-warning {{1st function call argument is an uninitialized value}}
131 ssize_t getline(char **restrict, size_t *restrict, FILE *restrict);
132 ssize_t getdelim(char **restrict, size_t *restrict, int, FILE *restrict);
133 void test_getline(FILE *fp) {
134 char *line = 0;
135 size_t n = 0;
136 ssize_t len;
137 while ((len = getline(&line, &n, fp)) != -1) {
138 clang_analyzer_eval(len == 0); // expected-warning{{FALSE}}
142 int isascii(int);
143 void test_isascii(int x) {
144 clang_analyzer_eval(isascii(123)); // expected-warning{{TRUE}}
145 clang_analyzer_eval(isascii(-1)); // expected-warning{{FALSE}}
146 if (isascii(x)) {
147 clang_analyzer_eval(x < 128); // expected-warning{{TRUE}}
148 clang_analyzer_eval(x >= 0); // expected-warning{{TRUE}}
149 } else {
150 if (x > 42)
151 clang_analyzer_eval(x >= 128); // expected-warning{{TRUE}}
152 else
153 clang_analyzer_eval(x < 0); // expected-warning{{TRUE}}
155 glob = 1;
156 isascii('a');
157 clang_analyzer_eval(glob); // expected-warning{{TRUE}}
160 int islower(int);
161 void test_islower(int x) {
162 clang_analyzer_eval(islower('x')); // expected-warning{{TRUE}}
163 clang_analyzer_eval(islower('X')); // expected-warning{{FALSE}}
164 if (islower(x))
165 clang_analyzer_eval(x < 'a'); // expected-warning{{FALSE}}
168 int getchar(void);
169 void test_getchar(void) {
170 int x = getchar();
171 if (x == EOF)
172 return;
173 clang_analyzer_eval(x < 0); // expected-warning{{FALSE}}
174 clang_analyzer_eval(x < 256); // expected-warning{{TRUE}}
177 int isalpha(int);
178 void test_isalpha(void) {
179 clang_analyzer_eval(isalpha(']')); // expected-warning{{FALSE}}
180 clang_analyzer_eval(isalpha('Q')); // expected-warning{{TRUE}}
181 clang_analyzer_eval(isalpha(128)); // expected-warning{{UNKNOWN}}
184 int isalnum(int);
185 void test_alnum(void) {
186 clang_analyzer_eval(isalnum('1')); // expected-warning{{TRUE}}
187 clang_analyzer_eval(isalnum(')')); // expected-warning{{FALSE}}
190 int isblank(int);
191 void test_isblank(void) {
192 clang_analyzer_eval(isblank('\t')); // expected-warning{{TRUE}}
193 clang_analyzer_eval(isblank(' ')); // expected-warning{{TRUE}}
194 clang_analyzer_eval(isblank('\n')); // expected-warning{{FALSE}}
197 int ispunct(int);
198 void test_ispunct(int x) {
199 clang_analyzer_eval(ispunct(' ')); // expected-warning{{FALSE}}
200 clang_analyzer_eval(ispunct(-1)); // expected-warning{{FALSE}}
201 clang_analyzer_eval(ispunct('#')); // expected-warning{{TRUE}}
202 clang_analyzer_eval(ispunct('_')); // expected-warning{{TRUE}}
203 if (ispunct(x))
204 clang_analyzer_eval(x < 127); // expected-warning{{TRUE}}
207 int isupper(int);
208 void test_isupper(int x) {
209 if (isupper(x))
210 clang_analyzer_eval(x < 'A'); // expected-warning{{FALSE}}
213 int isgraph(int);
214 int isprint(int);
215 void test_isgraph_isprint(int x) {
216 char y = x;
217 if (isgraph(y))
218 clang_analyzer_eval(isprint(x)); // expected-warning{{TRUE}}
221 int isdigit(int);
222 void test_mixed_branches(int x) {
223 if (isdigit(x)) {
224 clang_analyzer_eval(isgraph(x)); // expected-warning{{TRUE}}
225 clang_analyzer_eval(isblank(x)); // expected-warning{{FALSE}}
226 } else if (isascii(x)) {
227 // isalnum() bifurcates here.
228 clang_analyzer_eval(isalnum(x)); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
229 clang_analyzer_eval(isprint(x)); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
233 int isspace(int);
234 void test_isspace(int x) {
235 if (!isascii(x))
236 return;
237 char y = x;
238 if (y == ' ')
239 clang_analyzer_eval(isspace(x)); // expected-warning{{TRUE}}
242 int isxdigit(int);
243 void test_isxdigit(int x) {
244 if (isxdigit(x) && isupper(x)) {
245 clang_analyzer_eval(x >= 'A'); // expected-warning{{TRUE}}
246 clang_analyzer_eval(x <= 'F'); // expected-warning{{TRUE}}
250 void test_call_by_pointer(void) {
251 typedef int (*func)(int);
252 func f = isascii;
253 clang_analyzer_eval(f('A')); // expected-warning{{TRUE}}
254 f = ispunct;
255 clang_analyzer_eval(f('A')); // expected-warning{{FALSE}}
258 char *getenv(const char *name);
259 void test_getenv(void) {
260 // getenv() bifurcates here.
261 clang_analyzer_eval(getenv("FOO") == 0);
262 // expected-warning@-1 {{TRUE}}
263 // expected-warning@-2 {{FALSE}}