================
@@ -0,0 +1,106 @@
+//===- unittests/StaticAnalyzer/CheckLifetimeEndTest.cpp 
------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "CheckerRegistration.h"
+#include "Reusables.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
+#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
+#include "clang/StaticAnalyzer/Core/Checker.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
+#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
+#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
+#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
+#include "llvm/Config/llvm-config.h"
+#include "gtest/gtest.h"
+
+namespace {
+using namespace clang;
+using namespace ento;
+
+class LifetimeEndReporter : public Checker<check::LifetimeEnd> {
+  const BugType LifetimeEndNode{this, "LifetimeEndReporter"};
+
+  bool report(CheckerContext &C, Twine Description) const {
+    ExplodedNode *Node = C.generateNonFatalErrorNode(C.getState());
+    if (!Node)
+      return false;
+
+    auto Report = std::make_unique<PathSensitiveBugReport>(
+        LifetimeEndNode, Description.str(), Node);
+    C.emitReport(std::move(Report));
+    return true;
+  }
+
+public:
+  void checkLifetimeEnd(const VarDecl *D, CheckerContext &C) const {
+    auto II = D->getIdentifier();
+    ASSERT_TRUE(II != nullptr);
----------------
necto wrote:

This checker is only ever exercised in this test (specifically the 3 test cases 
defined with `TEST` in this file).
In these cases `D->getIdentifier()` never returns null, which is confirmed by 
`ASSERT_TRUE` you highlighted.
I cannot tell of the top of my head if it can ever return null.
However, it makes no difference, since it does not need to work for all cases, 
only for the code snippets in these 3 cases.

The point of this test is to show that the callback is being triggered at the 
end of a variable lifetime. 

It might be interesting to exercise the case when `D->getIdentifier()` returns 
a null pointer, but it is not the point of this PR, since it is not specific to 
LifetimeEnd nodes.

You are welcome to propose such a test case on top of this patch, though (maybe 
with C++ structured bindind?). If you add it to the `CheckLifetimeEnd` suite 
with this `LifetimeEndReporter` checker, you'd need to extend how it handles 
null pointers.

https://github.com/llvm/llvm-project/pull/201123
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to