12#include "llvm/Support/raw_ostream.h"
27 return S.substr(0, Offset).str();
32 if (S.empty() || !
isDigit(S[0]))
35 while (Idx < S.size() &&
isDigit(S[Idx]))
37 (void)S.substr(0, Idx).getAsInteger(10, ULL);
43 std::ifstream Input(
Filename.str().c_str());
48 std::string CurrentType;
50 bool ExpectingType =
false;
52 while (Input.good()) {
56 StringRef LineStr(
Line);
59 if (LineStr.contains(
"*** Dumping AST Record Layout")) {
61 if (!CurrentType.empty())
62 Layouts[CurrentType] = CurrentLayout;
63 CurrentLayout = Layout();
71 ExpectingType =
false;
73 StringRef::size_type Pos;
74 if ((Pos = LineStr.find(
"struct ")) != StringRef::npos)
75 LineStr = LineStr.substr(Pos + strlen(
"struct "));
76 else if ((Pos = LineStr.find(
"class ")) != StringRef::npos)
77 LineStr = LineStr.substr(Pos + strlen(
"class "));
78 else if ((Pos = LineStr.find(
"union ")) != StringRef::npos)
79 LineStr = LineStr.substr(Pos + strlen(
"union "));
85 CurrentLayout = Layout();
90 StringRef::size_type Pos = LineStr.find(
" Size:");
91 if (Pos != StringRef::npos) {
93 LineStr = LineStr.substr(Pos + strlen(
" Size:"));
95 unsigned long long Size = 0;
97 CurrentLayout.Size = Size;
102 Pos = LineStr.find(
"Alignment:");
103 if (Pos != StringRef::npos) {
105 LineStr = LineStr.substr(Pos + strlen(
"Alignment:"));
107 unsigned long long Alignment = 0;
109 CurrentLayout.Align = Alignment;
115 Pos = LineStr.find(
"sizeof=");
116 if (Pos != StringRef::npos) {
118 LineStr = LineStr.substr(Pos + strlen(
"sizeof="));
121 unsigned long long Size = 0;
123 CurrentLayout.Size = Size * 8;
125 Pos = LineStr.find(
"align=");
126 if (Pos != StringRef::npos) {
128 LineStr = LineStr.substr(Pos + strlen(
"align="));
131 unsigned long long Alignment = 0;
133 CurrentLayout.Align = Alignment * 8;
140 Pos = LineStr.find(
"FieldOffsets: [");
141 if (Pos != StringRef::npos) {
142 LineStr = LineStr.substr(Pos + strlen(
"FieldOffsets: ["));
143 while (!LineStr.empty() &&
isDigit(LineStr[0])) {
144 unsigned long long Offset = 0;
146 CurrentLayout.FieldOffsets.push_back(Offset);
149 LineStr = LineStr.substr(1);
151 LineStr = LineStr.substr(1);
156 Pos = LineStr.find(
"VBaseOffsets: [");
157 if (Pos != StringRef::npos) {
158 LineStr = LineStr.substr(Pos + strlen(
"VBaseOffsets: ["));
159 while (!LineStr.empty() &&
isDigit(LineStr[0])) {
160 unsigned long long Offset = 0;
165 LineStr = LineStr.substr(1);
167 LineStr = LineStr.substr(1);
173 Pos = LineStr.find(
"BaseOffsets: [");
174 if (Pos != StringRef::npos) {
175 LineStr = LineStr.substr(Pos + strlen(
"BaseOffsets: ["));
176 while (!LineStr.empty() &&
isDigit(LineStr[0])) {
177 unsigned long long Offset = 0;
182 LineStr = LineStr.substr(1);
184 LineStr = LineStr.substr(1);
190 if (!CurrentType.empty())
191 Layouts[CurrentType] = CurrentLayout;
196 uint64_t &Size, uint64_t &Alignment,
197 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
198 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
199 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
202 if (!Record->getIdentifier())
206 llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());
207 if (Known == Layouts.end())
211 unsigned NumFields = 0;
213 FEnd = Record->field_end();
214 F != FEnd; ++F, ++NumFields) {
215 if (NumFields >= Known->second.FieldOffsets.size())
218 FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
222 if (NumFields != Known->second.FieldOffsets.size())
226 if (
const auto *RD = dyn_cast<CXXRecordDecl>(Record)) {
229 for (
const auto &I : RD->vbases()) {
230 if (NumVB >= Known->second.VBaseOffsets.size())
232 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
233 VirtualBaseOffsets[VBase] = Known->second.VBaseOffsets[NumVB++];
235 for (
const auto &I : RD->bases()) {
236 if (I.isVirtual() || NumNB >= Known->second.BaseOffsets.size())
239 BaseOffsets[
Base] = Known->second.BaseOffsets[NumNB++];
243 Size = Known->second.Size;
244 Alignment = Known->second.Align;
249 raw_ostream &OS = llvm::errs();
250 for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
251 LEnd = Layouts.end();
253 OS <<
"Type: blah " << L->first() <<
'\n';
254 OS <<
" Size:" << L->second.Size <<
'\n';
255 OS <<
" Alignment:" << L->second.Align <<
'\n';
256 OS <<
" FieldOffsets: [";
257 for (
unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
260 OS << L->second.FieldOffsets[I];
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static bool parseUnsigned(StringRef &S, unsigned long long &ULL)
Parse an unsigned integer and move S to the next non-digit character.
static std::string parseName(StringRef S)
Parse a simple identifier.
Represents a C++ struct/union/class.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
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_].