Coverage Report

Created: 2020-11-28 09:00

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/VTTBuilder.cpp
Line
Count
Source
1
//===- VTTBuilder.cpp - C++ VTT layout builder ----------------------------===//
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 contains code dealing with generation of the layout of virtual table
10
// tables (VTT).
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/VTTBuilder.h"
15
#include "clang/AST/ASTContext.h"
16
#include "clang/AST/BaseSubobject.h"
17
#include "clang/AST/CharUnits.h"
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/DeclCXX.h"
20
#include "clang/AST/RecordLayout.h"
21
#include "clang/AST/Type.h"
22
#include "clang/Basic/LLVM.h"
23
#include "llvm/Support/Casting.h"
24
#include <cassert>
25
#include <cstdint>
26
27
using namespace clang;
28
29
#define DUMP_OVERRIDERS 0
30
31
VTTBuilder::VTTBuilder(ASTContext &Ctx,
32
                       const CXXRecordDecl *MostDerivedClass,
33
                       bool GenerateDefinition)
34
    : Ctx(Ctx), MostDerivedClass(MostDerivedClass),
35
      MostDerivedClassLayout(Ctx.getASTRecordLayout(MostDerivedClass)),
36
1.20k
      GenerateDefinition(GenerateDefinition) {
37
  // Lay out this VTT.
38
1.20k
  LayoutVTT(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
39
1.20k
            /*BaseIsVirtual=*/false);
40
1.20k
}
41
42
void VTTBuilder::AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
43
4.47k
                                  const CXXRecordDecl *VTableClass) {
44
  // Store the vtable pointer index if we're generating the primary VTT.
45
4.47k
  if (VTableClass == MostDerivedClass) {
46
2.44k
    assert(!SecondaryVirtualPointerIndices.count(Base) &&
47
2.44k
           "A virtual pointer index already exists for this base subobject!");
48
2.44k
    SecondaryVirtualPointerIndices[Base] = VTTComponents.size();
49
2.44k
  }
50
51
4.47k
  if (!GenerateDefinition) {
52
3.00k
    VTTComponents.push_back(VTTComponent());
53
3.00k
    return;
54
3.00k
  }
55
56
1.46k
  VTTComponents.push_back(VTTComponent(VTableIndex, Base));
57
1.46k
}
58
59
2.20k
void VTTBuilder::LayoutSecondaryVTTs(BaseSubobject Base) {
60
2.20k
  const CXXRecordDecl *RD = Base.getBase();
61
62
3.05k
  for (const auto &I : RD->bases()) {
63
    // Don't layout virtual bases.
64
3.05k
    if (I.isVirtual())
65
1.83k
        continue;
66
67
1.22k
    const auto *BaseDecl =
68
1.22k
        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
69
70
1.22k
    const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
71
1.22k
    CharUnits BaseOffset = Base.getBaseOffset() +
72
1.22k
      Layout.getBaseClassOffset(BaseDecl);
73
74
    // Layout the VTT for this base.
75
1.22k
    LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/false);
76
1.22k
  }
77
2.20k
}
78
79
void
80
VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
81
                                           bool BaseIsMorallyVirtual,
82
                                           uint64_t VTableIndex,
83
                                           const CXXRecordDecl *VTableClass,
