Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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
561k
void TypeLocBuilder::pushFullCopy(TypeLoc L) {
19
561k
  size_t Size = L.getFullDataSize();
20
561k
  reserve(Size);
21
22
561k
  SmallVector<TypeLoc, 4> TypeLocs;
23
561k
  TypeLoc CurTL = L;
24
1.14M
  while (CurTL) {
25
583k
    TypeLocs.push_back(CurTL);
26
583k
    CurTL = CurTL.getNextTypeLoc();
27
583k
  }
28
29
1.14M
  for (unsigned i = 0, e = TypeLocs.size(); i < e; 
++i583k
) {
30
583k
    TypeLoc CurTL = TypeLocs[e-i-1];
31
583k
    switch (CurTL.getTypeLocClass()) {
32
0
#define ABSTRACT_TYPELOC(CLASS, PARENT)
33
0
#define TYPELOC(CLASS, PARENT) \
34
583k
    case TypeLoc::CLASS: { \
35
583k
      CLASS##TypeLoc NewTL = push<class CLASS##TypeLoc>(CurTL.getType()); \
36
583k
      memcpy(NewTL.getOpaqueData(), CurTL.getOpaqueData(), NewTL.getLocalDataSize()); \
37
583k
      break; \
38
583k
    }
39
583k
#include 
"clang/AST/TypeLocNodes.def"0
40
583k
    }
41
583k
  }
42
561k
}
43
44
1.42M
void TypeLocBuilder::grow(size_t NewCapacity) {
45
1.42M
  assert(NewCapacity > Capacity);
46
47
  // Allocate the new buffer and copy the old data into it.
48
0
  char *NewBuffer = new char[NewCapacity];
49
1.42M
  unsigned NewIndex = Index + NewCapacity - Capacity;
50
1.42M
  memcpy(&NewBuffer[NewIndex],
51
1.42M
         &Buffer[Index],
52
1.42M
         Capacity - Index);
53
54
1.42M
  if (Buffer != InlineBuffer)
55
17.4k
    delete[] Buffer;
56
57
1.42M
  Buffer = NewBuffer;
58
1.42M
  Capacity = NewCapacity;
59
1.42M
  Index = NewIndex;
60
1.42M
}
61
62
20.3M
TypeLoc TypeLocBuilder::pushImpl(QualType T, size_t LocalSize, unsigned LocalAlignment) {
63
20.3M
#ifndef NDEBUG
64
20.3M
  QualType TLast = TypeLoc(T, nullptr).getNextTypeLoc().getType();
65
20.3M
  assert(TLast == LastTy &&
66
20.3M
         "mismatch between last type and new type's inner type");
67
0
  LastTy = T;
68
20.3M
#endif
69
70
20.3M
  assert(LocalAlignment <= BufferMaxAlignment && "Unexpected alignment");
71
72
  // If we need to grow, grow by a factor of 2.
73
20.3M
  if (LocalSize > Index) {
74
586k
    size_t RequiredCapacity = Capacity + (LocalSize - Index);
75
586k
    size_t NewCapacity = Capacity * 2;
76
588k
    while (RequiredCapacity > NewCapacity)
77
2.49k
      NewCapacity *= 2;
78
586k
    grow(NewCapacity);
79
586k
  }
80
81
  // Because we're adding elements to the TypeLoc backwards, we have to
82
  // do some extra work to keep everything aligned appropriately.
83
  // FIXME: This algorithm is a absolute mess because every TypeLoc returned
84
  // needs to be valid.  Partial TypeLocs are a terrible idea.
85
  // FIXME: 4 and 8 are sufficient at the moment, but it's pretty ugly to
86
  // hardcode them.
87
20.3M
  if (LocalAlignment == 4) {
88
11.4M
    if (NumBytesAtAlign8 == 0) {
89
11.0M
      NumBytesAtAlign4 += LocalSize;
90
11.0M
    } else {
91
340k
      unsigned Padding = NumBytesAtAlign4 % 8;
92
340k
      if (Padding == 0) {
93
338k
        if (LocalSize % 8 == 0) {
94
          // Everything is set: there's no padding and we don't need to add
95
          // any.
96
332k
        } else {
97
332k
          assert(LocalSize % 8 == 4);
98
          // No existing padding; add in 4 bytes padding
99
0
          memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
100
332k
          Index -= 4;
101
332k
        }
102
338k
      } else {
103
1.41k
        assert(Padding == 4);
104
1.41k
        if (LocalSize % 8 == 0) {
105
          // Everything is set: there's 4 bytes padding and we don't need
106
          // to add any.
107
1.41k
        } else {
108
1.41k
          assert(LocalSize % 8 == 4);
109
          // There are 4 bytes padding, but we don't need any; remove it.
110
0
          memmove(&Buffer[Index + 4], &Buffer[Index], NumBytesAtAlign4);
111
1.41k
          Index += 4;
112
1.41k
        }
113
1.41k
      }
114
0
      NumBytesAtAlign4 += LocalSize;
115
340k
    }
116
11.4M
  } else 
if (8.89M
LocalAlignment == 88.89M
) {
117
8.88M
    if (NumBytesAtAlign8 == 0) {
118
      // We have not seen any 8-byte aligned element yet. We insert a padding
119
      // only if the new Index is not 8-byte-aligned.
120
8.71M
      if ((Index - LocalSize) % 8 != 0) {
121
1.21M
        memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
122
1.21M
        Index -= 4;
123
1.21M
      }
124
8.71M
    } else {
125
171k
      unsigned Padding = NumBytesAtAlign4 % 8;
126
171k
      if (Padding == 0) {
127
156k
        if (LocalSize % 8 == 0) {
128
          // Everything is set: there's no padding and we don't need to add
129
          // any.
130
156k
        } else {
131
0
          assert(LocalSize % 8 == 4);
132
          // No existing padding; add in 4 bytes padding
133
0
          memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
134
0
          Index -= 4;
135
0
        }
136
156k
      } else {
137
15.8k
        assert(Padding == 4);
138
15.8k
        if (LocalSize % 8 == 0) {
139
          // Everything is set: there's 4 bytes padding and we don't need
140
          // to add any.
141
15.8k
        } else {
142
0
          assert(LocalSize % 8 == 4);
143
          // 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.8k
      }
148
171k
    }
149
150
    // Forget about any padding.
151
0
    NumBytesAtAlign4 = 0;
152
8.88M
    NumBytesAtAlign8 += LocalSize;
153
8.88M
  } else {
154
1.14k
    assert(LocalSize == 0);
155
1.14k
  }
156
157
0
  Index -= LocalSize;
158
159
20.3M
  assert(Capacity - Index == TypeLoc::getFullDataSizeForType(T) &&
160
20.3M
         "incorrect data size provided to CreateTypeSourceInfo!");
161
162
0
  return getTemporaryTypeLoc(T);
163
20.3M
}