Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APValue][BoundsSafety] Migrate away from PointerUnion::{is,get} (NFC) #9834

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions clang/include/clang/AST/APValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,19 @@ class ForgedPtrLValue {
class DynamicAllocOrForgedPtrLValue
: public llvm::PointerUnion<DynamicAllocLValue, ForgedPtrLValue> {

public:
using BaseTy = llvm::PointerUnion<DynamicAllocLValue, ForgedPtrLValue>;

public:
DynamicAllocOrForgedPtrLValue() = default;
DynamicAllocOrForgedPtrLValue(DynamicAllocLValue LV) : BaseTy(LV) {}
DynamicAllocOrForgedPtrLValue(ForgedPtrLValue LV) : BaseTy(LV) {}

explicit operator bool() const {
if (is<DynamicAllocLValue>()) {
return static_cast<bool>(get<DynamicAllocLValue>());
PointerUnion pu = *this;
if (isa<DynamicAllocLValue>(pu)) {
return static_cast<bool>(cast<DynamicAllocLValue>(pu));
}
return static_cast<bool>(get<ForgedPtrLValue>());
return static_cast<bool>(cast<ForgedPtrLValue>(pu));
}

static DynamicAllocOrForgedPtrLValue getFromOpaqueValue(void *Value) {
Expand Down Expand Up @@ -250,30 +251,19 @@ class APValue {
return isa<T>(Ptr);
}

template <class T> EnableIfDAOrForged<T, bool> is() const {
if (!Ptr.is<DynamicAllocOrForgedPtrLValue>())
return false;
return Ptr.get<DynamicAllocOrForgedPtrLValue>().is<T>();
}
template <class T> EnableIfDAOrForged<T, bool> is() const;

template <class T> EnableIfNotDANorForged<T> get() const {
return cast<T>(Ptr);
}

template <class T> EnableIfDAOrForged<T> get() const {
assert(is<T>() && "Invalid accessor called");
return Ptr.get<DynamicAllocOrForgedPtrLValue>().get<T>();
}
template <class T> EnableIfDAOrForged<T> get() const;

template <class T> EnableIfNotDANorForged<T> dyn_cast() const {
return Ptr.dyn_cast<T>();
}

template <class T> EnableIfDAOrForged<T> dyn_cast() const {
if (is<T>())
return Ptr.get<DynamicAllocOrForgedPtrLValue>().dyn_cast<T>();
return T();
}
template <class T> EnableIfDAOrForged<T> dyn_cast() const;

void *getOpaqueValue() const;
// TO_UPSTREAM(BoundsSafety)
Expand Down
42 changes: 42 additions & 0 deletions clang/lib/AST/APValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,48 @@ APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V)
APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V)
: Ptr(P), Local{I, V} {}

// Separated definitions of these template functions to avoid nasty build issues

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: guard this by // TO_UPSTREAM(BoundsSafety)?

// caused by header dependency.
template <class T>
APValue::LValueBase::EnableIfDAOrForged<T, bool>
APValue::LValueBase::is() const {
if (!isa<DynamicAllocOrForgedPtrLValue>(Ptr))
return false;
DynamicAllocOrForgedPtrLValue::BaseTy DAOrForged =
cast<DynamicAllocOrForgedPtrLValue>(Ptr);
return isa<T>(DAOrForged);
}

template APValue::LValueBase::EnableIfDAOrForged<ForgedPtrLValue, bool>
APValue::LValueBase::is<ForgedPtrLValue>() const;
template APValue::LValueBase::EnableIfDAOrForged<DynamicAllocLValue, bool>
APValue::LValueBase::is<DynamicAllocLValue>() const;

template <class T>
APValue::LValueBase::EnableIfDAOrForged<T> APValue::LValueBase::get() const {
DynamicAllocOrForgedPtrLValue::BaseTy DAOrForged =
cast<DynamicAllocOrForgedPtrLValue>(Ptr);
return cast<T>(DAOrForged);
}

template APValue::LValueBase::EnableIfDAOrForged<ForgedPtrLValue>
APValue::LValueBase::get<ForgedPtrLValue>() const;
template APValue::LValueBase::EnableIfDAOrForged<DynamicAllocLValue>
APValue::LValueBase::get<DynamicAllocLValue>() const;

template <class T>
APValue::LValueBase::EnableIfDAOrForged<T>
APValue::LValueBase::dyn_cast() const {
if (is<T>())
return cast<DynamicAllocOrForgedPtrLValue>(Ptr).dyn_cast<T>();
return T();
}

template APValue::LValueBase::EnableIfDAOrForged<ForgedPtrLValue>
APValue::LValueBase::dyn_cast<ForgedPtrLValue>() const;
template APValue::LValueBase::EnableIfDAOrForged<DynamicAllocLValue>
APValue::LValueBase::dyn_cast<DynamicAllocLValue>() const;

APValue::LValueBase APValue::LValueBase::getDynamicAlloc(DynamicAllocLValue LV,
QualType Type) {
LValueBase Base;
Expand Down