Skip to content

Commit 6600371

Browse files
committed
CFG-based lifetime analysis using existing annotations
1 parent b87fdd9 commit 6600371

File tree

9 files changed

+1662
-32
lines changed

9 files changed

+1662
-32
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_DANGLING_REFERENCE_H
2+
#define LLVM_CLANG_ANALYSIS_ANALYSES_DANGLING_REFERENCE_H
3+
#include "clang/AST/Decl.h"
4+
#include "clang/AST/DeclBase.h"
5+
#include "clang/Analysis/AnalysisDeclContext.h"
6+
#include "clang/Analysis/CFG.h"
7+
namespace clang {
8+
class DanglingReferenceReporter {
9+
public:
10+
DanglingReferenceReporter() = default;
11+
virtual ~DanglingReferenceReporter() = default;
12+
13+
virtual void ReportReturnLocalVar(const Expr *RetExpr,
14+
const Decl *LocalDecl) {}
15+
virtual void ReportReturnTemporaryExpr(const Expr *TemporaryExpr) {}
16+
virtual void ReportDanglingReference(const VarDecl *VD) {}
17+
virtual void SuggestLifetimebound(const ParmVarDecl *PVD,
18+
const Expr *RetExpr) {}
19+
};
20+
21+
void runDanglingReferenceAnalysis(const DeclContext &dc, const CFG &cfg,
22+
AnalysisDeclContext &ac,
23+
DanglingReferenceReporter *reporter);
24+
25+
} // namespace clang
26+
27+
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_DANGLING_REFERENCE_H

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,13 @@ def Dangling : DiagGroup<"dangling", [DanglingAssignment,
472472
DanglingInitializerList,
473473
DanglingGsl,
474474
ReturnStackAddress]>;
475+
def ReturnStackAddressCFG : DiagGroup<"return-stack-address-cfg">;
476+
def SuggestLifetimeboundCFG : DiagGroup<"suggest-lifetimebound-cfg">;
477+
def DanglingReferenceCFG : DiagGroup<"dangling-reference-cfg">;
478+
def DanglingCFG
479+
: DiagGroup<"dangling-cfg", [ReturnStackAddressCFG, DanglingReferenceCFG,
480+
SuggestLifetimeboundCFG]>;
481+
475482
def DistributedObjectModifiers : DiagGroup<"distributed-object-modifiers">;
476483
def DllexportExplicitInstantiationDecl : DiagGroup<"dllexport-explicit-instantiation-decl">;
477484
def ExcessInitializers : DiagGroup<"excess-initializers">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10169,6 +10169,25 @@ def err_lifetimebound_implicit_object_parameter_void_return_type : Error<
1016910169
"parameter of a function that returns void; "
1017010170
"did you mean 'lifetime_capture_by(X)'">;
1017110171

10172+
// CFG based lifetime analysis.
10173+
def warn_ret_stack_variable_ref_cfg
10174+
: Warning<"returning reference to a stack variable">,
10175+
InGroup<ReturnStackAddressCFG>,
10176+
DefaultIgnore;
10177+
def note_local_variable_declared_here
10178+
: Note<"reference to this stack variable is returned">;
10179+
def warn_ret_stack_temporary_ref_cfg
10180+
: Warning<"returning reference to a temporary object">,
10181+
InGroup<ReturnStackAddressCFG>,
10182+
DefaultIgnore;
10183+
def warn_dangling_reference_cfg : Warning<"reference to local object dangles">,
10184+
InGroup<DanglingReferenceCFG>,
10185+
DefaultIgnore; // TODO: add note on loc of
10186+
def warn_suggest_lifetimebound_cfg
10187+
: Warning<"param should be marked lifetimebound">,
10188+
InGroup<SuggestLifetimeboundCFG>,
10189+
DefaultIgnore;
10190+
1017210191
// CHECK: returning address/reference of stack memory
1017310192
def warn_ret_stack_addr_ref : Warning<
1017410193
"%select{address of|reference to}0 stack memory associated with "

clang/lib/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_clang_library(clangAnalysis
1616
ConstructionContext.cpp
1717
Consumed.cpp
1818
CodeInjector.cpp
19+
DanglingReference.cpp
1920
Dominators.cpp
2021
ExprMutationAnalyzer.cpp
2122
IntervalPartition.cpp

0 commit comments

Comments
 (0)