1 //===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "clang/Frontend/LayoutOverrideSource.h"
10 #include "clang/AST/Decl.h"
11 #include "llvm/Support/raw_ostream.h"
15 using namespace clang;
17 /// \brief Parse a simple identifier.
18 static std::string parseName(StringRef S) {
20 while (Offset < S.size() &&
21 (isalpha(S[Offset]) || S[Offset] == '_' ||
22 (Offset > 0 && isdigit(S[Offset]))))
25 return S.substr(0, Offset).str();
28 LayoutOverrideSource::LayoutOverrideSource(llvm::StringRef Filename) {
29 std::ifstream Input(Filename.str().c_str());
33 // Parse the output of -fdump-record-layouts.
34 std::string CurrentType;
36 bool ExpectingType = false;
38 while (Input.good()) {
42 StringRef LineStr(Line);
44 // Determine whether the following line will start a
45 if (LineStr.find("*** Dumping AST Record Layout") != StringRef::npos) {
46 // Flush the last type/layout, if there is one.
47 if (!CurrentType.empty())
48 Layouts[CurrentType] = CurrentLayout;
49 CurrentLayout = Layout();
55 // If we're expecting a type, grab it.
57 ExpectingType = false;
59 StringRef::size_type Pos;
60 if ((Pos = LineStr.find("struct ")) != StringRef::npos)
61 LineStr = LineStr.substr(Pos + strlen("struct "));
62 else if ((Pos = LineStr.find("class ")) != StringRef::npos)
63 LineStr = LineStr.substr(Pos + strlen("class "));
64 else if ((Pos = LineStr.find("union ")) != StringRef::npos)
65 LineStr = LineStr.substr(Pos + strlen("union "));
69 // Find the name of the type.
70 CurrentType = parseName(LineStr);
71 CurrentLayout = Layout();
75 // Check for the size of the type.
76 StringRef::size_type Pos = LineStr.find(" Size:");
77 if (Pos != StringRef::npos) {
78 // Skip past the " Size:" prefix.
79 LineStr = LineStr.substr(Pos + strlen(" Size:"));
81 unsigned long long Size = 0;
82 (void)LineStr.getAsInteger(10, Size);
83 CurrentLayout.Size = Size;
87 // Check for the alignment of the type.
88 Pos = LineStr.find("Alignment:");
89 if (Pos != StringRef::npos) {
90 // Skip past the "Alignment:" prefix.
91 LineStr = LineStr.substr(Pos + strlen("Alignment:"));
93 unsigned long long Alignment = 0;
94 (void)LineStr.getAsInteger(10, Alignment);
95 CurrentLayout.Align = Alignment;
99 // Check for the size/alignment of the type.
100 Pos = LineStr.find("sizeof=");
101 if (Pos != StringRef::npos) {
102 /* Skip past the sizeof= prefix. */
103 LineStr = LineStr.substr(Pos + strlen("sizeof="));
106 unsigned long long Size = 0;
107 (void)LineStr.getAsInteger(10, Size);
108 CurrentLayout.Size = Size;
110 Pos = LineStr.find("align=");
111 if (Pos != StringRef::npos) {
112 /* Skip past the align= prefix. */
113 LineStr = LineStr.substr(Pos + strlen("align="));
116 unsigned long long Alignment = 0;
117 (void)LineStr.getAsInteger(10, Alignment);
118 CurrentLayout.Align = Alignment;
124 // Check for the field offsets of the type.
125 Pos = LineStr.find("FieldOffsets: [");
126 if (Pos == StringRef::npos)
129 LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));
130 while (!LineStr.empty() && isdigit(LineStr[0])) {
131 // Parse this offset.
133 while (Idx < LineStr.size() && isdigit(LineStr[Idx]))
136 unsigned long long Offset = 0;
137 (void)LineStr.substr(0, Idx).getAsInteger(10, Offset);
139 CurrentLayout.FieldOffsets.push_back(Offset);
141 // Skip over this offset, the following comma, and any spaces.
142 LineStr = LineStr.substr(Idx + 1);
143 while (!LineStr.empty() && isspace(LineStr[0]))
144 LineStr = LineStr.substr(1);
148 // Flush the last type/layout, if there is one.
149 if (!CurrentType.empty())
150 Layouts[CurrentType] = CurrentLayout;
154 LayoutOverrideSource::layoutRecordType(const RecordDecl *Record,
155 uint64_t &Size, uint64_t &Alignment,
156 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
157 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
158 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
160 // We can't override unnamed declarations.
161 if (!Record->getIdentifier())
164 // Check whether we have a layout for this record.
165 llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());
166 if (Known == Layouts.end())
169 // Provide field layouts.
170 unsigned NumFields = 0;
171 for (RecordDecl::field_iterator F = Record->field_begin(),
172 FEnd = Record->field_end();
173 F != FEnd; ++F, ++NumFields) {
174 if (NumFields >= Known->second.FieldOffsets.size())
177 FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
180 // Wrong number of fields.
181 if (NumFields != Known->second.FieldOffsets.size())
184 Size = Known->second.Size;
185 Alignment = Known->second.Align;
189 void LayoutOverrideSource::dump() {
190 llvm::raw_ostream &OS = llvm::errs();
191 for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
192 LEnd = Layouts.end();
194 OS << "Type: blah " << L->first() << '\n';
195 OS << " Size:" << L->second.Size << '\n';
196 OS << " Alignment:" << L->second.Align << '\n';
197 OS << " FieldOffsets: [";
198 for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
201 OS << L->second.FieldOffsets[I];