blob: 76762d58fe254525ff41b335b3b85fe1f1a23eb8 [file] [log] [blame]
Douglas Gregore9fc3772012-01-26 07:55:45 +00001//===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Douglas Gregore9fc3772012-01-26 07:55:45 +00006//
7//===----------------------------------------------------------------------===//
8#include "clang/Frontend/LayoutOverrideSource.h"
9#include "clang/AST/Decl.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000010#include "clang/Basic/CharInfo.h"
Douglas Gregore9fc3772012-01-26 07:55:45 +000011#include "llvm/Support/raw_ostream.h"
12#include <fstream>
13#include <string>
14
15using namespace clang;
16
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000017/// Parse a simple identifier.
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +000018static std::string parseName(StringRef S) {
Jordan Rosea7d03842013-02-08 22:30:41 +000019 if (S.empty() || !isIdentifierHead(S[0]))
20 return "";
21
22 unsigned Offset = 1;
23 while (Offset < S.size() && isIdentifierBody(S[Offset]))
Douglas Gregore9fc3772012-01-26 07:55:45 +000024 ++Offset;
Fangrui Song6907ce22018-07-30 19:24:48 +000025
Douglas Gregore9fc3772012-01-26 07:55:45 +000026 return S.substr(0, Offset).str();
27}
28
Dmitri Gribenkof8579502013-01-12 19:30:44 +000029LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {
Douglas Gregore9fc3772012-01-26 07:55:45 +000030 std::ifstream Input(Filename.str().c_str());
31 if (!Input.is_open())
32 return;
Fangrui Song6907ce22018-07-30 19:24:48 +000033
Douglas Gregore9fc3772012-01-26 07:55:45 +000034 // Parse the output of -fdump-record-layouts.
35 std::string CurrentType;
36 Layout CurrentLayout;
37 bool ExpectingType = false;
Fangrui Song6907ce22018-07-30 19:24:48 +000038
Douglas Gregore9fc3772012-01-26 07:55:45 +000039 while (Input.good()) {
40 std::string Line;
41 getline(Input, Line);
Fangrui Song6907ce22018-07-30 19:24:48 +000042
Douglas Gregore9fc3772012-01-26 07:55:45 +000043 StringRef LineStr(Line);
44
Fangrui Song6907ce22018-07-30 19:24:48 +000045 // Determine whether the following line will start a
Douglas Gregore9fc3772012-01-26 07:55:45 +000046 if (LineStr.find("*** Dumping AST Record Layout") != StringRef::npos) {
47 // Flush the last type/layout, if there is one.
48 if (!CurrentType.empty())
49 Layouts[CurrentType] = CurrentLayout;
50 CurrentLayout = Layout();
Fangrui Song6907ce22018-07-30 19:24:48 +000051
Douglas Gregore9fc3772012-01-26 07:55:45 +000052 ExpectingType = true;
53 continue;
54 }
Fangrui Song6907ce22018-07-30 19:24:48 +000055
Douglas Gregore9fc3772012-01-26 07:55:45 +000056 // If we're expecting a type, grab it.
57 if (ExpectingType) {
58 ExpectingType = false;
Fangrui Song6907ce22018-07-30 19:24:48 +000059
Douglas Gregore9fc3772012-01-26 07:55:45 +000060 StringRef::size_type Pos;
61 if ((Pos = LineStr.find("struct ")) != StringRef::npos)
62 LineStr = LineStr.substr(Pos + strlen("struct "));
63 else if ((Pos = LineStr.find("class ")) != StringRef::npos)
64 LineStr = LineStr.substr(Pos + strlen("class "));
65 else if ((Pos = LineStr.find("union ")) != StringRef::npos)
66 LineStr = LineStr.substr(Pos + strlen("union "));
67 else
68 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +000069
Douglas Gregore9fc3772012-01-26 07:55:45 +000070 // Find the name of the type.
71 CurrentType = parseName(LineStr);
72 CurrentLayout = Layout();
73 continue;
74 }
Fangrui Song6907ce22018-07-30 19:24:48 +000075
Douglas Gregore9fc3772012-01-26 07:55:45 +000076 // Check for the size of the type.
Douglas Gregor3dd5fe22012-02-03 19:31:51 +000077 StringRef::size_type Pos = LineStr.find(" Size:");
Douglas Gregore9fc3772012-01-26 07:55:45 +000078 if (Pos != StringRef::npos) {
Douglas Gregor3dd5fe22012-02-03 19:31:51 +000079 // Skip past the " Size:" prefix.
80 LineStr = LineStr.substr(Pos + strlen(" Size:"));
Fangrui Song6907ce22018-07-30 19:24:48 +000081
Douglas Gregore9fc3772012-01-26 07:55:45 +000082 unsigned long long Size = 0;
83 (void)LineStr.getAsInteger(10, Size);
84 CurrentLayout.Size = Size;
85 continue;
86 }
87
88 // Check for the alignment of the type.
89 Pos = LineStr.find("Alignment:");
90 if (Pos != StringRef::npos) {
91 // Skip past the "Alignment:" prefix.
92 LineStr = LineStr.substr(Pos + strlen("Alignment:"));
Fangrui Song6907ce22018-07-30 19:24:48 +000093
Douglas Gregore9fc3772012-01-26 07:55:45 +000094 unsigned long long Alignment = 0;
95 (void)LineStr.getAsInteger(10, Alignment);
96 CurrentLayout.Align = Alignment;
97 continue;
98 }
Fangrui Song6907ce22018-07-30 19:24:48 +000099
Douglas Gregore9fc3772012-01-26 07:55:45 +0000100 // Check for the size/alignment of the type.
101 Pos = LineStr.find("sizeof=");
102 if (Pos != StringRef::npos) {
103 /* Skip past the sizeof= prefix. */
104 LineStr = LineStr.substr(Pos + strlen("sizeof="));
105
106 // Parse size.
107 unsigned long long Size = 0;
108 (void)LineStr.getAsInteger(10, Size);
109 CurrentLayout.Size = Size;
110
111 Pos = LineStr.find("align=");
112 if (Pos != StringRef::npos) {
113 /* Skip past the align= prefix. */
114 LineStr = LineStr.substr(Pos + strlen("align="));
Fangrui Song6907ce22018-07-30 19:24:48 +0000115
Douglas Gregore9fc3772012-01-26 07:55:45 +0000116 // Parse alignment.
117 unsigned long long Alignment = 0;
118 (void)LineStr.getAsInteger(10, Alignment);
119 CurrentLayout.Align = Alignment;
120 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000121
Douglas Gregore9fc3772012-01-26 07:55:45 +0000122 continue;
123 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000124
Douglas Gregore9fc3772012-01-26 07:55:45 +0000125 // Check for the field offsets of the type.
126 Pos = LineStr.find("FieldOffsets: [");
127 if (Pos == StringRef::npos)
128 continue;
129
130 LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));
Jordan Rosea7d03842013-02-08 22:30:41 +0000131 while (!LineStr.empty() && isDigit(LineStr[0])) {
Douglas Gregore9fc3772012-01-26 07:55:45 +0000132 // Parse this offset.
133 unsigned Idx = 1;
Jordan Rosea7d03842013-02-08 22:30:41 +0000134 while (Idx < LineStr.size() && isDigit(LineStr[Idx]))
Douglas Gregore9fc3772012-01-26 07:55:45 +0000135 ++Idx;
Fangrui Song6907ce22018-07-30 19:24:48 +0000136
Douglas Gregore9fc3772012-01-26 07:55:45 +0000137 unsigned long long Offset = 0;
138 (void)LineStr.substr(0, Idx).getAsInteger(10, Offset);
Fangrui Song6907ce22018-07-30 19:24:48 +0000139
Douglas Gregore9fc3772012-01-26 07:55:45 +0000140 CurrentLayout.FieldOffsets.push_back(Offset);
Fangrui Song6907ce22018-07-30 19:24:48 +0000141
Douglas Gregore9fc3772012-01-26 07:55:45 +0000142 // Skip over this offset, the following comma, and any spaces.
143 LineStr = LineStr.substr(Idx + 1);
Jordan Rosea7d03842013-02-08 22:30:41 +0000144 while (!LineStr.empty() && isWhitespace(LineStr[0]))
Douglas Gregore9fc3772012-01-26 07:55:45 +0000145 LineStr = LineStr.substr(1);
146 }
147 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000148
Douglas Gregore9fc3772012-01-26 07:55:45 +0000149 // Flush the last type/layout, if there is one.
150 if (!CurrentType.empty())
151 Layouts[CurrentType] = CurrentLayout;
152}
153
Fangrui Song6907ce22018-07-30 19:24:48 +0000154bool
Douglas Gregore9fc3772012-01-26 07:55:45 +0000155LayoutOverrideSource::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,
Fangrui Song6907ce22018-07-30 19:24:48 +0000159 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
Douglas Gregore9fc3772012-01-26 07:55:45 +0000160{
161 // We can't override unnamed declarations.
162 if (!Record->getIdentifier())
163 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000164
Douglas Gregore9fc3772012-01-26 07:55:45 +0000165 // Check whether we have a layout for this record.
166 llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());
167 if (Known == Layouts.end())
168 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000169
Douglas Gregore9fc3772012-01-26 07:55:45 +0000170 // Provide field layouts.
171 unsigned NumFields = 0;
Fangrui Song6907ce22018-07-30 19:24:48 +0000172 for (RecordDecl::field_iterator F = Record->field_begin(),
Douglas Gregore9fc3772012-01-26 07:55:45 +0000173 FEnd = Record->field_end();
174 F != FEnd; ++F, ++NumFields) {
175 if (NumFields >= Known->second.FieldOffsets.size())
176 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +0000177
David Blaikie40ed2972012-06-06 20:45:41 +0000178 FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
Douglas Gregore9fc3772012-01-26 07:55:45 +0000179 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000180
Douglas Gregore9fc3772012-01-26 07:55:45 +0000181 // Wrong number of fields.
182 if (NumFields != Known->second.FieldOffsets.size())
183 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000184
Douglas Gregore9fc3772012-01-26 07:55:45 +0000185 Size = Known->second.Size;
186 Alignment = Known->second.Align;
187 return true;
188}
189
Yaron Kerencdae9412016-01-29 19:38:18 +0000190LLVM_DUMP_METHOD void LayoutOverrideSource::dump() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000191 raw_ostream &OS = llvm::errs();
Fangrui Song6907ce22018-07-30 19:24:48 +0000192 for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
Douglas Gregore9fc3772012-01-26 07:55:45 +0000193 LEnd = Layouts.end();
194 L != LEnd; ++L) {
195 OS << "Type: blah " << L->first() << '\n';
196 OS << " Size:" << L->second.Size << '\n';
197 OS << " Alignment:" << L->second.Align << '\n';
198 OS << " FieldOffsets: [";
199 for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
200 if (I)
201 OS << ", ";
202 OS << L->second.FieldOffsets[I];
203 }
204 OS << "]\n";
205 }
206}
207