Skip to content

Commit e811ab7

Browse files
[DebugInfo] Add symbol for debugger with VTable information.
Address comments from reviewers: - Created a helper function to get the alignment. - Remove the 'internString' call. Use a local variable. - Remove the 'createArtificialType' call by updating the flags to include the 'artificial' bit.
1 parent 4bd0c48 commit e811ab7

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,11 +2525,13 @@ StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
25252525
// existing information in the DWARF. The type is assumed to be 'void *'.
25262526
void CGDebugInfo::emitVTableSymbol(llvm::GlobalVariable *VTable,
25272527
const CXXRecordDecl *RD) {
2528+
if (!CGM.getTarget().getCXXABI().isItaniumFamily())
2529+
return;
2530+
25282531
ASTContext &Context = CGM.getContext();
25292532
SmallString<64> Buffer;
2530-
Twine SymbolName = internString("_vtable$");
2531-
StringRef SymbolNameRef = SymbolName.toStringRef(Buffer);
2532-
DeclContext *DC = static_cast<DeclContext *>(const_cast<CXXRecordDecl *>(RD));
2533+
StringRef SymbolName = "_vtable$";
2534+
const DeclContext *DC = static_cast<const DeclContext *>(RD);
25332535
SourceLocation Loc;
25342536
QualType VoidPtr = Context.getPointerType(Context.VoidTy);
25352537

@@ -2539,32 +2541,28 @@ void CGDebugInfo::emitVTableSymbol(llvm::GlobalVariable *VTable,
25392541
// - The DIGlobalVariable for the vtable is put in the DICompileUnitScope.
25402542

25412543
// The created non-member should be mark as 'artificial'. It will be
2542-
// placed it inside the scope of the C++ class/structure.
2544+
// placed inside the scope of the C++ class/structure.
25432545
llvm::DIScope *DContext = getContextDescriptor(cast<Decl>(DC), TheCU);
25442546
auto *Ctxt = cast<llvm::DICompositeType>(DContext);
25452547
llvm::DIFile *Unit = getOrCreateFile(Loc);
25462548
llvm::DIType *VTy = getOrCreateType(VoidPtr, Unit);
2547-
llvm::DINode::DIFlags Flags = getAccessFlag(AccessSpecifier::AS_private, RD);
2549+
llvm::DINode::DIFlags Flags = getAccessFlag(AccessSpecifier::AS_private, RD) |
2550+
llvm::DINode::FlagArtificial;
25482551
auto Tag = CGM.getCodeGenOpts().DwarfVersion >= 5
25492552
? llvm::dwarf::DW_TAG_variable
25502553
: llvm::dwarf::DW_TAG_member;
2551-
llvm::DIDerivedType *OldDT = DBuilder.createStaticMemberType(
2552-
Ctxt, SymbolNameRef, Unit, /*LineNumber=*/0, VTy, Flags,
2554+
llvm::DIDerivedType *DT = DBuilder.createStaticMemberType(
2555+
Ctxt, SymbolName, Unit, /*LineNumber=*/0, VTy, Flags,
25532556
/*Val=*/nullptr, Tag);
2554-
llvm::DIDerivedType *DT =
2555-
static_cast<llvm::DIDerivedType *>(DBuilder.createArtificialType(OldDT));
25562557

25572558
// Use the same vtable pointer to global alignment for the symbol.
2558-
LangAS AS = CGM.GetGlobalVarAddressSpace(nullptr);
2559-
unsigned PAlign = CGM.getItaniumVTableContext().isRelativeLayout()
2560-
? 32
2561-
: CGM.getTarget().getPointerAlign(AS);
2559+
unsigned PAlign = CGM.getGlobalVarAlignment();
25622560

25632561
// The global variable is in the CU scope, and links back to the type it's
25642562
// "within" via the declaration field.
25652563
llvm::DIGlobalVariableExpression *GVE =
25662564
DBuilder.createGlobalVariableExpression(
2567-
TheCU, SymbolNameRef, VTable->getName(), Unit, /*LineNo=*/0,
2565+
TheCU, SymbolName, VTable->getName(), Unit, /*LineNo=*/0,
25682566
getOrCreateType(VoidPtr, Unit), VTable->hasLocalLinkage(),
25692567
/*isDefined=*/true, nullptr, DT, /*TemplateParameters=*/nullptr,
25702568
PAlign);

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,15 @@ class CodeGenModule : public CodeGenTypeCache {
18161816
void requireVectorDestructorDefinition(const CXXRecordDecl *RD);
18171817
bool classNeedsVectorDestructor(const CXXRecordDecl *RD);
18181818

1819+
// Helper to get the alignment for a variable.
1820+
unsigned getGlobalVarAlignment(const VarDecl *D = nullptr) {
1821+
LangAS AS = GetGlobalVarAddressSpace(D);
1822+
unsigned PAlign = getItaniumVTableContext().isRelativeLayout()
1823+
? 32
1824+
: getTarget().getPointerAlign(AS);
1825+
return PAlign;
1826+
}
1827+
18191828
private:
18201829
bool shouldDropDLLAttribute(const Decl *D, const llvm::GlobalValue *GV) const;
18211830

clang/lib/CodeGen/ItaniumCXXABI.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,10 +2168,7 @@ llvm::GlobalVariable *ItaniumCXXABI::getAddrOfVTable(const CXXRecordDecl *RD,
21682168
// Use pointer to global alignment for the vtable. Otherwise we would align
21692169
// them based on the size of the initializer which doesn't make sense as only
21702170
// single values are read.
2171-
LangAS AS = CGM.GetGlobalVarAddressSpace(nullptr);
2172-
unsigned PAlign = CGM.getItaniumVTableContext().isRelativeLayout()
2173-
? 32
2174-
: CGM.getTarget().getPointerAlign(AS);
2171+
unsigned PAlign = CGM.getGlobalVarAlignment();
21752172

21762173
VTable = CGM.CreateOrReplaceCXXRuntimeVariable(
21772174
Name, VTableType, llvm::GlobalValue::ExternalLinkage,

0 commit comments

Comments
 (0)