Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / stream-note.c
blobb9fdc16b19e55e21f5c4bacd41f918bb2e90516e
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream -analyzer-output text \
2 // RUN: -verify %s
3 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.unix.Stream,unix.StdCLibraryFunctions -analyzer-output text \
4 // RUN: -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true -verify=expected,stdargs %s
6 #include "Inputs/system-header-simulator.h"
8 void check_note_at_correct_open(void) {
9 FILE *F1 = tmpfile(); // expected-note {{Stream opened here}}
10 // stdargs-note@-1 {{'tmpfile' is successful}}
11 if (!F1)
12 // expected-note@-1 {{'F1' is non-null}}
13 // expected-note@-2 {{Taking false branch}}
14 return;
15 FILE *F2 = tmpfile();
16 if (!F2) {
17 // expected-note@-1 {{'F2' is non-null}}
18 // expected-note@-2 {{Taking false branch}}
19 fclose(F1);
20 return;
22 rewind(F2);
23 fclose(F2);
24 rewind(F1);
26 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
27 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
29 void check_note_fopen(void) {
30 FILE *F = fopen("file", "r"); // expected-note {{Stream opened here}}
31 // stdargs-note@-1 {{'fopen' is successful}}
32 if (!F)
33 // expected-note@-1 {{'F' is non-null}}
34 // expected-note@-2 {{Taking false branch}}
35 return;
37 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
38 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
40 void check_note_freopen(void) {
41 FILE *F = fopen("file", "r"); // expected-note {{Stream opened here}}
42 // stdargs-note@-1 {{'fopen' is successful}}
43 if (!F)
44 // expected-note@-1 {{'F' is non-null}}
45 // expected-note@-2 {{Taking false branch}}
46 return;
47 F = freopen(0, "w", F); // expected-note {{Stream reopened here}}
48 // stdargs-note@-1 {{'freopen' is successful}}
49 if (!F)
50 // expected-note@-1 {{'F' is non-null}}
51 // expected-note@-2 {{Taking false branch}}
52 return;
54 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
55 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
57 void check_note_leak_2(int c) {
58 FILE *F1 = fopen("foo1.c", "r"); // expected-note {{Stream opened here}}
59 // stdargs-note@-1 {{'fopen' is successful}}
60 if (!F1)
61 // expected-note@-1 {{'F1' is non-null}}
62 // expected-note@-2 {{Taking false branch}}
63 // expected-note@-3 {{'F1' is non-null}}
64 // expected-note@-4 {{Taking false branch}}
65 return;
66 FILE *F2 = fopen("foo2.c", "r"); // expected-note {{Stream opened here}}
67 // stdargs-note@-1 {{'fopen' is successful}}
68 if (!F2) {
69 // expected-note@-1 {{'F2' is non-null}}
70 // expected-note@-2 {{Taking false branch}}
71 // expected-note@-3 {{'F2' is non-null}}
72 // expected-note@-4 {{Taking false branch}}
73 fclose(F1);
74 return;
76 if (c)
77 // expected-note@-1 {{Assuming 'c' is not equal to 0}}
78 // expected-note@-2 {{Taking true branch}}
79 // expected-note@-3 {{Assuming 'c' is not equal to 0}}
80 // expected-note@-4 {{Taking true branch}}
81 return;
82 // expected-warning@-1 {{Opened stream never closed. Potential resource leak}}
83 // expected-note@-2 {{Opened stream never closed. Potential resource leak}}
84 // expected-warning@-3 {{Opened stream never closed. Potential resource leak}}
85 // expected-note@-4 {{Opened stream never closed. Potential resource leak}}
86 fclose(F1);
87 fclose(F2);
90 void check_track_null(void) {
91 FILE *F;
92 F = fopen("foo1.c", "r"); // expected-note {{Value assigned to 'F'}} expected-note {{Assuming pointer value is null}}
93 // stdargs-note@-1 {{'fopen' fails}}
94 if (F != NULL) { // expected-note {{Taking false branch}} expected-note {{'F' is equal to NULL}}
95 fclose(F);
96 return;
98 fclose(F); // expected-warning {{Stream pointer might be NULL}}
99 // expected-note@-1 {{Stream pointer might be NULL}}
102 void check_eof_notes_feof_after_feof(void) {
103 FILE *F;
104 char Buf[10];
105 F = fopen("foo1.c", "r");
106 if (F == NULL) { // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
107 return;
109 fread(Buf, 1, 1, F);
110 if (feof(F)) { // expected-note {{Taking true branch}}
111 clearerr(F);
112 fread(Buf, 1, 1, F); // expected-note {{Assuming stream reaches end-of-file here}}
113 if (feof(F)) { // expected-note {{Taking true branch}}
114 fread(Buf, 1, 1, F); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
115 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}
118 fclose(F);
121 void check_eof_notes_feof_after_no_feof(void) {
122 FILE *F;
123 char Buf[10];
124 F = fopen("foo1.c", "r");
125 if (F == NULL) { // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
126 return;
128 fread(Buf, 1, 1, F);
129 if (feof(F)) { // expected-note {{Taking false branch}}
130 fclose(F);
131 return;
132 } else if (ferror(F)) { // expected-note {{Taking false branch}}
133 fclose(F);
134 return;
136 fread(Buf, 1, 1, F); // expected-note {{Assuming stream reaches end-of-file here}}
137 if (feof(F)) { // expected-note {{Taking true branch}}
138 fread(Buf, 1, 1, F); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
139 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}
141 fclose(F);
144 void check_eof_notes_feof_or_no_error(void) {
145 FILE *F;
146 char Buf[10];
147 F = fopen("foo1.c", "r");
148 if (F == NULL) // expected-note {{Taking false branch}} expected-note {{'F' is not equal to NULL}}
149 return;
150 int RRet = fread(Buf, 1, 1, F); // expected-note {{Assuming stream reaches end-of-file here}}
151 if (ferror(F)) { // expected-note {{Taking false branch}}
152 } else {
153 fread(Buf, 1, 1, F); // expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
154 // expected-note@-1 {{Read function called when stream is in EOF state. Function has no effect}}
156 fclose(F);