Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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() || !isIdentifierHead(S[0]))
20
0
    return "";
21
39
22
39
  unsigned Offset = 1;
23
87
  while (Offset < S.size() && 
isIdentifierBody(S[Offset])51
)
24
48
    ++Offset;
25
39
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
6
34
6
  // Parse the output of -fdump-record-layouts.
35
6
  std::string CurrentType;
36
6
  Layout CurrentLayout;
37
6
  bool ExpectingType = false;
38
6
39
349
  while (Input.good()) {
40
343
    std::string Line;
41
343
    getline(Input, Line);
42
343
43
343
    StringRef LineStr(Line);
44
343
45
343
    // Determine whether the following line will start a
46
343
    if (LineStr.find("*** Dumping AST Record Layout") != StringRef::npos)  {
47
39
      // Flush the last type/layout, if there is one.
48
39
      if (!CurrentType.empty())
49
33
        Layouts[CurrentType] = CurrentLayout;
50
39
      CurrentLayout = Layout();
51
39
52
39
      ExpectingType = true;
53
39
      continue;
54
39
    }
55
304
56
304
    // If we're expecting a type, grab it.
57
304
    if (ExpectingType) {
58
39
      ExpectingType = false;
59
39
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
39
70
39
      // Find the name of the type.
71
39
      CurrentType = parseName(LineStr);
72
39
      CurrentLayout = Layout();
73
39
      continue;
74
39
    }
75
265
76
265
    // Check for the size of the type.
77
265
    StringRef::size_type Pos = LineStr.find(" Size:");
78
265
    if (Pos != StringRef::npos) {
79
39
      // Skip past the " Size:" prefix.
80
39
      LineStr = LineStr.substr(Pos + strlen(" Size:"));
81
39
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
226
88
226
    // Check for the alignment of the type.
89
226
    Pos = LineStr.find("Alignment:");
90
226
    if (Pos != StringRef::npos) {
91
35
      // Skip past the "Alignment:" prefix.
92
35
      LineStr = LineStr.substr(Pos + strlen("Alignment:"));
93
35
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
191
100
191
    // Check for the size/alignment of the type.
101
191
    Pos = LineStr.find("sizeof=");
102
191
    if (Pos != StringRef::npos) {
103
0
      /* Skip past the sizeof= prefix. */
104
0
      LineStr = LineStr.substr(Pos + strlen("sizeof="));
105
0
106
0
      // Parse size.
107
0
      unsigned long long Size = 0;
108
0
      (void)LineStr.getAsInteger(10, Size);
109
0
      CurrentLayout.Size = Size;
110
0
111
0
      Pos = LineStr.find("align=");
112
0
      if (Pos != StringRef::npos) {
113
0
        /* Skip past the align= prefix. */
114
0
        LineStr = LineStr.substr(Pos + strlen("align="));
115
0
116
0
        // Parse alignment.
117
0
        unsigned long long Alignment = 0;
118
0
        (void)LineStr.getAsInteger(10, Alignment);
119
0
        CurrentLayout.Align = Alignment;
120
0
      }
121
0
122
0
      continue;
123
0
    }
124
191
125
191
    // Check for the field offsets of the type.
126
191
    Pos = LineStr.find("FieldOffsets: [");
127
191
    if (Pos == StringRef::npos)
128
152
      continue;
129
39
130
39
    LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));
131
126
    while (!LineStr.empty() && isDigit(LineStr[0])) {
132
87
      // Parse this offset.
133
87
      unsigned Idx = 1;
134
145
      while (Idx < LineStr.size() && isDigit(LineStr[Idx]))
135
58
        ++Idx;
136
87
137
87
      unsigned long long Offset = 0;
138
87
      (void)LineStr.substr(0, Idx).getAsInteger(10, Offset);
139
87
140
87
      CurrentLayout.FieldOffsets.push_back(Offset);
141
87
142
87
      // 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
6
149
6
  // 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
42
  // We can't override unnamed declarations.
162
42
  if (!Record->getIdentifier())
163
1
    return false;
164
41
165
41
  // 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
38
170
38
  // 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
85
178
85
    FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
179
85
  }
180
38
181
38
  // Wrong number of fields.
182
38
  if (NumFields != Known->second.FieldOffsets.size())
183
0
    return false;
184
38
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