blob: d3653b58a508b4d451257c2d913fe4d1862763ec [file] [log] [blame]
Luca Versari99fddff2022-05-25 10:22:32 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#include "lifetime_analysis/lifetime_lattice.h"
6
7#include <assert.h>
8
9#include <string>
10#include <tuple>
11#include <utility>
12
13#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
14#include "llvm/Support/ErrorHandling.h"
15
16namespace clang {
17namespace tidy {
18namespace lifetimes {
19
20std::string LifetimeLattice::ToString() const {
21 if (IsError()) {
22 return Error().str();
23 }
24 return PointsTo().DebugString();
25}
26
27PointsToMap& LifetimeLattice::PointsTo() {
Luca Versari81222852022-08-05 06:36:10 -070028 return std::get<0>(var_).first;
Luca Versari99fddff2022-05-25 10:22:32 -070029}
30
31const PointsToMap& LifetimeLattice::PointsTo() const {
Luca Versari81222852022-08-05 06:36:10 -070032 return std::get<0>(var_).first;
33}
34
35LifetimeConstraints& LifetimeLattice::Constraints() {
Luca Versari81222852022-08-05 06:36:10 -070036 return std::get<0>(var_).second;
37}
38
39const LifetimeConstraints& LifetimeLattice::Constraints() const {
Luca Versari81222852022-08-05 06:36:10 -070040 return std::get<0>(var_).second;
Luca Versari99fddff2022-05-25 10:22:32 -070041}
42
43llvm::StringRef LifetimeLattice::Error() const {
Luca Versari99fddff2022-05-25 10:22:32 -070044 return std::get<std::string>(var_);
45}
46
47clang::dataflow::LatticeJoinEffect LifetimeLattice::join(
48 const LifetimeLattice& other) {
49 if (IsError()) {
50 return clang::dataflow::LatticeJoinEffect::Unchanged;
51 }
52 if (other.IsError()) {
53 *this = other;
54 return clang::dataflow::LatticeJoinEffect::Changed;
55 }
56
Luca Versari81222852022-08-05 06:36:10 -070057 auto constraints_effect = Constraints().join(other.Constraints());
58
Luca Versari99fddff2022-05-25 10:22:32 -070059 PointsToMap joined_points_to_map = PointsTo().Union(other.PointsTo());
Luca Versari81222852022-08-05 06:36:10 -070060 if (PointsTo() == joined_points_to_map &&
61 constraints_effect == clang::dataflow::LatticeJoinEffect::Unchanged) {
Luca Versari99fddff2022-05-25 10:22:32 -070062 return clang::dataflow::LatticeJoinEffect::Unchanged;
63 }
64
Luca Versari81222852022-08-05 06:36:10 -070065 PointsTo() = std::move(joined_points_to_map);
Luca Versari99fddff2022-05-25 10:22:32 -070066 return clang::dataflow::LatticeJoinEffect::Changed;
67}
68
69bool LifetimeLattice::operator==(const LifetimeLattice& other) const {
70 if (IsError() || other.IsError()) {
71 // Any error compares equal to any other error.
72 return IsError() && other.IsError();
73 }
Luca Versari91a56ff2022-08-22 01:58:33 -070074 return PointsTo() == other.PointsTo() && Constraints() == other.Constraints();
Luca Versari99fddff2022-05-25 10:22:32 -070075}
76
77} // namespace lifetimes
78} // namespace tidy
79} // namespace clang