Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 1 | //===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | #include "clang/Frontend/LayoutOverrideSource.h" |
| 9 | #include "clang/AST/Decl.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 10 | #include "clang/Basic/CharInfo.h" |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 11 | #include "llvm/Support/raw_ostream.h" |
| 12 | #include <fstream> |
| 13 | #include <string> |
| 14 | |
| 15 | using namespace clang; |
| 16 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 17 | /// Parse a simple identifier. |
Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 18 | static std::string parseName(StringRef S) { |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 19 | if (S.empty() || !isIdentifierHead(S[0])) |
| 20 | return ""; |
| 21 | |
| 22 | unsigned Offset = 1; |
| 23 | while (Offset < S.size() && isIdentifierBody(S[Offset])) |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 24 | ++Offset; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 25 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 26 | return S.substr(0, Offset).str(); |
| 27 | } |
| 28 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 29 | LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 30 | std::ifstream Input(Filename.str().c_str()); |
| 31 | if (!Input.is_open()) |
| 32 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 34 | // Parse the output of -fdump-record-layouts. |
| 35 | std::string CurrentType; |
| 36 | Layout CurrentLayout; |
| 37 | bool ExpectingType = false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 38 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 39 | while (Input.good()) { |
| 40 | std::string Line; |
| 41 | getline(Input, Line); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 43 | StringRef LineStr(Line); |
| 44 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 45 | // Determine whether the following line will start a |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 46 | 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 Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 51 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 52 | ExpectingType = true; |
| 53 | continue; |
| 54 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 55 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 56 | // If we're expecting a type, grab it. |
| 57 | if (ExpectingType) { |
| 58 | ExpectingType = false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 60 | 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 Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 70 | // Find the name of the type. |
| 71 | CurrentType = parseName(LineStr); |
| 72 | CurrentLayout = Layout(); |
| 73 | continue; |
| 74 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 76 | // Check for the size of the type. |
Douglas Gregor | 3dd5fe2 | 2012-02-03 19:31:51 +0000 | [diff] [blame] | 77 | StringRef::size_type Pos = LineStr.find(" Size:"); |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 78 | if (Pos != StringRef::npos) { |
Douglas Gregor | 3dd5fe2 | 2012-02-03 19:31:51 +0000 | [diff] [blame] | 79 | // Skip past the " Size:" prefix. |
| 80 | LineStr = LineStr.substr(Pos + strlen(" Size:")); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 81 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 82 | 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 Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 94 | unsigned long long Alignment = 0; |
| 95 | (void)LineStr.getAsInteger(10, Alignment); |
| 96 | CurrentLayout.Align = Alignment; |
| 97 | continue; |
| 98 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 99 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 100 | // 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 Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 115 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 116 | // Parse alignment. |
| 117 | unsigned long long Alignment = 0; |
| 118 | (void)LineStr.getAsInteger(10, Alignment); |
| 119 | CurrentLayout.Align = Alignment; |
| 120 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 121 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 122 | continue; |
| 123 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 125 | // 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 Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 131 | while (!LineStr.empty() && isDigit(LineStr[0])) { |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 132 | // Parse this offset. |
| 133 | unsigned Idx = 1; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 134 | while (Idx < LineStr.size() && isDigit(LineStr[Idx])) |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 135 | ++Idx; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 136 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 137 | unsigned long long Offset = 0; |
| 138 | (void)LineStr.substr(0, Idx).getAsInteger(10, Offset); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 139 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 140 | CurrentLayout.FieldOffsets.push_back(Offset); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 141 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 142 | // Skip over this offset, the following comma, and any spaces. |
| 143 | LineStr = LineStr.substr(Idx + 1); |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 144 | while (!LineStr.empty() && isWhitespace(LineStr[0])) |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 145 | LineStr = LineStr.substr(1); |
| 146 | } |
| 147 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 148 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 149 | // Flush the last type/layout, if there is one. |
| 150 | if (!CurrentType.empty()) |
| 151 | Layouts[CurrentType] = CurrentLayout; |
| 152 | } |
| 153 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 154 | bool |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 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, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 159 | llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets) |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 160 | { |
| 161 | // We can't override unnamed declarations. |
| 162 | if (!Record->getIdentifier()) |
| 163 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 165 | // 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 Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 170 | // Provide field layouts. |
| 171 | unsigned NumFields = 0; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 172 | for (RecordDecl::field_iterator F = Record->field_begin(), |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 173 | FEnd = Record->field_end(); |
| 174 | F != FEnd; ++F, ++NumFields) { |
| 175 | if (NumFields >= Known->second.FieldOffsets.size()) |
| 176 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 177 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 178 | FieldOffsets[*F] = Known->second.FieldOffsets[NumFields]; |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 179 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 180 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 181 | // Wrong number of fields. |
| 182 | if (NumFields != Known->second.FieldOffsets.size()) |
| 183 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 184 | |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 185 | Size = Known->second.Size; |
| 186 | Alignment = Known->second.Align; |
| 187 | return true; |
| 188 | } |
| 189 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 190 | LLVM_DUMP_METHOD void LayoutOverrideSource::dump() { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 191 | raw_ostream &OS = llvm::errs(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 192 | for (llvm::StringMap<Layout>::iterator L = Layouts.begin(), |
Douglas Gregor | e9fc377 | 2012-01-26 07:55:45 +0000 | [diff] [blame] | 193 | 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 | |