1//===- LifetimeSafety.cpp - C++ Lifetime Safety Analysis -*--------- C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the main LifetimeSafetyAnalysis class, which coordinates
10// the various components (fact generation, loan propagation, live origins
11// analysis, and checking) to detect lifetime safety violations in C++ code.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/Expr.h"
17#include "clang/AST/Type.h"
18#include "clang/Analysis/Analyses/LifetimeSafety/Checker.h"
19#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
20#include "clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h"
21#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeStats.h"
22#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
23#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
24#include "clang/Analysis/Analyses/LifetimeSafety/Origins.h"
25#include "clang/Analysis/AnalysisDeclContext.h"
26#include "clang/Analysis/CFG.h"
27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/TimeProfiler.h"
31#include <memory>
32
33namespace clang::lifetimes {
34namespace internal {
35
36#ifndef NDEBUG
37static void DebugOnlyFunction(AnalysisDeclContext &AC, const CFG &Cfg,
38 FactManager &FactMgr) {
39 std::string Name;
40 if (const Decl *D = AC.getDecl()) {
41 if (const auto *ND = dyn_cast<NamedDecl>(D))
42 Name = ND->getQualifiedNameAsString();
43 };
44 DEBUG_WITH_TYPE(Name.c_str(), AC.getDecl()->dumpColor());
45 DEBUG_WITH_TYPE(Name.c_str(), Cfg.dump(AC.getASTContext().getLangOpts(),
46 /*ShowColors=*/true));
47 DEBUG_WITH_TYPE(Name.c_str(), FactMgr.dump(Cfg, AC));
48}
49#endif
50
51LifetimeSafetyAnalysis::LifetimeSafetyAnalysis(
52 AnalysisDeclContext &AC, LifetimeSafetySemaHelper *SemaHelper)
53 : AC(AC), SemaHelper(SemaHelper) {}
54
55void LifetimeSafetyAnalysis::run() {
56 llvm::TimeTraceScope TimeProfile("LifetimeSafetyAnalysis");
57
58 const CFG &Cfg = *AC.getCFG();
59 DEBUG_WITH_TYPE("PrintCFG", Cfg.dump(AC.getASTContext().getLangOpts(),
60 /*ShowColors=*/true));
61
62 FactMgr = std::make_unique<FactManager>(args&: AC, args: Cfg);
63
64 FactsGenerator FactGen(*FactMgr, AC);
65 FactGen.run();
66
67 DEBUG_WITH_TYPE("LifetimeFacts", FactMgr->dump(Cfg, AC));
68
69 // Debug print facts for a specific function using
70 // -debug-only=EnableFilterByFunctionName,YourFunctionNameFoo
71 DEBUG_WITH_TYPE("EnableFilterByFunctionName",
72 DebugOnlyFunction(AC, Cfg, *FactMgr));
73
74 /// TODO(opt): Consider optimizing individual blocks before running the
75 /// dataflow analysis.
76 /// 1. Expression Origins: These are assigned once and read at most once,
77 /// forming simple chains. These chains can be compressed into a single
78 /// assignment.
79 /// 2. Block-Local Loans: Origins of expressions are never read by other
80 /// blocks; only Decls are visible. Therefore, loans in a block that
81 /// never reach an Origin associated with a Decl can be safely dropped by
82 /// the analysis.
83 /// 3. Collapse ExpireFacts belonging to same source location into a single
84 /// Fact.
85 LoanPropagation = std::make_unique<LoanPropagationAnalysis>(
86 args: Cfg, args&: AC, args&: *FactMgr, args&: Factory.OriginMapFactory, args&: Factory.LoanSetFactory);
87
88 LiveOrigins = std::make_unique<LiveOriginsAnalysis>(
89 args: Cfg, args&: AC, args&: *FactMgr, args&: Factory.LivenessMapFactory);
90 DEBUG_WITH_TYPE("LiveOrigins",
91 LiveOrigins->dump(llvm::dbgs(), FactMgr->getTestPoints()));
92
93 runLifetimeChecker(LoanPropagation: *LoanPropagation, LiveOrigins: *LiveOrigins, FactMgr: *FactMgr, ADC&: AC, SemaHelper);
94}
95
96void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM,
97 LifetimeSafetyStats &Stats) {
98 Stmt *FunctionBody = AC.getBody();
99 if (FunctionBody == nullptr)
100 return;
101 OM.collectMissingOrigins(FunctionBody&: *FunctionBody, LSStats&: Stats);
102}
103} // namespace internal
104
105void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC,
106 LifetimeSafetySemaHelper *SemaHelper,
107 LifetimeSafetyStats &Stats, bool CollectStats) {
108 internal::LifetimeSafetyAnalysis Analysis(AC, SemaHelper);
109 Analysis.run();
110 if (CollectStats)
111 collectLifetimeStats(AC, OM&: Analysis.getFactManager().getOriginMgr(), Stats);
112}
113} // namespace clang::lifetimes
114