Coverage Report

Created: 2021-08-24 07:12

/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
554k
void TypeLocBuilder::pushFullCopy(TypeLoc L) {
19
554k
  size_t Size = L.getFullDataSize();
20
554k
  reserve(Size);
21
22
554k
  SmallVector<TypeLoc, 4> TypeLocs;
23
554k
  TypeLoc CurTL = L;
24
1.13M
  while (CurTL) {
25
576k
    TypeLocs.push_back(CurTL);
26
576k
    CurTL = CurTL.getNextTypeLoc();
27
576k
  }
28
29
1.13M
  for (unsigned i = 0, e = TypeLocs.size(); i < e; 
++i576k
) {
30
576k
    TypeLoc CurTL = TypeLocs[e-i-1];
31
576k
    switch (CurTL.getTypeLocClass()) {
32
0
#define ABSTRACT_TYPELOC(CLASS, PARENT)
33
0
#define TYPELOC(CLASS, PARENT) \
34
576k
    case TypeLoc::CLASS: { \
35
576k
      CLASS##TypeLoc NewTL = push<class CLASS##TypeLoc>(CurTL.getType()); \
36
576k
      memcpy(NewTL.getOpaqueData(), CurTL.getOpaqueData(), NewTL.getLocalDataSize()); \
37
576k
      break; \
38
576k
    }
39
576k
#include 
"clang/AST/TypeLocNodes.def"0
40
576k
    }
41
576k
  }
42
554k
}
43
44
1.59M
void TypeLocBuilder::grow(size_t NewCapacity) {
45
1.59M
  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.59M
  unsigned NewIndex = Index + NewCapacity - Capacity;
50
1.59M
  memcpy(&NewBuffer[NewIndex],
51
1.59M
         &Buffer[Index],
52
1.59M
         Capacity - Index);
53
54
1.59M
  if (Buffer != InlineBuffer)
55
16.7k
    delete[] Buffer;
56
57
1.59M
  Buffer = NewBuffer;
58
1.59M
  Capacity = NewCapacity;
59
1.59M
  Index = NewIndex;
60
1.59M
}
61
62
20.1M
TypeLoc TypeLocBuilder::pushImpl(QualType T, size_t LocalSize, unsigned LocalAlignment) {
63
20.1M
#ifndef NDEBUG
64
20.1M
  QualType TLast = TypeLoc(T, nullptr).getNextTypeLoc().getType();
65
20.1M
  assert(TLast == LastTy &&
66
20.1M
         "mismatch between last type and new type's inner type");
67
0
  LastTy = T;
68
20.1M
#endif
69
70
20.1M
  assert(LocalAlignment <= BufferMaxAlignment && "Unexpected alignment");
71
72
  // If we need to grow, grow by a factor of 2.
73
20.1M
  if (LocalSize > Index) {
74
578k
    size_t RequiredCapacity = Capacity + (LocalSize - Index);
75
578k
    size_t NewCapacity = Capacity * 2;
76
581k
    while (RequiredCapacity > NewCapacity)
77
2.53k
      NewCapacity *= 2;
78
578k
    grow(NewCapacity);
79
578k
  }
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.1M
  if (LocalAlignment == 4) {
88
11.3M
    if (NumBytesAtAlign8 == 0) {
89
11.0M
      NumBytesAtAlign4 += LocalSize;
90
11.0M
    } else {
91
322k
      unsigned Padding = NumBytesAtAlign4 % 8;
92
322k
      if (Padding == 0) {
93
321k
        if (LocalSize % 8 == 0) {
94
          // Everything is set: there's no padding and we don't need to add
95
          // any.
96
315k
        } else {
97
315k
          assert(LocalSize % 8 == 4);
98
          // No existing padding; add in 4 bytes padding
99
0
          memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
100
315k
          Index -= 4;
101
315k
        }
102
321k
      } else {
103
1.37k
        assert(Padding == 4);
104
1.37k
        if (LocalSize % 8 == 0) {
105
          // Everything is set: there's 4 bytes padding and we don't need
106
          // to add any.
107
1.37k
        } else {
108
1.37k
          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.37k
          Index += 4;
112
1.37k
        }
113
1.37k
      }
114
0
      NumBytesAtAlign4 += LocalSize;
115
322k
    }
116
11.3M
  } else 
if (8.76M
LocalAlignment == 88.76M
) {
117
8.76M
    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.56M
      if ((Index - LocalSize) % 8 != 0) {
121
1.19M
        memmove(&Buffer[Index - 4], &Buffer[Index], NumBytesAtAlign4);
122
1.19M
        Index -= 4;
123
1.19M
      }
124
8.56M
    } else {
125
200k
      unsigned Padding = NumBytesAtAlign4 % 8;
126
200k
      if (Padding == 0) {
127
189k
        if (LocalSize % 8 == 0) {
128
          // Everything is set: there's no padding and we don't need to add
129
          // any.
130
189k
        } 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
189k
      } else {
137
11.6k
        assert(Padding == 4);
138
11.6k
        if (LocalSize % 8 == 0) {
139
          // Everything is set: there's 4 bytes padding and we don't need
140
          // to add any.
141
11.6k
        } 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
11.6k
      }
148
200k
    }
149
150
    // Forget about any padding.
151
0
    NumBytesAtAlign4 = 0;
152
8.76M
    NumBytesAtAlign8 += LocalSize;
153
8.76M
  } else {
154
1.14k
    assert(LocalSize == 0);
155
1.14k
  }
156
157
0
  Index -= LocalSize;
158
159
20.1M
  assert(Capacity - Index == TypeLoc::getFullDataSizeForType(T) &&
160
20.1M
         "incorrect data size provided to CreateTypeSourceInfo!");
161
162
0
  return getTemporaryTypeLoc(T);
163
20.1M
}