[clang] [clang][analyzer][NFC] Add more tests of 'StreamChecker' about 'tmpfile' (PR #70540)

Ben Shi via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 31 02:13:13 PDT 2023


https://github.com/benshi001 updated https://github.com/llvm/llvm-project/pull/70540

>From 73d6ce1980fcbb1869b1be522adda6ab5af8a385 Mon Sep 17 00:00:00 2001
From: Ben Shi <bennshi at tencent.com>
Date: Tue, 31 Oct 2023 13:05:19 +0800
Subject: [PATCH] [clang][analyzer] Update CallDescription of 'tmpfile' in
 StreamChecker

---
 .../StaticAnalyzer/Checkers/StreamChecker.cpp |  2 +-
 clang/test/Analysis/stream-wild-function.c    | 22 +++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Analysis/stream-wild-function.c

diff --git a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
index 4b7103c20557cc4..34df62f073e9a8c 100644
--- a/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
@@ -241,7 +241,7 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
       {{{"fopen"}}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
       {{{"freopen"}, 3},
        {&StreamChecker::preFreopen, &StreamChecker::evalFreopen, 2}},
-      {{{"tmpfile"}}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
+      {{{"tmpfile"}, 0}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
       {{{"fclose"}, 1},
        {&StreamChecker::preDefault, &StreamChecker::evalFclose, 0}},
       {{{"fread"}, 4},
diff --git a/clang/test/Analysis/stream-wild-function.c b/clang/test/Analysis/stream-wild-function.c
new file mode 100644
index 000000000000000..6503f200ee5f67f
--- /dev/null
+++ b/clang/test/Analysis/stream-wild-function.c
@@ -0,0 +1,22 @@
+// RUN: %clang_analyze_cc1 -fno-builtin -analyzer-checker=core,alpha.unix.Stream -verify %s
+
+typedef struct _FILE FILE;
+
+// All these functions are not standard C library functions.
+// We should not report "Resource Leak" on them.
+FILE *fopen(const char *restrict path, int mode);
+FILE *tmpfile(const char *restrict path);
+FILE *freopen(const char *restrict pathname, FILE *restrict stream);
+
+void test_fopen(void) {
+  FILE *fp = fopen("file", 123);
+  // FIXME: We should not report non standard 'fopen'.
+} // expected-warning {{Opened stream never closed. Potential resource leak}}
+
+void test_tmpfile(void) {
+  FILE *fp = tmpfile("file");
+} // no warning
+
+void test_freopen(FILE *old) {
+  FILE *fp = freopen("file", old);
+} // no warning



More information about the cfe-commits mailing list