clang 22.0.0git
Origins.cpp
Go to the documentation of this file.
1//===- Origins.cpp - Origin Implementation -----------------------*- 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
10
12
13void OriginManager::dump(OriginID OID, llvm::raw_ostream &OS) const {
14 OS << OID << " (";
15 Origin O = getOrigin(OID);
16 if (const ValueDecl *VD = O.getDecl())
17 OS << "Decl: " << VD->getNameAsString();
18 else if (const Expr *E = O.getExpr())
19 OS << "Expr: " << E->getStmtClassName();
20 else
21 OS << "Unknown";
22 OS << ")";
23}
24
26 AllOrigins.emplace_back(ID, &D);
27 return AllOrigins.back();
28}
29
31 AllOrigins.emplace_back(ID, &E);
32 return AllOrigins.back();
33}
34
35// TODO: Mark this method as const once we remove the call to getOrCreate.
37 auto It = ExprToOriginID.find(&E);
38 if (It != ExprToOriginID.end())
39 return It->second;
40 // If the expression itself has no specific origin, and it's a reference
41 // to a declaration, its origin is that of the declaration it refers to.
42 // For pointer types, where we don't pre-emptively create an origin for the
43 // DeclRefExpr itself.
44 if (const auto *DRE = dyn_cast<DeclRefExpr>(&E))
45 return get(*DRE->getDecl());
46 // TODO: This should be an assert(It != ExprToOriginID.end()). The current
47 // implementation falls back to getOrCreate to avoid crashing on
48 // yet-unhandled pointer expressions, creating an empty origin for them.
49 return getOrCreate(E);
50}
51
53 auto It = DeclToOriginID.find(&D);
54 // TODO: This should be an assert(It != DeclToOriginID.end()). The current
55 // implementation falls back to getOrCreate to avoid crashing on
56 // yet-unhandled pointer expressions, creating an empty origin for them.
57 if (It == DeclToOriginID.end())
58 return getOrCreate(D);
59
60 return It->second;
61}
62
64 auto It = ExprToOriginID.find(&E);
65 if (It != ExprToOriginID.end())
66 return It->second;
67
68 OriginID NewID = getNextOriginID();
69 addOrigin(NewID, E);
70 ExprToOriginID[&E] = NewID;
71 return NewID;
72}
73
75 assert(ID.Value < AllOrigins.size());
76 return AllOrigins[ID.Value];
77}
78
80 auto It = DeclToOriginID.find(&D);
81 if (It != DeclToOriginID.end())
82 return It->second;
83 OriginID NewID = getNextOriginID();
84 addOrigin(NewID, D);
85 DeclToOriginID[&D] = NewID;
86 return NewID;
87}
88
89} // namespace clang::lifetimes::internal
This represents one expression.
Definition Expr.h:112
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
const Origin & getOrigin(OriginID ID) const
Definition Origins.cpp:74
OriginID getOrCreate(const Expr &E)
Definition Origins.cpp:63
void dump(OriginID OID, llvm::raw_ostream &OS) const
Definition Origins.cpp:13
Origin & addOrigin(OriginID ID, const clang::ValueDecl &D)
Definition Origins.cpp:25
utils::ID< struct OriginTag > OriginID
Definition Origins.h:23
An Origin is a symbolic identifier that represents the set of possible loans a pointer-like object co...
Definition Origins.h:37
const clang::Expr * getExpr() const
Definition Origins.h:50
const clang::ValueDecl * getDecl() const
Definition Origins.h:47