Skip to content

Commit ace83da

Browse files
committed
[clang][Interp][NFC] Improve Program dump()ing
Add colors as well as more details for global variables.
1 parent b014944 commit ace83da

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

clang/lib/AST/Interp/Descriptor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ struct Descriptor final {
213213
bool isRecord() const { return !IsArray && ElemRecord; }
214214
/// Checks if this is a dummy descriptor.
215215
bool isDummy() const { return IsDummy; }
216+
217+
void dump() const;
218+
void dump(llvm::raw_ostream &OS) const;
216219
};
217220

218221
/// Bitfield tracking the initialisation status of elements of primitive arrays.

clang/lib/AST/Interp/Disasm.cpp

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "Opcode.h"
1717
#include "PrimType.h"
1818
#include "Program.h"
19+
#include "clang/AST/ASTDumperUtils.h"
1920
#include "clang/AST/DeclCXX.h"
2021
#include "llvm/Support/Compiler.h"
2122
#include "llvm/Support/Format.h"
@@ -55,7 +56,10 @@ inline IntegralAP<true> ReadArg<IntegralAP<true>>(Program &P, CodePtr &OpPC) {
5556
LLVM_DUMP_METHOD void Function::dump() const { dump(llvm::errs()); }
5657

5758
LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
58-
OS << getName() << " " << (const void *)this << "\n";
59+
{
60+
ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_GREEN, true});
61+
OS << getName() << " " << (const void *)this << "\n";
62+
}
5963
OS << "frame size: " << getFrameSize() << "\n";
6064
OS << "arg size: " << getArgSize() << "\n";
6165
OS << "rvo: " << hasRVO() << "\n";
@@ -83,14 +87,67 @@ LLVM_DUMP_METHOD void Function::dump(llvm::raw_ostream &OS) const {
8387
LLVM_DUMP_METHOD void Program::dump() const { dump(llvm::errs()); }
8488

8589
LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
86-
OS << ":: Program\n";
87-
OS << "Global Variables: " << Globals.size() << "\n";
88-
OS << "Functions: " << Funcs.size() << "\n";
89-
OS << "\n";
90-
for (auto &Func : Funcs) {
90+
{
91+
ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_RED, true});
92+
OS << "\n:: Program\n";
93+
}
94+
95+
{
96+
ColorScope SC(OS, true, {llvm::raw_ostream::WHITE, true});
97+
OS << "Total memory : " << Allocator.getTotalMemory() << " bytes\n";
98+
OS << "Global Variables: " << Globals.size() << "\n";
99+
}
100+
unsigned GI = 0;
101+
for (const Global *G : Globals) {
102+
const Descriptor *Desc = G->block()->getDescriptor();
103+
OS << GI << ": " << (void *)G->block() << " ";
104+
Desc->dump(OS);
105+
OS << "\n";
106+
++GI;
107+
}
108+
109+
{
110+
ColorScope SC(OS, true, {llvm::raw_ostream::WHITE, true});
111+
OS << "Functions: " << Funcs.size() << "\n";
112+
}
113+
for (const auto &Func : Funcs) {
91114
Func.second->dump();
92115
}
93-
for (auto &Anon : AnonFuncs) {
116+
for (const auto &Anon : AnonFuncs) {
94117
Anon->dump();
95118
}
96119
}
120+
121+
LLVM_DUMP_METHOD void Descriptor::dump() const {
122+
dump(llvm::errs());
123+
llvm::errs() << '\n';
124+
}
125+
126+
LLVM_DUMP_METHOD void Descriptor::dump(llvm::raw_ostream &OS) const {
127+
// Source
128+
{
129+
ColorScope SC(OS, true, {llvm::raw_ostream::BLUE, true});
130+
if (const auto *ND = dyn_cast_if_present<NamedDecl>(asDecl()))
131+
OS << ND->getName();
132+
else if (asExpr())
133+
OS << "expr (TODO)";
134+
}
135+
136+
// Print a few interesting bits about the descriptor.
137+
if (isPrimitiveArray())
138+
OS << " primitive-array";
139+
else if (isCompositeArray())
140+
OS << " composite-array";
141+
else if (isRecord())
142+
OS << " record";
143+
else if (isPrimitive())
144+
OS << " primitive";
145+
146+
if (isZeroSizeArray())
147+
OS << " zero-size-arrary";
148+
else if (isUnknownSizeArray())
149+
OS << " unknown-size-array";
150+
151+
if (isDummy())
152+
OS << " dummy";
153+
}

clang/lib/AST/Interp/Program.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class Program final {
190190
std::byte *data() { return B.data(); }
191191
/// Return a pointer to the block.
192192
Block *block() { return &B; }
193+
const Block *block() const { return &B; }
193194

194195
private:
195196
/// Required metadata - does not actually track pointers.

0 commit comments

Comments
 (0)