Coverage Report

Created: 2024-01-17 10:31

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