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 if (auto *ParenIgnored = E.IgnoreParens(); ParenIgnored != &E)
38 return get(*ParenIgnored);
39 auto It = ExprToOriginID.find(&E);
40 if (It != ExprToOriginID.end())
41 return It->second;
42 // If the expression itself has no specific origin, and it's a reference
43 // to a declaration, its origin is that of the declaration it refers to.
44 // For pointer types, where we don't pre-emptively create an origin for the
45 // DeclRefExpr itself.
46 if (const auto *DRE = dyn_cast<DeclRefExpr>(&E))
47 return get(*DRE->getDecl());
48 // TODO: This should be an assert(It != ExprToOriginID.end()). The current
49 // implementation falls back to getOrCreate to avoid crashing on
50 // yet-unhandled pointer expressions, creating an empty origin for them.
51 return getOrCreate(E);
52}
53
55 auto It = DeclToOriginID.find(&D);
56 // TODO: This should be an assert(It != DeclToOriginID.end()). The current
57 // implementation falls back to getOrCreate to avoid crashing on
58 // yet-unhandled pointer expressions, creating an empty origin for them.
59 if (It == DeclToOriginID.end())
60 return getOrCreate(D);
61
62 return It->second;
63}
64
66 auto It = ExprToOriginID.find(&E);
67 if (It != ExprToOriginID.end())
68 return It->second;
69
70 OriginID NewID = getNextOriginID();
71 addOrigin(NewID, E);
72 ExprToOriginID[&E] = NewID;
73 return NewID;
74}
75
77 assert(ID.Value < AllOrigins.size());
78 return AllOrigins[ID.Value];
79}
80
82 auto It = DeclToOriginID.find(&D);
83 if (It != DeclToOriginID.end())
84 return It->second;
85 OriginID NewID = getNextOriginID();
86 addOrigin(NewID, D);
87 DeclToOriginID[&D] = NewID;
88 return NewID;
89}
90
91} // namespace clang::lifetimes::internal
This represents one expression.
Definition Expr.h:112
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3081
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:76
OriginID getOrCreate(const Expr &E)
Definition Origins.cpp:65
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