clang 22.0.0git
LifetimeSafety.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
15#include "clang/AST/Decl.h"
16#include "clang/AST/Expr.h"
17#include "clang/AST/Type.h"
24#include "clang/Analysis/CFG.h"
25#include "llvm/ADT/FoldingSet.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/TimeProfiler.h"
29#include <memory>
30
31namespace clang::lifetimes {
32namespace internal {
33
37
39 llvm::TimeTraceScope TimeProfile("LifetimeSafetyAnalysis");
40
41 const CFG &Cfg = *AC.getCFG();
42 DEBUG_WITH_TYPE("PrintCFG", Cfg.dump(AC.getASTContext().getLangOpts(),
43 /*ShowColors=*/true));
44
45 FactsGenerator FactGen(FactMgr, AC);
46 FactGen.run();
47 DEBUG_WITH_TYPE("LifetimeFacts", FactMgr.dump(Cfg, AC));
48
49 /// TODO(opt): Consider optimizing individual blocks before running the
50 /// dataflow analysis.
51 /// 1. Expression Origins: These are assigned once and read at most once,
52 /// forming simple chains. These chains can be compressed into a single
53 /// assignment.
54 /// 2. Block-Local Loans: Origins of expressions are never read by other
55 /// blocks; only Decls are visible. Therefore, loans in a block that
56 /// never reach an Origin associated with a Decl can be safely dropped by
57 /// the analysis.
58 /// 3. Collapse ExpireFacts belonging to same source location into a single
59 /// Fact.
60 LoanPropagation = std::make_unique<LoanPropagationAnalysis>(
61 Cfg, AC, FactMgr, Factory.OriginMapFactory, Factory.LoanSetFactory);
62
63 LiveOrigins = std::make_unique<LiveOriginsAnalysis>(
64 Cfg, AC, FactMgr, Factory.LivenessMapFactory);
65 DEBUG_WITH_TYPE("LiveOrigins",
66 LiveOrigins->dump(llvm::dbgs(), FactMgr.getTestPoints()));
67
68 runLifetimeChecker(*LoanPropagation, *LiveOrigins, FactMgr, AC, Reporter);
69}
70} // namespace internal
71
73 LifetimeSafetyReporter *Reporter) {
74 internal::LifetimeSafetyAnalysis Analysis(AC, Reporter);
75 Analysis.run();
76}
77} // namespace clang::lifetimes
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
C Language Family Type Representation.
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1222
void dump(const LangOptions &LO, bool ShowColors) const
dump - A simple pretty printer of a CFG that outputs to stderr.
Definition CFG.cpp:6222
Running the lifetime safety analysis and querying its results.
LifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetyReporter *Reporter)
void runLifetimeChecker(const LoanPropagationAnalysis &LoanPropagation, const LiveOriginsAnalysis &LiveOrigins, const FactManager &FactMgr, AnalysisDeclContext &ADC, LifetimeSafetyReporter *Reporter)
Runs the lifetime checker, which detects use-after-free errors by examining loan expiration points an...
Definition Checker.cpp:122
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetyReporter *Reporter)
The main entry point for the analysis.