12 #include "llvm/Support/raw_ostream.h" 16 using namespace clang;
27 return S.substr(0, Offset).str();
31 std::ifstream Input(Filename.str().c_str());
36 std::string CurrentType;
38 bool ExpectingType =
false;
40 while (Input.good()) {
44 StringRef LineStr(Line);
47 if (LineStr.find(
"*** Dumping AST Record Layout") != StringRef::npos) {
49 if (!CurrentType.empty())
50 Layouts[CurrentType] = CurrentLayout;
51 CurrentLayout = Layout();
59 ExpectingType =
false;
61 StringRef::size_type Pos;
62 if ((Pos = LineStr.find(
"struct ")) != StringRef::npos)
63 LineStr = LineStr.substr(Pos + strlen(
"struct "));
64 else if ((Pos = LineStr.find(
"class ")) != StringRef::npos)
65 LineStr = LineStr.substr(Pos + strlen(
"class "));
66 else if ((Pos = LineStr.find(
"union ")) != StringRef::npos)
67 LineStr = LineStr.substr(Pos + strlen(
"union "));
73 CurrentLayout = Layout();
78 StringRef::size_type Pos = LineStr.find(
" Size:");
79 if (Pos != StringRef::npos) {
81 LineStr = LineStr.substr(Pos + strlen(
" Size:"));
83 unsigned long long Size = 0;
84 (void)LineStr.getAsInteger(10, Size);
85 CurrentLayout.Size = Size;
90 Pos = LineStr.find(
"Alignment:");
91 if (Pos != StringRef::npos) {
93 LineStr = LineStr.substr(Pos + strlen(
"Alignment:"));
95 unsigned long long Alignment = 0;
96 (void)LineStr.getAsInteger(10, Alignment);
97 CurrentLayout.Align = Alignment;
102 Pos = LineStr.find(
"sizeof=");
103 if (Pos != StringRef::npos) {
105 LineStr = LineStr.substr(Pos + strlen(
"sizeof="));
108 unsigned long long Size = 0;
109 (void)LineStr.getAsInteger(10, Size);
110 CurrentLayout.Size = Size;
112 Pos = LineStr.find(
"align=");
113 if (Pos != StringRef::npos) {
115 LineStr = LineStr.substr(Pos + strlen(
"align="));
118 unsigned long long Alignment = 0;
119 (void)LineStr.getAsInteger(10, Alignment);
120 CurrentLayout.Align = Alignment;
127 Pos = LineStr.find(
"FieldOffsets: [");
128 if (Pos == StringRef::npos)
131 LineStr = LineStr.substr(Pos + strlen(
"FieldOffsets: ["));
132 while (!LineStr.empty() &&
isDigit(LineStr[0])) {
135 while (Idx < LineStr.size() &&
isDigit(LineStr[Idx]))
138 unsigned long long Offset = 0;
139 (void)LineStr.substr(0, Idx).getAsInteger(10, Offset);
141 CurrentLayout.FieldOffsets.push_back(Offset);
144 LineStr = LineStr.substr(Idx + 1);
146 LineStr = LineStr.substr(1);
151 if (!CurrentType.empty())
152 Layouts[CurrentType] = CurrentLayout;
157 uint64_t &Size, uint64_t &Alignment,
158 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
159 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
160 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
167 llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->
getName());
168 if (Known == Layouts.end())
172 unsigned NumFields = 0;
175 F != FEnd; ++F, ++NumFields) {
176 if (NumFields >= Known->second.FieldOffsets.size())
179 FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
183 if (NumFields != Known->second.FieldOffsets.size())
186 Size = Known->second.Size;
187 Alignment = Known->second.Align;
192 raw_ostream &OS = llvm::errs();
193 for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
194 LEnd = Layouts.end();
196 OS <<
"Type: blah " << L->first() <<
'\n';
197 OS <<
" Size:" << L->second.Size <<
'\n';
198 OS <<
" Alignment:" << L->second.Align <<
'\n';
199 OS <<
" FieldOffsets: [";
200 for (
unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
203 OS << L->second.FieldOffsets[I];
static std::string parseName(StringRef S)
Parse a simple identifier.
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
RecordDecl - Represents a struct/union/class.
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.
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t', '\f', '\v', '\n', '\r'.
field_iterator field_begin() const
LLVM_READONLY bool isIdentifierHead(unsigned char c, bool AllowDollar=false)
Returns true if this is a valid first character of a C identifier, which is [a-zA-Z_].
void dump()
Dump the overridden layouts.
const AnnotatedLine * Line
field_iterator field_end() const
Dataflow Directional Tag Classes.
LLVM_READONLY bool isIdentifierBody(unsigned char c, bool AllowDollar=false)
Returns true if this is a body character of a C identifier, which is [a-zA-Z0-9_].
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext, providing only those that are of type SpecificDecl (or a class derived from it).
LayoutOverrideSource(StringRef Filename)
Create a new AST source that overrides the layout of some set of record types.
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.