Coverage Report

Created: 2020-02-25 14:32

/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.17k
      GenerateDefinition(GenerateDefinition) {
37
1.17k
  // Lay out this VTT.
38
1.17k
  LayoutVTT(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
39
1.17k
            /*BaseIsVirtual=*/false);
40
1.17k
}
41
42
void VTTBuilder::AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
43
4.38k
                                  const CXXRecordDecl *VTableClass) {
44
4.38k
  // Store the vtable pointer index if we're generating the primary VTT.
45
4.38k
  if (VTableClass == MostDerivedClass) {
46
2.38k
    assert(!SecondaryVirtualPointerIndices.count(Base) &&
47
2.38k
           "A virtual pointer index already exists for this base subobject!");
48
2.38k
    SecondaryVirtualPointerIndices[Base] = VTTComponents.size();
49
2.38k
  }
50
4.38k
51
4.38k
  if (!GenerateDefinition) {
52
2.94k
    VTTComponents.push_back(VTTComponent());
53
2.94k
    return;
54
2.94k
  }
55
1.43k
56
1.43k
  VTTComponents.push_back(VTTComponent(VTableIndex, Base));
57
1.43k
}
58
59
2.16k
void VTTBuilder::LayoutSecondaryVTTs(BaseSubobject Base) {
60
2.16k
  const CXXRecordDecl *RD = Base.getBase();
61
2.16k
62
3.00k
  for (const auto &I : RD->bases()) {
63
3.00k
    // Don't layout virtual bases.
64
3.00k
    if (I.isVirtual())
65
1.80k
        continue;
66
1.20k
67
1.20k
    const auto *BaseDecl =
68
1.20k
        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
69
1.20k
70
1.20k
    const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
71
1.20k
    CharUnits BaseOffset = Base.getBaseOffset() +
72
1.20k
      Layout.getBaseClassOffset(BaseDecl);
73
1.20k
74
1.20k
    // Layout the VTT for this base.
75
1.20k
    LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/false);
76
1.20k
  }
77
2.16k
}
78
79
void
80
VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
81
                                           bool BaseIsMorallyVirtual,
82
                                           uint64_t VTableIndex,
83
                                           const CXXRecordDecl *VTableClass,
