/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/LayoutOverrideSource.cpp
Line | Count | Source (jump to first uncovered line) |
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/Basic/CharInfo.h" |
11 | | #include "llvm/Support/raw_ostream.h" |
12 | | #include <fstream> |
13 | | #include <string> |
14 | | |
15 | | using namespace clang; |
16 | | |
17 | | /// Parse a simple identifier. |
18 | 39 | static std::string parseName(StringRef S) { |
19 | 39 | if (S.empty() || !isAsciiIdentifierStart(S[0])) |
20 | 0 | return ""; |
21 | | |
22 | 39 | unsigned Offset = 1; |
23 | 87 | while (Offset < S.size() && isAsciiIdentifierContinue(S[Offset])51 ) |
24 | 48 | ++Offset; |
25 | | |
26 | 39 | return S.substr(0, Offset).str(); |
27 | 39 | } |
28 | | |
29 | 6 | LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) { |
30 | 6 | std::ifstream Input(Filename.str().c_str()); |
31 | 6 | if (!Input.is_open()) |
32 | 0 | return; |
33 | | |
34 | | // Parse the output of -fdump-record-layouts. |
35 | 6 | std::string CurrentType; |
36 | 6 | Layout CurrentLayout; |
37 | 6 | bool ExpectingType = false; |
38 | | |
39 | 349 | while (Input.good()) { |
40 | 343 | std::string Line; |
41 | 343 | getline(Input, Line); |
42 | | |
43 | 343 | StringRef LineStr(Line); |
44 | | |
45 | | // Determine whether the following line will start a |
46 | 343 | if (LineStr.contains("*** Dumping AST Record Layout")) { |
47 | | // Flush the last type/layout, if there is one. |
48 | 39 | if (!CurrentType.empty()) |
49 | 33 | Layouts[CurrentType] = CurrentLayout; |
50 | 39 | CurrentLayout = Layout(); |
51 | | |
52 | 39 | ExpectingType = true; |
53 | 39 | continue; |
54 | 39 | } |
55 | | |
56 | | // If we're expecting a type, grab it. |
57 | 304 | if (ExpectingType) { |
58 | 39 | ExpectingType = false; |
59 | | |
60 | 39 | StringRef::size_type Pos; |
61 | 39 | if ((Pos = LineStr.find("struct ")) != StringRef::npos) |
62 | 31 | LineStr = LineStr.substr(Pos + strlen("struct ")); |
63 | 8 | else if ((Pos = LineStr.find("class ")) != StringRef::npos) |
64 | 4 | LineStr = LineStr.substr(Pos + strlen("class ")); |
65 | 4 | else if ((Pos = LineStr.find("union ")) != StringRef::npos) |
66 | 4 | LineStr = LineStr.substr(Pos + strlen("union ")); |
67 | 0 | else |
68 | 0 | continue; |
69 | | |
70 | | // Find the name of the type. |
71 | 39 | CurrentType = parseName(LineStr); |
72 | 39 | CurrentLayout = Layout(); |
73 | 39 | continue; |
74 | 39 | } |
75 | | |
76 | | // Check for the size of the type. |
77 | 265 | StringRef::size_type Pos = LineStr.find(" Size:"); |
78 | 265 | if (Pos != StringRef::npos) { |
79 | | // Skip past the " Size:" prefix. |
80 | 39 | LineStr = LineStr.substr(Pos + strlen(" Size:")); |
81 | | |
82 | 39 | unsigned long long Size = 0; |
83 | 39 | (void)LineStr.getAsInteger(10, Size); |
84 | 39 | CurrentLayout.Size = Size; |
85 | 39 | continue; |
86 | 39 | } |
87 | | |
88 | | // Check for the alignment of the type. |
89 | 226 | Pos = LineStr.find("Alignment:"); |
90 | 226 | if (Pos != StringRef::npos) { |
91 | | // Skip past the "Alignment:" prefix. |
92 | 35 | LineStr = LineStr.substr(Pos + strlen("Alignment:")); |
93 | | |
94 | 35 | unsigned long long Alignment = 0; |
95 | 35 | (void)LineStr.getAsInteger(10, Alignment); |
96 | 35 | CurrentLayout.Align = Alignment; |
97 | 35 | continue; |
98 | 35 | } |
99 | | |
100 | | // Check for the size/alignment of the type. |
101 | 191 | Pos = LineStr.find("sizeof="); |
102 | 191 | if (Pos != StringRef::npos) { |
103 | | /* Skip past the sizeof= prefix. */ |
104 | 0 | LineStr = LineStr.substr(Pos + strlen("sizeof=")); |
105 | | |
106 | | // Parse size. |
107 | 0 | unsigned long long Size = 0; |
108 | 0 | (void)LineStr.getAsInteger(10, Size); |
109 | 0 | CurrentLayout.Size = Size; |
110 | |
|
111 | 0 | Pos = LineStr.find("align="); |
112 | 0 | if (Pos != StringRef::npos) { |
113 | | /* Skip past the align= prefix. */ |
114 | 0 | LineStr = LineStr.substr(Pos + strlen("align=")); |
115 | | |
116 | | // Parse alignment. |
117 | 0 | unsigned long long Alignment = 0; |
118 | 0 | (void)LineStr.getAsInteger(10, Alignment); |
119 | 0 | CurrentLayout.Align = Alignment; |
120 | 0 | } |
121 | |
|
122 | 0 | continue; |
123 | 0 | } |
124 | | |
125 | | // Check for the field offsets of the type. |
126 | 191 | Pos = LineStr.find("FieldOffsets: ["); |
127 | 191 | if (Pos == StringRef::npos) |
128 | 152 | continue; |
129 | | |
130 | 39 | LineStr = LineStr.substr(Pos + strlen("FieldOffsets: [")); |
131 | 126 | while (!LineStr.empty() && isDigit(LineStr[0])) { |
132 | | // Parse this offset. |
133 | 87 | unsigned Idx = 1; |
134 | 145 | while (Idx < LineStr.size() && isDigit(LineStr[Idx])) |
135 | 58 | ++Idx; |
136 | | |
137 | 87 | unsigned long long Offset = 0; |
138 | 87 | (void)LineStr.substr(0, Idx).getAsInteger(10, Offset); |
139 | | |
140 | 87 | CurrentLayout.FieldOffsets.push_back(Offset); |
141 | | |
142 | | // Skip over this offset, the following comma, and any spaces. |
143 | 87 | LineStr = LineStr.substr(Idx + 1); |
144 | 138 | while (!LineStr.empty() && isWhitespace(LineStr[0])) |
145 | 51 | LineStr = LineStr.substr(1); |
146 | 87 | } |
147 | 39 | } |
148 | | |
149 | | // Flush the last type/layout, if there is one. |
150 | 6 | if (!CurrentType.empty()) |
151 | 6 | Layouts[CurrentType] = CurrentLayout; |
152 | 6 | } |
153 | | |
154 | | bool |
155 | | LayoutOverrideSource::layoutRecordType(const RecordDecl *Record, |
156 | | uint64_t &Size, uint64_t &Alignment, |
157 | | llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets, |
158 | | llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets, |
159 | | llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets) |
160 | 42 | { |
161 | | // We can't override unnamed declarations. |
162 | 42 | if (!Record->getIdentifier()) |
163 | 1 | return false; |
164 | | |
165 | | // Check whether we have a layout for this record. |
166 | 41 | llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName()); |
167 | 41 | if (Known == Layouts.end()) |
168 | 3 | return false; |
169 | | |
170 | | // Provide field layouts. |
171 | 38 | unsigned NumFields = 0; |
172 | 38 | for (RecordDecl::field_iterator F = Record->field_begin(), |
173 | 38 | FEnd = Record->field_end(); |
174 | 123 | F != FEnd; ++F, ++NumFields85 ) { |
175 | 85 | if (NumFields >= Known->second.FieldOffsets.size()) |
176 | 0 | continue; |
177 | | |
178 | 85 | FieldOffsets[*F] = Known->second.FieldOffsets[NumFields]; |
179 | 85 | } |
180 | | |
181 | | // Wrong number of fields. |
182 | 38 | if (NumFields != Known->second.FieldOffsets.size()) |
183 | 0 | return false; |
184 | | |
185 | 38 | Size = Known->second.Size; |
186 | 38 | Alignment = Known->second.Align; |
187 | 38 | return true; |
188 | 38 | } |
189 | | |
190 | 0 | LLVM_DUMP_METHOD void LayoutOverrideSource::dump() { |
191 | 0 | raw_ostream &OS = llvm::errs(); |
192 | 0 | for (llvm::StringMap<Layout>::iterator L = Layouts.begin(), |
193 | 0 | LEnd = Layouts.end(); |
194 | 0 | L != LEnd; ++L) { |
195 | 0 | OS << "Type: blah " << L->first() << '\n'; |
196 | 0 | OS << " Size:" << L->second.Size << '\n'; |
197 | 0 | OS << " Alignment:" << L->second.Align << '\n'; |
198 | 0 | OS << " FieldOffsets: ["; |
199 | 0 | for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) { |
200 | 0 | if (I) |
201 | 0 | OS << ", "; |
202 | 0 | OS << L->second.FieldOffsets[I]; |
203 | 0 | } |
204 | 0 | OS << "]\n"; |
205 | 0 | } |
206 | 0 | } |
207 | | |