1// RUN: %check_clang_tidy %s android-cloexec-fopen %t
2
3#define FILE_OPEN_RO "r"
4
5typedef int FILE;
6
7extern "C" FILE *fopen(const char *filename, const char *mode, ...);
8extern "C" FILE *open(const char *filename, const char *mode, ...);
9
10void f() {
11 fopen(filename: "filename", mode: "r");
12 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use 'fopen' mode 'e' to set O_CLOEXEC [android-cloexec-fopen]
13 // CHECK-FIXES: fopen("filename", "re");
14
15 fopen(filename: "filename", FILE_OPEN_RO);
16 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use 'fopen' mode 'e'
17 // CHECK-FIXES: fopen("filename", FILE_OPEN_RO "e");
18
19 fopen(filename: "filename", mode: "er");
20 // CHECK-MESSAGES-NOT: warning:
21 fopen(filename: "filename", mode: "re");
22 // CHECK-MESSAGES-NOT: warning:
23 fopen(filename: "filename", mode: "e");
24 // CHECK-MESSAGES-NOT: warning:
25 open(filename: "filename", mode: "e");
26 // CHECK-MESSAGES-NOT: warning:
27
28 char *str = "r";
29 fopen(filename: "filename", mode: str);
30 // CHECK-MESSAGES-NOT: warning:
31 str = "re";
32 fopen(filename: "filename", mode: str);
33 // CHECK-MESSAGES-NOT: warning:
34 char arr[2] = "r";
35 fopen(filename: "filename", mode: arr);
36 // CHECK-MESSAGES-NOT: warning:
37 char arr2[3] = "re";
38 fopen(filename: "filename", mode: arr2);
39 // CHECK-MESSAGES-NOT: warning:
40}
41
42namespace i {
43int *fopen(const char *filename, const char *mode, ...);
44void g() {
45 fopen(filename: "filename", mode: "e");
46 // CHECK-MESSAGES-NOT: warning:
47}
48} // namespace i
49
50class C {
51public:
52 int *fopen(const char *filename, const char *mode, ...);
53 void h() {
54 fopen(filename: "filename", mode: "e");
55 // CHECK-MESSAGES-NOT: warning:
56 }
57};
58

source code of clang-tools-extra/test/clang-tidy/checkers/android/cloexec-fopen.cpp