xref: /llvm-project/clang/lib/Frontend/LayoutOverrideSource.cpp (revision 5d54213ee557a86fae82af0f75498adf02f24e82)
1 //===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "clang/Frontend/LayoutOverrideSource.h"
9 #include "clang/AST/Decl.h"
10 #include "clang/AST/DeclCXX.h"
11 #include "clang/Basic/CharInfo.h"
12 #include "llvm/Support/raw_ostream.h"
13 #include <fstream>
14 #include <string>
15 
16 using namespace clang;
17 
18 /// Parse a simple identifier.
19 static std::string parseName(StringRef S) {
20   if (S.empty() || !isAsciiIdentifierStart(S[0]))
21     return "";
22 
23   unsigned Offset = 1;
24   while (Offset < S.size() && isAsciiIdentifierContinue(S[Offset]))
25     ++Offset;
26 
27   return S.substr(0, Offset).str();
28 }
29 
30 /// Parse an unsigned integer and move S to the next non-digit character.
31 static bool parseUnsigned(StringRef &S, unsigned long long &ULL) {
32   if (S.empty() || !isDigit(S[0]))
33     return false;
34   unsigned Idx = 1;
35   while (Idx < S.size() && isDigit(S[Idx]))
36     ++Idx;
37   (void)S.substr(0, Idx).getAsInteger(10, ULL);
38   S = S.substr(Idx);
39   return true;
40 }
41 
42 LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {
43   std::ifstream Input(Filename.str().c_str());
44   if (!Input.is_open())
45     return;
46 
47   // Parse the output of -fdump-record-layouts.
48   std::string CurrentType;
49   Layout CurrentLayout;
50   bool ExpectingType = false;
51 
52   while (Input.good()) {
53     std::string Line;
54     getline(Input, Line);
55 
56     StringRef LineStr(Line);
57 
58     // Determine whether the following line will start a
59     if (LineStr.contains("*** Dumping AST Record Layout")) {
60       // Flush the last type/layout, if there is one.
61       if (!CurrentType.empty())
62         Layouts[CurrentType] = CurrentLayout;
63       CurrentLayout = Layout();
64 
65       ExpectingType = true;
66       continue;
67     }
68 
69     // If we're expecting a type, grab it.
70     if (ExpectingType) {
71       ExpectingType = false;
72 
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 "));
80       else
81         continue;
82 
83       // Find the name of the type.
84       CurrentType = parseName(LineStr);
85       CurrentLayout = Layout();
86       continue;
87     }
88 
89     // Check for the size of the type.
90     StringRef::size_type Pos = LineStr.find(" Size:");
91     if (Pos != StringRef::npos) {
92       // Skip past the " Size:" prefix.
93       LineStr = LineStr.substr(Pos + strlen(" Size:"));
94 
95       unsigned long long Size = 0;
96       if (parseUnsigned(LineStr, Size))
97         CurrentLayout.Size = Size;
98       continue;
99     }
100 
101     // Check for the alignment of the type.
102     Pos = LineStr.find("Alignment:");
103     if (Pos != StringRef::npos) {
104       // Skip past the "Alignment:" prefix.
105       LineStr = LineStr.substr(Pos + strlen("Alignment:"));
106 
107       unsigned long long Alignment = 0;
108       if (parseUnsigned(LineStr, Alignment))
109         CurrentLayout.Align = Alignment;
110       continue;
111     }
112 
113     // Check for the size/alignment of the type. The number follows "size=" or
114     // "align=" indicates number of bytes.
115     Pos = LineStr.find("sizeof=");
116     if (Pos != StringRef::npos) {
117       /* Skip past the sizeof= prefix. */
118       LineStr = LineStr.substr(Pos + strlen("sizeof="));
119 
120       // Parse size.
121       unsigned long long Size = 0;
122       if (parseUnsigned(LineStr, Size))
123         CurrentLayout.Size = Size * 8;
124 
125       Pos = LineStr.find("align=");
126       if (Pos != StringRef::npos) {
127         /* Skip past the align= prefix. */
128         LineStr = LineStr.substr(Pos + strlen("align="));
129 
130         // Parse alignment.
131         unsigned long long Alignment = 0;
132         if (parseUnsigned(LineStr, Alignment))
133           CurrentLayout.Align = Alignment * 8;
134       }
135 
136       continue;
137     }
138 
139     // Check for the field offsets of the type.
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;
145         if (parseUnsigned(LineStr, Offset))
146           CurrentLayout.FieldOffsets.push_back(Offset);
147 
148         // Skip over this offset, the following comma, and any spaces.
149         LineStr = LineStr.substr(1);
150         while (!LineStr.empty() && isWhitespace(LineStr[0]))
151           LineStr = LineStr.substr(1);
152       }
153     }
154 
155     // Check for the base offsets.
156     Pos = LineStr.find("BaseOffsets: [");
157     if (Pos != StringRef::npos) {
158       LineStr = LineStr.substr(Pos + strlen("BaseOffsets: ["));
159       while (!LineStr.empty() && isDigit(LineStr[0])) {
160         unsigned long long Offset = 0;
161         if (parseUnsigned(LineStr, Offset))
162           CurrentLayout.BaseOffsets.push_back(CharUnits::fromQuantity(Offset));
163 
164         // Skip over this offset, the following comma, and any spaces.
165         LineStr = LineStr.substr(1);
166         while (!LineStr.empty() && isWhitespace(LineStr[0]))
167           LineStr = LineStr.substr(1);
168       }
169     }
170 
171     // Check for the virtual base offsets.
172     Pos = LineStr.find("VBaseOffsets: [");
173     if (Pos != StringRef::npos) {
174       LineStr = LineStr.substr(Pos + strlen("VBaseOffsets: ["));
175       while (!LineStr.empty() && isDigit(LineStr[0])) {
176         unsigned long long Offset = 0;
177         if (parseUnsigned(LineStr, Offset))
178           CurrentLayout.VBaseOffsets.push_back(CharUnits::fromQuantity(Offset));
179 
180         // Skip over this offset, the following comma, and any spaces.
181         LineStr = LineStr.substr(1);
182         while (!LineStr.empty() && isWhitespace(LineStr[0]))
183           LineStr = LineStr.substr(1);
184       }
185     }
186   }
187 
188   // Flush the last type/layout, if there is one.
189   if (!CurrentType.empty())
190     Layouts[CurrentType] = CurrentLayout;
191 }
192 
193 bool
194 LayoutOverrideSource::layoutRecordType(const RecordDecl *Record,
195   uint64_t &Size, uint64_t &Alignment,
196   llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
197   llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
198   llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
199 {
200   // We can't override unnamed declarations.
201   if (!Record->getIdentifier())
202     return false;
203 
204   // Check whether we have a layout for this record.
205   llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());
206   if (Known == Layouts.end())
207     return false;
208 
209   // Provide field layouts.
210   unsigned NumFields = 0;
211   for (RecordDecl::field_iterator F = Record->field_begin(),
212                                FEnd = Record->field_end();
213        F != FEnd; ++F, ++NumFields) {
214     if (NumFields >= Known->second.FieldOffsets.size())
215       continue;
216 
217     FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
218   }
219 
220   // Wrong number of fields.
221   if (NumFields != Known->second.FieldOffsets.size())
222     return false;
223 
224   // Provide base offsets.
225   if (const auto *RD = dyn_cast<CXXRecordDecl>(Record)) {
226     unsigned NumNB = 0;
227     unsigned NumVB = 0;
228     for (const auto &I : RD->vbases()) {
229       if (NumVB >= Known->second.VBaseOffsets.size())
230         continue;
231       const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
232       VirtualBaseOffsets[VBase] = Known->second.VBaseOffsets[NumVB++];
233     }
234     for (const auto &I : RD->bases()) {
235       if (I.isVirtual() || NumNB >= Known->second.BaseOffsets.size())
236         continue;
237       const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl();
238       BaseOffsets[Base] = Known->second.BaseOffsets[NumNB++];
239     }
240   }
241 
242   Size = Known->second.Size;
243   Alignment = Known->second.Align;
244   return true;
245 }
246 
247 LLVM_DUMP_METHOD void LayoutOverrideSource::dump() {
248   raw_ostream &OS = llvm::errs();
249   for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
250                                       LEnd = Layouts.end();
251        L != LEnd; ++L) {
252     OS << "Type: blah " << L->first() << '\n';
253     OS << "  Size:" << L->second.Size << '\n';
254     OS << "  Alignment:" << L->second.Align << '\n';
255     OS << "  FieldOffsets: [";
256     for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
257       if (I)
258         OS << ", ";
259       OS << L->second.FieldOffsets[I];
260     }
261     OS << "]\n";
262   }
263 }
264 
265