-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang][Interp] Fix getField() for integral pointers #102120
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Instead of just adding the Record::Field offset, instead get the FieldDecl offset in the RecordLayout. Unfortunately, the offset we pass to the ops here is not made to easily go back to a FieldDecl, so we have to iterate over the parent Record.
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesInstead of just adding the Record::Field offset, instead get the FieldDecl offset in the RecordLayout. Unfortunately, the offset we pass to the ops here is not made to easily go back to a FieldDecl, so we have to iterate over the parent Record. Full diff: https://github.com/llvm/llvm-project/pull/102120.diff 5 Files Affected:
diff --git a/clang/lib/AST/Interp/Compiler.cpp b/clang/lib/AST/Interp/Compiler.cpp
index e5280491dfd73..02cbe38f5fb1f 100644
--- a/clang/lib/AST/Interp/Compiler.cpp
+++ b/clang/lib/AST/Interp/Compiler.cpp
@@ -335,6 +335,10 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
if (!PointeeType.isNull()) {
if (std::optional<PrimType> T = classify(PointeeType))
Desc = P.createDescriptor(SubExpr, *T);
+ else
+ Desc = P.createDescriptor(SubExpr, PointeeType.getTypePtr(),
+ std::nullopt, true, false,
+ /*IsMutable=*/false, nullptr);
}
return this->emitNull(classifyPrim(CE->getType()), Desc, CE);
}
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 04f88efdc0acf..2eed0d3d1f16b 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -1504,6 +1504,12 @@ inline bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off) {
if (Ptr.isBlockPointer() && Off > Ptr.block()->getSize())
return false;
+
+ if (Ptr.isIntegralPointer()) {
+ S.Stk.push<Pointer>(Ptr.asIntPointer().atOffset(S.getCtx(), Off));
+ return true;
+ }
+
S.Stk.push<Pointer>(Ptr.atField(Off));
return true;
}
@@ -1527,6 +1533,11 @@ inline bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off) {
if (Ptr.isBlockPointer() && Off > Ptr.block()->getSize())
return false;
+ if (Ptr.isIntegralPointer()) {
+ S.Stk.push<Pointer>(Ptr.asIntPointer().atOffset(S.getCtx(), Off));
+ return true;
+ }
+
S.Stk.push<Pointer>(Ptr.atField(Off));
return true;
}
diff --git a/clang/lib/AST/Interp/Pointer.cpp b/clang/lib/AST/Interp/Pointer.cpp
index 2b1f8b460510c..ba9683a059e18 100644
--- a/clang/lib/AST/Interp/Pointer.cpp
+++ b/clang/lib/AST/Interp/Pointer.cpp
@@ -597,3 +597,30 @@ std::optional<APValue> Pointer::toRValue(const Context &Ctx,
return std::nullopt;
return Result;
}
+
+IntPointer IntPointer::atOffset(const ASTContext &ASTCtx,
+ unsigned Offset) const {
+ if (!this->Desc)
+ return *this;
+ const Record *R = this->Desc->ElemRecord;
+ if (!R)
+ return *this;
+
+ const Record::Field *F = nullptr;
+ for (auto &It : R->fields()) {
+ if (It.Offset == Offset) {
+ F = &It;
+ break;
+ }
+ }
+ if (!F)
+ return *this;
+
+ const FieldDecl *FD = F->Decl;
+ const ASTRecordLayout &Layout = ASTCtx.getASTRecordLayout(FD->getParent());
+ unsigned FieldIndex = FD->getFieldIndex();
+ uint64_t FieldOffset =
+ ASTCtx.toCharUnitsFromBits(Layout.getFieldOffset(FieldIndex))
+ .getQuantity();
+ return IntPointer{this->Desc, FieldOffset};
+}
diff --git a/clang/lib/AST/Interp/Pointer.h b/clang/lib/AST/Interp/Pointer.h
index 6f6983458ab60..b7b4f82f16f66 100644
--- a/clang/lib/AST/Interp/Pointer.h
+++ b/clang/lib/AST/Interp/Pointer.h
@@ -44,6 +44,8 @@ struct BlockPointer {
struct IntPointer {
const Descriptor *Desc;
uint64_t Value;
+
+ IntPointer atOffset(const ASTContext &ASTCtx, unsigned Offset) const;
};
enum class Storage { Block, Int, Fn };
@@ -88,6 +90,9 @@ class Pointer {
PointeeStorage.Int.Value = 0;
PointeeStorage.Int.Desc = nullptr;
}
+ Pointer(IntPointer &&IntPtr) : StorageKind(Storage::Int) {
+ PointeeStorage.Int = std::move(IntPtr);
+ }
Pointer(Block *B);
Pointer(Block *B, uint64_t BaseAndOffset);
Pointer(const Pointer &P);
@@ -161,9 +166,8 @@ class Pointer {
/// Creates a pointer to a field.
[[nodiscard]] Pointer atField(unsigned Off) const {
+ assert(isBlockPointer());
unsigned Field = Offset + Off;
- if (isIntegralPointer())
- return Pointer(asIntPointer().Value + Field, asIntPointer().Desc);
return Pointer(asBlockPointer().Pointee, Field, Field);
}
diff --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
index 9ec305d59c68c..13a5e082a125f 100644
--- a/clang/test/AST/Interp/c.c
+++ b/clang/test/AST/Interp/c.c
@@ -101,8 +101,6 @@ int somefunc(int i) {
// all-warning {{overflow in expression; result is 131'073 with type 'int'}}
}
-/// FIXME: The following test is incorrect in the new interpreter.
-/// The null pointer returns 16 from its getIntegerRepresentation().
#pragma clang diagnostic ignored "-Wpointer-to-int-cast"
struct ArrayStruct {
char n[1];
@@ -111,10 +109,7 @@ char name2[(int)&((struct ArrayStruct*)0)->n]; // expected-warning {{folded to c
// pedantic-expected-warning {{folded to constant array}} \
// ref-warning {{folded to constant array}} \
// pedantic-ref-warning {{folded to constant array}}
-_Static_assert(sizeof(name2) == 0, ""); // expected-error {{failed}} \
- // expected-note {{evaluates to}} \
- // pedantic-expected-error {{failed}} \
- // pedantic-expected-note {{evaluates to}}
+_Static_assert(sizeof(name2) == 0, "");
#ifdef __SIZEOF_INT128__
void *PR28739d = &(&PR28739d)[(__int128)(unsigned long)-1]; // all-warning {{refers past the last possible element}}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Instead of just adding the Record::Field offset, instead get the FieldDecl offset in the RecordLayout.
Unfortunately, the offset we pass to the ops here is not made to easily go back to a FieldDecl, so we have to iterate over the parent Record.