Skip to content

Commit acff0b0

Browse files
committed
[clang][Interp][NFC] Improve Record debugging
Add Record::dump() and return the diagnostic name from getName()
1 parent 5d1f779 commit acff0b0

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

clang/lib/AST/Interp/Disasm.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,34 @@ LLVM_DUMP_METHOD void InterpFrame::dump(llvm::raw_ostream &OS,
233233
F = F->Caller;
234234
}
235235
}
236+
237+
LLVM_DUMP_METHOD void Record::dump(llvm::raw_ostream &OS, unsigned Indentation,
238+
unsigned Offset) const {
239+
unsigned Indent = Indentation * 2;
240+
OS.indent(Indent);
241+
{
242+
ColorScope SC(OS, true, {llvm::raw_ostream::BLUE, true});
243+
OS << getName() << "\n";
244+
}
245+
246+
unsigned I = 0;
247+
for (const Record::Base &B : bases()) {
248+
OS.indent(Indent) << "- Base " << I << ". Offset " << (Offset + B.Offset)
249+
<< "\n";
250+
B.R->dump(OS, Indentation + 1, Offset + B.Offset);
251+
++I;
252+
}
253+
254+
// FIXME: Virtual bases.
255+
256+
I = 0;
257+
for (const Record::Field &F : fields()) {
258+
OS.indent(Indent) << "- Field " << I << ": ";
259+
{
260+
ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_RED, true});
261+
OS << F.Decl->getName();
262+
}
263+
OS << ". Offset " << (Offset + F.Offset) << "\n";
264+
++I;
265+
}
266+
}

clang/lib/AST/Interp/Record.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "Record.h"
10+
#include "clang/AST/ASTContext.h"
1011

1112
using namespace clang;
1213
using namespace clang::interp;
@@ -27,6 +28,14 @@ Record::Record(const RecordDecl *Decl, BaseList &&SrcBases,
2728
VirtualBaseMap[V.Decl] = &V;
2829
}
2930

31+
const std::string Record::getName() const {
32+
std::string Ret;
33+
llvm::raw_string_ostream OS(Ret);
34+
Decl->getNameForDiagnostic(OS, Decl->getASTContext().getPrintingPolicy(),
35+
/*Qualified=*/true);
36+
return Ret;
37+
}
38+
3039
const Record::Field *Record::getField(const FieldDecl *FD) const {
3140
auto It = FieldMap.find(FD);
3241
assert(It != FieldMap.end() && "Missing field");

clang/lib/AST/Interp/Record.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Record final {
5151
/// Returns the underlying declaration.
5252
const RecordDecl *getDecl() const { return Decl; }
5353
/// Returns the name of the underlying declaration.
54-
const std::string getName() const { return Decl->getNameAsString(); }
54+
const std::string getName() const;
5555
/// Checks if the record is a union.
5656
bool isUnion() const { return getDecl()->isUnion(); }
5757
/// Returns the size of the record.
@@ -100,6 +100,10 @@ class Record final {
100100
unsigned getNumVirtualBases() const { return VirtualBases.size(); }
101101
const Base *getVirtualBase(unsigned I) const { return &VirtualBases[I]; }
102102

103+
void dump(llvm::raw_ostream &OS, unsigned Indentation = 0,
104+
unsigned Offset = 0) const;
105+
void dump() const { dump(llvm::errs()); }
106+
103107
private:
104108
/// Constructor used by Program to create record descriptors.
105109
Record(const RecordDecl *, BaseList &&Bases, FieldList &&Fields,

0 commit comments

Comments
 (0)