84
5.49k
                                           VisitedVirtualBasesSetTy &VBases) {
85
5.49k
  const CXXRecordDecl *RD = Base.getBase();
86
5.49k
87
5.49k
  // We're not interested in bases that don't have virtual bases, and not
88
5.49k
  // morally virtual bases.
89
5.49k
  if (!RD->getNumVBases() && 
!BaseIsMorallyVirtual1.72k
)
90
94
    return;
91
5.39k
92
5.88k
  
for (const auto &I : RD->bases())5.39k
{
93
5.88k
    const auto *BaseDecl =
94
5.88k
        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
95
5.88k
96
5.88k
    // Itanium C++ ABI 2.6.2:
97
5.88k
    //   Secondary virtual pointers are present for all bases with either
98
5.88k
    //   virtual bases or virtual function declarations overridden along a
99
5.88k
    //   virtual path.
100
5.88k
    //
101
5.88k
    // If the base class is not dynamic, we don't want to add it, nor any
102
5.88k
    // of its base classes.
103
5.88k
    if (!BaseDecl->isDynamicClass())
104
2.01k
      continue;
105
3.86k
106
3.86k
    bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual;
107
3.86k
    bool BaseDeclIsNonVirtualPrimaryBase = false;
108
3.86k
    CharUnits BaseOffset;
109
3.86k
    if (I.isVirtual()) {
110
2.39k
      // Ignore virtual bases that we've already visited.
111
2.39k
      if (!VBases.insert(BaseDecl).second)
112
538
        continue;
113
1.85k
114
1.85k
      BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
115
1.85k
      BaseDeclIsMorallyVirtual = true;
116
1.85k
    } else {
117
1.47k
      const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
118
1.47k
119
1.47k
      BaseOffset = Base.getBaseOffset() +
120
1.47k
        Layout.getBaseClassOffset(BaseDecl);
121
1.47k
122
1.47k
      if (!Layout.isPrimaryBaseVirtual() &&
123
1.47k
          Layout.getPrimaryBase() == BaseDecl)
124
1.09k
        BaseDeclIsNonVirtualPrimaryBase = true;
125
1.47k
    }
126
3.86k
127
3.86k
    // Itanium C++ ABI 2.6.2:
128
3.86k
    //   Secondary virtual pointers: for each base class X which (a) has virtual
129
3.86k
    //   bases or is reachable along a virtual path from D, and (b) is not a
130
3.86k
    //   non-virtual primary base, the address of the virtual table for X-in-D
131
3.86k
    //   or an appropriate construction virtual table.
132
3.86k
    
if (3.32k
!BaseDeclIsNonVirtualPrimaryBase3.32k
&&
133
3.32k
        
(2.23k
BaseDecl->getNumVBases()2.23k
||
BaseDeclIsMorallyVirtual1.40k
)) {
134
2.21k
      // Add the vtable pointer.
135
2.21k
      AddVTablePointer(BaseSubobject(BaseDecl, BaseOffset), VTableIndex,
136
2.21k
                       VTableClass);
137
2.21k
    }
138
3.32k
139
3.32k
    // And lay out the secondary virtual pointers for the base class.
140
3.32k
    LayoutSecondaryVirtualPointers(BaseSubobject(BaseDecl, BaseOffset),
141
3.32k
                                   BaseDeclIsMorallyVirtual, VTableIndex,
142
3.32k
                                   VTableClass, VBases);
143
3.32k
  }
144
5.39k
}
145
146
void
147
VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
148
2.16k
                                           uint64_t VTableIndex) {
149
2.16k
  VisitedVirtualBasesSetTy VBases;
150
2.16k
  LayoutSecondaryVirtualPointers(Base, /*BaseIsMorallyVirtual=*/false,
151
2.16k
                                 VTableIndex, Base.getBase(), VBases);
152
2.16k
}
153
154
void VTTBuilder::LayoutVirtualVTTs(const CXXRecordDecl *RD,
155
2.16k
                                   VisitedVirtualBasesSetTy &VBases) {
156
3.00k
  for (const auto &I : RD->bases()) {
157
3.00k
    const auto *BaseDecl =
158
3.00k
        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
159
3.00k
160
3.00k
    // Check if this is a virtual base.
161
3.00k
    if (I.isVirtual()) {
162
1.80k
      // Check if we've seen this base before.
163
1.80k
      if (!VBases.insert(BaseDecl).second)
164
372
        continue;
165
1.43k
166
1.43k
      CharUnits BaseOffset =
167
1.43k
        MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
168
1.43k
169
1.43k
      LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/true);
170
1.43k
    }
171
3.00k
172
3.00k
    // We only need to layout virtual VTTs for this base if it actually has
173
3.00k
    // virtual bases.
174
3.00k
    
if (2.63k
BaseDecl->getNumVBases()2.63k
)
175
987
      LayoutVirtualVTTs(BaseDecl, VBases);
176
2.63k
  }
177
2.16k
}
178
179
3.81k
void VTTBuilder::LayoutVTT(BaseSubobject Base, bool BaseIsVirtual) {
180
3.81k
  const CXXRecordDecl *RD = Base.getBase();
181
3.81k
182
3.81k
  // Itanium C++ ABI 2.6.2:
183
3.81k
  //   An array of virtual table addresses, called the VTT, is declared for
184
3.81k
  //   each class type that has indirect or direct virtual base classes.
185
3.81k
  if (RD->getNumVBases() == 0)
186
1.65k
    return;
187
2.16k
188
2.16k
  bool IsPrimaryVTT = Base.getBase() == MostDerivedClass;
189
2.16k
190
2.16k
  if (!IsPrimaryVTT) {
191
987
    // Remember the sub-VTT index.
192
987
    SubVTTIndicies[Base] = VTTComponents.size();
193
987
  }
194
2.16k
195
2.16k
  uint64_t VTableIndex = VTTVTables.size();
196
2.16k
  VTTVTables.push_back(VTTVTable(Base, BaseIsVirtual));
197
2.16k
198
2.16k
  // Add the primary vtable pointer.
199
2.16k
  AddVTablePointer(Base, VTableIndex, RD);
200
2.16k
201
2.16k
  // Add the secondary VTTs.
202
2.16k
  LayoutSecondaryVTTs(Base);
203
2.16k
204
2.16k
  // Add the secondary virtual pointers.
205
2.16k
  LayoutSecondaryVirtualPointers(Base, VTableIndex);
206
2.16k
207
2.16k
  // If this is the primary VTT, we want to lay out virtual VTTs as well.
208
2.16k
  if (IsPrimaryVTT) {
209
1.17k
    VisitedVirtualBasesSetTy VBases;
210
1.17k
    LayoutVirtualVTTs(Base.getBase(), VBases);
211
1.17k
  }
212
2.16k
}