[Analyzer][StreamChecker] Use BugType::SuppressOnSink at resource leak report.
authorBalázs Kéri <1.int32@gmail.com>
Thu, 23 Jul 2020 08:53:16 +0000 (10:53 +0200)
committerBalázs Kéri <1.int32@gmail.com>
Thu, 23 Jul 2020 09:53:25 +0000 (11:53 +0200)
Summary:
Use the built-in functionality BugType::SuppressOnSink
instead of a manual solution in StreamChecker.

Differential Revision: https://reviews.llvm.org/D83120

clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
clang/test/Analysis/stream.c

index 1e96324..6b176b3 100644 (file)
@@ -204,7 +204,8 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
   BugType BT_IllegalWhence{this, "Illegal whence argument",
                            "Stream handling error"};
   BugType BT_StreamEof{this, "Stream already in EOF", "Stream handling error"};
-  BugType BT_ResourceLeak{this, "Resource leak", "Stream handling error"};
+  BugType BT_ResourceLeak{this, "Resource leak", "Stream handling error",
+                          /*SuppressOnSink =*/true};
 
 public:
   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
@@ -965,11 +966,6 @@ void StreamChecker::reportFEofWarning(CheckerContext &C,
 ExplodedNode *
 StreamChecker::reportLeaks(const SmallVector<SymbolRef, 2> &LeakedSyms,
                            CheckerContext &C, ExplodedNode *Pred) const {
-  // Do not warn for non-closed stream at program exit.
-  // FIXME: Use BugType::SuppressOnSink instead.
-  if (Pred && Pred->getCFGBlock() && Pred->getCFGBlock()->hasNoReturnElement())
-    return Pred;
-
   ExplodedNode *Err = C.generateNonFatalErrorNode(C.getState(), Pred);
   if (!Err)
     return Pred;
index cd5d28a..c57f315 100644 (file)
@@ -261,7 +261,9 @@ void check_leak_noreturn_2() {
   if (!F1)
     return;
   if (Test == 1) {
-    return; // expected-warning {{Opened stream never closed. Potential resource leak}}
+    return; // no warning
   }
   rewind(F1);
-} // no warning
+} // expected-warning {{Opened stream never closed. Potential resource leak}}
+// FIXME: This warning should be placed at the `return` above.
+// See https://reviews.llvm.org/D83120 about details.