84
5.60k
                                           VisitedVirtualBasesSetTy &VBases) {
85
5.60k
  const CXXRecordDecl *RD = Base.getBase();
86
87
  // We're not interested in bases that don't have virtual bases, and not
88
  // morally virtual bases.
89
5.60k
  if (!RD->getNumVBases() && 
!BaseIsMorallyVirtual1.78k
)
90
94
    return;
91
92
5.95k
  
for (const auto &I : RD->bases())5.51k
{
93
5.95k
    const auto *BaseDecl =
94
5.95k
        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
95
96
    // Itanium C++ ABI 2.6.2:
97
    //   Secondary virtual pointers are present for all bases with either
98
    //   virtual bases or virtual function declarations overridden along a
99
    //   virtual path.
100
    //
101
    // If the base class is not dynamic, we don't want to add it, nor any
102
    // of its base classes.
103
5.95k
    if (!BaseDecl->isDynamicClass())
104
2.01k
      continue;
105
106
3.94k
    bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual;
107
3.94k
    bool BaseDeclIsNonVirtualPrimaryBase = false;
108
3.94k
    CharUnits BaseOffset;
109
3.94k
    if (I.isVirtual()) {
110
      // Ignore virtual bases that we've already visited.
111
2.44k
      if (!VBases.insert(BaseDecl).second)
112
542
        continue;
113
114
1.89k
      BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
115
1.89k
      BaseDeclIsMorallyVirtual = true;
116
1.50k
    } else {
117
1.50k
      const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
118
119
1.50k
      BaseOffset = Base.getBaseOffset() +
120
1.50k
        Layout.getBaseClassOffset(BaseDecl);
121
122
1.50k
      if (!Layout.isPrimaryBaseVirtual() &&
123
1.50k
          Layout.getPrimaryBase() == BaseDecl)
124
1.11k
        BaseDeclIsNonVirtualPrimaryBase = true;
125
1.50k
    }
126
127
    // Itanium C++ ABI 2.6.2:
128
    //   Secondary virtual pointers: for each base class X which (a) has virtual
129
    //   bases or is reachable along a virtual path from D, and (b) is not a
130
    //   non-virtual primary base, the address of the virtual table for X-in-D
131
    //   or an appropriate construction virtual table.
132
3.40k
    if (!BaseDeclIsNonVirtualPrimaryBase &&
133
2.28k
        (BaseDecl->getNumVBases() || 
BaseDeclIsMorallyVirtual1.44k
)) {
134
      // Add the vtable pointer.
135
2.26k
      AddVTablePointer(BaseSubobject(BaseDecl, BaseOffset), VTableIndex,
136
2.26k
                       VTableClass);
137
2.26k
    }
138
139
    // And lay out the secondary virtual pointers for the base class.
140
3.40k
    LayoutSecondaryVirtualPointers(BaseSubobject(BaseDecl, BaseOffset),
141
3.40k
                                   BaseDeclIsMorallyVirtual, VTableIndex,
142
3.40k
                                   VTableClass, VBases);
143
3.40k
  }
144
5.51k
}
145
146
void
147
VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
148
2.20k
                                           uint64_t VTableIndex) {
149
2.20k
  VisitedVirtualBasesSetTy VBases;
150
2.20k
  LayoutSecondaryVirtualPointers(Base, /*BaseIsMorallyVirtual=*/false,
151
2.20k
                                 VTableIndex, Base.getBase(), VBases);
152
2.20k
}
153
154
void VTTBuilder::LayoutVirtualVTTs(const CXXRecordDecl *RD,
155
2.20k
                                   VisitedVirtualBasesSetTy &VBases) {
156
3.05k
  for (const auto &I : RD->bases()) {
157
3.05k
    const auto *BaseDecl =
158
3.05k
        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
159
160
    // Check if this is a virtual base.
161
3.05k
    if (I.isVirtual()) {
162
      // Check if we've seen this base before.
163
1.83k
      if (!VBases.insert(BaseDecl).second)
164
376
        continue;
165
166
1.45k
      CharUnits BaseOffset =
167
1.45k
        MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
168
169
1.45k
      LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/true);
170
1.45k
    }
171
172
    // We only need to layout virtual VTTs for this base if it actually has
173
    // virtual bases.
174
2.68k
    if (BaseDecl->getNumVBases())
175
1.00k
      LayoutVirtualVTTs(BaseDecl, VBases);
176
2.68k
  }
177
2.20k
}
178
179
3.88k
void VTTBuilder::LayoutVTT(BaseSubobject Base, bool BaseIsVirtual) {
180
3.88k
  const CXXRecordDecl *RD = Base.getBase();
181
182
  // Itanium C++ ABI 2.6.2:
183
  //   An array of virtual table addresses, called the VTT, is declared for
184
  //   each class type that has indirect or direct virtual base classes.
185
3.88k
  if (RD->getNumVBases() == 0)
186
1.67k
    return;
187
188
2.20k
  bool IsPrimaryVTT = Base.getBase() == MostDerivedClass;
189
190
2.20k
  if (!IsPrimaryVTT) {
191
    // Remember the sub-VTT index.
192
1.00k
    SubVTTIndicies[Base] = VTTComponents.size();
193
1.00k
  }
194
195
2.20k
  uint64_t VTableIndex = VTTVTables.size();
196
2.20k
  VTTVTables.push_back(VTTVTable(Base, BaseIsVirtual));
197
198
  // Add the primary vtable pointer.
199
2.20k
  AddVTablePointer(Base, VTableIndex, RD);
200
201
  // Add the secondary VTTs.
202
2.20k
  LayoutSecondaryVTTs(Base);
203
204
  // Add the secondary virtual pointers.
205
2.20k
  LayoutSecondaryVirtualPointers(Base, VTableIndex);
206
207
  // If this is the primary VTT, we want to lay out virtual VTTs as well.
208
2.20k
  if (IsPrimaryVTT) {
209
1.20k
    VisitedVirtualBasesSetTy VBases;
210
1.20k
    LayoutVirtualVTTs(Base.getBase(), VBases);
211
1.20k
  }
212
2.20k
}