diff options
author | Timm Baeder <tbaeder@redhat.com> | 2025-04-01 09:00:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-01 09:00:46 +0200 |
commit | 7267dbfe1032f5ebd698403848fab4bbfcbe0b19 (patch) | |
tree | 3a2a228a7793531ff7d670f6d5f9bf3290310fe5 /clang/lib/AST/ByteCode/Pointer.cpp | |
parent | 49f080afc4466ddf415d7fc7e98989c0bd07d8ea (diff) | |
download | llvm-7267dbfe1032f5ebd698403848fab4bbfcbe0b19.zip llvm-7267dbfe1032f5ebd698403848fab4bbfcbe0b19.tar.gz llvm-7267dbfe1032f5ebd698403848fab4bbfcbe0b19.tar.bz2 |
[clang][bytecode] Fix comparing the addresses of union members (#133852)
Union members get the same address, so we can't just use
`Pointer::getByteOffset()`.
Diffstat (limited to 'clang/lib/AST/ByteCode/Pointer.cpp')
-rw-r--r-- | clang/lib/AST/ByteCode/Pointer.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp index 79b47c2..5b52261 100644 --- a/clang/lib/AST/ByteCode/Pointer.cpp +++ b/clang/lib/AST/ByteCode/Pointer.cpp @@ -339,6 +339,39 @@ void Pointer::print(llvm::raw_ostream &OS) const { } } +/// Compute an integer that can be used to compare this pointer to +/// another one. +size_t Pointer::computeOffsetForComparison() const { + if (!isBlockPointer()) + return Offset; + + size_t Result = 0; + Pointer P = *this; + while (!P.isRoot()) { + if (P.isArrayRoot()) { + P = P.getBase(); + continue; + } + if (P.isArrayElement()) { + P = P.expand(); + Result += (P.getIndex() * P.elemSize()); + P = P.getArray(); + continue; + } + + if (const Record *R = P.getBase().getRecord(); R && R->isUnion()) { + // Direct child of a union - all have offset 0. + P = P.getBase(); + continue; + } + + Result += P.getInlineDesc()->Offset; + P = P.getBase(); + } + + return Result; +} + std::string Pointer::toDiagnosticString(const ASTContext &Ctx) const { if (isZero()) return "nullptr"; |