13#include "llvm/Support/raw_ostream.h"
28 return S.substr(0, Offset).str();
33 if (S.empty() || !
isDigit(S[0]))
36 while (Idx < S.size() &&
isDigit(S[Idx]))
38 (void)S.substr(0, Idx).getAsInteger(10, ULL);
44 std::ifstream Input(
Filename.str().c_str());
49 std::string CurrentType;
51 bool ExpectingType =
false;
53 while (Input.good()) {
57 StringRef LineStr(
Line);
60 if (LineStr.contains(
"*** Dumping AST Record Layout")) {
62 if (!CurrentType.empty())
63 Layouts[CurrentType] = CurrentLayout;
64 CurrentLayout = Layout();
72 ExpectingType =
false;
74 StringRef::size_type Pos;
75 if ((Pos = LineStr.find(
"struct ")) != StringRef::npos)
76 LineStr = LineStr.substr(Pos + strlen(
"struct "));
77 else if ((Pos = LineStr.find(
"class ")) != StringRef::npos)
78 LineStr = LineStr.substr(Pos + strlen(
"class "));
79 else if ((Pos = LineStr.find(
"union ")) != StringRef::npos)
80 LineStr = LineStr.substr(Pos + strlen(
"union "));
86 CurrentLayout = Layout();
91 StringRef::size_type Pos = LineStr.find(
" Size:");
92 if (Pos != StringRef::npos) {
94 LineStr = LineStr.substr(Pos + strlen(
" Size:"));
96 unsigned long long Size = 0;
98 CurrentLayout.Size = Size;
103 Pos = LineStr.find(
"Alignment:");
104 if (Pos != StringRef::npos) {
106 LineStr = LineStr.substr(Pos + strlen(
"Alignment:"));
108 unsigned long long Alignment = 0;
110 CurrentLayout.Align = Alignment;
116 Pos = LineStr.find(
"sizeof=");
117 if (Pos != StringRef::npos) {
119 LineStr = LineStr.substr(Pos + strlen(
"sizeof="));
122 unsigned long long Size = 0;
124 CurrentLayout.Size = Size * 8;
126 Pos = LineStr.find(
"align=");
127 if (Pos != StringRef::npos) {
129 LineStr = LineStr.substr(Pos + strlen(
"align="));
132 unsigned long long Alignment = 0;
134 CurrentLayout.Align = Alignment * 8;
141 Pos = LineStr.find(
"FieldOffsets: [");
142 if (Pos != StringRef::npos) {
143 LineStr = LineStr.substr(Pos + strlen(
"FieldOffsets: ["));
144 while (!LineStr.empty() &&
isDigit(LineStr[0])) {
145 unsigned long long Offset = 0;
147 CurrentLayout.FieldOffsets.push_back(Offset);
150 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);
172 Pos = LineStr.find(
"BaseOffsets: [");
173 if (Pos != StringRef::npos) {
174 LineStr = LineStr.substr(Pos + strlen(
"BaseOffsets: ["));
175 while (!LineStr.empty() &&
isDigit(LineStr[0])) {
176 unsigned long long Offset = 0;
181 LineStr = LineStr.substr(1);
188 if (!CurrentType.empty())
189 Layouts[CurrentType] = CurrentLayout;
194 uint64_t &Size, uint64_t &Alignment,
195 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
196 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
197 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
200 if (!
Record->getIdentifier())
204 llvm::StringMap<Layout>::iterator Known = Layouts.find(
Record->getName());
205 if (Known == Layouts.end())
209 unsigned NumFields = 0;
211 FEnd =
Record->field_end();
212 F != FEnd; ++F, ++NumFields) {
213 if (NumFields >= Known->second.FieldOffsets.size())
216 FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
220 if (NumFields != Known->second.FieldOffsets.size())
224 if (
const auto *RD = dyn_cast<CXXRecordDecl>(
Record)) {
227 for (
const auto &I : RD->vbases()) {
228 if (NumVB >= Known->second.VBaseOffsets.size())
230 const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
231 VirtualBaseOffsets[VBase] = Known->second.VBaseOffsets[NumVB++];
233 for (
const auto &I : RD->bases()) {
234 if (I.isVirtual() || NumNB >= Known->second.BaseOffsets.size())
237 BaseOffsets[
Base] = Known->second.BaseOffsets[NumNB++];
241 Size = Known->second.Size;
242 Alignment = Known->second.Align;
247 raw_ostream &OS = llvm::errs();
248 for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
249 LEnd = Layouts.end();
251 OS <<
"Type: blah " << L->first() <<
'\n';
252 OS <<
" Size:" << L->second.Size <<
'\n';
253 OS <<
" Alignment:" << L->second.Align <<
'\n';
254 OS <<
" FieldOffsets: [";
255 for (
unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
258 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.
llvm::MachO::Record Record
C Language Family Type Representation.
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.
The JSON file list parser is used to communicate input to InstallAPI.
LLVM_READONLY bool isAsciiIdentifierContinue(unsigned char c)
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 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_].