Coverage Report

Created: 2019-07-24 05:18

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