blob: b31fbd087ba78f4dbe9962d2da544997367c67c3 [file] [log] [blame]
Douglas Gregore9fc3772012-01-26 07:55:45 +00001//===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "clang/Frontend/LayoutOverrideSource.h"
10#include "clang/AST/Decl.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000011#include "clang/Basic/CharInfo.h"
Douglas Gregore9fc3772012-01-26 07:55:45 +000012#include "llvm/Support/raw_ostream.h"
13#include <fstream>
14#include <string>
15
16using namespace clang;
17
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000018/// Parse a simple identifier.
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +000019static std::string parseName(StringRef S) {
Jordan Rosea7d03842013-02-08 22:30:41 +000020 if (S.empty() || !isIdentifierHead(S[0]))
21 return "";
22
23 unsigned Offset = 1;
24 while (Offset < S.size() && isIdentifierBody(S[Offset]))
Douglas Gregore9fc3772012-01-26 07:55:45 +000025 ++Offset;
Fangrui Song6907ce22018-07-30 19:24:48 +000026
Douglas Gregore9fc3772012-01-26 07:55:45 +000027 return S.substr(0, Offset).str();
28}
29
Dmitri Gribenkof8579502013-01-12 19:30:44 +000030LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {
Douglas Gregore9fc3772012-01-26 07:55:45 +000031 std::ifstream Input(Filename.str().c_str());
32 if (!Input.is_open())
33 return;
Fangrui Song6907ce22018-07-30 19:24:48 +000034
Douglas Gregore9fc3772012-01-26 07:55:45 +000035 // Parse the output of -fdump-record-layouts.
36 std::string CurrentType;
37 Layout CurrentLayout;
38 bool ExpectingType = false;
Fangrui Song6907ce22018-07-30 19:24:48 +000039
Douglas Gregore9fc3772012-01-26 07:55:45 +000040 while (Input.good()) {
41 std::string Line;
42 getline(Input, Line);
Fangrui Song6907ce22018-07-30 19:24:48 +000043
Douglas Gregore9fc3772012-01-26 07:55:45 +000044 StringRef LineStr(Line);
45
Fangrui Song6907ce22018-07-30 19:24:48 +000046 // Determine whether the following line will start a
Douglas Gregore9fc3772012-01-26 07:55:45 +000047 if (LineStr.find("*** Dumping AST Record Layout") != StringRef::npos) {
48 // Flush the last type/layout, if there is one.
49 if (!CurrentType.empty())
50 Layouts[CurrentType] = CurrentLayout;
51 CurrentLayout = Layout();
Fangrui Song6907ce22018-07-30 19:24:48 +000052
Douglas Gregore9fc3772012-01-26 07:55:45 +000053 ExpectingType = true;
54 continue;
55 }
Fangrui Song6907ce22018-07-30 19:24:48 +000056
Douglas Gregore9fc3772012-01-26 07:55:45 +000057 // If we're expecting a type, grab it.
58 if (ExpectingType) {
59 ExpectingType = false;
Fangrui Song6907ce22018-07-30 19:24:48 +000060
Douglas Gregore9fc3772012-01-26 07:55:45 +000061 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 "));
68 else
69 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +000070
Douglas Gregore9fc3772012-01-26 07:55:45 +000071 // Find the name of the type.
72 CurrentType = parseName(LineStr);
73 CurrentLayout = Layout();
74 continue;
75 }
Fangrui Song6907ce22018-07-30 19:24:48 +000076
Douglas Gregore9fc3772012-01-26 07:55:45 +000077 // Check for the size of the type.
Douglas Gregor3dd5fe22012-02-03 19:31:51 +000078 StringRef::size_type Pos = LineStr.find(" Size:");
Douglas Gregore9fc3772012-01-26 07:55:45 +000079 if (Pos != StringRef::npos) {
Douglas Gregor3dd5fe22012-02-03 19:31:51 +000080 // Skip past the " Size:" prefix.
81 LineStr = LineStr.substr(Pos + strlen(" Size:"));
Fangrui Song6907ce22018-07-30 19:24:48 +000082
Douglas Gregore9fc3772012-01-26 07:55:45 +000083 unsigned long long Size = 0;
84 (void)LineStr.getAsInteger(10, Size);
85 CurrentLayout.Size = Size;
86 continue;
87 }
88
89 // Check for the alignment of the type.
90 Pos = LineStr.find("Alignment:");
91 if (Pos != StringRef::npos) {
92 // Skip past the "Alignment:" prefix.
93 LineStr = LineStr.substr(Pos + strlen("Alignment:"));
Fangrui Song6907ce22018-07-30 19:24:48 +000094
Douglas Gregore9fc3772012-01-26 07:55:45 +000095 unsigned long long Alignment = 0;
96 (void)LineStr.getAsInteger(10, Alignment);
97 CurrentLayout.Align = Alignment;
98 continue;
99 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000100
Douglas Gregore9fc3772012-01-26 07:55:45 +0000101 // Check for the size/alignment of the type.
102 Pos = LineStr.find("sizeof=");
103 if (Pos != StringRef::npos) {
104 /* Skip past the sizeof= prefix. */
105 LineStr = LineStr.substr(Pos + strlen("sizeof="));
106
107 // Parse size.
108 unsigned long long Size = 0;
109 (void)LineStr.getAsInteger(10, Size);
110 CurrentLayout.Size = Size;
111
112 Pos = LineStr.find("align=");
113 if (Pos != StringRef::npos) {
114 /* Skip past the align= prefix. */
115 LineStr = LineStr.substr(Pos + strlen("align="));
Fangrui Song6907ce22018-07-30 19:24:48 +0000116
Douglas Gregore9fc3772012-01-26 07:55:45 +0000117 // Parse alignment.
118 unsigned long long Alignment = 0;
119 (void)LineStr.getAsInteger(10, Alignment);
120 CurrentLayout.Align = Alignment;
121 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000122
Douglas Gregore9fc3772012-01-26 07:55:45 +0000123 continue;
124 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000125
Douglas Gregore9fc3772012-01-26 07:55:45 +0000126 // Check for the field offsets of the type.
127 Pos = LineStr.find("FieldOffsets: [");
128 if (Pos == StringRef::npos)
129 continue;
130
131 LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));
Jordan Rosea7d03842013-02-08 22:30:41 +0000132 while (!LineStr.empty() && isDigit(LineStr[0])) {
Douglas Gregore9fc3772012-01-26 07:55:45 +0000133 // Parse this offset.
134 unsigned Idx = 1;
Jordan Rosea7d03842013-02-08 22:30:41 +0000135 while (Idx < LineStr.size() && isDigit(LineStr[Idx]))
Douglas Gregore9fc3772012-01-26 07:55:45 +0000136 ++Idx;
Fangrui Song6907ce22018-07-30 19:24:48 +0000137
Douglas Gregore9fc3772012-01-26 07:55:45 +0000138 unsigned long long Offset = 0;
139 (void)LineStr.substr(0, Idx).getAsInteger(10, Offset);
Fangrui Song6907ce22018-07-30 19:24:48 +0000140
Douglas Gregore9fc3772012-01-26 07:55:45 +0000141 CurrentLayout.FieldOffsets.push_back(Offset);
Fangrui Song6907ce22018-07-30 19:24:48 +0000142
Douglas Gregore9fc3772012-01-26 07:55:45 +0000143 // Skip over this offset, the following comma, and any spaces.
144 LineStr = LineStr.substr(Idx + 1);
Jordan Rosea7d03842013-02-08 22:30:41 +0000145 while (!LineStr.empty() && isWhitespace(LineStr[0]))
Douglas Gregore9fc3772012-01-26 07:55:45 +0000146 LineStr = LineStr.substr(1);
147 }
148 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000149
Douglas Gregore9fc3772012-01-26 07:55:45 +0000150 // Flush the last type/layout, if there is one.
151 if (!CurrentType.empty())
152 Layouts[CurrentType] = CurrentLayout;
153}
154
Fangrui Song6907ce22018-07-30 19:24:48 +0000155bool
Douglas Gregore9fc3772012-01-26 07:55:45 +0000156LayoutOverrideSource::layoutRecordType(const RecordDecl *Record,
157 uint64_t &Size, uint64_t &Alignment,
158 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
159 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
Fangrui Song6907ce22018-07-30 19:24:48 +0000160 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
Douglas Gregore9fc3772012-01-26 07:55:45 +0000161{
162 // We can't override unnamed declarations.
163 if (!Record->getIdentifier())
164 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000165
Douglas Gregore9fc3772012-01-26 07:55:45 +0000166 // Check whether we have a layout for this record.
167 llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());
168 if (Known == Layouts.end())
169 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000170
Douglas Gregore9fc3772012-01-26 07:55:45 +0000171 // Provide field layouts.
172 unsigned NumFields = 0;
Fangrui Song6907ce22018-07-30 19:24:48 +0000173 for (RecordDecl::field_iterator F = Record->field_begin(),
Douglas Gregore9fc3772012-01-26 07:55:45 +0000174 FEnd = Record->field_end();
175 F != FEnd; ++F, ++NumFields) {
176 if (NumFields >= Known->second.FieldOffsets.size())
177 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +0000178
David Blaikie40ed2972012-06-06 20:45:41 +0000179 FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
Douglas Gregore9fc3772012-01-26 07:55:45 +0000180 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000181
Douglas Gregore9fc3772012-01-26 07:55:45 +0000182 // Wrong number of fields.
183 if (NumFields != Known->second.FieldOffsets.size())
184 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000185
Douglas Gregore9fc3772012-01-26 07:55:45 +0000186 Size = Known->second.Size;
187 Alignment = Known->second.Align;
188 return true;
189}
190
Yaron Kerencdae9412016-01-29 19:38:18 +0000191LLVM_DUMP_METHOD void LayoutOverrideSource::dump() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000192 raw_ostream &OS = llvm::errs();
Fangrui Song6907ce22018-07-30 19:24:48 +0000193 for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
Douglas Gregore9fc3772012-01-26 07:55:45 +0000194 LEnd = Layouts.end();
195 L != LEnd; ++L) {
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) {
201 if (I)
202 OS << ", ";
203 OS << L->second.FieldOffsets[I];
204 }
205 OS << "]\n";
206 }
207}
208