Skip to content

Commit 2f43ace

Browse files
committed
[clang][Interp] Fix expected values for Pointer API
Add a unit test for this. Differential Revision: https://reviews.llvm.org/D158069
1 parent 22b6b8d commit 2f43ace

File tree

5 files changed

+420
-1
lines changed

5 files changed

+420
-1
lines changed

clang/lib/AST/Interp/Context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class Context final {
8686
return false;
8787
}
8888

89+
/// Returns the program. This is only needed for unittests.
90+
Program &getProgram() const { return *P.get(); }
91+
8992
private:
9093
/// Runs a function.
9194
bool Run(State &Parent, const Function *Func, APValue &Result);

clang/lib/AST/Interp/Pointer.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ class Pointer {
7878
void operator=(const Pointer &P);
7979
void operator=(Pointer &&P);
8080

81+
/// Equality operators are just for tests.
82+
bool operator==(const Pointer &P) const {
83+
return Pointee == P.Pointee && Base == P.Base && Offset == P.Offset;
84+
}
85+
86+
bool operator!=(const Pointer &P) const {
87+
return Pointee != P.Pointee || Base != P.Base || Offset != P.Offset;
88+
}
89+
8190
/// Converts the pointer to an APValue.
8291
APValue toAPValue() const;
8392

@@ -276,7 +285,8 @@ class Pointer {
276285
const Record *getRecord() const { return getFieldDesc()->ElemRecord; }
277286
/// Returns the element record type, if this is a non-primive array.
278287
const Record *getElemRecord() const {
279-
return getFieldDesc()->ElemDesc->ElemRecord;
288+
const Descriptor *ElemDesc = getFieldDesc()->ElemDesc;
289+
return ElemDesc ? ElemDesc->ElemRecord : nullptr;
280290
}
281291
/// Returns the field information.
282292
const FieldDecl *getField() const { return getFieldDesc()->asFieldDecl(); }
@@ -326,6 +336,11 @@ class Pointer {
326336
int64_t getIndex() const {
327337
if (isElementPastEnd())
328338
return 1;
339+
340+
// narrow()ed element in a composite array.
341+
if (Base > 0 && Base == Offset)
342+
return 0;
343+
329344
if (auto ElemSize = elemSize())
330345
return getOffset() / ElemSize;
331346
return 0;

clang/unittests/AST/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ set(LLVM_LINK_COMPONENTS
55
)
66

77

8+
add_subdirectory(Interp)
9+
810
add_clang_unittest(ASTTests
911
ASTContextParentMapTest.cpp
1012
ASTExprTest.cpp
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_clang_unittest(InterpTests
2+
Descriptor.cpp
3+
)
4+
5+
clang_target_link_libraries(InterpTests
6+
PRIVATE
7+
clangAST
8+
clangBasic
9+
)
10+
11+
target_link_libraries(InterpTests
12+
PRIVATE
13+
clangTesting
14+
)

0 commit comments

Comments
 (0)