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