Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Sema/TypeLocBuilder.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- TypeLocBuilder.cpp - Type Source Info collector ------------------===//
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
//
9
//  This files defines TypeLocBuilder, a class for building TypeLocs
10
//  bottom-up.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "TypeLocBuilder.h"
15
16
using namespace clang;
17
18
299k
void TypeLocBuilder::pushFullCopy(TypeLoc L) {
19
299k
  size_t Size = L.getFullDataSize();
20
299k
  reserve(Size);
21
299k
22
299k
  SmallVector<TypeLoc, 4> TypeLocs;
23
299k
  TypeLoc CurTL = L;
24
639k
  while (CurTL) {
25
339k
    TypeLocs.push_back(CurTL);
26
339k
    CurTL = CurTL.getNextTypeLoc();
27
339k
  }
28
299k
29
639k
  for (unsigned i = 0, e = TypeLocs.size(); i < e; 
++i339k
) {
30
339k
    TypeLoc CurTL = TypeLocs[e-i-1];
31
339k
    switch (CurTL.getTypeLocClass()) {
32
339k
#define ABSTRACT_TYPELOC(CLASS, PARENT)
33
339k
#define TYPELOC(CLASS, PARENT) \
34
339k
    case TypeLoc::CLASS: { \
35
339k
      CLASS##TypeLoc NewTL = push<class CLASS##TypeLoc>(CurTL.getType()); \
36
339k
      memcpy(NewTL.getOpaqueData(), CurTL.getOpaqueData(), NewTL.getLocalDataSize()); \
37
339k
      break; \
38
339k
    }
39
339k
#include 
"clang/AST/TypeLocNodes.def"4.84k
40
339k
    }
41
339k
  }
42
299k
}
43
44
7.65M
void TypeLocBuilder::grow(size_t NewCapacity) {
45
7.65M
  assert(NewCapacity > Capacity);
46
7.65M
47
7.65M
  // Allocate the new buffer and copy the old data into it.
48
7.65M
  char *NewBuffer = new char[NewCapacity];
49
7.65M
  unsigned NewIndex = Index + NewCapacity - Capacity;
50
7.65M
  memcpy(&NewBuffer[NewIndex],
51
7.65M
         &Buffer[Index],
52
7.65M
         Capacity - Index);
53
7.65M
54
7.65M
  if (Buffer != InlineBuffer.buffer)
55
58.2k
    delete[] Buffer;
56
7.65M
57
7.65M
  Buffer = NewBuffer;
58
7.65M
  Capacity = NewCapacity;
59
7.65M
  Index = NewIndex;
60
7.65M
}
61
62
25.3M
TypeLoc TypeLocBuilder::pushImpl(QualType T, size_t LocalSize, unsigned LocalAlignment) {
63
#ifndef NDEBUG
64
  QualType TLast = TypeLoc(T, nullptr).getNextTypeLoc().getType();
65
  assert(TLast == LastTy &&
66
         "mismatch between last type and new type's inner type");
67
  LastTy = T;
68
#endif
69
70
25.3M
  assert(LocalAlignment <= BufferMaxAlignment && "Unexpected alignment");
71
25.3M
72
25.3M
  // If we need to grow, grow by a factor of 2.
73
25.3M
  if (LocalSize > Index) {
74
5.44M
    size_t RequiredCapacity = Capacity + (LocalSize - Index);
75
5.44M
    size_t NewCapacity = Capacity * 2;
76
6.00M
    while (RequiredCapacity > NewCapacity)
77
553k
      NewCapacity *= 2;
78
5.44M
    grow(NewCapacity);
79
5.44M
  }
80
25.3M
81
25.3M
  // Because we're adding elements to the TypeLoc backwards, we have to
82
25.3M
  // do some extra work to keep everything aligned appropriately.
83
25.3M
  // FIXME: This algorithm is a absolute mess because every TypeLoc returned
84
25.3M
  // needs to be valid.  Partial TypeLocs are a terrible idea.
85
25.3M
  // FIXME: 4 and 8 are sufficient at the moment, but it's pretty ugly to
86
25.3M
  // hardcode them.
87
25.3M
  if (LocalAlignment == 4) {
88
14.9M
    if (NumBytesAtAlign8 == 0) {
89
14.6M
      NumBytesAtAlign4 += LocalSize;
90
14.6M
    } else {
91
296k
      unsigned Padding = NumBytesAtAlign4 % 8;
92
296k
      if (Padding == 0) {
93
295k
        if (LocalSize % 8 == 0) {
94
6.82k
          // Everything is set: there's no padding and we don't need to add
95
6.82k
          // any.
96
288k
        } else {
97
288k
          assert(LocalSize % 8 == 4);
98
288k
          // No existing padding; add in 4 bytes padding
99
288k
          memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
100
288k
          Index -= 4;
101
288k
        }
102
295k
      } else {
103
942
        assert(Padding == 4);
104
942
        if (LocalSize % 8 == 0) {
105
0
          // Everything is set: there's 4 bytes padding and we don't need
106
0
          // to add any.
107
942
        } else {
108
942
          assert(LocalSize % 8 == 4);
109
942
          // There are 4 bytes padding, but we don't need any; remove it.
110
942
          memmove(&Buffer[Index + 4], &Buffer[Index], NumBytesAtAlign4);
111
942
          Index += 4;
112
942
        }
113
942
      }
114
296k
      NumBytesAtAlign4 += LocalSize;
115
296k
    }
116
14.9M
  } else 
if (10.4M
LocalAlignment == 810.4M
) {
117
10.4M
    if (NumBytesAtAlign8 == 0) {
118
10.2M
      // We have not seen any 8-byte aligned element yet. We insert a padding
119
10.2M
      // only if the new Index is not 8-byte-aligned.
120
10.2M
      if ((Index - LocalSize) % 8 != 0) {
121
2.17M
        memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
122
2.17M
        Index -= 4;
123
2.17M
      }
124
10.2M
    } else {
125
235k
      unsigned Padding = NumBytesAtAlign4 % 8;
126
235k
      if (Padding == 0) {
127
219k
        if (LocalSize % 8 == 0) {
128
219k
          // Everything is set: there's no padding and we don't need to add
129
219k
          // any.
130
219k
        } else {
131
0
          assert(LocalSize % 8 == 4);
132
0
          // No existing padding; add in 4 bytes padding
133
0
          memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
134
0
          Index -= 4;
135
0
        }
136
219k
      } else {
137
15.6k
        assert(Padding == 4);
138
15.6k
        if (LocalSize % 8 == 0) {
139
15.6k
          // Everything is set: there's 4 bytes padding and we don't need
140
15.6k
          // to add any.
141
15.6k
        } else {
142
0
          assert(LocalSize % 8 == 4);
143
0
          // There are 4 bytes padding, but we don't need any; remove it.
144
0
          memmove(&Buffer[Index + 4], &Buffer[Index], NumBytesAtAlign4);
145
0
          Index += 4;
146
0
        }
147
15.6k
      }
148
235k
    }
149
10.4M
150
10.4M
    // Forget about any padding.
151
10.4M
    NumBytesAtAlign4 = 0;
152
10.4M
    NumBytesAtAlign8 += LocalSize;
153
10.4M
  } else {
154
4.86k
    assert(LocalSize == 0);
155
4.86k
  }
156
25.3M
157
25.3M
  Index -= LocalSize;
158
25.3M
159
25.3M
  assert(Capacity - Index == TypeLoc::getFullDataSizeForType(T) &&
160
25.3M
         "incorrect data size provided to CreateTypeSourceInfo!");
161
25.3M
162
25.3M
  return getTemporaryTypeLoc(T);
163
25.3M
}