Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / std-c-library-functions.c
blobb7eb6b284460e5b913733abe9fafac19f39292bf
1 // RUN: %clang_analyze_cc1 %s \
2 // RUN: -analyzer-checker=core \
3 // RUN: -analyzer-checker=unix.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=unix.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=unix.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=unix.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=unix.StdCLibraryFunctions \
36 // RUN: -analyzer-config unix.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 toupper(int)
54 // CHECK-NEXT: Loaded summary for: int tolower(int)
55 // CHECK-NEXT: Loaded summary for: int toascii(int)
56 // CHECK-NEXT: Loaded summary for: int getc(FILE *)
57 // CHECK-NEXT: Loaded summary for: int fgetc(FILE *)
58 // CHECK-NEXT: Loaded summary for: int getchar(void)
59 // CHECK-NEXT: Loaded summary for: unsigned int fread(void *restrict, size_t, size_t, FILE *restrict)
60 // CHECK-NEXT: Loaded summary for: unsigned int fwrite(const void *restrict, size_t, size_t, FILE *restrict)
61 // CHECK-NEXT: Loaded summary for: ssize_t read(int, void *, size_t)
62 // CHECK-NEXT: Loaded summary for: ssize_t write(int, const void *, size_t)
63 // CHECK-NEXT: Loaded summary for: ssize_t getline(char **restrict, size_t *restrict, FILE *restrict)
64 // CHECK-NEXT: Loaded summary for: ssize_t getdelim(char **restrict, size_t *restrict, int, FILE *restrict)
65 // CHECK-NEXT: Loaded summary for: char *getenv(const char *)
67 #include "Inputs/std-c-library-functions.h"
69 void clang_analyzer_eval(int);
71 int glob;
73 void test_getc(FILE *fp) {
74 int x;
75 while ((x = getc(fp)) != EOF) {
76 clang_analyzer_eval(x > 255); // expected-warning{{FALSE}}
77 clang_analyzer_eval(x >= 0); // expected-warning{{TRUE}}
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}}
86 void test_read_write(int fd, char *buf) {
87 glob = 1;
88 ssize_t x = write(fd, buf, 10);
89 clang_analyzer_eval(glob); // expected-warning{{UNKNOWN}}
90 if (x >= 0) {
91 clang_analyzer_eval(x <= 10); // expected-warning{{TRUE}}
92 ssize_t y = read(fd, &glob, sizeof(glob));
93 if (y >= 0) {
94 clang_analyzer_eval(y <= sizeof(glob)); // expected-warning{{TRUE}}
95 } else {
96 // -1 overflows on promotion!
97 clang_analyzer_eval(y <= sizeof(glob)); // expected-warning{{FALSE}}
99 } else {
100 clang_analyzer_eval(x == -1); // expected-warning{{TRUE}}
104 void test_fread_fwrite(FILE *fp, int *buf) {
106 size_t x = fwrite(buf, sizeof(int), 10, fp);
107 clang_analyzer_eval(x <= 10); // expected-warning{{TRUE}}
109 size_t y = fread(buf, sizeof(int), 10, fp);
110 clang_analyzer_eval(y <= 10); // expected-warning{{TRUE}}
112 size_t z = fwrite(buf, sizeof(int), y, fp);
113 clang_analyzer_eval(z <= y); // expected-warning{{TRUE}}
116 void test_fread_uninitialized(void) {
117 void *ptr;
118 size_t sz;
119 size_t nmem;
120 FILE *fp;
121 (void)fread(ptr, sz, nmem, fp); // expected-warning {{1st function call argument is an uninitialized value}}
124 void test_getline(FILE *fp) {
125 char *line = 0;
126 size_t n = 0;
127 ssize_t len;
128 while ((len = getline(&line, &n, fp)) != -1) {
129 clang_analyzer_eval(len == 0); // expected-warning{{FALSE}}
133 void test_isascii(int x) {
134 clang_analyzer_eval(isascii(123)); // expected-warning{{TRUE}}
135 clang_analyzer_eval(isascii(-1)); // expected-warning{{FALSE}}
136 if (isascii(x)) {
137 clang_analyzer_eval(x < 128); // expected-warning{{TRUE}}
138 clang_analyzer_eval(x >= 0); // expected-warning{{TRUE}}
139 } else {
140 if (x > 42)
141 clang_analyzer_eval(x >= 128); // expected-warning{{TRUE}}
142 else
143 clang_analyzer_eval(x < 0); // expected-warning{{TRUE}}
145 glob = 1;
146 isascii('a');
147 clang_analyzer_eval(glob); // expected-warning{{TRUE}}
150 void test_islower(int x) {
151 clang_analyzer_eval(islower('x')); // expected-warning{{TRUE}}
152 clang_analyzer_eval(islower('X')); // expected-warning{{FALSE}}
153 if (islower(x))
154 clang_analyzer_eval(x < 'a'); // expected-warning{{FALSE}}
157 void test_getchar(void) {
158 int x = getchar();
159 if (x == EOF)
160 return;
161 clang_analyzer_eval(x < 0); // expected-warning{{FALSE}}
162 clang_analyzer_eval(x < 256); // expected-warning{{TRUE}}
165 void test_isalpha(void) {
166 clang_analyzer_eval(isalpha(']')); // expected-warning{{FALSE}}
167 clang_analyzer_eval(isalpha('Q')); // expected-warning{{TRUE}}
168 clang_analyzer_eval(isalpha(128)); // expected-warning{{UNKNOWN}}
171 void test_alnum(void) {
172 clang_analyzer_eval(isalnum('1')); // expected-warning{{TRUE}}
173 clang_analyzer_eval(isalnum(')')); // expected-warning{{FALSE}}
176 void test_isblank(void) {
177 clang_analyzer_eval(isblank('\t')); // expected-warning{{TRUE}}
178 clang_analyzer_eval(isblank(' ')); // expected-warning{{TRUE}}
179 clang_analyzer_eval(isblank('\n')); // expected-warning{{FALSE}}
182 void test_ispunct(int x) {
183 clang_analyzer_eval(ispunct(' ')); // expected-warning{{FALSE}}
184 clang_analyzer_eval(ispunct(-1)); // expected-warning{{FALSE}}
185 clang_analyzer_eval(ispunct('#')); // expected-warning{{TRUE}}
186 clang_analyzer_eval(ispunct('_')); // expected-warning{{TRUE}}
187 if (ispunct(x))
188 clang_analyzer_eval(x < 127); // expected-warning{{TRUE}}
191 void test_isupper(int x) {
192 if (isupper(x))
193 clang_analyzer_eval(x < 'A'); // expected-warning{{FALSE}}
196 void test_isgraph_isprint(int x) {
197 char y = x;
198 if (isgraph(y))
199 clang_analyzer_eval(isprint(x)); // expected-warning{{TRUE}}
202 void test_mixed_branches(int x) {
203 if (isdigit(x)) {
204 clang_analyzer_eval(isgraph(x)); // expected-warning{{TRUE}}
205 clang_analyzer_eval(isblank(x)); // expected-warning{{FALSE}}
206 } else if (isascii(x)) {
207 // isalnum() bifurcates here.
208 clang_analyzer_eval(isalnum(x)); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
209 clang_analyzer_eval(isprint(x)); // expected-warning{{TRUE}} // expected-warning{{FALSE}}
213 void test_isspace(int x) {
214 if (!isascii(x))
215 return;
216 char y = x;
217 if (y == ' ')
218 clang_analyzer_eval(isspace(x)); // expected-warning{{TRUE}}
221 void test_isxdigit(int x) {
222 if (isxdigit(x) && isupper(x)) {
223 clang_analyzer_eval(x >= 'A'); // expected-warning{{TRUE}}
224 clang_analyzer_eval(x <= 'F'); // expected-warning{{TRUE}}
228 void test_call_by_pointer(void) {
229 typedef int (*func)(int);
230 func f = isascii;
231 clang_analyzer_eval(f('A')); // expected-warning{{TRUE}}
232 f = ispunct;
233 clang_analyzer_eval(f('A')); // expected-warning{{FALSE}}
236 void test_getenv(void) {
237 // getenv() bifurcates here.
238 clang_analyzer_eval(getenv("FOO") == 0);
239 // expected-warning@-1 {{TRUE}}
240 // expected-warning@-2 {{FALSE}}