https://github.com/necto created
https://github.com/llvm/llvm-project/pull/201123
This patch adds handling of the `CFGLifetimeEnd` element to the CSA, and
produces a newly created callback `checkLifetimeEnd` for each occurrence of it.
It is useful to implement detection of dangling pointers as in:
```
void su_use_after_block () { int* p=0; { int x=1; p=&x; } *p = 2; }
// ^ p dangles
```
This patch does not implement the check itself. it is motivated by the
discussion in
https://discourse.llvm.org/t/what-is-the-status-of-scopeend-and-scopebegin/90861
--
upstreamed part of CPP-4539
>From 6cc76118ddb7d2c35faed407a6d8108d84e233b1 Mon Sep 17 00:00:00 2001
From: tomasz-kaminski-sonarsource
<[email protected]>
Date: Fri, 23 Jun 2023 14:17:47 +0200
Subject: [PATCH] [clang] Trigger checkLifetimeEnd callback from
CFGLifetimeEnds element
This patch adds handling of the `CFGLifetimeEnd` element to the CSA, and
produces a newly created callback `checkLifetimeEnd` for each occurrence
of it.
It is useful to implement detection of dangling pointers as in:
```
void su_use_after_block () { int* p=0; { int x=1; p=&x; } *p = 2; }
// ^ p dangles
```
This patch does not implement the check itself. it is motivated by the
discussion in
https://discourse.llvm.org/t/what-is-the-status-of-scopeend-and-scopebegin/90861
--
upstreamed from CPP-4539
---
clang/include/clang/Analysis/CFG.h | 75 ++++++++-------
clang/include/clang/Analysis/ProgramPoint.h | 80 ++++++++++------
.../clang/StaticAnalyzer/Core/Checker.h | 14 +++
.../StaticAnalyzer/Core/CheckerManager.h | 13 +++
.../Core/PathSensitive/ExprEngine.h | 1 +
clang/lib/Analysis/PathDiagnostic.cpp | 6 +-
clang/lib/Analysis/ProgramPoint.cpp | 5 +
.../StaticAnalyzer/Core/CheckerManager.cpp | 54 +++++++++--
clang/lib/StaticAnalyzer/Core/CoreEngine.cpp | 18 ++--
clang/lib/StaticAnalyzer/Core/ExprEngine.cpp | 16 ++++
clang/unittests/StaticAnalyzer/CMakeLists.txt | 1 +
.../StaticAnalyzer/CheckLifetimeEndTest.cpp | 93 +++++++++++++++++++
12 files changed, 303 insertions(+), 73 deletions(-)
create mode 100644 clang/unittests/StaticAnalyzer/CheckLifetimeEndTest.cpp
diff --git a/clang/include/clang/Analysis/CFG.h
b/clang/include/clang/Analysis/CFG.h
index 6c214d9ce10e2..57ebfa47ec90e 100644
--- a/clang/include/clang/Analysis/CFG.h
+++ b/clang/include/clang/Analysis/CFG.h
@@ -57,12 +57,15 @@ class CFGElement {
enum Kind {
// main kind
Initializer,
- ScopeBegin,
- ScopeEnd,
NewAllocator,
- LifetimeEnds,
LoopExit,
FullExprCleanup,
+ // scope marker kind
+ ScopeBegin,
+ ScopeEnd,
+ LifetimeEnds,
+ SCOPE_BEGIN = ScopeBegin,
+ SCOPE_END = LifetimeEnds,
// stmt kind
Statement,
Constructor,
@@ -290,18 +293,38 @@ class CFGLoopExit : public CFGElement {
}
};
+/// Base class for representing elements related to the lifetime of automatic
+/// objects
+class CFGScopeMarker : public CFGElement {
+public:
+ LLVM_ATTRIBUTE_RETURNS_NONNULL const Stmt *getTriggerStmt() const {
+ return static_cast<const Stmt *>(Data1.getPointer());
+ }
+
+private:
+ friend class CFGElement;
+
+ static bool isKind(const CFGElement &E) {
+ return E.getKind() >= SCOPE_BEGIN && E.getKind() <= SCOPE_END;
+ }
+
+protected:
+ CFGScopeMarker() = default;
+
+ explicit CFGScopeMarker(Kind K, const Stmt *S, const void *Ptr2 = nullptr)
+ : CFGElement(K, S, Ptr2) {
+ assert(isKind(*this));
+ }
+};
+
/// Represents the point where the lifetime of an automatic object ends
-class CFGLifetimeEnds : public CFGElement {
+class CFGLifetimeEnds : public CFGScopeMarker {
public:
explicit CFGLifetimeEnds(const VarDecl *var, const Stmt *stmt)
- : CFGElement(LifetimeEnds, var, stmt) {}
+ : CFGScopeMarker(LifetimeEnds, stmt, var) {}
const VarDecl *getVarDecl() const {
- return static_cast<const VarDecl *>(Data1.getPointer());
- }
-
- const Stmt *getTriggerStmt() const {
- return static_cast<const Stmt *>(Data2.getPointer());
+ return static_cast<const VarDecl *>(Data2.getPointer());
}
private:
@@ -349,50 +372,40 @@ class CFGFullExprCleanup : public CFGElement {
/// Represents beginning of a scope implicitly generated
/// by the compiler on encountering a CompoundStmt
-class CFGScopeBegin : public CFGElement {
+class CFGScopeBegin : public CFGScopeMarker {
public:
CFGScopeBegin() {}
CFGScopeBegin(const VarDecl *VD, const Stmt *S)
- : CFGElement(ScopeBegin, VD, S) {}
-
- // Get statement that triggered a new scope.
- const Stmt *getTriggerStmt() const {
- return static_cast<const Stmt *>(Data2.getPointer());
- }
+ : CFGScopeMarker(ScopeBegin, S, VD) {}
// Get VD that triggered a new scope.
const VarDecl *getVarDecl() const {
- return static_cast<const VarDecl *>(Data1.getPointer());
+ return static_cast<const VarDecl *>(Data2.getPointer());
}
private:
friend class CFGElement;
- static bool isKind(const CFGElement &E) {
- Kind kind = E.getKind();
- return kind == ScopeBegin;
+ static bool isKind(const CFGElement &elem) {
+ return elem.getKind() == ScopeBegin;
}
};
/// Represents end of a scope implicitly generated by
/// the compiler after the last Stmt in a CompoundStmt's body
-class CFGScopeEnd : public CFGElement {
+class CFGScopeEnd : public CFGScopeMarker {
public:
CFGScopeEnd() {}
- CFGScopeEnd(const VarDecl *VD, const Stmt *S) : CFGElement(ScopeEnd, VD, S)
{}
+ CFGScopeEnd(const VarDecl *VD, const Stmt *S)
+ : CFGScopeMarker(ScopeEnd, S, VD) {}
const VarDecl *getVarDecl() const {
- return static_cast<const VarDecl *>(Data1.getPointer());
- }
-
- const Stmt *getTriggerStmt() const {
- return static_cast<const Stmt *>(Data2.getPointer());
+ return static_cast<const VarDecl *>(Data2.getPointer());
}
private:
friend class CFGElement;
- static bool isKind(const CFGElement &E) {
- Kind kind = E.getKind();
- return kind == ScopeEnd;
+ static bool isKind(const CFGElement &elem) {
+ return elem.getKind() == ScopeEnd;
}
};
diff --git a/clang/include/clang/Analysis/ProgramPoint.h
b/clang/include/clang/Analysis/ProgramPoint.h
index b0fedea2fd8ee..51ae43a334f1f 100644
--- a/clang/include/clang/Analysis/ProgramPoint.h
+++ b/clang/include/clang/Analysis/ProgramPoint.h
@@ -59,33 +59,36 @@ class SimpleProgramPointTag : public ProgramPointTag {
class ProgramPoint {
public:
- enum Kind { BlockEdgeKind,
- BlockEntranceKind,
- BlockExitKind,
- PreStmtKind,
- PreStmtPurgeDeadSymbolsKind,
- PostStmtPurgeDeadSymbolsKind,
- PostStmtKind,
- PreLoadKind,
- PostLoadKind,
- PreStoreKind,
- PostStoreKind,
- PostConditionKind,
- PostLValueKind,
- PostAllocatorCallKind,
- MinPostStmtKind = PostStmtKind,
- MaxPostStmtKind = PostAllocatorCallKind,
- PostInitializerKind,
- CallEnterKind,
- CallExitBeginKind,
- CallExitEndKind,
- FunctionExitKind,
- PreImplicitCallKind,
- PostImplicitCallKind,
- MinImplicitCallKind = PreImplicitCallKind,
- MaxImplicitCallKind = PostImplicitCallKind,
- LoopExitKind,
- EpsilonKind};
+ enum Kind {
+ BlockEdgeKind,
+ BlockEntranceKind,
+ BlockExitKind,
+ PreStmtKind,
+ PreStmtPurgeDeadSymbolsKind,
+ PostStmtPurgeDeadSymbolsKind,
+ PostStmtKind,
+ PreLoadKind,
+ PostLoadKind,
+ PreStoreKind,
+ PostStoreKind,
+ PostConditionKind,
+ PostLValueKind,
+ PostAllocatorCallKind,
+ MinPostStmtKind = PostStmtKind,
+ MaxPostStmtKind = PostAllocatorCallKind,
+ PostInitializerKind,
+ CallEnterKind,
+ CallExitBeginKind,
+ CallExitEndKind,
+ FunctionExitKind,
+ PreImplicitCallKind,
+ PostImplicitCallKind,
+ MinImplicitCallKind = PreImplicitCallKind,
+ MaxImplicitCallKind = PostImplicitCallKind,
+ LoopExitKind,
+ LifetimeEndKind,
+ EpsilonKind
+ };
static StringRef getProgramPointKindName(Kind K);
std::optional<SourceLocation> getSourceLocation() const;
@@ -725,6 +728,29 @@ class LoopExit : public ProgramPoint {
}
};
+/// Represents a point when the lifetime ends of any automatic object.
+class LifetimeEnd : public ProgramPoint {
+public:
+ LifetimeEnd(const Stmt *S, const VarDecl *D, const StackFrame *SF)
+ : ProgramPoint(S, D, LifetimeEndKind, SF) {}
+
+ LLVM_ATTRIBUTE_RETURNS_NONNULL const Stmt *getTriggerStmt() const {
+ return static_cast<const Stmt *>(getData1());
+ }
+
+ /// Returns a variable declaration that uniquely identifies the scope
+ LLVM_ATTRIBUTE_RETURNS_NONNULL const VarDecl *getDecl() const {
+ return static_cast<const VarDecl *>(getData2());
+ }
+
+private:
+ friend class ProgramPoint;
+ LifetimeEnd() = default;
+ static bool isKind(const ProgramPoint &Location) {
+ return Location.getKind() == LifetimeEndKind;
+ }
+};
+
/// This is a meta program point, which should be skipped by all the diagnostic
/// reasoning etc.
class EpsilonPoint : public ProgramPoint {
diff --git a/clang/include/clang/StaticAnalyzer/Core/Checker.h
b/clang/include/clang/StaticAnalyzer/Core/Checker.h
index ab38a05bfd79d..78772b12d4709 100644
--- a/clang/include/clang/StaticAnalyzer/Core/Checker.h
+++ b/clang/include/clang/StaticAnalyzer/Core/Checker.h
@@ -206,6 +206,20 @@ class Location {
}
};
+class LifetimeEnd {
+ template <typename CHECKER>
+ static void _checkCall(void *checker, const VarDecl *D, CheckerContext &C) {
+ ((const CHECKER *)checker)->checkLifetimeEnd(D, C);
+ }
+
+public:
+ template <typename CHECKER>
+ static void _register(CHECKER *checker, CheckerManager &mgr) {
+ mgr._registerForLifetimeEnd(
+ CheckerManager::CheckLifetimeEndFunc(checker, _checkCall<CHECKER>));
+ }
+};
+
class Bind {
template <typename CHECKER>
static void _checkBind(void *checker, SVal location, SVal val, const Stmt *S,
diff --git a/clang/include/clang/StaticAnalyzer/Core/CheckerManager.h
b/clang/include/clang/StaticAnalyzer/Core/CheckerManager.h
index 9d352960c1db7..e1cd01ef13f2d 100644
--- a/clang/include/clang/StaticAnalyzer/Core/CheckerManager.h
+++ b/clang/include/clang/StaticAnalyzer/Core/CheckerManager.h
@@ -330,6 +330,12 @@ class CheckerManager {
const CallEvent &Call, ExprEngine &Eng,
bool wasInlined = false);
+ /// Run checkers for end of variable lifetime
+ void runCheckersForLifetimeEnd(ExplodedNodeSet &Dst,
+ const ExplodedNodeSet &Src,
+ const VarDecl *Decl, const Stmt *TriggerStmt,
+ ExprEngine &Eng);
+
/// Run checkers for load/store of a location.
void runCheckersForLocation(ExplodedNodeSet &Dst,
const ExplodedNodeSet &Src,
@@ -493,6 +499,9 @@ class CheckerManager {
using CheckCallFunc =
CheckerFn<void (const CallEvent &, CheckerContext &)>;
+ using CheckLifetimeEndFunc =
+ CheckerFn<void(const VarDecl *, CheckerContext &)>;
+
using CheckLocationFunc = CheckerFn<void(SVal location, bool isLoad,
const Stmt *S, CheckerContext &)>;
@@ -557,6 +566,8 @@ class CheckerManager {
void _registerForPreCall(CheckCallFunc checkfn);
void _registerForPostCall(CheckCallFunc checkfn);
+ void _registerForLifetimeEnd(CheckLifetimeEndFunc checkfn);
+
void _registerForLocation(CheckLocationFunc checkfn);
void _registerForBind(CheckBindFunc checkfn);
@@ -665,6 +676,8 @@ class CheckerManager {
std::vector<CheckCallFunc> PreCallCheckers;
std::vector<CheckCallFunc> PostCallCheckers;
+ std::vector<CheckLifetimeEndFunc> LifetimeEndCheckers;
+
std::vector<CheckLocationFunc> LocationCheckers;
std::vector<CheckBindFunc> BindCheckers;
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h
b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h
index e4ddabbe9c927..bb2de45cec92a 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h
@@ -368,6 +368,7 @@ class ExprEngine {
void ProcessStmt(const Stmt *S, ExplodedNode *Pred);
void ProcessLoopExit(const Stmt* S, ExplodedNode *Pred);
+ void ProcessLifetimeEnd(const Stmt *S, const VarDecl *D, ExplodedNode *Pred);
void ProcessInitializer(const CFGInitializer I, ExplodedNode *Pred);
diff --git a/clang/lib/Analysis/PathDiagnostic.cpp
b/clang/lib/Analysis/PathDiagnostic.cpp
index 24fed40a93ed1..d8af4548f761e 100644
--- a/clang/lib/Analysis/PathDiagnostic.cpp
+++ b/clang/lib/Analysis/PathDiagnostic.cpp
@@ -719,7 +719,11 @@ PathDiagnosticLocation::create(const ProgramPoint& P,
BE->getBlock()->getTerminatorStmt()->getBeginLoc(), SMng);
} else if (std::optional<FunctionExitPoint> FE =
P.getAs<FunctionExitPoint>()) {
- return PathDiagnosticLocation(FE->getStmt(), SMng, FE->getStackFrame());
+ return PathDiagnosticLocation(FE->getStmt(), SMng,
+ FE->getStackFrame());
+ } else if (std::optional<LifetimeEnd> LE = P.getAs<LifetimeEnd>()) {
+ return PathDiagnosticLocation::createEnd(LE->getTriggerStmt(), SMng,
+ LE->getStackFrame());
} else {
llvm_unreachable("Unexpected ProgramPoint");
}
diff --git a/clang/lib/Analysis/ProgramPoint.cpp
b/clang/lib/Analysis/ProgramPoint.cpp
index 11c8c8242eb19..697e0afd30ed2 100644
--- a/clang/lib/Analysis/ProgramPoint.cpp
+++ b/clang/lib/Analysis/ProgramPoint.cpp
@@ -217,6 +217,11 @@ void ProgramPoint::printJson(llvm::raw_ostream &Out, const
char *NL) const {
<< castAs<LoopExit>().getLoopStmt()->getStmtClassName() << '\"';
break;
+ case ProgramPoint::LifetimeEndKind:
+ Out << "LifetimeEnd\", \"var\": \""
+ << castAs<LifetimeEnd>().getDecl()->getNameAsString() << '\"';
+ break;
+
case ProgramPoint::PreImplicitCallKind: {
ImplicitCallPoint PC = castAs<ImplicitCallPoint>();
Out << "PreCall\", \"decl\": \""
diff --git a/clang/lib/StaticAnalyzer/Core/CheckerManager.cpp
b/clang/lib/StaticAnalyzer/Core/CheckerManager.cpp
index 35603e333bf17..4fdbfd6d3c50b 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerManager.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerManager.cpp
@@ -35,17 +35,17 @@ using namespace clang;
using namespace ento;
bool CheckerManager::hasPathSensitiveCheckers() const {
- const auto IfAnyAreNonEmpty = [](const auto &... Callbacks) -> bool {
+ const auto IfAnyAreNonEmpty = [](const auto &...Callbacks) -> bool {
return (!Callbacks.empty() || ...);
};
return IfAnyAreNonEmpty(
StmtCheckers, PreObjCMessageCheckers, ObjCMessageNilCheckers,
PostObjCMessageCheckers, PreCallCheckers, PostCallCheckers,
- LocationCheckers, BindCheckers, BlockEntranceCheckers,
- EndAnalysisCheckers, BeginFunctionCheckers, EndFunctionCheckers,
- BranchConditionCheckers, NewAllocatorCheckers, LiveSymbolsCheckers,
- DeadSymbolsCheckers, RegionChangesCheckers, PointerEscapeCheckers,
- EvalAssumeCheckers, EvalCallCheckers, EndOfTranslationUnitCheckers);
+ LifetimeEndCheckers, LocationCheckers, BindCheckers,
BlockEntranceCheckers, EndAnalysisCheckers,
+ BeginFunctionCheckers, EndFunctionCheckers, BranchConditionCheckers,
+ NewAllocatorCheckers, LiveSymbolsCheckers, DeadSymbolsCheckers,
+ RegionChangesCheckers, PointerEscapeCheckers, EvalAssumeCheckers,
+ EvalCallCheckers, EndOfTranslationUnitCheckers);
}
void CheckerManager::reportInvalidCheckerOptionValue(
@@ -311,6 +311,44 @@ void CheckerManager::runCheckersForCallEvent(bool
isPreVisit,
expandGraphWithCheckers(C, Dst, Src);
}
+namespace {
+
+struct CheckLifetimeEndContext {
+ using CheckersTy = std::vector<CheckerManager::CheckLifetimeEndFunc>;
+
+ const CheckersTy &Checkers;
+ const VarDecl *Decl;
+ const Stmt *TriggerStmt;
+ ExprEngine &Eng;
+
+ CheckLifetimeEndContext(const CheckersTy &checkers, const VarDecl *decl,
+ const Stmt *stmt, ExprEngine &eng)
+ : Checkers(checkers), Decl(decl), TriggerStmt(stmt), Eng(eng) {}
+
+ CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
+ CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
+
+ void runChecker(CheckerManager::CheckLifetimeEndFunc checkFn,
+ NodeBuilder &Bldr, ExplodedNode *Pred) {
+ assert(Pred->getLocation().getAs<LifetimeEnd>().has_value());
+ const ProgramPoint L = Pred->getLocation().withTag(checkFn.Checker);
+ CheckerContext C(Bldr, Eng, Pred, L);
+ checkFn(Decl, C);
+ }
+};
+
+} // namespace
+
+/// Run checkers for end of variable lifetime
+void CheckerManager::runCheckersForLifetimeEnd(ExplodedNodeSet &Dst,
+ const ExplodedNodeSet &Src,
+ const VarDecl *Decl,
+ const Stmt *TriggerStmt,
+ ExprEngine &Eng) {
+ CheckLifetimeEndContext C(LifetimeEndCheckers, Decl, TriggerStmt, Eng);
+ expandGraphWithCheckers(C, Dst, Src);
+}
+
namespace {
struct CheckLocationContext {
@@ -903,6 +941,10 @@ void CheckerManager::_registerForPostCall(CheckCallFunc
checkfn) {
PostCallCheckers.push_back(checkfn);
}
+void CheckerManager::_registerForLifetimeEnd(CheckLifetimeEndFunc checkfn) {
+ LifetimeEndCheckers.push_back(checkfn);
+}
+
void CheckerManager::_registerForLocation(CheckLocationFunc checkfn) {
LocationCheckers.push_back(checkfn);
}
diff --git a/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
b/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
index 2a64e340ed214..b38dfdfa6f2c4 100644
--- a/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CoreEngine.cpp
@@ -254,11 +254,9 @@ void CoreEngine::dispatchWorkItem(ExplodedNode *Pred,
ProgramPoint Loc,
break;
}
default:
- assert(Loc.getAs<PostStmt>() ||
- Loc.getAs<PostInitializer>() ||
- Loc.getAs<PostImplicitCall>() ||
- Loc.getAs<CallExitEnd>() ||
- Loc.getAs<LoopExit>() ||
+ assert(Loc.getAs<PostStmt>() || Loc.getAs<PostInitializer>() ||
+ Loc.getAs<PostImplicitCall>() || Loc.getAs<CallExitEnd>() ||
+ Loc.getAs<LoopExit>() || Loc.getAs<LifetimeEnd>() ||
Loc.getAs<PostAllocatorCall>());
HandlePostStmt(WU.getBlock(), WU.getIndex(), Pred);
break;
@@ -306,6 +304,9 @@ void CoreEngine::HandleBlockEdge(const BlockEdge &L,
ExplodedNode *Pred) {
} else if (std::optional<CFGAutomaticObjDtor> AutoDtor =
LastElement.getAs<CFGAutomaticObjDtor>()) {
RS = dyn_cast<ReturnStmt>(AutoDtor->getTriggerStmt());
+ } else if (std::optional<CFGScopeMarker> ScopeMarker =
+ LastElement.getAs<CFGScopeMarker>()) {
+ RS = dyn_cast<ReturnStmt>(ScopeMarker->getTriggerStmt());
}
}
@@ -591,9 +592,10 @@ void CoreEngine::enqueueStmtNode(ExplodedNode *N,
// Do not create extra nodes. Move to the next CFG element.
if (N->getLocation().getAs<PostInitializer>() ||
- N->getLocation().getAs<PostImplicitCall>()||
- N->getLocation().getAs<LoopExit>()) {
- WList->enqueue(N, Block, Idx+1);
+ N->getLocation().getAs<PostImplicitCall>() ||
+ N->getLocation().getAs<LoopExit>() ||
+ N->getLocation().getAs<LifetimeEnd>()) {
+ WList->enqueue(N, Block, Idx + 1);
return;
}
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
index 34ca88705cba6..21dff1961a76d 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -977,6 +977,9 @@ void ExprEngine::processCFGElement(const CFGElement E,
ExplodedNode *Pred,
ProcessLoopExit(E.castAs<CFGLoopExit>().getLoopStmt(), Pred);
return;
case CFGElement::LifetimeEnds:
+ ProcessLifetimeEnd(E.castAs<CFGLifetimeEnds>().getTriggerStmt(),
+ E.castAs<CFGLifetimeEnds>().getVarDecl(), Pred);
+ return;
case CFGElement::CleanupFunction:
case CFGElement::FullExprCleanup:
case CFGElement::ScopeBegin:
@@ -1140,6 +1143,19 @@ void ExprEngine::ProcessLoopExit(const Stmt* S,
ExplodedNode *Pred) {
Engine.enqueueStmtNode(N, getCurrBlock(), currStmtIdx);
}
+void ExprEngine::ProcessLifetimeEnd(const Stmt *S, const VarDecl *D,
+ ExplodedNode *Pred) {
+ ExplodedNodeSet Src;
+ NodeBuilder Bldr(Pred, Src, *currBldrCtx);
+ LifetimeEnd PP(S, D, Pred->getStackFrame());
+ Bldr.generateNode(PP, Pred->getState(), Pred);
+
+ ExplodedNodeSet Dst;
+ getCheckerManager().runCheckersForLifetimeEnd(Dst, Src, D, S, *this);
+ Engine.enqueueStmtNodes(Dst, currBldrCtx->getBlock(), currStmtIdx);
+ return;
+}
+
void ExprEngine::ProcessInitializer(const CFGInitializer CFGInit,
ExplodedNode *Pred) {
const CXXCtorInitializer *BMI = CFGInit.getInitializer();
diff --git a/clang/unittests/StaticAnalyzer/CMakeLists.txt
b/clang/unittests/StaticAnalyzer/CMakeLists.txt
index ed16a1372bea2..943850e49b0b5 100644
--- a/clang/unittests/StaticAnalyzer/CMakeLists.txt
+++ b/clang/unittests/StaticAnalyzer/CMakeLists.txt
@@ -6,6 +6,7 @@ add_clang_unittest(StaticAnalysisTests
BugReportInterestingnessTest.cpp
CallDescriptionTest.cpp
CallEventTest.cpp
+ CheckLifetimeEndTest.cpp
ConflictingEvalCallsTest.cpp
ExprEngineVisitTest.cpp
FalsePositiveRefutationBRVisitorTest.cpp
diff --git a/clang/unittests/StaticAnalyzer/CheckLifetimeEndTest.cpp
b/clang/unittests/StaticAnalyzer/CheckLifetimeEndTest.cpp
new file mode 100644
index 0000000000000..9b3dd4bae7d9a
--- /dev/null
+++ b/clang/unittests/StaticAnalyzer/CheckLifetimeEndTest.cpp
@@ -0,0 +1,93 @@
+//===- 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 clang {
+namespace ento {
+namespace {
+
+class LifetimeEndReporter : public Checker<check::LifetimeEnd> {
+ using Self = LifetimeEndReporter;
+ const BugType LifetimeEndNode{this, "LifetimeEndReporter"};
+
+ bool report(CheckerContext &C, StringRef Description) const {
+ ExplodedNode *Node = C.generateNonFatalErrorNode(C.getState());
+ if (!Node)
+ return false;
+
+ auto Report = std::make_unique<PathSensitiveBugReport>(LifetimeEndNode,
+ Description, Node);
+ C.emitReport(std::move(Report));
+ return true;
+ }
+
+public:
+ void checkLifetimeEnd(const VarDecl *D, CheckerContext &C) const {
+ if (auto II = D->getIdentifier())
+ report(C, (II->getName() + " LIFETIME END").str());
+ }
+};
+
+void addLifetimeEndReporter(AnalysisASTConsumer &AnalysisConsumer,
+ AnalyzerOptions &AnOpts) {
+ AnOpts.CheckersAndPackages = {
+ {"test.LifetimeEndReporter", true},
+ };
+ AnalysisConsumer.AddCheckerRegistrationFn([](CheckerRegistry &Registry) {
+ Registry.addChecker<LifetimeEndReporter>(
+ "test.LifetimeEndReporter", "EmptyDescription", "EmptyDocsUri");
+ });
+}
+
+const std::vector<std::string> DisableLifetimeArgs{
+ "-Xclang", "-analyzer-config", "-Xclang", "cfg-lifetime=false"};
+const std::vector<std::string> EnableLifetimeArgs{
+ "-Xclang", "-analyzer-config", "-Xclang", "cfg-lifetime=true"};
+
+TEST(CheckLifetimeEnd, CFGLifetimeEnabled) {
+ constexpr auto Code = R"(
+void foo() {
+ int i = 0;
+}
+ )";
+
+ std::string Diags;
+ EXPECT_TRUE(runCheckerOnCodeWithArgs<addLifetimeEndReporter>(
+ Code, EnableLifetimeArgs, Diags, /*OnlyEmitWarnings=*/true));
+ EXPECT_EQ(Diags, "test.LifetimeEndReporter: i LIFETIME END\n");
+}
+
+TEST(CheckLifetimeEnd, CFGLifetimeDisabled) {
+ constexpr auto Code = R"(
+void foo() {
+ int i = 0;
+}
+ )";
+
+ std::string Diags;
+ EXPECT_TRUE(runCheckerOnCodeWithArgs<addLifetimeEndReporter>(
+ Code, DisableLifetimeArgs, Diags, /*OnlyEmitWarnings=*/true));
+ EXPECT_TRUE(Diags.empty());
+}
+
+} // namespace
+} // namespace ento
+} // namespace clang
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits