Coverage Report

Created: 2021-01-19 06:58

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