[cfe-commits] r82733 - /cfe/trunk/lib/Frontend/ASTConsumers.cpp
Anders Carlsson
andersca at mac.com
Thu Sep 24 16:50:43 PDT 2009
Author: andersca
Date: Thu Sep 24 18:50:42 2009
New Revision: 82733
URL: http://llvm.org/viewvc/llvm-project?rev=82733&view=rev
Log:
Improve the record layout dumper.
Modified:
cfe/trunk/lib/Frontend/ASTConsumers.cpp
Modified: cfe/trunk/lib/Frontend/ASTConsumers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTConsumers.cpp?rev=82733&r1=82732&r2=82733&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTConsumers.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTConsumers.cpp Thu Sep 24 18:50:42 2009
@@ -24,6 +24,7 @@
#include "clang/AST/PrettyPrinter.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "llvm/Module.h"
+#include "llvm/Support/Format.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Path.h"
@@ -425,11 +426,95 @@
class RecordLayoutDumper : public ASTConsumer {
llvm::raw_ostream& Out;
- // FIXME: Maybe this be useful in ASTContext.cpp.
+ void PrintOffset(uint64_t Offset, unsigned IndentLevel) {
+ Out << llvm::format("%4d | ", Offset);
+ for (unsigned I = 0; I < IndentLevel * 2; ++I) Out << ' ';
+ }
+
+ void DumpRecordLayoutOffsets(const CXXRecordDecl *RD, ASTContext &C,
+ uint64_t Offset,
+ unsigned IndentLevel, const char* Description,
+ bool IncludeVirtualBases) {
+ const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
+
+ PrintOffset(Offset, IndentLevel);
+ Out << C.getTypeDeclType((CXXRecordDecl *)RD).getAsString();
+ if (Description)
+ Out << ' ' << Description;
+ if (RD->isEmpty())
+ Out << " (empty)";
+ Out << '\n';
+
+ IndentLevel++;
+
+ const CXXRecordDecl *PrimaryBase = Info.getPrimaryBase();
+
+ // Vtable pointer.
+ if (RD->isDynamicClass() && !PrimaryBase) {
+ PrintOffset(Offset, IndentLevel);
+ Out << '(' << RD->getNameAsString() << " vtable pointer)\n";
+ }
+ // Dump (non-virtual) bases
+ for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
+ E = RD->bases_end(); I != E; ++I) {
+ if (I->isVirtual())
+ continue;
+
+ const CXXRecordDecl *Base =
+ cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
+
+ uint64_t BaseOffset = Offset + Info.getBaseClassOffset(Base) / 8;
+
+ DumpRecordLayoutOffsets(Base, C, BaseOffset, IndentLevel,
+ Base == PrimaryBase ? "(primary base)" : "(base)",
+ /*IncludeVirtualBases=*/false);
+ }
+
+ // Dump fields.
+ uint64_t FieldNo = 0;
+ for (CXXRecordDecl::field_iterator I = RD->field_begin(),
+ E = RD->field_end(); I != E; ++I, ++FieldNo) {
+ const FieldDecl *Field = *I;
+ uint64_t FieldOffset = Offset + Info.getFieldOffset(FieldNo) / 8;
+
+ if (const RecordType *RT = Field->getType()->getAs<RecordType>()) {
+ if (const CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
+ DumpRecordLayoutOffsets(D, C, FieldOffset, IndentLevel,
+ Field->getNameAsCString(),
+ /*IncludeVirtualBases=*/true);
+ continue;
+ }
+ }
+
+ PrintOffset(FieldOffset, IndentLevel);
+ Out << Field->getType().getAsString() << ' ';
+ Out << Field->getNameAsString() << '\n';
+ }
+
+ if (!IncludeVirtualBases)
+ return;
+
+ // Dump virtual bases.
+ for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
+ E = RD->vbases_end(); I != E; ++I) {
+ assert(I->isVirtual() && "Found non-virtual class!");
+ const CXXRecordDecl *VBase =
+ cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
+
+ uint64_t VBaseOffset = Offset + Info.getVBaseClassOffset(VBase) / 8;
+ DumpRecordLayoutOffsets(VBase, C, VBaseOffset, IndentLevel,
+ VBase == PrimaryBase ?
+ "(primary virtual base)" : "(virtual base)",
+ /*IncludeVirtualBases=*/false);
+ }
+ }
+
+ // FIXME: Maybe this could be useful in ASTContext.cpp.
void DumpRecordLayout(const CXXRecordDecl *RD, ASTContext &C) {
const ASTRecordLayout &Info = C.getASTRecordLayout(RD);
- Out << RD->getKindName() << ' ' << RD->getQualifiedNameAsString() << '\n';
+ DumpRecordLayoutOffsets(RD, C, 0, 0, 0,
+ /*IncludeVirtualBases=*/true);
Out << " sizeof=" << Info.getSize() / 8;
Out << ", dsize=" << Info.getDataSize() / 8;
Out << ", align=" << Info.getAlignment() / 8 << '\n';
More information about the cfe-commits
mailing list