11#include "llvm/Support/raw_ostream.h"
26 return S.substr(0,
Offset).str();
30 std::ifstream Input(
Filename.str().c_str());
35 std::string CurrentType;
37 bool ExpectingType =
false;
39 while (Input.good()) {
43 StringRef LineStr(Line);
46 if (LineStr.contains(
"*** Dumping AST Record Layout")) {
48 if (!CurrentType.empty())
49 Layouts[CurrentType] = CurrentLayout;
50 CurrentLayout = Layout();
58 ExpectingType =
false;
60 StringRef::size_type Pos;
61 if ((Pos = LineStr.find(
"struct ")) != StringRef::npos)
62 LineStr = LineStr.substr(Pos + strlen(
"struct "));
63 else if ((Pos = LineStr.find(
"class ")) != StringRef::npos)
64 LineStr = LineStr.substr(Pos + strlen(
"class "));
65 else if ((Pos = LineStr.find(
"union ")) != StringRef::npos)
66 LineStr = LineStr.substr(Pos + strlen(
"union "));
72 CurrentLayout = Layout();
77 StringRef::size_type Pos = LineStr.find(
" Size:");
78 if (Pos != StringRef::npos) {
80 LineStr = LineStr.substr(Pos + strlen(
" Size:"));
82 unsigned long long Size = 0;
83 (void)LineStr.getAsInteger(10, Size);
84 CurrentLayout.Size = Size;
89 Pos = LineStr.find(
"Alignment:");
90 if (Pos != StringRef::npos) {
92 LineStr = LineStr.substr(Pos + strlen(
"Alignment:"));
94 unsigned long long Alignment = 0;
95 (void)LineStr.getAsInteger(10, Alignment);
96 CurrentLayout.Align = Alignment;
101 Pos = LineStr.find(
"sizeof=");
102 if (Pos != StringRef::npos) {
104 LineStr = LineStr.substr(Pos + strlen(
"sizeof="));
107 unsigned long long Size = 0;
108 (void)LineStr.getAsInteger(10, Size);
109 CurrentLayout.Size = Size;
111 Pos = LineStr.find(
"align=");
112 if (Pos != StringRef::npos) {
114 LineStr = LineStr.substr(Pos + strlen(
"align="));
117 unsigned long long Alignment = 0;
118 (void)LineStr.getAsInteger(10, Alignment);
119 CurrentLayout.Align = Alignment;
126 Pos = LineStr.find(
"FieldOffsets: [");
127 if (Pos == StringRef::npos)
130 LineStr = LineStr.substr(Pos + strlen(
"FieldOffsets: ["));
131 while (!LineStr.empty() &&
isDigit(LineStr[0])) {
134 while (Idx < LineStr.size() &&
isDigit(LineStr[Idx]))
137 unsigned long long Offset = 0;
138 (void)LineStr.substr(0, Idx).getAsInteger(10,
Offset);
140 CurrentLayout.FieldOffsets.push_back(
Offset);
143 LineStr = LineStr.substr(Idx + 1);
145 LineStr = LineStr.substr(1);
150 if (!CurrentType.empty())
151 Layouts[CurrentType] = CurrentLayout;
156 uint64_t &Size, uint64_t &Alignment,
157 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
158 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
159 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
162 if (!Record->getIdentifier())
166 llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());
167 if (Known == Layouts.end())
171 unsigned NumFields = 0;
173 FEnd = Record->field_end();
174 F != FEnd; ++F, ++NumFields) {
175 if (NumFields >= Known->second.FieldOffsets.size())
178 FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
182 if (NumFields != Known->second.FieldOffsets.size())
185 Size = Known->second.Size;
186 Alignment = Known->second.Align;
191 raw_ostream &OS = llvm::errs();
192 for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
193 LEnd = Layouts.end();
195 OS <<
"Type: blah " << L->first() <<
'\n';
196 OS <<
" Size:" << L->second.Size <<
'\n';
197 OS <<
" Alignment:" << L->second.Align <<
'\n';
198 OS <<
" FieldOffsets: [";
199 for (
unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
202 OS << L->second.FieldOffsets[I];
static std::string parseName(StringRef S)
Parse a simple identifier.
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext,...
bool layoutRecordType(const RecordDecl *Record, uint64_t &Size, uint64_t &Alignment, llvm::DenseMap< const FieldDecl *, uint64_t > &FieldOffsets, llvm::DenseMap< const CXXRecordDecl *, CharUnits > &BaseOffsets, llvm::DenseMap< const CXXRecordDecl *, CharUnits > &VirtualBaseOffsets) override
If this particular record type has an overridden layout, return that layout.
void dump()
Dump the overridden layouts.
LayoutOverrideSource(StringRef Filename)
Create a new AST source that overrides the layout of some set of record types.
Represents a struct/union/class.
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t',...
LLVM_READONLY bool isAsciiIdentifierContinue(unsigned char c, bool AllowDollar=false)
Returns true if this is a body character of a C identifier, which is [a-zA-Z0-9_].
LLVM_READONLY bool isAsciiIdentifierStart(unsigned char c, bool AllowDollar=false)
Returns true if this is a valid first character of a C identifier, which is [a-zA-Z_].