Skip to content

[clang] Add dump() support for lvalue APValues #124476

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 4 commits into from
Jan 27, 2025
Merged
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
30 changes: 28 additions & 2 deletions clang/lib/AST/TextNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,36 @@ void TextNodeDumper::Visit(const APValue &Value, QualType Ty) {
<< GetApproxValue(Value.getComplexFloatImag()) << 'i';
}
return;
case APValue::LValue:
case APValue::LValue: {
(void)Context;
OS << "LValue <todo>";
OS << "LValue Base=";
APValue::LValueBase B = Value.getLValueBase();
if (B.isNull())
OS << "null";
else if (const auto *BE = B.dyn_cast<const Expr *>()) {
OS << BE->getStmtClassName() << ' ';
dumpPointer(BE);
} else {
const auto *VDB = B.get<const ValueDecl *>();
OS << VDB->getDeclKindName() << "Decl";
dumpPointer(VDB);
}
OS << ", Null=" << Value.isNullPointer()
<< ", Offset=" << Value.getLValueOffset().getQuantity()
<< ", HasPath=" << Value.hasLValuePath();
if (Value.hasLValuePath()) {
OS << ", PathLength=" << Value.getLValuePath().size();
OS << ", Path=(";
llvm::ListSeparator Sep;
for (const auto &PathEntry : Value.getLValuePath()) {
// We're printing all entries as array indices because don't have the
// type information here to do anything else.
OS << Sep << PathEntry.getAsArrayIndex();
}
OS << ")";
}
return;
}
case APValue::Array: {
unsigned ArraySize = Value.getArraySize();
unsigned NumInitializedElements = Value.getArrayInitializedElts();
Expand Down
50 changes: 50 additions & 0 deletions clang/test/AST/ast-dump-APValue-lvalue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Test without serialization:
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -Wno-unused-value -std=gnu++17 \
// RUN: -ast-dump %s -ast-dump-filter Test \
// RUN: | FileCheck --strict-whitespace --match-full-lines %s
//
// Test with serialization:
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -Wno-unused-value -std=gnu++17 -emit-pch -o %t %s
// RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -Wno-unused-value -std=gnu++17 \
// RUN: -include-pch %t -ast-dump-all -ast-dump-filter Test /dev/null \
// RUN: | sed -e "s/ <undeserialized declarations>//" -e "s/ imported//" \
// RUN: | FileCheck --strict-whitespace --match-full-lines %s

int i;
struct S {
int i;
int ii;
};
S s;

struct F {
char padding[12];
S s;
};
F f;

void Test(int (&arr)[10]) {
constexpr int *pi = &i;
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} pi 'int *const' constexpr cinit
// CHECK-NEXT: | |-value: LValue Base=VarDecl {{.*}}, Null=0, Offset=0, HasPath=1, PathLength=0, Path=()

constexpr int *psi = &s.i;
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} psi 'int *const' constexpr cinit
// CHECK-NEXT: | |-value: LValue Base=VarDecl {{.*}}, Null=0, Offset=0, HasPath=1, PathLength=1, Path=({{.*}})

constexpr int *psii = &s.ii;
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} psii 'int *const' constexpr cinit
// CHECK-NEXT: | |-value: LValue Base=VarDecl {{.*}}, Null=0, Offset=4, HasPath=1, PathLength=1, Path=({{.*}})

constexpr int *pf = &f.s.ii;
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} pf 'int *const' constexpr cinit
// CHECK-NEXT: | |-value: LValue Base=VarDecl {{.*}}, Null=0, Offset=16, HasPath=1, PathLength=2, Path=({{.*}}, {{.*}})

constexpr char *pc = &f.padding[2];
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} pc 'char *const' constexpr cinit
// CHECK-NEXT: | |-value: LValue Base=VarDecl {{.*}}, Null=0, Offset=2, HasPath=1, PathLength=2, Path=({{.*}}, 2)

constexpr const int *n = nullptr;
// CHECK: `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} n 'const int *const' constexpr cinit
// CHECK-NEXT: |-value: LValue Base=null, Null=1, Offset=0, HasPath=1, PathLength=0, Path=()
}
4 changes: 0 additions & 4 deletions clang/test/AST/ast-dump-APValue-todo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ struct S {
};

void Test() {
constexpr int *pi = &i;
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} pi 'int *const' constexpr cinit
// CHECK-NEXT: | |-value: LValue <todo>

constexpr int(S::*pmi) = &S::i;
// CHECK: `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} pmi 'int (S::*const)' constexpr cinit
// CHECK-NEXT: |-value: MemberPointer <todo>
Expand Down
Loading